Find unique number of days

Viewed 1365

I wish to write a SQL query to find the number of unique working days for each employee from table times.

*---------------------------------------*
|emp_id  task_id  start_day   end_day   |
*---------------------------------------*
|  1        1     'monday'  'wednesday' |
|  1        2     'monday'  'tuesday'   |
|  1        3     'friday'  'friday'    |
|  2        1     'monday'  'friday'    |
|  2        1     'tuesday' 'wednesday' |
*---------------------------------------*

Expected output:

*-------------------*
|emp_id  no_of_days |
*-------------------*
|  1        4       |
|  2        5       |
*-------------------*

I have written the query sqlfiddle which is giving me the expected output but for curiosity is there a better way to write this query? Can I use Calender or Tally table?

with days_num as  
(
  select
    *,
    case 
      when start_day = 'monday' then 1
      when start_day = 'tuesday' then 2
      when start_day = 'wednesday' then 3
      when start_day = 'thursday' then 4
      when start_day = 'friday' then 5
    end as start_day_num,

    case 
      when end_day = 'monday' then 1
      when end_day = 'tuesday' then 2
      when end_day = 'wednesday' then 3
      when end_day = 'thursday' then 4
      when end_day = 'friday' then 5
    end as end_day_num

  from times
),
day_diff as
(
  select
    emp_id,
    case
      when  
        (end_day_num - start_day_num) = 0
      then
        1
      else
        (end_day_num - start_day_num)
    end as total_diff
  from days_num  
)

select emp_id,
  sum(total_diff) as uniq_working_days
from day_diff
group by
  emp_id

Any suggestions would be great.

7 Answers

One possible approach to simplify the statement in the question(fiddle), is to use VALUES table value constructor and appropriate joins:

SELECT 
   t.emp_id,
   SUM(CASE 
      WHEN d1.day_no = d2.day_no THEN 1
      ELSE d2.day_no - d1.day_no
   END) AS no_of_days
FROM times t
JOIN (VALUES ('monday', 1), ('tuesday', 2), ('wednesday', 3), ('thursday', 4), ('friday', 5)) d1 (day, day_no) 
   ON t.start_day = d1.day
JOIN (VALUES ('monday', 1), ('tuesday', 2), ('wednesday', 3), ('thursday', 4), ('friday', 5)) d2 (day, day_no) 
   ON t.end_day = d2.day
GROUP BY t.emp_id

But if you want to count the distinct days, the statement is different. You need to find all days between the start_day and end_day range and count the distinct days:

;WITH daysCTE (day, day_no) AS (
   SELECT 'monday', 1 UNION ALL
   SELECT 'tuesday', 2 UNION ALL
   SELECT 'wednesday', 3 UNION ALL
   SELECT 'thursday', 4 UNION ALL
   SELECT 'friday', 5 
)
SELECT t.emp_id, COUNT(DISTINCT d3.day_no)
FROM times t
JOIN daysCTE d1 ON t.start_day = d1.day
JOIN daysCTE d2 ON t.end_day = d2.day
JOIN daysCTE d3 ON d3.day_no BETWEEN d1.day_no AND d2.day_no
GROUP BY t.emp_id

You need to basically find the intersection of the days worked by each emp_id on each task with all the days of the week, and then count the distinct days:

with days_num as (
  SELECT *
  FROM (
    VALUES ('monday', 1), ('tuesday', 2), ('wednesday', 3), ('thursday', 4), ('friday', 5)
  ) AS d (day, day_no)
),
emp_day_nums as (
  select emp_id, d1.day_no AS start_day_no, d2.day_no AS end_day_no
  from times t
  join days_num d1 on d1.day = t.start_day
  join days_num d2 on d2.day = t.end_day
)
select emp_id, count(distinct d.day_no) AS distinct_days
from emp_day_nums e
join days_num d on d.day_no between e.start_day_no and e.end_day_no
group by emp_id

Output:

emp_id  distinct_days
1       4
2       5

Demo on SQLFiddle

with cte as 
(Select id, start_day as day
   group by id, start_day
 union 
 Select id, end_day as day
   group by id, end_day
)

select id, count(day)
from cte
group by id

Your query is not correct. Try Monday to Tuesday with Wednesday to Thursday. This should result in 4 days, but your query returns 2 days. Your query doesn't even detect whether two ranges are adjacent or overlapping or neither.

One way to solve this is to write a recursive CTE to get all days from a range and then count distinct days.

with weekdays (day_name, day_number) as
(
  select * from (values ('monday', 1), ('tuesday', 2), ('wednesday', 3),
                        ('thursday', 4), ('friday', 5)) as t(x,y)
)
, emp_days(emp_id, day, last_day)
as
(
  select emp_id, wds.day_number, wde.day_number
  from times t
  join weekdays wds on wds.day_name = t.start_day
  join weekdays wde on wde.day_name = t.end_day
  union all
  select emp_id, day + 1, last_day
  from emp_days
  where day < last_day
)
select emp_id, count(distinct day)
from emp_days
group by emp_id
order by emp_id;

Demo: http://sqlfiddle.com/#!18/4a5ac/16

(As can be seen I could not apply the values constructor directly as in with weekdays (day_name, day_number) as (values ('monday', 1), ...). I don't know why. Is that SQL Server or me? Well, with the additional select it works :-)

declare @times table
(
  emp_id int,
  task_id int,
  start_day varchar(50),
  end_day varchar(50)
);

insert into @times(emp_id, task_id, start_day, end_day)
values
(1, 1, 'monday', 'wednesday'),
(1, 2, 'monday', 'tuesday'),
(1, 3, 'friday', 'friday'),
--
(2, 1, 'monday', 'friday'),
(2, 2, 'tuesday', 'wednesday'),
--
(3, 1, 'monday', 'wednesday'),
(3, 2, 'monday', 'tuesday'),
(3, 3, 'monday', 'tuesday');

--for sql 2019, APPROX_COUNT_DISTINCT() eliminates distinct sort (!!)...
-- ...with a clustered index on emp_id (to eliminate the hashed aggregation) the query cost gets 5 times cheaper ("overlooking" the increase in memory) !!??!!
/*
select t.emp_id, APPROX_COUNT_DISTINCT(v.val) as distinctweekdays
from
(
select *, .........
*/


select t.emp_id, count(distinct v.val) as distinctweekdays
from
(
select *, 
case start_day when 'monday' then 1
      when 'tuesday' then 2
      when 'wednesday' then 3
      when 'thursday' then 4
      when 'friday' then 5
    end as start_day_num,
case end_day when 'monday' then 1
      when 'tuesday' then 2
      when 'wednesday' then 3
      when 'thursday' then 4
      when 'friday' then 5
    end as end_day_num
from @times
) as t
join (values(1),(2), (3), (4), (5)) v(val) on v.val between t.start_day_num and t.end_day_num
group by t.emp_id;
WITH tmp 
     AS (SELECT emp_id, 
                task_id, 
                start_day, 
                end_day, 
                CASE 
                  WHEN start_day = 'monday' THEN 1 
                  WHEN start_day = 'tuesday' THEN 2 
                  WHEN start_day = 'wednesday' THEN 3 
                  WHEN start_day = 'thursday' THEN 4 
                  WHEN start_day = 'friday' THEN 5 
                  WHEN start_day = 'saturday' THEN 6 
                  WHEN start_day = 'sunday' THEN 7 
                END AS start_d, 
                CASE 
                  WHEN end_day = 'monday' THEN 1 
                  WHEN end_day = 'tuesday' THEN 2 
                  WHEN end_day = 'wednesday' THEN 3 
                  WHEN end_day = 'thursday' THEN 4 
                  WHEN end_day = 'friday' THEN 5 
                  WHEN end_day = 'saturday' THEN 6 
                  WHEN end_day = 'sunday' THEN 7 
                END AS end_d 
         FROM   #tmp) 
SELECT emp_id, 
       Sum(CASE 
             WHEN end_d >= start_d THEN end_d - start_d + 1 
             WHEN end_d < start_d THEN end_d + 7 - start_d + 1 
           END) AS day_diff 
FROM   tmp 
GROUP  BY emp_id 
SELECT emp_id, (MAX(Monday) + MAX(Tuesday)+ MAX(Wednesday)+ MAX(Thursday) + MAX(Friday)) Total
    FROM
        (SELECT *, IIF(1>=start_num AND 1<=end_num, 1, 0) Monday, IIF(2>=start_num AND 2<=end_num, 1, 0) Tuesday, 
                  IIF(3>=start_num AND 3<=end_num, 1, 0) Wednesday, IIF(4>=start_num AND 4<=end_num, 1, 0) Thursday, 
IIF(5>=start_num AND 5<=end_num, 1, 0) Friday
        FROM
            (SELECT emp_id, CASE WHEN start_day = 'Monday' THEN 1
                           WHEN start_day = 'Tuesday' THEN 2
                           WHEN start_day = 'Wednesday' THEN 3
                           WHEN start_day = 'Thursday' THEN 4
                           ELSE 5
                      END AS start_num,
                      CASE WHEN end_day = 'Monday' THEN 1
                           WHEN end_day = 'Tuesday' THEN 2
                           WHEN end_day = 'Wednesday' THEN 3
                           WHEN end_day = 'Thursday' THEN 4
                           ELSE 5
                      END AS end_num
            FROM Times) a
        )b
    GROUP BY emp_id
Related