-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy path09b-test-tricky-query.sql
108 lines (92 loc) · 1.9 KB
/
09b-test-tricky-query.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
select * from dbo.Phone for system_time all
go
select * from dbo.[Address] for system_time all
go
create or alter view dbo.DenormalizedTemporalView
as
select
a.customer_id,
a.address,
a.valid_from as address_valid_from,
a.valid_to as address_valid_to,
p.phone,
p.valid_from as phone_valid_from,
p.valid_to as phone_valid_to
from
dbo.[Address] a
full join
dbo.[Phone] p on a.customer_id = p.customer_id;
go
WITH TimeStamps AS
(
SELECT [valid_from] AS ts FROM dbo.Phone for system_time all
UNION ALL
SELECT [valid_to] from dbo.Phone for system_time all
UNION ALL
SELECT [valid_from] FROM dbo.Address for system_time all
UNION
SELECT [valid_to] FROM dbo.Address for system_time all
),
TSDR AS
(
SELECT ts, ROW_NUMBER() OVER(ORDER BY ts) AS pos
FROM TimeStamps
),
Intervals AS
(
SELECT
[valid_from] = Cur.ts,
[valid_to] = Nxt.ts
FROM
TSDR AS Cur
inner JOIN
TSDR AS Nxt ON Nxt.pos = Cur.pos + 1
),
UnifiedValues AS
(
SELECT
attr = 'phone',
val = phone,
[valid_from],
[valid_to]
FROM
dbo.Phone for system_time all
UNION ALL
SELECT
'address',
[address],
[valid_from],
[valid_to]
FROM
dbo.Address for system_time all
),
ValidValues AS
(
SELECT
I.[valid_from],
I.[valid_to],
V.attr,
V.val
FROM
Intervals AS I
JOIN
UnifiedValues AS V ON V.[valid_to] > I.[valid_from] AND V.[valid_from] < I.[valid_to] -- "OVERLAPS" operator
)
SELECT
*
FROM
ValidValues
PIVOT(MAX(val) FOR attr IN(phone, address)) AS P;
go
select customer_id, [address], phone
from dbo.DenormalizedTemporalView for system_time as of '2007-12-02'
go
select customer_id, [address], phone
from dbo.DenormalizedTemporalView for system_time as of '2007-12-20'
go
select customer_id, [address], phone
from dbo.DenormalizedTemporalView for system_time as of '2008-01-05'
go
select customer_id, [address], phone
from dbo.DenormalizedTemporalView for system_time as of '2021-10-20'
go