I have a table named Absence Details and I want to group sequential dates. Here, is the data
EID AbsenceType AbsenceStartDate AbsenceEndDate
769 Holiday 2012-06-25 00:00:00.000 2012-06-25 23:59:59.000
769 Holiday 2012-06-26 00:00:00.000 2012-06-26 23:59:59.000
769 Holiday 2012-09-03 00:00:00.000 2012-09-03 23:59:59.000
769 Holiday 2012-09-04 00:00:00.000 2012-09-04 23:59:59.000
769 Holiday 2012-09-05 00:00:00.000 2012-09-05 23:59:59.000
769 Holiday 2012-09-06 00:00:00.000 2012-09-06 23:59:59.000
769 Holiday 2012-09-07 00:00:00.000 2012-09-07 23:59:59.000
The result i am trying to get is
EID AbsenceType AbsenceStartDate AbsenceEndDate
769 Holiday 2012-06-25 00:00:00.000 2012-06-26 23:59:59.000
769 Holiday 2012-09-03 00:00:00.000 2012-09-07 23:59:59.000
Any help is much appreciated.
I have simplified your scenario to isolate the main problem. Let's supose this table with gaps:
with ns as (
select 1 as n union
select 2 as n union
select 3 as n union
select 8 as n union --gap
select 9 as n )
select *
into #ns
from ns;
Now, the result you are expecting for is:
ini fi
--- --
1 3
8 9
To get this results I massage the data in this way: first I create two views with start and end periods and second, I join both views to get final result. Notice that I join table with it self to locate starts and ends periods:
with
inis as -- identifying start periods
(
select n1.n
from #ns n1
left outer join #ns n2
on n1.n = n2.n + 1
where n2.n is null
),
fis as -- identifying ends periods
(
select n1.n
from #ns n1
left outer join #ns n2
on n1.n = n2.n - 1
where n2.n is null
)
select inis.n as ini, min( fis.n ) as fi -- joining starts and ends
from inis
inner join fis
on inis.n <= fis.n
group by inis.n
;
You can transfer this technique to your data and data types. If you have any issue translating query be free to ask.
Check query and results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With