-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCumulativeTabelComplexData.sql
More file actions
65 lines (59 loc) · 1.48 KB
/
Copy pathCumulativeTabelComplexData.sql
File metadata and controls
65 lines (59 loc) · 1.48 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
CREATE TYPE season_stats AS(
season INTEGER,
gp INTEGER,
pts REAL,
reb REAL,
ast REAL
)
CREATE TYPE scoring_class AS ENUM('star', 'good', 'average', 'bad')
INSERT INTO players
WITH yesterday AS (
SELECT * FROM players
WHERE current_season = 2000
),
today AS (
SELECT * FROM player_seasons
WHERE season = 2001
)
SELECT
COALESCE(t.player_name,y.player_name) AS player_name,
COALESCE(t.height,y.height) AS height,
COALESCE(t.college,y.college) AS college,
COALESCE(t.country,y.country) AS country,
COALESCE(t.draft_year,y.draft_year) AS draft_year,
COALESCE(t.draft_round,y.draft_round) AS draft_round,
COALESCE(t.draft_number,y.draft_number) AS draft_number,
CASE WHEN y.season_stats is NULL
THEN ARRAY[ROW(
t.season,
t.gp,
t.pts,
t.reb,
t.ast)::season_stats]
WHEN t.season IS NOT NULL THEN y.season_stats || ARRAY[ROW(
t.season,
t.gp,
t.pts,
t.reb,
t.ast)::season_stats]
ELSE y.season_stats
END as season_stats,
CASE
WHEN t.season IS NOT NULL THEN
CASE WHEN t.pts > 20 THEN 'star'
WHEN t.pts > 15 THEN 'good'
WHEN t.pts > 10 THEN 'average'
ELSE 'bad'
END::scoring_class
ELSE y.scoring_class
END as scoring_class,
CASE
WHEN t.season IS NOT NULL THEN 0
ELSE y.years_since_last_season + 1
END as years_since_last_season,
COALESCE(t.season, y.current_season+1) as current_season,
CASE WHEN t.season IS NOT NULL THEN TRUE
ELSE FALSE
END AS is_active
FROM today t FULL OUTER JOIN yesterday y
ON t.player_name = y.player_name