i have this code below (running on sql server 2017):
WITH selection AS (
SELECT servertimestamp
FROM eventlog
WHERE servertimestamp BETWEEN '5/29/2018' AND DATEADD(dd, +1, '6/29/2019')
AND (attributes LIKE '%N<=>PeopleIn%'))
(SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0) as timestamp , COUNT(servertimestamp) AS GONE_OUT
FROM selection
WHERE DATEPART(hh, servertimestamp) BETWEEN 8 AND 20
GROUP BY DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0))
ORDER BY timestamp
Also the screenshot below shows the result of the executed code:
What this code does is showing how many people came in a building each day. The data is grouped in a 2 hour basis.
What i want to do, is adding a column that shows how many people have gone out of the building for the same time slots that i'm already using. Below i'm giving you an example of what i want to do:
Notice that on the 6th line i'm using the LIKE operator (attributes LIKE '%N<=>PeopleIn%'). This means that for the additional column, i'll have to make similar selections, but with the difference of using attributes LIKE '%N<=>PeopleOut%'.
Can i make it by using the UNION operator? Is there any other more obvious or easier way to do it?
Your help will be appreciated, thank you.