Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
Posted
Comments
Tomas Takac 12-Nov-15 2:01am    
What vendor? What version? Update your tags!

1 solution

i have now solve my own problem

this is my code for future use

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900