I have a query that get's the last 24 hours or records and counters them and then groups the records by an ID.
What I would like is for it to select the past 24 hours plus the first 8 hours of the current day
SELECT controllerID,
DATEPART (HOUR, dateadd(hour, datediff(hour, 0, dtReading), 0)) as TimeStampHour,
Count(*) As Count
FROM [ReaderData]
WHERE dtReading >= dateadd(day,datediff(day,1,GETDATE()),0)
AND dtReading < dateadd(day,datediff(day,0,GETDATE()),0)
AND (EventType = '(0x03)Door state low')
AND CardID = 'fffffff0'
GROUP BY controllerID, dateadd(hour, datediff(hour, 0, dtReading), 0)
ORDER BY controllerID, dateadd(hour, datediff(hour, 0, dtReading), 0);
Right now i can change the day that is being selected by changing the -1 and 0 in the where conditions to change the date. at present this query produces a result like this
controllerID TimeStampHour Count
13 0 129
13 1 114
13 2 104
13 3 96
13 4 111
13 5 114
13 6 97
13 7 116
13 8 62
13 9 82
13 11 62
13 12 112
13 13 78
13 14 20
13 15 11
13 16 116
13 17 122
13 18 106
13 19 126
13 20 125
13 21 105
13 22 122
13 23 16
28 10 12
28 16 8
30 0 162
30 1 161
30 2 161
30 3 62
30 4 61
30 5 62
As you can see there is a count for each hour for the controller ID
What i would like is for the hours to extend 8 hours in to the next day for example after the 23rd hour for controllerID 13 the TimeStampHour would be 0 but that would be the next day after the days that have already been selected