Solved

Select records based on column sum

Posted on 2006-07-11
5
647 Views
Last Modified: 2012-08-13
Need a sql statement where I could specify a value and return only enough records where the quantity satisfies the requirement.

Row      Quantity
A      4
B      2
C      5
D      5
E      12

If I needed a quantity of 10 then I would want rows A, B and C (4 + 2 + 5 = 11 which satisfies the requirement of 10).
If I needed a qty of 13 then I would want rows A, B, C and D (4 + 2 + 5 + 5 = 16)

I know I could use a cursor for this but would really like to do this within a single select statement.

TIA...
0
Comment
Question by:jgv
5 Comments
 
LVL 25

Accepted Solution

by:
jrb1 earned 500 total points
ID: 17086655
select top 1 row, quantity
from
(select a.row, a.quantity, sum(b.quantity) as ttlqty
from yourtable a
   , yourtable b
where b.row <= a.row
group by a.row, a.quantity)
where ttlqty > {yournumber}
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17086780
Hi jgv,

You're not going to be able to use a simple SELECT statement to do this.  Your best bet is probably to use a
stored procedure that tests this out.

Regards,

Patrick
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 17087104
If your table have Primary Key(PK), try this

SELECT * FROM
(select Row,(select SUM(Quantity) as c from yourtable as a where a.PK<yourtable.PK) as c from yourtable) AS t1
where c>10

Hope this help
0
 
LVL 19

Expert Comment

by:grant300
ID: 17087259
Let's break this down.

First, can you get a list of the Last Item and the sum of it and it's predicessors values?  Once you do that, it is a simple matter of selecting that item and it's predicessors.

What you want to start with is a result set like:
A    4
B    6
C    11
D    16
E    28

There are a couple of ways to do this.  The first, as eluded to by HuyBD is to join each row in the table to itself and every other predicessor row.  This gets to be expensive in a hurry if you have a lot of items.  If there are only a few dozen, who cares; it is just a couple of million machine cycles.  Let's assume the problem is larger and you want the best performance.

The slick way to do this is with a temp table and a tricky update statement.  First create a temp table with an extra column.  Then do the tricky update, then you can answer the questions out of that temp table, at least until you update the base table.

-- STEP 1:
SELECT PK, Quantity, CONVERT(INT,0) AS RunningTotal, CONVERT(INT,0) as RunningMin
   INTO #T
FROM YourTable
ORDER BY PK

-- STEP 2:
DECLARE @RunningTotal INT
SET @RunningTotal = 0
UPDATE #T
     SET RunningTotal = @RunningTotal + Quantity,
            RunningMin = @RunningTotal + 1,
            @RunningTotal = @RunningTotal + Quantity

-- STEP 3 - n:
SELECT PK, Quantity
   FROM #T
 WHERE RunningMin < @DesiredValue

YourTable
A     4
B     2
C     5
D     5
E     12

#T after creation
A     4      4      1
B     2      6      5
C     5    11      7
D     5    16     12
E    12    28     17

Query for the rows necessary to add up to 10...  WHERE RunningMin <= 10 and you get
A, B, and C

Mock it up and let us know how it goes.

Bill
0
 
LVL 12

Author Comment

by:jgv
ID: 17087394
It took a bit of tweaking but your jrb1's solution worked...one inline statement to pull only the records that satisfy the requirement

Bill, I hear you on the performance end but in this case the trade off is worth it.

Thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now