這是Oracle版本,在我的理解你看起來是什麼g下:
SELECT t_stamp
, MAX(count(t_stamp)) OVER (PARTITION BY t_stamp ORDER BY t_stamp) hits_per_hr_interval
FROM
(
SELECT name, EXTRACT(hour From Cast(t_stamp as timestamp)) t_stamp
FROM stack_test
)
GROUP BY t_stamp
/
T_STAMP HITS_PER_HR_INTERVAL
-------------------------------
12 3
13 2
或使用ROW_NUMBER():
SELECT * FROM
(
SELECT t_stamp
, Count(t_stamp) OVER (PARTITION BY t_stamp ORDER BY t_stamp) hits_per_hr_interval
, ROW_NUMBER() OVER (PARTITION BY t_stamp ORDER BY t_stamp) hr_seq
FROM
(
SELECT EXTRACT(hour From Cast(t_stamp as timestamp)) t_stamp
FROM stack_test
)
)
-- WHERE hr_seq = 1 -- optional to see only first row per group as in above example
/
T_STAMP HITS_PER_HR_INTERVAL HR_SEQ
------------------------------------------
12 3 1
12 3 2
12 3 3
13 2 1
13 2 2
您可以添加更多像您的小時12只有1之間的過濾器...
來源
2013-02-28 15:30:01
Art
是什麼類型的時間列? – 2013-02-28 11:57:27
DATETIME。如果查詢更容易,我們可以將其類型更改爲其他類型。 – user674669 2013-02-28 12:01:03
沒有日期時間字段很好 – 2013-02-28 12:02:17