如何在记录之间分配值
我有值 1000 和表:
table
(
Id int (PK),
Percent float,
Value int
)
我必须根据两条法则(两个单独的策略)为该表中的每个记录分配 1000 字段值:
1) 均等分配策略
2) 分配策略的 %
按策略输入 1)
Id Percent Value
1, - 0
2, - 0
3, - 0
输出
Id Percent Value
1, - 333
2, - 333
3, - 334
策略输入2)
Id Percent Value
1, 10% 0
2, 90% 0
输出
Id Percent Value
1, 10% 100
2, 90% 900
任何人都可以解释一种优雅的方法来做到这一点。 谢谢。
I have value 1000 and table:
table
(
Id int (PK),
Percent float,
Value int
)
I have to distribute 1000 for every record in this table for field Value according to two law (two separated strategy):
1) equal distribution strategy
2) % of distribution strategy
Input by strategy 1)
Id Percent Value
1, - 0
2, - 0
3, - 0
Output
Id Percent Value
1, - 333
2, - 333
3, - 334
Input by strategy 2)
Id Percent Value
1, 10% 0
2, 90% 0
Output
Id Percent Value
1, 10% 100
2, 90% 900
Could anyone explain an elegant way to do this.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但不确定如何处理舍入错误。
编辑:也许在第一种情况下,第二个语句将缺失的数字添加到最后一行。
但这并不好,因为最后一行最终可能会出现一个更大的数字……您最终会得到比其余行大得多的数字。 (尝试运行具有 6 个输入行的查询...)
Not sure how to handle rounding errors, though.
edit: maybe in the first case with a second statement that adds the missing numbers to the last row.
But this isn't good, since the last row can end up with a much bigger number... you will end up with a significantly larger number than the rest. (try running the query with 6 input rows...)