Hello,
Consider the following table that gives the validity dates of an item :
create table #table1(
DD_S int,
DF_S int,
id_prod int,
is_valid bit
)
insert into #table1
values
(20190101,20190601,10,0),
(20190601,20190901,10,0),
(20190901,20200701,10,0),
(20200701,20211001,10,0),
(20211001,20211101,10,0),
(20211101,20220401,10,1),
(20220401,20291231,10,0),
(20190101,20200201,15,1),
(20200201,20301101,15,0)
Now consider the following table that gives the identifier of a item throughout time:
create table #table2(
DD int,
DF int,
identifier int,
id_prod int
)
insert into #table2
VALUES
(20190101,20211001,5,10),
(20211001,20211101,4,10),
(20211101,20291231,5,10),
(20190101,20190501,6,15),
(20190501,20400101,7,15)
The goal is to 'slice' the first table to associate to each Item , while taking into account the time periods. The result should look like this:
DD_S DF_S id_Prod identifier DD DF IS_Valid
20190101 20190601 10 5 20190101 20211001 0
20190601 20190901 10 5 20190101 20211001 0
20190901 20200701 10 5 20190101 20211001 0
20200701 20211001 10 5 20190101 20211001 0
20211001 20211101 10 4 20211001 20211101 0
20211101 20220401 10 5 20211101 20291231 1
20220401 20291231 10 5 20211101 20291231 0
20190101 20190501 15 6 20190101 20190501 1
20190501 20200201 15 7 20190501 20400101 1
20200201 20301101 15 7 20190501 20400101 0
The idea is Slicing time periods by related time
Any idea or best solution to do that ?
Thanks