簡體   English   中英

獲取每小時發生的事件數

[英]Get number of events occurring per hour

我有一個包含事件和 Unix 時間戳的表:

event_id | start_time | end_time
----------------------------------
1        | 1485388800 | 1485410400
2        | 1485396000 | 1485403200
3        | 1485406800 | 1485414000

我想編寫一個查詢,它需要一個開始時間和一個結束時間,並告訴我每小時發生了多少事件。 給定開始時間 1485385200 和結束時間 1485414000,上表的結果將是:

event_count | time
------------------------
0           | 1485385200
1           | 1485388800
1           | 1485392400
2           | 1485396000
2           | 1485399600
1           | 1485403200
2           | 1485406800
1           | 1485410400
0           | 1485414000

編寫此查詢的最佳方法是什么? 我一直堅持生成這個范圍,也堅持檢查事件的范圍,最好不要多次閱讀表格,因為它可能非常大。

正如評論中提到的,最好的方法是加入某種日歷表。

但是,這里有一個(有點hacky)解決方案,不使用這樣的表。 它使用變量生成小時序列。

您唯一需要記住的是,您需要一個足夠大的表來容納您嘗試創建的時間間隔數(即,表的記錄數至少與您選擇的開始之間的小時數一樣多)和結束時間)。 在我的示例中,我使用了內置的mysql.help_topic表,但如果您的events表本身足夠大,您可以改用該表(或任何其他表)。

SET @x:=1485385200, @y:=1485414000;

SELECT COUNT(event_id) AS event_count, hours.start AS time
FROM (
    SELECT @x AS start, @x := @x + 3600 AS end
    FROM mysql.help_topic
    WHERE @x <= @y
) AS hours
LEFT JOIN events AS e
ON e.start_time < hours.end AND e.end_time > hours.start
GROUP BY hours.start

這為我提供了您提供的測試數據的以下輸出:

+-------------+------------+
| event_count | time       |
+-------------+------------+
|           0 | 1485385200 |
|           1 | 1485388800 |
|           1 | 1485392400 |
|           2 | 1485396000 |
|           2 | 1485399600 |
|           1 | 1485403200 |
|           2 | 1485406800 |
|           1 | 1485410400 |
|           0 | 1485414000 |
+-------------+------------+

它看起來有點亂,但有效。 這個想法是使用聯合制作一張日歷表並將其與您的表連接。 您可以根據您的要求傳遞開始時間和結束時間,我已經做了 24 小時

SELECT count(event_id) as event_count,t 
FROM
(
SELECT
DATE_FORMAT(FROM_UNIXTIME(`start_time`), '%Y-%m-%d %H:%i:%s') as start_time,
DATE_FORMAT(FROM_UNIXTIME(`end_time`), '%Y-%m-%d %H:%i:%s') as end_time,
tmp.*,
event_id
from test
right  JOIN
(
(select concat(date(now()),' ','00:00') as t)
UNION
(select concat(date(now()),' ','01:00') as t)
UNION
(select concat(date(now()),' ','02:00') as t)
UNION
(select concat(date(now()),' ','03:00') as t)
UNION
(select concat(date(now()),' ','04:00') as t)
UNION
(select concat(date(now()),' ','05:00') as t)
UNION
(select concat(date(now()),' ','06:00') as t)
UNION
(select concat(date(now()),' ','07:00') as t)
UNION
(select concat(date(now()),' ','08:00') as t)
UNION
(select concat(date(now()),' ','09:00') as t)
UNION
(select concat(date(now()),' ','10:00') as t)
UNION
(select concat(date(now()),' ','11:00') as t)
UNION
(select concat(date(now()),' ','12:00') as t)
UNION
(select concat(date(now()),' ','13:00') as t)
UNION
(select concat(date(now()),' ','14:00') as t)
UNION
(select concat(date(now()),' ','15:00') as t)
UNION
(select concat(date(now()),' ','16:00') as t)
UNION
(select concat(date(now()),' ','17:00') as t)
UNION
(select concat(date(now()),' ','18:00') as t)
UNION
(select concat(date(now()),' ','19:00') as t)
UNION
(select concat(date(now()),' ','20:00') as t)
UNION
(select concat(date(now()),' ','21:00') as t)
UNION
(select concat(date(now()),' ','22:00') as t)
UNION
(select concat(date(now()),' ','23:00') as t)
)tmp
on(cast(tmp.t as datetime) between DATE_FORMAT(FROM_UNIXTIME(`start_time`), '%Y-%m-%d %H:%i:%s') and DATE_FORMAT(FROM_UNIXTIME(`end_time`), '%Y-%m-%d %H:%i:%s'))
)xxx
group by t

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM