Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find gaps in time not covered by records with start date and end date

I have a table of fee records (f_fee_item) as follows:

Fee_Item_ID    int
Fee_Basis_ID   int
Start_Date     date
End_Date       date

(irrelevant columns removed)

Assume that records for the same Fee_Basis_ID won't overlap.

I need to find the Start_Date and End_Date of each gap in the fee records for each Fee_Basis_ID between a supplied @Query_Start_Date and @Query_End_Date. I need this data to calculate fee accruals for all periods where fees have not been charged.

I also need the query to return a record if there are no fee records at all for a given Fee_Basis_ID (Fee_Basis_ID is a foreign key to D_Fee_Basis.Fee_Basis_ID if that helps).

For example:

@Query_Start_Date = '2011-01-01'
@Query_Start_Date = '2011-09-30'

D_Fee_Basis:

F_Fee_Item
1
2
3

F_Fee_Item:

Fee_Item_ID  Fee_Basis_ID  Start_Date  End_Date
1            1             2011-01-01  2011-03-31
2            1             2011-04-01  2011-06-30
3            2             2011-01-01  2011-03-31
4            2             2011-05-01  2011-06-30

Required Results:

Fee_Basis_ID   Start_Date  End_Date
1              2011-07-01  2011-09-30
2              2011-04-01  2011-04-30
2              2011-07-01  2011-09-30
3              2011-01-01  2011-09-30

I've bee trying different self-joins for days trying to get it working but with no luck.

Please help!!

like image 555
Aphillippe Avatar asked Nov 05 '22 13:11

Aphillippe


1 Answers

Here is a solution:

declare @Query_Start_Date date= '2011-01-01' 
declare @Query_End_Date date = '2011-09-30' 

declare @D_Fee_Basis table(F_Fee_Item int)
insert @D_Fee_Basis values(1) 
insert @D_Fee_Basis values(2) 
insert @D_Fee_Basis values(3) 

declare @F_Fee_Item table(Fee_Item_ID int, Fee_Basis_ID int,Start_Date date,End_Date date)
insert @F_Fee_Item values(1,1,'2011-01-01','2011-03-31') 
insert @F_Fee_Item values(2,1,'2011-04-01','2011-06-30') 
insert @F_Fee_Item values(3,2,'2011-01-01','2011-03-31')
insert @F_Fee_Item values(4,2,'2011-05-01','2011-06-30')

;with a as
(-- find all days between Start_Date and End_Date
select @Query_Start_Date d
union all 
select dateadd(day, 1, d)
from a
where d <  @Query_end_Date
), b as
(--find all unused days
select a.d, F_Fee_Item Fee
from a, @D_Fee_Basis Fee
where not exists(select 1 from @F_Fee_Item where a.d between Start_Date and End_Date and Fee.F_Fee_Item = Fee_Basis_ID)
),
c as
(--find all start dates
select d, Fee, rn = row_number() over (order by fee, d) from b 
where not exists (select 1 from b b2 where dateadd(day,1, b2.d) = b.d and b2.Fee= b.Fee)
),
e as
(--find all end dates
select d, Fee, rn = row_number() over (order by fee, d) from b 
where not exists (select 1 from b b2 where dateadd(day,-1, b2.d) = b.d and b2.Fee= b.Fee)
)
--join start dates with end dates
select c.Fee Fee_Basis_ID, c.d Start_Date, e.d End_Date from c join e on c.Fee = e.Fee and c.rn = e.rn
option (maxrecursion 0)

Link for result: https://data.stackexchange.com/stackoverflow/q/114193/

like image 50
t-clausen.dk Avatar answered Nov 09 '22 07:11

t-clausen.dk