SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 WHERE clause help needed
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sqlrob
Starting Member

2 Posts

Posted - 03/17/2013 :  10:34:45  Show Profile  Reply with Quote
SELECT SUM(Amount) AS Sales
FROM Bank
WHERE
Class = 'incomecc' AND Class ='incomecheck'


want to total the Amount column when the Class column has incomecc and incomecheck...this does not work

Edited by - sqlrob on 03/17/2013 10:44:16

James K
Flowing Fount of Yak Knowledge

3587 Posts

Posted - 03/17/2013 :  10:47:19  Show Profile  Reply with Quote
Class = 'incomecc' AND Class ='incomecheck' <-- That will always return false. Perhaps you want to use OR rather than AND?
SELECT SUM(Amount) AS Sales
FROM Bank
WHERE 
Class = 'incomecc' OR Class ='incomecheck'

That will add up the amounts for the two classes 'incomecc' and 'incomecheck'
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 03/17/2013 :  10:49:48  Show Profile  Reply with Quote
SELECT SUM(CASE WHEN Class in ('incomecc','incomecheck') THEN Amount ELSE 0 END)
FROM Bank

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 03/17/2013 :  10:58:06  Show Profile  Reply with Quote
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 like

SELECT customer, SUM(Amount)
FROM bank
GROUP BY customer
having sum(case when Class = 'incomecc' then Amount else 0 end) > 0
and sum(case when Class = 'incomecheck' then Amount else 0 end) > 0

Jim


Everyday I learn something that somebody else already knew

Edited by - jimf on 03/17/2013 10:58:48
Go to Top of Page

sqlrob
Starting Member

2 Posts

Posted - 03/17/2013 :  11:02:31  Show Profile  Reply with Quote
thank you --great!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000