Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select rows until value = something

Author  Topic 

c9726306
Starting Member

3 Posts

Posted - 2008-09-01 : 07:33:38
First time poster so be gentle
I have a table with 2 columns - Description (Type: Varchar) and Volume(Type: Integer).

I need to build a query which selects rows from the table in Descending order. Here's where it gets complicated - I only want to get the rows who's volumes add up to 80% of the total of the volume column.

Eg. The Volume column below adds to 100. My query should return Desc5, Desc1, Desc2 to give 80% of the total volume

Description Volume
Desc1 | 20
Desc2 | 20
Desc3 | 10
Desc4 | 10
Desc5 | 40


Any help would be appreciated

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-01 : 08:33:38
Why not Desc1, Desc3, Desc4 and Desc5 which also gives 80% ?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

c9726306
Starting Member

3 Posts

Posted - 2008-09-01 : 08:35:59
The results need to be in descending order
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-01 : 09:37:10
Descending according to what criteria?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-01 : 09:38:05
What if 80% cannot be accomplished?
If the only alternatives are 70% or 90%, what should happen then?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

c9726306
Starting Member

3 Posts

Posted - 2008-09-01 : 12:37:15
Apologies, I should have specified - Descending based on Volume.

The total needs to be as close to 80% as possible without exceeding it.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-01 : 13:59:10
Try with a recursive cte.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -