We help IT Professionals succeed at work.
Get Started

t-sql Group of records whose sum Equal a specific Number

Last Modified: 2015-11-12
I am trying to get each group of records whose sum equal 3000.....I'll try to explain with an example

Give this records below
Id   Number 1
1    1000
2     2000
3     3000
4    50
5   100
6   2500
7   350

Result would be, where I would find the min and max of the ID where the sum totals 3000 for the records
Min   Max   Sum
1          2       3000
3          3       3000
4          7        3000          

Here is the code to create the example table

create table #Example
(ID int identity(1,1)
,Number1 int)

insert into #Example
select 1000 union
select 2000 union
select 3000 union
select 50 union
select 100 union
select 2500 union
select 350
Watch Question
EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE