Count Distinct With IF in MySQL?
- by user1600801
I need to do a query with count distinct and IF, but the results always are 0.
What I need to do, is count the different users from a table in different months, using IF. My individual query, for one month is this:
SELECT
COUNT(DISTINCT(idUsers)) AS num_usuarios
FROM table01
WHERE date1='201207'
But I need to get the results by different months in the same query. What I'm trying to do is this:
SELECT
IF(date1=(201207), count(distinct(idUsers)), 0) as user30,
IF(fecha1=(201206), count(distinct(idUsers)), 0) as user60,
IF(fecha1=(201205), count(distinct(idUsers)), 0) as user90,
IF(fecha1=(201204), count(distinct(idUsers)), 0) as user120,
IF(fecha1=(201203), count(distinct(idUsers)), 0) as user150
FROM table01
But the all the results are always 0.