我正在尝试计算我的数据库中每个用户创建的已发布帖子数量。
POST ID | USER ID | STATUS
......一个例子是
用户ID 1有5个帖子(5个不同的帖子ID),其中3个STATUS = CREATED,2个STATUS = PUBLISHED。我希望输出显示以下列
USER CREATED PUBLISHED
----------------------------
1 3 2
答案 0 :(得分:1)
使用:
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