Skip to content
Advertisement

How to find days when workers don’t show up for work

I have a calender table for everyday in 2021. Also i have an entry table for workers everyday entries. It includes id and date columns. Calender just have date column. How can i find the days when the workers did not come? I thought searching days for every id would work

Advertisement

Answer

Left join the worker entries to all possible combinations of date & id.
Then the unmatched entries will be seen as NULL.

Extra:

Demo with a calendar and holidays

730 rows affected

year | min        | max       
:--- | :--------- | :---------
2021 | 2021-01-01 | 2021-12-31
2022 | 2022-01-01 | 2022-12-31
29 rows affected

3 rows affected
id | workingday | dayofweek
-: | :--------- | --------:
 1 | 2021-10-11 |         1
 2 | 2021-10-11 |         1
 3 | 2021-10-11 |         1
 1 | 2021-10-12 |         2
 2 | 2021-10-12 |         2
 3 | 2021-10-12 |         2
 1 | 2021-10-13 |         3
 2 | 2021-10-13 |         3
 3 | 2021-10-13 |         3
 1 | 2021-10-14 |         4
 2 | 2021-10-14 |         4
 3 | 2021-10-14 |         4
 3 | 2021-10-15 |         5
 1 | 2021-10-18 |         1
 3 | 2021-10-18 |         1
 1 | 2021-10-19 |         2
 3 | 2021-10-19 |         2
 1 | 2021-10-20 |         3
 2 | 2021-10-20 |         3
 3 | 2021-10-20 |         3
 1 | 2021-10-21 |         4
 2 | 2021-10-21 |         4
 3 | 2021-10-21 |         4
 1 | 2021-10-22 |         5
 2 | 2021-10-22 |         5
 3 | 2021-10-22 |         5
days       | worker_id
:--------- | --------:
2021-10-15 |         1
2021-10-15 |         2
2021-10-18 |         2
2021-10-19 |         2
cal_date   | worker_id | rolling_count
:--------- | --------: | ------------:
2021-10-15 |         2 |             1
2021-10-18 |         2 |             2
2021-10-19 |         2 |             3
worker_id | firstdate  | lastdate   | missing_days
--------: | :--------- | :--------- | :-----------
        2 | 2021-10-15 | 2021-10-15 | 1           
        2 | 2021-10-18 | 2021-10-19 | 2           

Demo on db<>fiddle here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement