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 2000 Forums
 SQL Server Development (2000)
 Get All Rows with Sum() Less than a Specific Value

Author  Topic 

Blastrix
Posting Yak Master

208 Posts

Posted - 2003-07-13 : 15:30:40
How can I get all rows that have a column that sums up to less than a specified value? The rows are ordered by date, and let's say that I need all of the rows in which the amounts add up to less than $1,000,000.

Thanks,
Steve

nr
SQLTeam MVY

12543 Posts

Posted - 2003-07-13 : 15:55:53
You mean you want all the earliest rows where the sum is less than 1000000?

There are more efficient ways probably but this should work or be close to it.

select *
from tbl
where dte < (select min(dte) from tbl t1 where 1000000 < (select sum(t2.col) from tbl t2 where t2.dte >= t1.dte))


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -