2016-07-26 35 views
0

我在過去問過類似的問題,並且想象已經有對此的回答,但似乎無法找出找到它的措辭。將子女日期範圍與家長合併

我有一個父表的日期範圍和一個子表,父表的日期範圍內可以有多個日期範圍的父表。我需要將它們合併爲一系列中的每個記錄。一個例子可以更好地解釋什麼,我想:

表1(父)

Date1 Date2 Person 
1/1/16 7/1/16 A 

和 表2(兒童)

Date1 Date2 Person 
2/1/16 2/4/16 B 
3/6/16 3/8/16 C 
5/4/16 5/9/16 B 

我想要一個合併表所示:

合併表

Date1 Date2 Person 
1/1/16 2/1/16 A 
2/1/16 2/4/16 B 
2/4/16 3/6/16 A 
3/6/16 3/8/16 C 
3/8/16 5/4/16 A 
5/4/16 5/9/16 B 
5/9/16 7/1/16 A 

必須有一個簡單的方法來做到這一點?我對一個複雜的while循環沒有問題,但是我也很難理解這一點。

+0

SQL2014。所以它應該是最新的功能/等。 – Julian

回答

1

按照要求,這是一個修改後的版本,其考慮多種PK/FK

Declare @Table1 table (PK int,Date1 Date,Date2 Date, Person varchar(25)) 
Insert into @Table1 values 
(1,'1/1/16','7/1/16','A'), 
(2,'2/1/16','8/1/16','A') 


Declare @Table2 table (FK int,Date1 Date,Date2 Date, Person varchar(25)) 
Insert into @Table2 values 
(1,'2/1/16','2/4/16','B'), 
(1,'3/6/16','3/8/16','C'), 
(1,'5/4/16','5/9/16','B'), 
(2,'3/1/16','3/4/16','B'), 
(2,'3/6/16','3/8/16','C'), 
(2,'5/4/16','5/9/16','B') 


;with cteBase as (
    Select * 
      ,Gap1 = Date2 
      ,Gap2 = Lead(Date1,1,(Select max(Date2) from @Table1 Where FK=PK)) over (Partition By FK Order by Date1) 
    From @Table2 
) 
Select PK,Date1,Date2=(Select min(Date1) from @Table2 Where FK=PK),Person From @Table1 
Union All 
Select FK,Date1,Date2,Person from cteBase 
Union All 
Select FK,Date1=Gap1,Date2=Gap2,Person=B.Person 
From cteBase A 
Join @Table1 B on FK=PK 
Where Gap1<>Gap2 
Order by PK,Date1 

返回

PK Date1  Date2  Person 
1 2016-01-01 2016-02-01 A 
1 2016-02-01 2016-02-04 B 
1 2016-02-04 2016-03-06 A 
1 2016-03-06 2016-03-08 C 
1 2016-03-08 2016-05-04 A 
1 2016-05-04 2016-05-09 B 
1 2016-05-09 2016-07-01 A 
2 2016-02-01 2016-03-01 A 
2 2016-03-01 2016-03-04 B 
2 2016-03-04 2016-03-06 A 
2 2016-03-06 2016-03-08 C 
2 2016-03-08 2016-05-04 A 
2 2016-05-04 2016-05-09 B 
2 2016-05-09 2016-08-01 A 
+0

完美,謝謝! – Julian

+0

@Julian再一次,快樂幫助,歡呼聲 –

1

也許這樣的事?

Declare @Table1 table (Date1 Date,Date2 Date, Person varchar(25)) 
Insert into @Table1 values 
('1/1/16','7/1/16','A') 

Declare @Table2 table (Date1 Date,Date2 Date, Person varchar(25)) 
Insert into @Table2 values 
('2/1/16','2/4/16','B'), 
('3/6/16','3/8/16','C'), 
('5/4/16','5/9/16','B') 

;with cteBase as (
    Select * 
      ,Gap1 = Date2 
      ,Gap2 = Lead(Date1,1,(Select max(Date2) from @Table1)) over (Order by Date1) 
    From @Table2 
) 
Select Date1,Date2=(Select min(Date1) from @Table2),Person From @Table1 
Union All 
Select Date1,Date2,Person from cteBase 
Union All 
Select Date1=Gap1,Date2=Gap2,Person=B.Person 
From cteBase A 
Join @Table1 B on 1=1 
Order by Date1 

返回

Date1  Date2  Person 
2016-01-01 2016-02-01 A 
2016-02-01 2016-02-04 B 
2016-02-04 2016-03-06 A 
2016-03-06 2016-03-08 C 
2016-03-08 2016-05-04 A 
2016-05-04 2016-05-09 B 
2016-05-09 2016-07-01 A 
+0

我無法自己構建,但它像一個魅力一樣工作!非常感謝!!!我喜歡這個社區!我可以自己用循環來解決其餘的問題,但是有沒有更好,更有效的方法在table1中通過PK/FK連接table2中的多行來完成同樣的過程?因此,如果Table1有兩行具有唯一ID,然後Table2有5個,其中3是一行的孩子,另一行是2,那麼只能合併那些匹配PK/FK的行?那有意義嗎? – Julian

+0

樂於助人,無需循環。如果您發佈更豐富(或擴展)的示例數據集,我可以修改我的答案 –