snippetsqlMajor
Do you know an easy way to generate one record for each hour of the past 12 hours?
Viewed 0 times
theknowyoueachhourshourwaypasteasygenerate
Problem
I have a report that shows the count of events for the past 12 hours, grouped by the hour. Sounds easy enough, but what I am struggling with is how to include records that cover the gaps.
Here is an example table:
Data looks like this:
I need to create a result set that has one record for every hour of the past 12 hours, regardless of there being events during that hour or not.
Assuming the current time is '2012-03-08 11:00:00', the report would show (roughly):
I came up with a solution that uses a table that has one record for every hour of the day. I managed to get the results I was looking for using a UNION and some convoluted case logic in the where clause, but I was hoping somebody had a more elegant solution.
Here is an example table:
Event
(
EventTime datetime,
EventType int
)Data looks like this:
'2012-03-08 08:00:04', 1
'2012-03-08 09:10:00', 2
'2012-03-08 09:11:04', 2
'2012-03-08 09:10:09', 1
'2012-03-08 10:00:17', 4
'2012-03-08 11:00:04', 1I need to create a result set that has one record for every hour of the past 12 hours, regardless of there being events during that hour or not.
Assuming the current time is '2012-03-08 11:00:00', the report would show (roughly):
Hour EventCount
---- ----------
23 0
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 1
9 3
10 1I came up with a solution that uses a table that has one record for every hour of the day. I managed to get the results I was looking for using a UNION and some convoluted case logic in the where clause, but I was hoping somebody had a more elegant solution.
Solution
For SQL Server 2005+ you can generate those 12 records very easily with a loop ar a recursive CTE. Here is an example of a recursive CTE:
Then you just nedd to join it with your events table.
DECLARE @Date DATETIME
SELECT @Date = '20120308 11:00:00'
;WITH Dates AS
(
SELECT DATEPART(HOUR,DATEADD(HOUR,-1,@Date)) [Hour],
DATEADD(HOUR,-1,@Date) [Date], 1 Num
UNION ALL
SELECT DATEPART(HOUR,DATEADD(HOUR,-1,[Date])),
DATEADD(HOUR,-1,[Date]), Num+1
FROM Dates
WHERE Num <= 11
)
SELECT [Hour], [Date]
FROM DatesThen you just nedd to join it with your events table.
Code Snippets
DECLARE @Date DATETIME
SELECT @Date = '20120308 11:00:00'
;WITH Dates AS
(
SELECT DATEPART(HOUR,DATEADD(HOUR,-1,@Date)) [Hour],
DATEADD(HOUR,-1,@Date) [Date], 1 Num
UNION ALL
SELECT DATEPART(HOUR,DATEADD(HOUR,-1,[Date])),
DATEADD(HOUR,-1,[Date]), Num+1
FROM Dates
WHERE Num <= 11
)
SELECT [Hour], [Date]
FROM DatesContext
StackExchange Database Administrators Q#14661, answer score: 20
Revisions (0)
No revisions yet.