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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

limit results to an aggregate function total

Status
Not open for further replies.

bebblebrox

IS-IT--Management
Aug 9, 2004
39
0
0
US
Hi,

My table (gift_history) looks like this:

Code:
donor_id number
gift_date datetime
gift_amount number

query looks like this:
Code:
select donor_id 
from gift_history 
where donor_id= 3890 And gift_date between '01/01/1991' and '01/01/1992' 
group by donor_id having sum(gift_amount) between 15 and 460

which returns 1 result, 3890

now donor 3890 does in fact have otal gifts from 1976 to 2006 of $450. However, what I need the query to return is donors who have gifts totalling between 15 and 460 only in 1991.

what do i need to change?

thanks!
 
Code:
select donor_id
from gift_history
where donor_id= 3890 And
      YEAR(gift_date)= 1991
group by donor_id, YEAR(gift_date)
having sum(gift_amount) between 15 and 460

Borislav Borissov
 
Code:
select donor_id 
from   gift_history 
where  donor_id= 3890 
       And gift_date between '01/01/1991' and '01/01/1992' 
group by donor_id 
having sum(gift_amount) between 15 and 460
[!]       And gift_date between '01/01/1991' and '01/01/1992' [/!]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
If you want all donors meeting that criteria then you need to take out the "donor_id= 3890" part - otherwise that's the only donor you'll ever get.
 
thanks guys...my sql is a little rusty.

nagorm...i had the donor id critera on purpose to limit the results to a particular donor for testing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top