Count Distinct With IF in MySQL?
Posted
by
user1600801
on Stack Overflow
See other posts from Stack Overflow
or by user1600801
Published on 2012-09-25T09:36:16Z
Indexed on
2012/09/25
9:37 UTC
Read the original article
Hit count: 409
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.
© Stack Overflow or respective owner