获得25%的数量总和 [英] getting the 25% sum of qty

查看:67
本文介绍了获得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.


这篇关于获得25%的数量总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆