My project has a table named CheckIns, basically containing all checking-ins performed by employees. Now I need to get 2 reports of the check-ins.
- First report: The total number of check-ins performed by day and hour
- Second report: The only check-in peak per day (ie. in what hour that most employees checked-in per day)
For the 1st requirement, I'm able to achieve it by using this query
SELECT CONVERT(DATE,CheckInDateTime) AS [Date]
, DATEPART(HOUR, CheckInDateTime) AS [Hour]
, COUNT(*) AS CheckInCount
FROM [dbo].[CheckIns]
GROUP BY CONVERT(DATE,CheckInDateTime), DATEPART(HOUR, CheckInDateTime)
ORDER BY CONVERT(DATE,CheckInDateTime)
For the 2nd requirement, by adding CheckInCount DESC
into the ORDER BY
part, I got the hour that had most check-ins on top for each day, as you can see an example of 03 Jun (most of the checkins appeared to be between 8pm and 9pm, so 8pm - 20:00 is on top).
However, this still does not satisfy the requirement, as it should only display the top peak record only for each day. Can someone please advise me on what to add to this query to achieve that goal?
SELECT CONVERT(DATE,CheckInDateTime) AS [Date]
, DATEPART(HOUR, CheckInDateTime) AS [Hour]
, COUNT(*) AS CheckInCount
FROM [dbo].[CheckIns]
GROUP BY CONVERT(DATE,CheckInDateTime), DATEPART(HOUR, CheckInDateTime)
ORDER BY CONVERT(DATE,CheckInDateTime), CheckInCount DESC
Thank you.