Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query to return set of rows with aggregate more than

Posted on 2014-01-25
5
Medium Priority
?
337 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

609 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