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)
 SQL Query using TOP and SUM

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-01-28 : 23:39:15
Jason writes "I have two fields in my table, processID and memorySize. The processID is simply an identity field and the memorySize is an integer between 1 and 10,000 and it is randomly generated. I have a fixed totalmemory variable with the value of 15,000. I want to select as many records as possible from my table with the sum of the memorySize fields being lower than totalmemory.

For example, if these were records in my table:{processID,memorySize},{1,100},{2,5000},{3,7000},{4,9000},{5,10}. I would select the first 3 records because adding the fourth would put me over the totalmemory size of 15,000. I want to take the records sequentially (ie. I don't want to loop through the table looking for the most records that will fit, I just want the TOP # of records that meet the memorysize requirements. Thanks for the help."
   

- Advertisement -