diff --git a/products/cdbg/models/intermediate/_intermediate_models.yml b/products/cdbg/models/intermediate/_intermediate_models.yml index c8e19b546..e1085e3fd 100644 --- a/products/cdbg/models/intermediate/_intermediate_models.yml +++ b/products/cdbg/models/intermediate/_intermediate_models.yml @@ -13,8 +13,10 @@ models: - name: residential_floor_area - name: residential_floor_area_percentage - name: total_population + - name: potential_lowmod_population - name: lowmod_population - name: lowmod_population_percentage + - name: low_mod_income_population_percentage_source - name: int__block_groups_raw description: census block group geoemtries and demographic data diff --git a/products/cdbg/models/intermediate/int__block_groups.sql b/products/cdbg/models/intermediate/int__block_groups.sql index 955abd722..78ef513dc 100644 --- a/products/cdbg/models/intermediate/int__block_groups.sql +++ b/products/cdbg/models/intermediate/int__block_groups.sql @@ -2,10 +2,30 @@ WITH lot_block_groups AS ( SELECT * FROM {{ ref("int__lot_block_groups_details") }} ), +block_groups_population AS ( + SELECT * FROM {{ ref("int__block_groups_raw") }} +), + block_groups_income AS ( SELECT * FROM {{ ref("stg__low_mod_by_block_group") }} ), +block_groups_demographics as ( + SELECT + block_groups_population.geoid, + block_groups_population.borough_code, + block_groups_population.borough_name, + block_groups_population.total_population, + block_groups_income.tract, + block_groups_income.block_group, + block_groups_income.potential_lowmod_population, + block_groups_income.low_mod_income_population, + block_groups_income.low_mod_income_population_percentage + FROM block_groups_population + LEFT JOIN block_groups_income + ON block_groups_population.geoid = block_groups_income.geoid +), + block_groups_floor_area AS ( SELECT block_group_geoid AS geoid, @@ -18,22 +38,28 @@ block_groups_floor_area AS ( block_group_details AS ( SELECT block_groups_floor_area.geoid, - block_groups_income.boro AS borough_name, - block_groups_income.tract, - block_groups_income.block_group, - total_floor_area, - residential_floor_area, + block_groups_demographics.borough_name, + block_groups_demographics.tract, + block_groups_demographics.block_group, + block_groups_floor_area.total_floor_area, + block_groups_floor_area.residential_floor_area, CASE - WHEN total_floor_area = 0 + WHEN block_groups_floor_area.total_floor_area = 0 THEN 0 - ELSE (residential_floor_area / total_floor_area) * 100 + ELSE (block_groups_floor_area.residential_floor_area / block_groups_floor_area.total_floor_area) * 100 END AS residential_floor_area_percentage, - block_groups_income.total_population, - block_groups_income.lowmod_population AS low_mod_income_population, - block_groups_income.lowmod_pct AS low_mod_income_population_percentage + block_groups_demographics.total_population, + block_groups_demographics.potential_lowmod_population, + block_groups_demographics.low_mod_income_population, + CASE + WHEN block_groups_demographics.total_population = 0 + THEN 0 + ELSE (block_groups_demographics.low_mod_income_population / block_groups_demographics.total_population) * 100 + END AS low_mod_income_population_percentage, + block_groups_demographics.low_mod_income_population_percentage AS low_mod_income_population_percentage_source FROM block_groups_floor_area - LEFT JOIN block_groups_income - ON block_groups_floor_area.geoid = block_groups_income.geoid + LEFT JOIN block_groups_demographics + ON block_groups_floor_area.geoid = block_groups_demographics.geoid ) SELECT * FROM block_group_details diff --git a/products/cdbg/models/staging/stg__low_mod_by_block_group.sql b/products/cdbg/models/staging/stg__low_mod_by_block_group.sql index 190f3a8d4..b3f26d002 100644 --- a/products/cdbg/models/staging/stg__low_mod_by_block_group.sql +++ b/products/cdbg/models/staging/stg__low_mod_by_block_group.sql @@ -8,7 +8,7 @@ SELECT "BORO" AS boro, "TRACT" AS tract, "BLKGRP" AS block_group, - REPLACE("LOWMODUNIV", ',', '')::numeric AS total_population, - REPLACE("LOWMOD", ',', '')::numeric AS lowmod_population, - RTRIM("LOWMOD_PCT", '%')::numeric AS lowmod_pct + REPLACE("LOWMODUNIV", ',', '')::numeric AS potential_lowmod_population, + REPLACE("LOWMOD", ',', '')::numeric AS low_mod_income_population, + RTRIM("LOWMOD_PCT", '%')::numeric AS low_mod_income_population_percentage FROM {{ source("recipe_sources", "hud_lowmodincomebyblockgroup") }}