MySQL 按小时分组

Posted

技术标签:

【中文标题】MySQL 按小时分组【英文标题】:MySQL group by hour epoch time 【发布时间】:2020-10-03 08:13:11 【问题描述】:

所以这是为了显示我的平台在一个小时的范围内有多少连接。

我注释“变量”的地方是将从前端接收的内容,此查询将在后端运行以填充图表。

SELECT
    count(server_events.event_type),
    server_events.time_stamp,
    hotspots.partner_id,
    hotspots.partner,
    hotspots.operator_id
FROM
    `msp-data`.server_events
INNER JOIN 
    `adserver`.hotspots, `adserver`.operator
WHERE 
    server_events.time_stamp between "1591930800" and "1592017199" -- variable
    and server_events.event_type = "auth_final"
    and server_events.nas_id = adserver.hotspots.code
    and hotspots.partner_id = "1" -- variable
    and hotspots.operator_id = "2" -- variable
GROUP BY
    server_events.time_stamp div 3600
ORDER BY
    server_events.time_stamp

这是我当前的输出 我发布了完整的查询,但在这个输出中我没有得到合作伙伴和合作伙伴 ID 或操作员 ID 的过滤器

count time_stamp partner_id operator_id
6   1591944931  1   1
12  1591945711  1   5
6   1591952103  1   1
36  1591952621  1   1
18  1591956063  1   1
12  1591962118  1   4
6   1591966538  1   1
6   1591968554  1   1
12  1591973267  1   5
18  1591976918  1   1
18  1591978620  1   5
12  1591983139  1   5
12  1591984830  1   1
24  1591989873  1   1
12  1591993080  1   1
30  1591995612  1   1

output

预期的输出是

10 1591930800-1591934399 
15 1591934400-1591937999

【问题讨论】:

您的查询应该失败。 SELECT 列与 GROUP BY 不兼容。你需要更好地解释你想做什么。 顺便说一句,在表/列标识符中包含数学运算符是一个非常糟糕的主意。 编辑问题以将示例数据和所需输出显示为文本表。 我期望知道每小时有多少“auth_final” event_type 之类的。 10 1591930800 - 1591934399 15 1591934400 - 1591937999 @FábioMagnoni 如果您将sqlfiddle.com 上的某些内容与您的表模式和此测试数据放在一起,那么其他人将更容易帮助您找出查询本身,因为我们将有一个工作构建的示例。请阅读how to ask 了解更多信息。让其他人更容易测试和探索您的数据将使您更容易获得有效的解决方案。 :) 【参考方案1】:

尝试在下面执行。并尝试考虑 Strawberry 的建议。

SELECT SUM(CNT), TM FROM(SELECT DISTINCT
    count(server_events.event_type) OVER(PARTITION BY hotspots.partner_id,hotspots.partner,hotspots.operator_id,server_events.time_stamp/3600) CNT
    server_events.time_stamp/3600 TM
FROM
    `msp-data`.server_events
INNER JOIN 
    `adserver`.hotspots, `adserver`.operator
WHERE 
    server_events.time_stamp between "1591930800" and "1592017199" -- variable
    and server_events.event_type = "auth_final"
    and server_events.nas_id = adserver.hotspots.code
    and hotspots.partner_id = "1" -- variable
    and hotspots.operator_id = "2" -- variable
)    
GROUP BY TM
ORDER BY
    2

mysql版本12下

SELECT SUM(CNT), TM FROM(SELECT 
    count(A.event_type) cnt,
hotspots.partner_id,hotspots.partner,hotspots.operator_id,tm
FROM
    (SELECT A.time_stamp/3600 AS TM,A.* FROM `msp-data`.server_events A ) A
INNER JOIN 
    `adserver`.hotspots, `adserver`.operator
WHERE 
    A.time_stamp between "1591930800" and "1592017199" -- variable
    and A.event_type = "auth_final"
    and A.nas_id = adserver.hotspots.code
    and hotspots.partner_id = "1" -- variable
    and hotspots.operator_id = "2" -- variable
    group by hotspots.partner_id,hotspots.partner,hotspots.operator_id,TM
)     aliaz
GROUP BY TM
ORDER BY
    2

【讨论】:

查询的输出是6 442206.9253 1 1 6 442207.1419 1 5 6 442207.9306 1 5 6 442208.9175 1 1 6 442209.0614 1 1 6 442209.2531 1 1 6 442209.3533 1 4 6 442209.4050 1 4 6 442209.4150 1 2 6 442209.4969 1 4 6 442210.0175 1 1 6 442210.1089 1 1 6 442210.4747 1 1 6 442211.6994 1 4 6 442211.7619 1 5 6 442212.9272 1 1 6 442213.4872 1 1 6 442214.0711 1 4 6 442214.7964 1 5 6 442215.4461 1 1 6 442215.5736 1 1 6 442215.8106 1 1 6 442216.2833 1 5 6 442216.2908 1 2 6 442216.7419 1 1 6 442217.5386 1 5 6 442217.6267 1 5 6 442218.0083 1 1 6 442218.3764 1 1 编辑了我的答案。检查它@FábioMagnoni 错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 3 行的“(PARTITION BY hotspots.partner_id,hotspots.partner,hotspots.operator_id,server_e'附近使用正确的语法 您的 db 版本似乎低于 12。请尝试其他解决方案。 错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 3 行的“server_events.time_stamp/3600 TM FROM msp-data.server_events INNER JOIN”附近使用正确的语法【参考方案2】:

@ismetguzelgun

SELECT SUM(CNT), TM FROM(SELECT 
    count(A.event_type) cnt,
    hotspots.partner_id,
    hotspots.partner,
    hotspots.operator_id,
    TM
FROM
    (SELECT 
        A.time_stamp/3600 AS TM,
        A.* FROM `msp-data`.server_events A
    ) A
INNER JOIN 
    `adserver`.hotspots, 
    `adserver`.operator
WHERE 
    A.time_stamp between "1591930800" and "1592017199" -- variable
    and A.event_type = "auth_final"
    and A.nas_id = adserver.hotspots.code
--     and hotspots.partner_id = "1" -- variable
--     and hotspots.operator_id = "2" -- variable
    GROUP BY
        hotspots.partner_id,
        hotspots.partner,
        hotspots.operator_id,
        TM
) B    
GROUP BY TM
ORDER BY
    2

现在给出这个输出:

SUM(CNT) TM
6   442206.9253
6   442207.1419
6   442207.9306
6   442208.9175
6   442209.0614
6   442209.2531
6   442209.3533
6   442209.4050
6   442209.4150
6   442209.4969
6   442210.0175
6   442210.1089
6   442210.4747
6   442211.6994
6   442211.7619
6   442212.9272
6   442213.4872
6   442214.0711
6   442214.7964
6   442215.4461
6   442215.5736
6   442215.8106
6   442216.2833
6   442216.2908
6   442216.7419
6   442217.5386
6   442217.6267
6   442218.0083
6   442218.3764
6   442219.4092
6   442219.6553
6   442219.8953
6   442219.9933
6   442220.3000
6   442220.9611
6   442221.0033
6   442221.0297
6   442221.6656
6   442221.7533
6   442221.7897

【讨论】:

您应该在您的 TM 上添加案例陈述。比如 TM> 1591930800 AND TM 我在没有你所有数据的情况下编写了这段代码。你应该按照你想要的方式改变它。它只是给你一个想法。

以上是关于MySQL 按小时分组的主要内容,如果未能解决你的问题,请参考以下文章

Mysql按周,按月,按日,按小时分组统计数据

oracle中怎么按每小时分组。数据如下:

oracle按小时分组查询

Sql按36小时分组[关闭]

如何按小时对 HKStatistics 进行分组?

Informix 按小时分组