question

Ashokkumar avatar image
Ashokkumar asked

Return a dataset when the running total crosses 75% of overall total

Hi I have a table as shown below id,balance 1,10000 2,20000 3,30000 4,40000 the output should be id,balance 4,40000 3,30000 2,20000 i created a running total for the table id,balance,running_balance 1,10000,10000 2,20000,30000 3,30000,60000 4,40000,100000 from here we should pick the dataset . i.e the output should gives a dataset with 75% of overall balance,the 75% balance should be in Descending order please help me
sql-server-2005t-sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
JohnM avatar image
JohnM answered
So, here is an example that might work for you. Also, this solution was taken from http://www.sqlteam.com/article/calculating-running-totals CREATE TABLE #temp (id INT, balance INT) GO INSERT #temp (id, balance) SELECT 1, 10000 UNION SELECT 2, 20000 UNION SELECT 3, 30000 UNION SELECT 4, 40000 GO SELECT id , balance , balance+COALESCE((SELECT SUM(balance) FROM #temp b WHERE b.balance
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.