tags:

views:

41

answers:

1

I'm trying to count how many created vs published posts there are per user in my database.

POST ID | USER ID | STATUS 

...and an example would be

User ID 1 has 5 posts (5 distinct post IDs) with 3 STATUS = CREATED and 2 STATUS = PUBLISHED. I want the output to show the following columns

USER  CREATED  PUBLISHED 
----------------------------
 1    3        2
+1  A: 

Use:

  SELECT t.user,
         SUM(CASE WHEN t.status = 'CREATED' THEN 1 ELSE 0 END) AS created,
         SUM(CASE WHEN t.status = 'PUBLISHED' THEN 1 ELSE 0 END) AS published
    FROM YOUR_TABLE t
GROUP BY t.user
OMG Ponies
does this work for MYSQL? (nevermind, got it to work...am goign to accept your answer in 2 minutes) thanks for the help ponies
st4ck0v3rfl0w