-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.sql
167 lines (127 loc) · 4.37 KB
/
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
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
SELECT chain
FROM projections
WHERE gcm = 'CNRM-CM5' AND exp = 'historical-rcp85' AND rcm = 'ALADIN63' AND bc = 'ADAMONT';
SELECT code, date, value
FROM data
WHERE chain = 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_SMASH' AND variable_en = 'QA' AND date BETWEEN '1975-01-01' AND '2005-12-31' LIMIT 100;
WITH selected_projections AS (
SELECT chain
FROM projections
WHERE gcm = 'CNRM-CM5' AND exp = 'historical-rcp85' AND rcm = 'ALADIN63' AND bc = 'ADAMONT'
)
SELECT *
FROM data
WHERE chain IN (SELECT chain FROM selected_projections);
SELECT d.code, p.*, d.value
FROM data d
JOIN projections p ON d.chain = p.chain
WHERE p.gcm = 'CNRM-CM5' AND p.exp = 'historical-rcp85' AND p.rcm = 'ALADIN63' AND p.bc = 'ADAMONT'
AND d.variable_en = 'QA' AND d.date BETWEEN '1975-01-01' AND '2005-12-31'
LIMIT 100;
-- "HadGEM2-ES_historical-rcp85_CCLM4-8-17_ADAMONT"
-- "EC-EARTH_historical-rcp85_HadREM3-GA7_ADAMONT"
-- "historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT"
-- "HadGEM2-ES_historical-rcp85_ALADIN63_ADAMONT"
-- EXPLAIN
-- EXPLAIN ANALYZE
WITH historical AS (
SELECT code, chain, AVG(value) AS historical_value
FROM data_historical_rcp85_qjxa
WHERE chain IN
('historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_CTRIP',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_EROS',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_GRSD',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_J2000',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_MORDOR-SD',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_MORDOR-TS',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_ORCHIDEE',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_SIM2',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_SMASH')
AND d.date BETWEEN '1975-01-01' AND '2005-12-31'
GROUP BY code, chain
),
future AS (
SELECT code, chain, AVG(value) AS future_value
FROM data_historical_rcp85_qjxa
WHERE chain IN
('historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_CTRIP',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_EROS',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_GRSD',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_J2000',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_MORDOR-SD',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_MORDOR-TS',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_ORCHIDEE',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_SIM2',
'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_SMASH')
AND d.date BETWEEN '2050-01-01' AND '2080-12-31'
GROUP BY code, chain
),
chain_delta AS (
SELECT h.code, h.chain, (f.future_value - h.historical_value) / NULLIF(h.historical_value, 0) * 100 AS delta
FROM historical h
JOIN future f ON
h.code = f.code
AND h.chain = f.chain
),
full_chain_delta AS (
SELECT d.code, d.chain, p.gcm, p.rcm, p.bc, d.delta
FROM chain_delta d
JOIN projections p ON
d.chain = p.chain
),
hm_average AS (
SELECT code, gcm, rcm, bc, AVG(delta) AS delta
FROM full_chain_delta
GROUP BY code, gcm, rcm, bc
),
bc_average AS (
SELECT code, AVG(delta) AS delta
FROM hm_average
GROUP BY code, gcm, rcm
)
SELECT code, AVG(delta) AS delta
FROM bc_average
GROUP BY code
ORDER BY code;
WITH hm_average AS (
SELECT code, gcm, rcm, bc, AVG(value) AS value
FROM delta_historical_rcp26_qa_h3
WHERE
-- chain IN (
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_CTRIP',
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_EROS',
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_GRSD',
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_J2000',
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_MORDOR-SD',
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_MORDOR-TS',
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_ORCHIDEE',
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_SIM2',
-- 'historical-rcp85_CNRM-CM5_ALADIN63_ADAMONT_SMASH'
-- )
-- AND
n >= 4
GROUP BY code, gcm, rcm, bc
),
bc_average AS (
SELECT code, AVG(value) AS value
FROM hm_average
GROUP BY code, gcm, rcm
)
-- SELECT s.*, b.value
-- FROM stations s
-- JOIN (
SELECT code, AVG(value) AS value
FROM bc_average
GROUP BY code
ORDER BY code;
-- ) b ON s.code = b.code;
SELECT *
FROM delta_historical_rcp26_qa
WHERE
chain IN (
'historical-rcp26_CNRM-CM5_ALADIN63_ADAMONT_SIM2',
'historical-rcp26_CNRM-CM5_ALADIN63_ADAMONT_SMASH'
)
AND
code = 'X211401001'
LIMIT 100;