?
Solved

Query to return set of rows with aggregate more than

Posted on 2014-01-25
5
Medium Priority
?
332 Views
Last Modified: 2014-01-26
Can we create a query that will return the minimum set of rows, ordered by column A, where sum(column B)>=X ?  So in the following example, with X=4, it should return first and second rows.

create table table1(id int, qty int)
insert into table1 (id,qty) select 1,3
insert into table1 (id,qty) select 2,2
insert into table1 (id,qty) select 3,6
0
Comment
Question by:Vadim Rapp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39809313
It is 2 different queries but you can Union them to get a single resultset:

declare @x int
set @x = 4
select top 1 * from table1 order by id
UNION
select * from table1 where qty >= @x
0
 
LVL 40

Author Comment

by:Vadim Rapp
ID: 39809372
Sorry, I don't see how this will return the rows with the sum of qty being >=4
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 39809478
I think you want something like this:
declare @x int
set @x = 4
;with data as (select t.id, t.qty, (select sum(x.qty) from yourtable x where x.id <= t.id ) sum_qty
          from yourtable t
)
select * from data
where sum_qty >= @x
  and sum_qty - qty < @x

Open in new window

0
 
LVL 40

Author Closing Comment

by:Vadim Rapp
ID: 39809499
Thanks; we in fact wanted not only the last row but all rows that make the total, so with little modification:

declare @x int
set @x = 10
;with data as (select t.id, t.qty, (select sum(x.qty) from table1 x where x.id <= t.id ) sum_qty
          from table1 t
)
select * from data
where (
            sum_qty <= @x
            or
            sum_qty - qty < @x
          )

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39809969
sorry for that, but I see you solved it.
you can even make the condition simpler:
declare @x int
set @x = 4
;with data as (select t.id, t.qty, (select sum(x.qty) from yourtable x where x.id <= t.id ) sum_qty
          from yourtable t
)
select * from data
where sum_qty - qty < @x

Open in new window

Glad I could help
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

741 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