select
neighborhood,
-- These are the values from the cross join lateral
a.pop,
a.count,
a.avg
from
nyc_neighborhoods
cross join lateral (
select
-- This selects the sum of all the intersecting areas
-- populations using the proportional overlap calculation
sum(
population * (
st_area(st_intersection(geom, nyc_neighborhoods.geom)) / st_area(geom)
)
) as pop,
count(*) as count,
-- This selects the average area overlapping area
-- of all the intersecting areas
avg(
(
st_area(st_intersection(nyc_neighborhoods.geom, geom)) / st_area(geom)
)
) as avg
from
nys_2021_census_block_groups
where
left(geoid, 5) in ('36061', '36005', '36047', '36081', '36085')
and st_intersects(nyc_neighborhoods.geom, geom)
) a
order by
a.pop desc