Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 60 additions & 42 deletions scripts/us_cdc/cdc500_state/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,68 @@
os.mkdir(_OUTPUT_FILE_PATH)

query = """
SELECT distinct * from(
SELECT
statvar,
SUBSTR(observation_about,0,8) as observation_about,
observation_date,
CONCAT('dcAggregate/',measurement_method) as measurement_method,
population_statvar,
SUM(CAST(pop_count AS FLOAT64))*100/SUM(CAST(population AS FLOAT64)) as percent
FROM
(
WITH cdc_sv AS (
SELECT
variable_measured AS cdc500,
CONCAT('Count_', REGEXP_SUBSTR(variable_measured, '(Person_.*ale|Person_.*Years|Person)')) AS pop_statvar
FROM `datcom-store.spanner_dc_graph_prod_DEFAULT.TimeSeries`
WHERE provenance = 'dc/base/CDC500'
AND variable_measured LIKE 'Percent_%'
GROUP BY cdc500, pop_statvar
),

svo_percent AS (
SELECT
SVO1.variable_measured as statvar,
SVO1.observation_about as observation_about,
SVO1.observation_date as observation_date,
SVO1.value as percent,
SVO1.measurement_method as measurement_method,
SVO2.variable_measured as population_statvar,
SVO2.value as population,
CAST(SVO2.value AS FLOAT64) * CAST(SVO1.value AS FLOAT64) / 100 as pop_count
FROM `datcom-store.dc_kg_latest.StatVarObservation` as SVO1
JOIN `datcom-store.dc_kg_latest.StatVarObservation` as SVO2 ON TRUE
JOIN (
# Get the statvars and corresponding population statvar
# with ‘Percent_’ replaced with ‘Count_’ and
# dropping the non-age, non-gender constraints.
SELECT
SVO.variable_measured as CDC500,
CONCAT('Count_', REGEXP_SUBSTR(SVO.variable_measured, '(Person_.*ale|Person_.*Years|Person)')) as pop_statvar
FROM `datcom-store.dc_kg_latest.StatVarObservation` as SVO
WHERE
SVO.prov_id = 'dc/base/CDC500'
AND SVO.variable_measured like 'Percent_%'
GROUP BY CDC500, pop_statvar
) AS CDC_SV ON TRUE
WHERE
SVO1.prov_id = 'dc/base/CDC500'
AND SVO1.variable_measured LIKE 'Percent%'
AND SVO1.observation_about = SVO2.observation_about
AND SVO1.observation_date = SVO2.observation_date
AND SVO1.variable_measured = CDC_SV.CDC500
AND SVO2.variable_measured = CDC_SV.pop_statvar
AND SVO1.observation_about like "geoId/%"
) group by 1,2,3,4,5
O.variable_measured AS statvar,
O.entity1 AS observation_about,
O.date AS observation_date,
O.value AS percent,
T.measurement_method AS measurement_method,
cdc_sv.pop_statvar
FROM `datcom-store.spanner_dc_graph_prod_DEFAULT.Observation` AS O
INNER JOIN `datcom-store.spanner_dc_graph_prod_DEFAULT.TimeSeries` AS T
ON O.variable_measured = T.variable_measured
AND O.entity1 = T.entity1
AND O.facet_id = T.facet_id
AND T.provenance = 'dc/base/CDC500'
AND T.variable_measured LIKE 'Percent_%'
INNER JOIN cdc_sv
ON O.variable_measured = cdc_sv.cdc500
WHERE O.entity1 LIKE 'geoId/%'
AND O.variable_measured LIKE 'Percent_%'
),

svo_count AS (
SELECT
O.variable_measured AS population_statvar,
O.entity1 AS observation_about,
O.date AS observation_date,
O.value AS population
FROM `datcom-store.spanner_dc_graph_prod_DEFAULT.Observation` AS O
INNER JOIN (
SELECT DISTINCT pop_statvar
FROM cdc_sv
) AS pop
ON O.variable_measured = pop.pop_statvar
WHERE O.entity1 LIKE 'geoId/%'
)

SELECT
p.statvar,
SUBSTR(p.observation_about, 0, 8) AS observation_about,
p.observation_date,
CONCAT('dcAggregate/', p.measurement_method) AS measurement_method,
p.pop_statvar AS population_statvar,
SAFE_DIVIDE(
SUM(CAST(c.population AS FLOAT64) * CAST(p.percent AS FLOAT64) / 100) * 100,
SUM(CAST(c.population AS FLOAT64))
) AS percent
FROM svo_percent AS p
INNER JOIN svo_count AS c
ON p.observation_about = c.observation_about
AND p.observation_date = c.observation_date
AND p.pop_statvar = c.population_statvar
GROUP BY 1, 2, 3, 4, 5
"""

client = bigquery.Client()
Expand Down