newjeep19
asked on
Get a count of records over the past 24 hours from a SQL table
I need to get the count of error messages from a SQL database table over the past 24 hours.
Example:
Time: Count:
1:00 AM 5 errors
2:00 AM 1 error
3:00 AM 10 errors
4:00 AM 5 errors
.......
12:00 PM 0 errors
.........
10:00 PM 6 errors
and so on. Below is my attempt but it is not correct.
select COUNT (messageType) as [Count], Datestamp
from Table1
where Datestamp between dateadd(hour, -24, CURRENT_TIMESTAMP)
AND CURRENT_TIMESTAMP
GROUP BY CAST(Datestamp as DATE) ,CASE
WHEN DATEPART(HOUR, Datestamp) > 12 THEN DATEPART(hour, Datestamp) - 12
ELSE DATEPART(Hour, Datestamp)
END , dateadd(hour, datediff(hour, 0, Datestamp), 0), Datestamp
ORDER BY dateadd(hour, datediff(hour, 0, Datestamp), 0) ASC;
Example:
Time: Count:
1:00 AM 5 errors
2:00 AM 1 error
3:00 AM 10 errors
4:00 AM 5 errors
.......
12:00 PM 0 errors
.........
10:00 PM 6 errors
and so on. Below is my attempt but it is not correct.
select COUNT (messageType) as [Count], Datestamp
from Table1
where Datestamp between dateadd(hour, -24, CURRENT_TIMESTAMP)
AND CURRENT_TIMESTAMP
GROUP BY CAST(Datestamp as DATE) ,CASE
WHEN DATEPART(HOUR, Datestamp) > 12 THEN DATEPART(hour, Datestamp) - 12
ELSE DATEPART(Hour, Datestamp)
END , dateadd(hour, datediff(hour, 0, Datestamp), 0), Datestamp
ORDER BY dateadd(hour, datediff(hour, 0, Datestamp), 0) ASC;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks the query works great
line 1 should be
ie, from 0 to 23... somehow it is ripped when I pasted :)
I am assuming you figured it out...
with h as (select 0 n union select 1 union ... union select 23)
ie, from 0 to 23... somehow it is ripped when I pasted :)
I am assuming you figured it out...
Open in new window