MySQL Forums
Forum List  »  InnoDB

How Count Distinct With IF
Posted by: Daniel Estrada
Date: September 25, 2012 03:45AM

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, and finally grouping by some value. My individual query, for one month is this:

SELECT
COUNT(DISTINCT(idUsers)) AS num_usuarios
FROM table01
WHERE date1='201207'
GROUPED BY cve_school

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
GROUPED BY cve_school


But the all the results are always 0.

Options: ReplyQuote


Subject
Views
Written By
Posted
How Count Distinct With IF
2108
September 25, 2012 03:45AM
750
September 27, 2012 10:47PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.