want to count only if variable is equal to xx

Anthony Matovu
Anthony Matovu used Ask the Experts™
on
I want to count in if a condition variable is equal to 7 in an sql statement using the statement like below

count(case when t.usaget = 7 then totalc else 0 end) as cnt_ussd1111
......
......

group by cnumber

write now it gives me occurances for cnumber.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AnujSQL Server DBA
Top Expert 2011

Commented:
This is because of group by cnumber, if you are not using Aggregate functions please remove Group by cNumber

Commented:
SELECT COUNT(Totalc) AS cnt_ussd1111 FROM Table WHERE t.usaget = 7

or do you mean SUM(Totalc)?
Business Analyst, MTN Uganda
Commented:
I have just found out this works

sum(case when t.usaget = 7 then 1 else 0 end) as cnt_ussd1111


Thanks

Commented:
Probably, but why use CASE when a simple SELECT works? :)

Commented:
SELECT COUNT(*) AS cnt_ussd1111 FROM Table WHERE t.usaget = 7
Anthony MatovuBusiness Analyst, MTN Uganda

Author

Commented:
Problem i want to produce different columns for different values of t.usaget
thanks
Anthony MatovuBusiness Analyst, MTN Uganda

Author

Commented:
Probably I did not explain my self well, but searched up the net and found out this was the best solution to what i wanted.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial