6 kyu

Employee Weekend Work: Counting Unique Weekends and Total Days Worked

Description:

You are a Data Analyst at a medium-sized retail company, known for its dynamic work schedule. The management is interested in understanding the weekend work patterns of their employees to make informed decisions about staffing and employee incentives. Your task is to analyze the employee attendance data for the year 2023, focusing on weekends.

Write an SQL query to analyze weekend attendance of employees in the year 2023. Your query should identify how many unique weekends each employee worked and the total number of weekend days they worked. Consider a weekend to include both Saturday and Sunday, and count consecutive Saturday and Sunday as one weekend.

We have employee_attendance table:

  • employee_id (integer): ID of the employee.
  • attendance_date (date): The date on which the employee worked.

Expected Output:

  1. employee_id: ID of the employee.
  2. weekends_worked: Number of unique weekends during which the employee worked at least once.
  3. total_weekend_days_worked: Total number of individual weekend days (Saturdays and Sundays) the employee worked.

Sorting: Descending order of weekends_worked, and then by total_weekend_days_worked in desccending order, and finally by employee_id - also in descending order.

Note: In the provided employee_attendance data, each employee is recorded only once per date, ensuring no duplicate dates or double shifts for the same employee. This unique entry per employee per day must be considered in your analysis.

For this sample data:

| employee_id | attendance_date |
+-------------+-----------------+
|           1 |      2023-01-07 |
|           1 |      2023-01-08 |
|           1 |      2023-01-14 |
|           1 |      2023-01-22 |
|           1 |      2023-02-04 |
|           1 |      2023-02-05 |
|           2 |      2023-01-14 |
|           2 |      2023-01-21 |
|           2 |      2023-01-22 |
|           2 |      2023-02-11 |
|           3 |      2023-01-08 |
|           4 |      2023-02-04 |

the expected output is the following:

employee_idweekends_workedtotal_weekend_days_worked
146
234
311
411

GLHF!

SQL
Databases
Date Time

Stats:

CreatedApr 9, 2024
PublishedApr 9, 2024
Warriors Trained513
Total Skips25
Total Code Submissions1559
Total Times Completed168
SQL Completions168
Total Stars9
% of votes with a positive feedback rating98% of 26
Total "Very Satisfied" Votes25
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments8
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • bornForThis Avatar
  • Mednoob Avatar
Ad