-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.sql
More file actions
732 lines (651 loc) · 24 KB
/
Copy pathscripts.sql
File metadata and controls
732 lines (651 loc) · 24 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
CREATE TYPE fiscal_metrics AS (
fiscal_year INTEGER,
total_count INTEGER,
measure_1 NUMERIC,
measure_2 NUMERIC,
measure_3 NUMERIC
);
CREATE TYPE performance_band AS ENUM ('star', 'good', 'average', 'bad');
CREATE TABLE customers (
customer_id TEXT,
dimension_1 TEXT,
source_system TEXT,
country_of_origin TEXT,
registration_year TEXT,
registration_round TEXT,
registration_number TEXT,
fiscal_data fiscal_metrics[],
performance_category performance_band,
years_since_last_activity INTEGER,
current_fiscal_year INTEGER,
is_currently_active BOOLEAN,
PRIMARY KEY (customer_id, current_fiscal_year)
);
SELECT *
FROM customer_fiscal_periods
ORDER BY customer_id, fiscal_period;
INSERT INTO customers
WITH fiscal_years AS (
SELECT *
FROM GENERATE_SERIES(1996, 2022) AS fiscal_period
),
c AS (
SELECT
customer_id,
MIN(fiscal_period) AS first_fiscal_period
FROM customer_fiscal_periods
GROUP BY customer_id
), customers_and_periods AS (
SELECT *
FROM c
JOIN fiscal_years fy
ON c.first_fiscal_period <= fy.fiscal_period
), windowed AS (
SELECT
cap.customer_id,
cap.fiscal_period,
-- possibilty of null for customer data in customer_fiscal_periods table
ARRAY_REMOVE(
ARRAY_AGG(
CASE
WHEN cfp.fiscal_period IS NOT NULL THEN
ROW(
cfp.fiscal_period,
cfp.total_count,
cfp.measure_1,
cfp.measure_2,
cfp.measure_3
)::fiscal_metrics
END
) OVER (
PARTITION BY cap.customer_id
ORDER BY COALESCE(cap.fiscal_period, cfp.fiscal_period)
),
NULL
) AS fiscal_data
FROM customers_and_periods cap
LEFT JOIN customer_fiscal_periods cfp
ON cap.customer_id = cfp.customer_id
AND cap.fiscal_period = cfp.fiscal_period
ORDER BY cap.customer_id, cap.fiscal_period
), static AS (
SELECT
customer_id,
MAX(dimension_1) AS dimension_1,
MAX(source_system) AS source_system,
MAX(country_of_origin) AS country_of_origin,
MAX(registration_year) AS registration_year,
MAX(registration_round) AS registration_round,
MAX(registration_number) AS registration_number
FROM customer_fiscal_periods
GROUP BY customer_id
)
SELECT
w.customer_id,
s.dimension_1,
s.source_system,
s.country_of_origin,
s.registration_year,
s.registration_round,
s.registration_number,
fiscal_data,
CASE
WHEN (fiscal_data[CARDINALITY(fiscal_data)]::fiscal_metrics).measure_1 > 20 THEN 'star'
WHEN (fiscal_data[CARDINALITY(fiscal_data)]::fiscal_metrics).measure_1 > 15 THEN 'good'
WHEN (fiscal_data[CARDINALITY(fiscal_data)]::fiscal_metrics).measure_1 > 10 THEN 'average'
ELSE 'bad'
END::performance_band AS performance_category, -- was scoring_class
w.fiscal_period - (fiscal_data[CARDINALITY(fiscal_data)]::fiscal_metrics).fiscal_year
AS years_since_last_activity, -- was years_since_last_active
w.fiscal_period,
(fiscal_data[CARDINALITY(fiscal_data)]::fiscal_metrics).fiscal_year = w.fiscal_period
AS is_currently_active -- was is_active
FROM windowed w
JOIN static s
ON w.customer_id = s.customer_id;
SELECT
customer_id
, performance_category
, is_currently_active
FROM customers
WHERE current_fiscal_year = 2022;
CREATE TABLE customers_scd (
customer_id TEXT,
performance_category performance_band,
is_currently_active BOOLEAN,
start_fiscal_year INTEGER,
end_fiscal_year INTEGER,
current_fiscal_year INTEGER,
PRIMARY KEY (customer_id, current_fiscal_year)
);
SELECT
customer_id AS customer_id,
current_fiscal_year AS current_fiscal_year,
performance_category AS performance_category,
LAG(performance_category, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_performance_category,
LAG(is_currently_active, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_is_currently_active,
is_currently_active
FROM customers;
WITH with_previous AS (
SELECT
customer_id AS customer_id,
current_fiscal_year AS current_fiscal_year,
performance_category AS performance_category,
LAG(performance_category, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_performance_category,
LAG(is_currently_active, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_is_currently_active,
is_currently_active AS is_currently_active
FROM customers
)
SELECT
*,
CASE
WHEN performance_category <> previous_performance_category THEN 1
ELSE 0
END AS performance_category_change_indicator,
CASE
WHEN is_currently_active <> previous_is_currently_active THEN 1
ELSE 0
END AS is_currently_active_change_indicator
FROM with_previous;
WITH with_previous AS (
SELECT
customer_id AS customer_id,
current_fiscal_year AS current_fiscal_year,
performance_category AS performance_category,
LAG(performance_category, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_performance_category,
LAG(is_currently_active, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_is_currently_active,
is_currently_active AS is_currently_active
FROM customers
),
with_indicators AS (
SELECT
*,
CASE
WHEN performance_category <> previous_performance_category THEN 1
WHEN is_currently_active <> previous_is_currently_active THEN 1
ELSE 0
END AS change_indicator
FROM with_previous
);
WITH with_previous AS (
SELECT
customer_id AS customer_id,
current_fiscal_year AS current_fiscal_year,
performance_category AS performance_category,
LAG(performance_category, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_performance_category,
LAG(is_currently_active, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_is_currently_active,
is_currently_active AS is_currently_active
FROM customers
),
with_indicators AS (
SELECT
*,
CASE
WHEN performance_category <> previous_performance_category THEN 1
WHEN is_currently_active <> previous_is_currently_active THEN 1
ELSE 0
END AS change_indicator
FROM with_previous
),
with_streaks AS (
SELECT
*,
SUM(change_indicator)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS streak_identifier
FROM with_indicators
);
WITH with_previous AS (
SELECT
customer_id AS customer_id,
current_fiscal_year AS current_fiscal_year,
performance_category AS performance_category,
LAG(performance_category, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_performance_category,
LAG(is_currently_active, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_is_currently_active,
is_currently_active AS is_currently_active
FROM customers
WHERE current_fiscal_year <= 2021
),
with_indicators AS (
SELECT
*,
CASE
WHEN performance_category <> previous_performance_category THEN 1
WHEN is_currently_active <> previous_is_currently_active THEN 1
ELSE 0
END AS change_indicator
FROM with_previous
),
with_streaks AS (
SELECT
*,
SUM(change_indicator)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year) AS streak_identifier
FROM with_indicators
)
SELECT
customer_id AS customer_id,
streak_identifier,
is_currently_active AS is_currently_active,
performance_category AS performance_category,
MIN(current_fiscal_year) AS start_fiscal_year,
MAX(current_fiscal_year) AS end_fiscal_year
FROM with_streaks
GROUP BY
customer_id,
streak_identifier,
is_currently_active,
performance_category;
CREATE TABLE customers_scd (
customer_id TEXT, -- was player_name
performance_category performance_band, -- was scoring_class
is_currently_active BOOLEAN, -- was is_active
start_fiscal_year INTEGER, -- was start_season
end_fiscal_year INTEGER, -- was end_season
current_fiscal_year INTEGER, -- was current_season
PRIMARY KEY (customer_id, start_fiscal_year)
);
INSERT INTO customers_scd
WITH with_previous AS (
SELECT
customer_id AS customer_id,
current_fiscal_year AS current_fiscal_year,
performance_category AS performance_category,
LAG(performance_category, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_performance_category,
LAG(is_currently_active, 1)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year)
AS previous_is_currently_active,
is_currently_active AS is_currently_active
FROM customers
WHERE current_fiscal_year <= 2021
),
with_indicators AS (
SELECT
*,
CASE
WHEN performance_category <> previous_performance_category THEN 1
WHEN is_currently_active <> previous_is_currently_active THEN 1
ELSE 0
END AS change_indicator
FROM with_previous
),
with_streaks AS (
SELECT
*,
SUM(change_indicator)
OVER (PARTITION BY customer_id ORDER BY current_fiscal_year) AS streak_identifier
FROM with_indicators
)
SELECT
customer_id,
performance_category,
is_currently_active,
MIN(current_fiscal_year) AS start_fiscal_year,
MAX(current_fiscal_year) AS end_fiscal_year,
2021 AS current_fiscal_year
FROM with_streaks
GROUP BY
customer_id,
streak_identifier,
is_currently_active,
performance_category
ORDER BY
customer_id,
streak_identifier;
-- incremental loading
WITH last_season_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year = 2021
),
historical_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year < 2021
),
this_season_data AS (
SELECT *
FROM customers
WHERE current_fiscal_year = 2022
)
SELECT
ts.customer_id,
ts.performance_category,
ts.is_currently_active,
ls.performance_category,
ls.is_currently_active
FROM this_season_data ts
LEFT JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id;
WITH last_season_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year = 2021
),
historical_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year < 2021
),
this_season_data AS (
SELECT *
FROM customers
WHERE current_fiscal_year = 2022
),
unchanged_records AS (
SELECT
ts.customer_id,
ts.performance_category,
ts.is_currently_active,
ls.performance_category,
ls.is_currently_active
FROM this_season_data ts
JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
ts.performance_category = ls.performance_category
AND ts.is_currently_active = ls.is_currently_active
);
WITH last_season_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year = 2021
),
historical_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year < 2021
),
this_season_data AS (
SELECT *
FROM customers
WHERE current_fiscal_year = 2022
),
unchanged_records AS (
SELECT
ts.customer_id,
ts.performance_category,
ts.is_currently_active,
ls.performance_category,
ls.is_currently_active
FROM this_season_data ts
JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
ts.performance_category = ls.performance_category
AND ts.is_currently_active = ls.is_currently_active
),
new_and_changed_records AS (
SELECT
ts.customer_id,
ts.performance_category,
ts.is_currently_active,
ls.start_fiscal_year,
ts.current_fiscal_year AS end_fiscal_year
FROM this_season_data ts
LEFT JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
(ts.performance_category <> ls.performance_category
OR ts.is_currently_active <> ls.is_currently_active
OR ls.customer_id IS NULL)
)
SELECT *
FROM new_and_changed_records;
CREATE TYPE scd_snapshot AS (
performance_category performance_band,
is_currently_active BOOLEAN,
start_fiscal_year INTEGER,
end_fiscal_year INTEGER
);
WITH last_season_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year = 2021
),
historical_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year < 2021
),
this_season_data AS (
SELECT *
FROM customers
WHERE current_fiscal_year = 2022
),
unchanged_records AS (
SELECT
ts.customer_id,
ts.performance_category,
ts.is_currently_active,
ls.performance_category,
ls.is_currently_active
FROM this_season_data ts
JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
ts.performance_category = ls.performance_category
AND ts.is_currently_active = ls.is_currently_active
),
new_and_changed_records AS (
SELECT
ts.customer_id,
UNNEST(
ARRAY[
ROW(
ls.performance_category,
ls.is_currently_active,
ls.start_fiscal_year,
ls.end_fiscal_year
)::scd_snapshot,
ROW(
ts.performance_category,
ts.is_currently_active,
ts.current_fiscal_year,
ts.current_fiscal_year
)::scd_snapshot
]
)
FROM this_season_data ts
LEFT JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
ts.performance_category <> ls.performance_category
OR ts.is_currently_active <> ls.is_currently_active
)
SELECT *
FROM new_and_changed_records;
CREATE TYPE scd_snapshot AS (
performance_category performance_band,
is_currently_active BOOLEAN,
start_fiscal_year INTEGER,
end_fiscal_year INTEGER
);
WITH last_season_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year = 2021
),
historical_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year < 2021
),
this_season_data AS (
SELECT *
FROM customers
WHERE current_fiscal_year = 2022
),
unchanged_records AS (
SELECT
ts.customer_id,
ts.performance_category,
ts.is_currently_active,
ls.performance_category,
ls.is_currently_active
FROM this_season_data ts
JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
ts.performance_category = ls.performance_category
AND ts.is_currently_active = ls.is_currently_active
),
changed_records AS (
SELECT
ts.customer_id,
UNNEST(
ARRAY[
ROW(
ls.performance_category,
ls.is_currently_active,
ls.start_fiscal_year,
ls.end_fiscal_year
)::scd_snapshot,
ROW(
ts.performance_category,
ts.is_currently_active,
ts.current_fiscal_year,
ts.current_fiscal_year
)::scd_snapshot
]
) AS records
FROM this_season_data ts
LEFT JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
ts.performance_category <> ls.performance_category
OR ts.is_currently_active <> ls.is_currently_active
),
unnested_changed_records AS (
SELECT
customer_id,
(records::scd_snapshot).performance_category,
(records::scd_snapshot).is_currently_active,
(records::scd_snapshot).start_fiscal_year,
(records::scd_snapshot).end_fiscal_year
FROM changed_records
)
SELECT *
FROM unnested_changed_records;
WITH last_season_scd AS (
SELECT *
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year = 2021
),
historical_scd AS (
SELECT
customer_id AS customer_id,
performance_category AS performance_category,
is_currently_active AS is_currently_active,
start_fiscal_year AS start_fiscal_year,
end_fiscal_year AS end_fiscal_year
FROM customers_scd
WHERE current_fiscal_year = 2021
AND end_fiscal_year < 2021
),
this_season_data AS (
SELECT *
FROM customers
WHERE current_fiscal_year = 2022
),
unchanged_records AS (
SELECT
ts.customer_id AS customer_id,
ts.performance_category AS performance_category,
ts.is_currently_active AS is_currently_active,
ls.performance_category AS previous_performance_category,
ls.is_currently_active AS previous_is_currently_active
FROM this_season_data ts
JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
ts.performance_category = ls.performance_category
AND ts.is_currently_active = ls.is_currently_active
),
changed_records AS (
SELECT
ts.customer_id AS customer_id,
UNNEST(
ARRAY[
ROW(
ls.performance_category,
ls.is_currently_active,
ls.start_fiscal_year,
ls.end_fiscal_year
)::scd_snapshot,
ROW(
ts.performance_category,
ts.is_currently_active,
ts.current_fiscal_year,
ts.current_fiscal_year
)::scd_snapshot
]
) AS records
FROM this_season_data ts
LEFT JOIN last_season_scd ls
ON ls.customer_id = ts.customer_id
WHERE
(ts.performance_category <> ls.performance_category
OR ts.is_currently_active <> ls.is_currently_active)
),
unnested_changed_records AS (
SELECT
customer_id,
(records::scd_snapshot).performance_category AS performance_category,
(records::scd_snapshot).is_currently_active AS is_currently_active,
(records::scd_snapshot).start_fiscal_year AS start_fiscal_year,
(records::scd_snapshot).end_fiscal_year AS end_fiscal_year
FROM changed_records
),
new_records AS (
SELECT
ts.customer_id AS customer_id,
ts.performance_category AS performance_category,
ts.is_currently_active AS is_currently_active,
ts.current_fiscal_year AS start_fiscal_year,
ts.current_fiscal_year AS end_fiscal_year
FROM this_season_data ts
LEFT JOIN last_season_scd ls
ON ts.customer_id = ls.customer_id
WHERE ls.customer_id IS NULL
)
SELECT * FROM historical_scd
UNION ALL
SELECT * FROM unchanged_records
UNION ALL
SELECT * FROM unnested_changed_records
UNION ALL
SELECT * FROM new_records;