oracle按小时分组查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle按小时分组查询相关的知识,希望对你有一定的参考价值。
A B
09/03/2013 17:40:51 24,760
09/03/2013 17:42:45 16,660
09/03/2013 17:45:02 25,050
09/03/2013 17:48:34 25,370
09/04/2013 09:35:38 25,690
09/04/2013 09:40:41 25,715
09/03/2013 10:15:05 25,685
09/04/2013 09:52:18 25,105
09/04/2013 09:54:40 25,160
09/04/2013 09:56:00 25,090
09/01/2013 09:18:02 25,000
09/01/2013 09:25:18 18,380
09/01/2013 09:26:33 25,630
09/01/2013 09:30:02 25,580
09/01/2013 09:37:18 25,655
09/03/2013 17:44:12 25,150
09/03/2013 17:45:45 25,230
表T中A,B两个字段,其中A按每天00:30——08:30;08:30——16:30;16:30——00:30三个时间段统计B的总量。求助高手,高分悬赏!
decode(tl, '00:00——00:30', to_char(a-1,'yyyymmdd'), to_char(a,'yyyymmdd')) as 天,
decode(tl, '00:00——00:30', '16:31——00:30', '16:31——00:00', '16:31——00:30', t1) as 时间段,
sum(b)
from
(
select
a,
case
when to_char(a, 'hhmi') <='0030' then '00:00——00:30'
when to_char(a, 'hhmi') between '0031' and '0830' then '00:30——08:30'
when to_char(a, 'hhmi') between '0031' and '1630' then '00:31——16:30'
when to_char(a, 'hhmi') >= '1631' then '16:31——00:00'
else ''
end as tl,
b
from table_name
)
group by
decode(tl, '00:00——00:30', to_char(a-1,'yyyymmdd'), to_char(a,'yyyymmdd')),
decode(tl, '00:00——00:30', '16:31——00:30', '16:31——00:00', '16:31——00:30', t1)追问
你好,我套用后已修改如下图,执行时报错:
ORA-00979: 不是 GROUP BY 表达式
请问该如何修改?
不匹配, 将 16:30:01---00:00:00 改为 16:30:01---23:59:59
神牛,能不能帮我把语句写出来?
追答。。下班没环境,如果需要明天早上帮你谢谢哈,话说楼上的是不是也可以用
MySQL 按小时分组
【中文标题】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 我在没有你所有数据的情况下编写了这段代码。你应该按照你想要的方式改变它。它只是给你一个想法。以上是关于oracle按小时分组查询的主要内容,如果未能解决你的问题,请参考以下文章