mysql - 计算用户点击 MySQL 中不同事件的数量

标签 mysql sql

我有一个名为“警报”的表,看起来像这样。

user_id event   alert_type       eventtime
1       clk       s         January, 19 2037 12:16:21
2       clk       b         January, 19 2037 00:28:25
3       clk       b         January, 19 2037 02:44:23
3       clk       b         January, 19 2037 00:16:25
1       clk       b         January, 19 2037 02:28:25
9       clk       s         January, 19 2037 22:40:25
9       clk       s         January, 19 2037 03:14:07
2       clk       b         January, 19 2037 03:22:07
6       clk       b         January, 19 2037 05:21:07
9       clk       p         January, 19 2037 15:24:07
3       clk       p         January, 19 2037 05:34:07
10      clk       s         January, 19 2037 12:39:07
4       clk       b         January, 19 2037 09:14:07
4       clk       s         January, 19 2037 09:34:07
1       clk       s         January, 19 2037 09:54:07
8       clk       b         January, 19 2037 13:14:07
8       clk       p         January, 19 2037 10:24:07
8       clk       p         January, 19 2037 13:45:07
10      clk       b         January, 19 2037 13:51:07
4       clk       b         January, 19 2037 14:14:07
12      clk       p         January, 19 2037 15:14:07
1       clk       s         January, 19 2037 00:44:07
10      clk       s         January, 19 2037 08:14:07
12      clk       s         January, 19 2037 16:24:07
7       clk       p         January, 19 2037 06:53:07
6       clk       b         January, 19 2037 20:14:07
7       clk       b         January, 19 2037 20:23:07

给定一个 alert_type,我试图计算在给定的一天有多少用户点击了一个提醒作为他们的第一个提醒

我想要的输出是

alert_type  Count of Users
b                4
p                3
s                3

我正在尝试这样做,但我没有得到我想要的东西

SELECT count(user_id) AS Count of Users, alert_type, MIN(eventtime)
FROM  alerts
GROUP BY alert_type
ORDER BY alert_type DESC;

请提供一些输入以实现所需的输出。

最佳答案

这是在一天内获得每个用户的第一个警报的一种方法:

select alert_type, count(*) as `Total Number of Users`
from alerts a
where a.eventtime = (select min(a2.eventtime)
                     from alerts a2
                     where a2.user_id = a.user_id and
                           date(a2.eventtime) = date(a.eventtime)
                    )
group by alert_type;

关于mysql - 计算用户点击 MySQL 中不同事件的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947921/

相关文章:

C#:包括没有 visual studio 的程序集

php - 从每个对话 sql 中获取最后一条消息

sql - 无组计数

mysql - 当我在两个表上执行 'select' 查询时,它会呈现表内的所有元素

sql - 在 Oracle 中描述查询结果的模式?

mysql - 有没有办法在mysql中查找具有相同列的行

php - 需要一些 GROUP_CONCAT MySQL 更好地理解请

php - 从表格收集的数据不完整

mysql - SQL 检查两个表是否具有特定 ID 的相同数据

mysql - rails : Scopes to find records where previous years of the same customer records exist