获得25%的数量总和
我有桌子x,其价值是
id desc cost
1 xx 10
2 xx 11
15 xx 11
3 xx 12
4 xx 13
5 xx 14
6 xx 14
14 xx 14
7 xx 15
8 xx 16
9 xx 18
10 xx 19
11 xx 20
12 xx 21
13 xx 23
所有项目的总成本是231.成本的25%是57.7我希望得到的项目是所有项目的总成本接近57.7
示例输出
id desc cost
1 xx 10
2 xx 11
15 xx 11
3 xx 12
4 xx 13
57
如何在sql中实现这一点?
i have table x where value is
id desc cost
1 xx 10
2 xx 11
15 xx 11
3 xx 12
4 xx 13
5 xx 14
6 xx 14
14 xx 14
7 xx 15
8 xx 16
9 xx 18
10 xx 19
11 xx 20
12 xx 21
13 xx 23
the total cost of all the item is 231. the 25% from the cost is 57.7 and i want that get the item were the total cost of all item is close to 57.7
example output
id desc cost
1 xx 10
2 xx 11
15 xx 11
3 xx 12
4 xx 13
57
how to achieve this in sql?
我现在已经解决了我自己的问题
这是我将来使用的代码
i have now solve my own problem
this is my code for future use
select id, desc, cost from
(select *, SUM(cost) over (order by cost desc)totalcost from TableX) t WHERE totalcost <=(select sum(cost) from tableX)*0.25
代码如何工作?
简单的添加新列在哪里将成本加到总成本中
样本:
how the code work?
its simple by adding new column where adding the cost to totalcost
sample:
id desc cost totalcost
1 xx 10 10
2 xx 11 21
15 xx 11 32
3 xx 12 44
4 xx 13 57
5 xx 14 71
6 xx 14 85
14 xx 14 99
7 xx 15 114
8 xx 16 130
9 xx 18 148
10 xx 19 167
11 xx 20 187
12 xx 21 208
13 xx 23 231
然后只根据我的情况选择匹配
抱歉说不好。
then selecting only the match to my condition
sorry for poor explanation.