Employee Register Log
(Download the CSV file used)
/* To see the file*/
SELECT * from EmployeeLog;
Day | Employee | Present |
---|---|---|
Mon | Alvin | 1 |
Mon | Simon | 1 |
Mon | Theodore | 1 |
Mon | Jim | 0 |
Tues | Alvin | 1 |
Tues | Simon | 0 |
Tues | Theodore | 1 |
Tues | Jim | 0 |
Weds | Alvin | 0 |
Weds | Simon | 1 |
Weds | Theodore | 1 |
Weds | Jim | 1 |
Thurs | Alvin | 1 |
Thurs | Simon | 1 |
Thurs | Theodore | 0 |
Thurs | Jim | 1 |
Fri | Alvin | 0 |
Fri | Simon | 1 |
Fri | Theodore | 1 |
Fri | Jim | 0 |
/* To see how many days of the weeek each employee worked */
SELECT Employee, SUM(Present) AS Total_Present_Days
FROM EmployeeLog
GROUP BY Employee
ORDER BY Total_Present_Days DESC;
/* Find the most consecutive days worked per employee, and order descending */
WITH ConsecutiveDays AS (
SELECT
Employee,
Present,
ROW_NUMBER() OVER (PARTITION BY Employee, Present ORDER BY Day) -
ROW_NUMBER() OVER (PARTITION BY Employee ORDER BY Day) AS ConsecutiveGroup
FROM EmployeeLog
)
, ConsecutiveStreaks AS (
SELECT
Employee,
MAX(COUNT(*)) AS MaxConsecutiveDays
FROM ConsecutiveDays
WHERE Present = 1
GROUP BY Employee, ConsecutiveGroup
)
SELECT
Employee,
MAX(MaxConsecutiveDays) AS Max_Consecutive_Days
FROM ConsecutiveStreaks
GROUP BY Employee
ORDER BY Max_Consecutive_Days DESC;