You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/server/endpoints/covid_hosp_state_timeseries.py
+13-14
Original file line number
Diff line number
Diff line change
@@ -176,21 +176,20 @@ def handle():
176
176
# ...Filter for most recent issues before a given as_of
177
177
cond_clause+=" AND (issue <= :as_of)"
178
178
q.params["as_of"] =as_of
179
-
union_subquery=f'''
180
-
(
181
-
SELECT * FROM (
182
-
SELECT *, 'D' as record_type, ROW_NUMBER() OVER (PARTITION BY state, date ORDER BY issue DESC) row_d FROM `covid_hosp_state_daily` c WHERE {cond_clause}
183
-
) sub_d WHERE row_d = 1
184
-
UNION ALL
185
-
SELECT * FROM (
186
-
SELECT *, 'T' as record_type, ROW_NUMBER() OVER (PARTITION BY state, date ORDER BY issue DESC) row_t FROM `covid_hosp_state_timeseries` c WHERE {cond_clause}
187
-
) sub_t WHERE row_t = 1
188
-
) c'''
179
+
189
180
query=f'''
190
-
SELECT {q.fields_clause} FROM (
191
-
SELECT {q.fields_clause}, ROW_NUMBER() OVER (PARTITION BY state, date ORDER BY issue DESC, record_type) `row`
192
-
FROM {union_subquery}
193
-
) {q.alias} WHERE `row` = 1 ORDER BY {q.order_clause}
181
+
WITH max_daily AS (
182
+
SELECT {q.fields_clause}, 'D' as record_type FROM (
183
+
SELECT {q.fields_clause}, max(issue) OVER (PARTITION BY state, date) `max_issue` FROM `covid_hosp_state_daily` c WHERE {cond_clause}
184
+
) c WHERE issue = max_issue
185
+
), max_timeseries AS (
186
+
SELECT {q.fields_clause}, 'T' as record_type FROM (
187
+
SELECT {q.fields_clause}, max(issue) OVER (PARTITION BY state, date) `max_issue` FROM `covid_hosp_state_timeseries` c WHERE {cond_clause}
188
+
) c WHERE issue = max_issue
189
+
) SELECT {q.fields_clause} FROM (
190
+
SELECT {q.fields_clause}, ROW_NUMBER() OVER (PARTITION BY issue, date ORDER BY issue DESC, record_type) `row`
191
+
FROM (max_daily UNION ALL max_timeseries) c
192
+
) {q.alias} WHERE `row` = 1 ORDER BY {q.order_clause}
0 commit comments