开发者

SQL Select Help

开发者 https://www.devze.com 2023-01-18 09:36 出处:网络
I\'m trying to count how many created vs published posts there are per user in my data开发者_如何学JAVAbase.

I'm trying to count how many created vs published posts there are per user in my data开发者_如何学JAVAbase.

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消