Count doesn't work: query wrong!

I can't understand because this SQL query doesn't work:

SELECT COUNT Department,
IF(Department = 'toys', COUNT(), 0) AS numberOfToys,
IF(Department = 'health', COUNT(
), 0) AS numberOfHealth


Department - Value
toys - A
toys - B
toys - C
health - K
health - F
toys - G
toys - R
toys - W
toys - Q

I'd like to count number of occurrences about both toys record and health ones into 2 columns.

department numberOfToys numberOfHealth

toys 7 0
health 0 2

WHY ?!


is this question for Microsoft SQL Server ?

For mysql but concept is the same, I think. What do you think?

concept and syntax two different things. question for you what if there is a new department, are you planning to come and change the query every time there is a new department or would you like this to be dynamic

First option, no dynamic action.
I'm not able to understand because the following example (using group by works)
I think COUNT should have the same behaviour but this time inside every group, what's difference?
Because here It works and in previous example , query fails ?!

SELECT Department, IF(Department = 'toys', COUNT(), NULL) AS numberOfToys, IF(Department = 'health', COUNT(), 0) AS numberOfHealth
GROUP BY Department

in tsql sql server

select department,count(*)
group by department

will work ...!!!

dont know if this helps you
if it helps great :slight_smile: :slight_smile: