返回过去24小时计数的记录

时间:2015-01-20 09:09:27

标签: sql sql-server database sql-server-2012

我正在尝试获取过去24小时内的记录,按小时按SQL Server中的计数分组?

我有以下样本数据:

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... 

因此,计数基于每小时的记录数量。

5 个答案:

答案 0 :(得分:3)

您可以round your values to the nearest hour,然后只需GROUP和COUNT:

SQL Fiddle Demo

MS SQL Server架构设置

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')
;

查询以返回汇总数据

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()返回当前日期时间值并从该点开始过去24小时。上面的查询使用从下面返回的值WHERE子句:

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

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

答案 1 :(得分:1)

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?

答案 2 :(得分:1)

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

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
.....

答案 3 :(得分:0)

它可能会帮助你

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

答案 4 :(得分:0)

试试这个

对于SQL SERVER

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