Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pull records that have a total value greater than something?

Status
Not open for further replies.

oh704

Technical User
Oct 20, 2009
8
0
0
US
Hi All,

I'm sorta newbie on SQL, and i'm trying to write a sql that will pull all records where it's total amount if greater than an amount.

ie.

Name $
John 2
John 3
John 4
John 5
Mike 1
Mike 2
Mike 3
Kate 9

i want to pull all the records for each person with an aggregate amount of greater than $10, so in this sample, the 4 records for John.

i can achieve this effect by joining two queries together in Access by name. But I need this to be in a single sql format because it gets built dynamically through some vb codes.

any advise is much appreciated.
Thanks
 
If you're using latest version of SQL Server, then
Code:
;with cte as (select Name, Amount, sum(Amount) over (partition by [Name]) as Total form MyTable)

select * from cte where Total > @PassedAmount

If you only want to select Name and Total Amount, then
Code:
select [Name], sum(Amount) as TotalAmount
from MyTable group by [Name] having sum(Amount) > @PassedAmount

Take a look at HAVING clause for the GROUP BY in BOL

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top