forked from cjfaulkner/SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlowlyChangingDImension_Daily.sql
More file actions
48 lines (45 loc) · 1.13 KB
/
SlowlyChangingDImension_Daily.sql
File metadata and controls
48 lines (45 loc) · 1.13 KB
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
DECLARE @SCD TABLE
( ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
DimValue char(1),
EffectiveDate date
);
INSERT INTO @SCD (DimValue, EffectiveDate)
VALUES ('A', CONVERT(date, dateadd(day, -12, GETDATE()))),
('B', CONVERT(date, dateadd(day, -9, GETDATE()))),
('C', CONVERT(date, dateadd(day, -5, GETDATE()))),
('D', CONVERT(date, dateadd(day, -2, GETDATE())));
SELECT * FROM @SCD;
WITH CTE_Dates AS
(
SELECT CONVERT(date, dateadd(day, -10, GETDATE())) AS RecordDate
UNION ALL
SELECT CONVERT(date, dateadd(day, 1, RecordDate)) AS RecordDate
FROM
CTE_Dates
WHERE
RecordDate < GETDATE()
), CTE_SCD AS
(
SELECT
s.ID,
s.DimValue,
s.EffectiveDate AS EffectiveFrom,
(SELECT MIN(EffectiveDate) FROM @SCD d WHERE d.EffectiveDate > s.EffectiveDate) AS EffectiveTo
-- In this case I could have used the ID but it isn't always the case (as in CDC)
FROM
@SCD s
)
SELECT DISTINCT
s.ID,
s.DimValue,
d.RecordDate
FROM
CTE_SCD s
INNER JOIN
CTE_Dates d
ON
d.RecordDate >= s.EffectiveFrom
AND
d.RecordDate < ISNULL(s.EffectiveTo, CONVERT(date, getdate()))
ORDER BY
d.RecordDate;