-4

I want to mark the off days as absent if the leaves are sandwiched

eg: consider off days (example saturday and sunday) as absent days when day before and after off days is absent.

example:

23-Nov | "Absent"
24-Nov | "Off Day"
25-Nov | "Off Day"
26-Nov | "Absent"

Output required.

23-Nov | "Absent"
24-Nov | "Absent"
25-Nov | "Absent"
26-Nov | "Absent"

I can't fix it to Saturday or sunday because the off days are not the same for all it could be on Mondays & Tuesdays or through out the week. I have the "shiftId" that is '1' for the "off day"

Sanal Sunny
  • 617
  • 3
  • 9
Faraz Naeem
  • 41
  • 1
  • 1
  • 7

1 Answers1

0

You have problems finding an algorithm? It's not so hard. Find out whether a record represents a Saturday or Sunday and for these find the neighboring Friday and Monday accordingly. Here is the pseudo code. Use appropriate date functions and LAG and LEAD to convert this into proper SQL.

select
  day,
  case 
    when weekday(day) = Saturday 
     and status(day-1) = 'Absent'
     and status(day+2) = 'Absent' then 'Absent'
    when weekday(day) = Sunday 
     and status(day-2) = 'Absent'
     and status(day+1) = 'Absent' then 'Absent'
    else status
  end as new_status
from mytable;
Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73