Absent ploicy:
If you are absents just before a holiday
and also absent just after a holiday
then all holidays in between will be treated as absents
How to find those holidays where one is absent
before and after holiday / holidays
I am trying
select a.*
from sandwich a
left join sandwich b on b.entry_date = dateadd(d,1,a.entry_date)
left join sandwich c on c.entry_date = dateadd(d,-1,a.entry_date)
where a.flag='H'
and b.flag='A'
and c.flag='A'
id | entry_date | flag |
---|---|---|
1 | 2020-10-08 | H |
1 | 2020-10-13 | H |
It is not bringing those holidays where they are more than 1 consecutively
the correct result should be
id | entry_date | flag |
---|---|---|
1 | 2020-10-03 | H |
1 | 2020-10-04 | H |
1 | 2020-10-08 | H |
1 | 2020-10-13 | H |
the schema and sample data is as below
CREATE TABLE [dbo].[sandwich](
[id] [int] NOT NULL,
[entry_date] [date] NOT NULL,
[flag] [varchar](1) NULL,
)
ALTER TABLE [dbo].[sandwich] ADD CONSTRAINT [PK_sandwich] PRIMARY KEY CLUSTERED
(
[id] ASC,
[entry_date] ASC
)
INSERT INTO [sandwich] ([id],[entry_date],[flag])
VALUES(1,'2020-10-01','P')
,(1,'2020-10-02','A')
,(1,'2020-10-03','H')
,(1,'2020-10-04','H')
,(1,'2020-10-05','A')
,(1,'2020-10-06','P')
,(1,'2020-10-07','A')
,(1,'2020-10-08','H')
,(1,'2020-10-09','A')
,(1,'2020-10-10','H')
,(1,'2020-10-11','P')
,(1,'2020-10-12','A')
,(1,'2020-10-13','H')
,(1,'2020-10-14','A')
P means present
A means absent
H means holiday