Return 条最近 24 小时的记录

Return records with counts for the last 24 hours

我正在尝试获取过去 24 小时的记录,按小时分组并在 SQL 服务器中进行计数?

我有这样的示例数据:

ID   Dat
1    2015-01-19 10:29:00.000
2    2015-01-19 11:29:00.000
3    2015-01-19 11:29:00.000
4    2015-01-19 11:29:00.000
5    2015-01-19 12:29:00.000
6    2015-01-19 12:29:00.000
7    2015-01-19 12:29:00.000
8    2015-01-19 12:29:00.000
9    2015-01-17 13:29:00.000
10   2015-01-17 13:29:00.000
11   2015-01-17 13:29:00.000
12   2015-01-17 13:29:00.000
13   2015-01-17 13:29:00.000
14   2015-01-17 13:29:00.000
15   2015-01-17 14:29:00.000
17   2015-01-17 15:29:00.000
18   2015-01-17 15:29:00.000
19   2015-01-17 16:29:00.000
20   2015-01-17 16:29:00.000
21   2015-01-15 16:29:00.000
22   2015-01-15 17:29:00.000
23   2015-01-15 18:29:00.000
24   2015-01-15 18:29:00.000
25   2015-01-15 18:29:00.000
26   2015-01-15 18:29:00.000
27   2015-01-15 18:29:00.000
28   2015-01-15 18:29:00.000
29   2015-01-15 19:29:00.000
30   2015-01-10 20:29:00.000

现在假设当前日期时间是 2015-01-19 12:30:00.000,我想要的输出是:

Date                        Count
2015-01-19 12:00:00.000     4
2015-01-19 11:00:00.000     3
2015-01-19 10:00:00.000     1   
2015-01-19 09:00:00.000     0
2015-01-19 08:00:00.000     0
2015-01-19 07:00:00.000     0
2015-01-19 06:00:00.000     0
2015-01-19 05:00:00.000     4   
and so on... 

所以计数是基于每小时的记录数。

可能对你有帮助

declare  @t table(t datetime)
insert into @t values(getdate()),(getdate())

SELECT cast(cast(getdate() as date) as datetime)+cast(datepart(hour,getdate()) as float)/24, count(*) 
from @t 
group by cast(cast(getdate() as date) as datetime)+cast(datepart(hour,getdate()) as float)/24
create table #temptable
(
    ID int ,    
    Date datetime 
)
go
insert into #temptable (id, Date)
select 1 , '2014-01-19 10:29:00.000' union all
select 2 , '2014-01-19 11:29:00.000' union all
select 3 , '2014-01-19 11:29:00.000' union all
select 4 , '2014-01-19 11:29:00.000' union all
select 5 , '2014-01-19 09:29:00.000' union all
select 6 , '2014-01-19 08:29:00.000' union all
select 7 , '2014-01-19 03:29:00.000'

SELECT CAST(Date as date) AS ForDate,
       DATEPART(hour,Date) AS OnHour,
       COUNT(*) AS Totals
FROM #temptable
GROUP BY CAST(Date as date),
       DATEPART(hour,Date)

SQL Server Group by Count of DateTime Per Hour?

您可以 round your values to the nearest hour 然后简单地 GROUP 和 COUNT:

SQL Fiddle Demo

MS SQL 服务器架构设置

CREATE TABLE DateTable
    ([ID] int, [Date] datetime)
;

INSERT INTO DateTable
    ([ID], [Date])
VALUES
    (1, '2015-01-19 10:29:00'),
    (2, '2015-01-19 11:29:00'),
    (3, '2015-01-19 11:29:00'),
    (4, '2015-01-19 11:29:00'),
    (5, '2015-01-19 12:29:00'),
    (6, '2015-01-19 12:29:00'),
    (7, '2015-01-19 12:29:00'),
    (8, '2015-01-19 12:29:00'),
    (9, '2015-01-17 13:29:00'),
    (10, '2015-01-17 13:29:00'),
    (11, '2015-01-17 13:29:00'),
    (12, '2015-01-17 13:29:00'),
    (13, '2015-01-17 13:29:00'),
    (14, '2015-01-17 13:29:00'),
    (15, '2015-01-17 14:29:00'),
    (17, '2015-01-17 15:29:00'),
    (18, '2015-01-17 15:29:00'),
    (19, '2015-01-17 16:29:00'),
    (20, '2015-01-17 16:29:00'),
    (21, '2015-01-15 16:29:00'),
    (22, '2015-01-15 17:29:00'),
    (23, '2015-01-15 18:29:00'),
    (24, '2015-01-15 18:29:00'),
    (25, '2015-01-15 18:29:00'),
    (26, '2015-01-15 18:29:00'),
    (27, '2015-01-15 18:29:00'),
    (28, '2015-01-15 18:29:00'),
    (29, '2015-01-15 19:29:00'),
    (30, '2015-01-10 20:29:00')
;

查询到 return 汇总数据:

SELECT  DATEADD(HOUR, DATEDIFF(HOUR, 0, [DATE]), 0) As [DateValue],
        COUNT(1) AS [COUNT]
FROM    DateTable
WHERE [DATE] >= DATEADD(day, -1, GETDATE())
GROUP BY DATEADD(HOUR, DATEDIFF(HOUR, 0, [DATE]), 0)
ORDER BY 1

Results:

|                      DATEVALUE | COUNT |
|--------------------------------|-------|
| January, 19 2015 10:00:00+0000 |     1 |
| January, 19 2015 11:00:00+0000 |     3 |
| January, 19 2015 12:00:00+0000 |     4 |

这是使用 GETDATE() 到 return 当前日期时间值并从该点开始的最后 24 小时。上面的查询使用 return 从下面为 WHERE 子句编辑的值:

SELECT DATEADD(day, -1, GETDATE())

如果需要,您可以用变量替换 WHERE 子句中的过滤器值。

试试这个

对于SQL服务器

select CAST( MyDateColumn as DATE) ,DATEPART(HOUR, MyDateColumn),COUNT(*)
from MyTable 
GROUP BY CAST( MyDateColumn as DATE) ,DATEPART(HOUR, MyDateColumn)

试试这个,它也会计算没有数据的小时数:

DECLARE @t table(ID int, Date datetime)
INSERT @t values
(1,'2015-01-19 10:29:00.000'), (2,'2015-01-19 11:29:00.000'), 
(3,'2015-01-19 11:29:00.000'), (4,'2015-01-19 11:29:00.000'), 
(5,'2015-01-19 12:29:00.000'), (6,'2015-01-19 12:29:00.000'), 
(7,'2015-01-19 12:29:00.000'), (8,'2015-01-19 12:29:00.000'), 
(9,'2015-01-17 13:29:00.000'), (10,'2015-01-17 13:29:00.000'), 
(11,'2015-01-17 13:29:00.000'),(12,'2015-01-17 13:29:00.000'), 
(13,'2015-01-17 13:29:00.000'),(14,'2015-01-17 13:29:00.000'), 
(15,'2015-01-17 14:29:00.000'),(17,'2015-01-17 15:29:00.000'), 
(18,'2015-01-17 15:29:00.000'),(19,'2015-01-17 16:29:00.000'), 
(20,'2015-01-17 16:29:00.000'),(21,'2015-01-15 16:29:00.000'), 
(22,'2015-01-15 17:29:00.000'),(23,'2015-01-15 18:29:00.000'), 
(24,'2015-01-15 18:29:00.000'),(25,'2015-01-15 18:29:00.000'), 
(26,'2015-01-15 18:29:00.000'),(27,'2015-01-15 18:29:00.000'), 
(28,'2015-01-15 18:29:00.000'),(29,'2015-01-15 19:29:00.000'), 
(30,'2015-01-10 20:29:00.000')

DECLARE @yourdate datetime = '2015-01-19T12:30:00.000'

;WITH CTE AS
(
  SELECT dateadd(hh, datediff(hh, 0, @yourdate), 0) Date
  UNION ALL
  SELECT dateadd(hh, -1, Date)
  FROM CTE 
  WHERE Date + 1 > @yourdate
)
SELECT CTE.Date, count(t.id) count
FROM CTE
LEFT JOIN @t t
ON CTE.Date <= t.Date
and dateadd(hh, 1, CTE.Date) > t.Date
GROUP BY CTE.Date
ORDER BY CTE.Date DESC

结果:

Date                    Count
2015-01-19 12:00:00.000 4
2015-01-19 11:00:00.000 3
2015-01-19 10:00:00.000 1
2015-01-19 09:00:00.000 0
2015-01-19 08:00:00.000 0
.....