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

trying to get grandtotal in sql query

Status
Not open for further replies.

sap1958

Technical User
Oct 22, 2009
138
US
sum(case when gifttype in ('b','g','y') then giftjntamt else 0 end)as Gifts,
sum(case when gifttype = 'c' then giftjntamt else 0 end)as Credits

sample output
Gifts Credits
25 0
55 25
250 100

I know want to get a total of the gifts and credits and show it in a separate column. I then want to use the grand total and say that if the grand total is less than 250, suppress it

--sum(Gifts)+(Credits) as GrandTotal
 
Code:
.....
    sum(case when gifttype in ('b','g','y') 
                  then giftjntamt 
             else 0 end)as Gifts,
   sum(case when gifttype = 'c' 
                 then giftjntamt
            else 0 end)as Credits
....
FROM .....
WHERE .....
GROUP BY .......
HAVING SUM(giftjntamt) > 249
....


Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 

This is totally random, but are you using fund-raising software (possibly made by JSI/Sage/Best)? The reason I ask is because I spent many years writing queries just like that for folks in a Development Office and it brought back some memories...
 
Alternative solution to Borislav:
Code:
select Gifts, Credits, Gifts+Credits as GrandTotal from (select 
sum(case when gifttype in ('b','g','y')                   then giftjntamt              else 0 end)as Gifts,   sum(case when gifttype = 'c'                  then giftjntamt            else 0 end)as Credits....FROM .....WHERE .....GROUP BY .......) X
where Gifts+Crdits >=250

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top