viewof smoothed_unfiltered = {
wide
await client.query(`DROP TABLE IF EXISTS pops`)
await client.query(`CREATE TABLE pops (place CHAR, population INT)`)
let pop_set;
if (dataset === "States") {
pop_set = state_pops
} else if (dataset == "Countries") {
pop_set = populations
} else {
pop_set = metro_values.map( d=> {
return {
name : d.get("name"),
"population_total" : d.get("pop")
}
})
}
let popmin = 0
if (dataset == "Countries") popmin = 10000000
if (dataset == "US Metros") popmin = 150000
const pop_vals = pop_set.map(({name, population_total}) => `('${name.replace("'", "")}', ${population_total})`).join(", ")
await client.query(`INSERT INTO pops VALUES ${pop_vals}`)
return client.table(`SELECT *, CASE WHEN smoothed > 0 THEN smoothed*1000000/population ELSE 0 END as avg FROM ( SELECT *, (AVG(new_cases) OVER (
PARTITION BY place
ORDER BY date
RANGE BETWEEN INTERVAL 13 DAYS PRECEDING
AND INTERVAL 0 DAYS FOLLOWING
)) as smoothed FROM tmp NATURAL JOIN pops WHERE population > ${popmin} order by place, date) t1 `)
}