Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sqlrob
Starting Member
2 Posts |
Posted - 2013-03-17 : 10:34:45
|
SELECT SUM(Amount) AS SalesFROM BankWHERE Class = 'incomecc' AND Class ='incomecheck'want to total the Amount column when the Class column has incomecc and incomecheck...this does not work |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-03-17 : 10:47:19
|
Class = 'incomecc' AND Class ='incomecheck' <-- That will always return false. Perhaps you want to use OR rather than AND? SELECT SUM(Amount) AS SalesFROM BankWHERE Class = 'incomecc' OR Class ='incomecheck' That will add up the amounts for the two classes 'incomecc' and 'incomecheck' |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2013-03-17 : 10:49:48
|
SELECT SUM(CASE WHEN Class in ('incomecc','incomecheck') THEN Amount ELSE 0 END)FROM BankJimEveryday I learn something that somebody else already knew |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2013-03-17 : 10:58:06
|
Actually, I think there's something else you're grouping on here? If you want Amounts of people who paid by check and cc (not just one or the other),then you'd need something likeSELECT customer, SUM(Amount)FROM bankGROUP BY customerhaving sum(case when Class = 'incomecc' then Amount else 0 end) > 0and sum(case when Class = 'incomecheck' then Amount else 0 end) > 0JimEveryday I learn something that somebody else already knew |
|
|
sqlrob
Starting Member
2 Posts |
Posted - 2013-03-17 : 11:02:31
|
thank you --great! |
|
|
|
|
|