T-SQL datepart, counts and unions

53 Views Asked by At

I am having issues with counting the number of events by date and hour that are recorded recorded across multiple tables.

I have a system manufacturer's database with multiple 'events' tables that are all formatted identically (same number of columns and data types in the same order) that each hold around 100,000 transaction events that looks like this:

EventID EventTimestamp              EventType   EventSubType    UnitGuid        DeviceGuid

1       2022-04-16 15:14:43.000     515         0               AAAA            BBBB
2       2022-04-16 15:14:44.000     520         0               AAAA            CCCC
3       2022-04-16 15:14:44.000     520         0               AAAA            BBBB

Because each table holds ~100,000 records, events that occur on a single day can be spread over one or more tables.

I am interested in obtaining a count of the total number of events per hour, per day which I am able to do on a table by table basis with the following query:

select DATEPART(DAY,EventTimestamp) as 'event date', DATEPART(HOUR,EventTimestamp) as 'event hour', count(*) as 'number of events'

from  Events_70
    
group by datepart(day,eventtimestamp), datepart(hour,eventtimestamp)
order by datepart(day,eventtimestamp), datepart(hour,eventtimestamp)

which produces output like:

event date      event hour      number of events
16              15              3966
16              16              4530
16              17              4357
...             ...             ...

I've been able to consolidate the data for multiple days in excel with a little manual work but there's got to be better way in SQL...

when i try to union two tables together with the following query:

select DATEPART(DAY,EventTimestamp) as 'event date', DATEPART(HOUR,EventTimestamp) as 'event hour', count(*) as 'number of events'
from  Events_70
  union all
select DATEPART(DAY,EventTimestamp) as 'event date', DATEPART(HOUR,EventTimestamp) as 'event hour', count(*) as 'number of events'
from  Events_71

group by datepart(day,eventtimestamp), datepart(hour,eventtimestamp)
order by datepart(day,eventtimestamp), datepart(hour,eventtimestamp)

I am met with:

Column 'Events_70.EventTimestamp' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 104, Level 16, State 1, Line 85
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
Msg 104, Level 16, State 1, Line 85
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.

I'm also guessing that a sum of counts for the same hour and day across two tables might be required but i've not got that far yet where one table's output looks like:

event date      event hour      number of events
19              21              2460
19              22              1963
**19                23              435**

And the next table's output looks like:

event date      event hour      number of events
**19                23              1057**
20              00              867
20              01              930

I've been searching around various forums this morning and haven't found a solution, any help would be appreciated.

0

There are 0 best solutions below