问题描述
我有桌子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
iddesccost
1xx10
2xx11
15xx11
3xx12
4xx13
5xx14
6xx14
14xx14
7xx15
8xx16
9xx18
10xx19
11xx20
12xx21
13xx23
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
iddesccost
1xx10
2xx11
15xx11
3xx12
4xx13
57
how to achieve this in sql?
推荐答案
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.
这篇关于获得25%的数量总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!