[英]Group and count by minute of day using MySQL
我想從大型時間戳數據庫中獲取一天中每一分鍾的計數。 我希望能夠根據數據構建圖表。
那么一天有 1440 分鍾,我如何計算每分鍾的計數?
我可以在一小時內獲得每分鍾的計數,我對如何擴展以獲取一天中每一分鍾的計數感到困惑。
我有我目前正在使用的代碼,
SELECT FROM_UNIXTIME(
CEILING(UNIX_TIMESTAMP(`timestamp`)/900)*900
) AS timeslice
, COUNT(*) AS mycount
FROM visitor
WHERE `timestamp` >= '20012-01-01'
AND `timestamp` < '20012-02-14'
GROUP BY timeslice
我希望這是有道理的,並感謝您的幫助!
有任何問題請問我
擔
根據您可以嘗試使用以下查詢的記錄數(檢查其執行時間)。
select d.everyminute,
(select count(*) from visitor
where `timestamp` between d.everyminute and
d.everyminute + interval 59 second) cnt
from
(
select @rownum:=@rownum+1,
date('2012-02-15') + interval (@rownum-1) minute everyminute from
(select 0 union all select 1 union all select 2) t,
(select 0 union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7 union all
select 8 union all select 9) t1,
(select 0 union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7 union all
select 8 union all select 9) t2,
(select 0 union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7 union all
select 8 union all select 9) t3,
(select @rownum:=0) r where @rownum < 24*60
) d
結果是(我在測試數據庫中放入了一些時間戳)
2012-02-15 00:00:00 0
2012-02-15 00:01:00 0
2012-02-15 00:02:00 2
2012-02-15 00:03:00 1
2012-02-15 00:04:00 0
2012-02-15 00:05:00 1
2012-02-15 00:06:00 0
2012-02-15 00:07:00 0
2012-02-15 00:08:00 0
2012-02-15 00:09:00 0
2012-02-15 00:10:00 0
等等直到2012-02-15 23:59:00
現有記錄的時間戳為2012-02-15 00:02:00
、 2012-02-15 00:02:00
、 2012-02-15 00:03:00
和2012-02-15 00:05:00
ps:考慮到夏季/冬季時間跳躍,您可以將where @rownum < 24*60
重寫為having everyminute < (date('2012-02-15') + interval 1 day - interval 1 minute)
如您所見,即使在這一分鍾內 db 中沒有記錄,它也會每分鍾計數一次。
最簡單的方法是創建一個已包含分鍾的實用程序表。 如果你很聰明,你也會把小時和子午線。
您可以通過搜索數據倉庫時間維度找到更多信息。
看起來您不希望它按天/月/年分組,只需幾分鍾:
select minute, sum(if(adate is null, 0, 1)) DateAmount from (
select @num := @num + 1 as minute from
(select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all
select 7 union all select 8 union all select 9 union all
select 10 union all select 11 union all select 12) as t1,
(select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all
select 7 union all select 8 union all select 9 union all
select 10 union all select 11 union all select 12) as t2,
(select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all
select 7 union all select 8 union all select 9 union all
select 10) as t3,
(select @num := - 1) as t4
) as MinuteInDay
left join visitors v
on minute(v.aDate) = MinuteInDay.minute
group by minute
對於這組數據:
+---------------------+
| aDate |
+---------------------+
| 2012-01-01 00:00:00 |
| 2012-01-01 00:01:00 |
| 2012-01-01 00:02:00 |
| 2012-01-01 00:02:00 |
| 2012-01-02 00:02:00 |
| 2012-01-03 00:03:00 |
+---------------------+
這將導致:
+--------+------------+
| MINUTE | DATEAMOUNT |
+--------+------------+
| 0 | 1 |
| 1 | 1 |
| 2 | 3 |
| 3 | 1 |
| 4 | 0 |
| 5 | 0 |
| ... | ... |
| 1439 | 0 |
+--------+------------+
希望這可以幫助。
PS:如果您不需要 0 中的分鍾並且只忽略它們會容易得多!
我會做這樣的事情:
SELECT MOD(FLOOR(UNIX_TIMESTAMP(
timestamp )/60), 1440) AS timeslice, COUNT(*) AS mycount FROM visitor WHERE
時間戳>= '20012-01-01' AND
時間戳< '20012-02-14' GROUP BY timeslice
它將計算自 unix 時代以來 1440 分鍾的批次,忽略夏令時等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.