查找按月分组的唯一用户-SQL

时间:2018-06-28 23:20:19

标签: sql

我有下表,并且我想查找例如触发了事件“ B”的唯一身份用户总数。然后按月分组。

表格

enter image description here

谢谢。

2 个答案:

答案 0 :(得分:0)

这有帮助吗?

SELECT MONTH, COUNT(USER_ID)
FROM TABLE1
WHERE EVENT = 'B'
GROUP BY MONTH
ORDER BY MONTH

答案 1 :(得分:0)

要仅获取曾经触发过B的用户,可以使用NOT EXISTS

SELECT *
       FROM elbat t1
       WHERE t1.event = 'B'
             AND NOT EXISTS (SELECT *
                                    FROM elbat t2
                                    WHERE t2.user_id = t1.user_id
                                          AND t2.event <> 'B');

我不确定“然后按月分组”的意思。也许每月计数?

SELECT t1.month,
       count(*)
       FROM elbat t1
       WHERE t1.event = 'B'
             AND NOT EXISTS (SELECT *
                                    FROM elbat t2
                                    WHERE t2.user_id = t1.user_id
                                          AND t2.event <> 'B')
       GROUP BY t1.month;