编写更简洁 PostgreSQL/PostGIS 查询
writing cleaner tidier PostgreSQL/PostGIS query
此查询创建了道路缓冲区内所有接收器计数的市政摘要报告,并按市政当局分组。
所以基本上输出是
munname school childcare hospitals etc...
"mun1" 3 4 0
"mun2" 1 0 9
select b.mun,b.county,q1.schools,q2.childcares,q3.hospitals,q4.nursinghomes,q5.infrastructure,q6.streamwmi,q7.streammi,q8.rez,b.geometry into roadreport
from (select muntruck.munname as mun,muntruck.muncounty as county,mun.geom as geometry from muntruck,mun
where muntruck.munname = mun.mun and muntruck.muncounty = mun.county) as b
left join(select count(schools.gid) as schools,muntruck.munname as mun,muntruck.muncounty as county from muntruck,schools
where st_intersects(muntruck.igeom,schools.geom) group by muntruck.muncounty,muntruck.munname) as q1
on b.mun = q1.mun and b.county = q1.county
left join(select count(childcare.gid) as childcares,muntruck.munname as mun,muntruck.muncounty as county from muntruck,childcare
where st_intersects(muntruck.igeom,childcare.geom) group by muntruck.muncounty,muntruck.munname) as q2
on b.mun = q2.mun and b.county = q2.county
left join(select count(hospitals.gid) as hospitals, muntruck.munname as mun,muntruck.muncounty as county from muntruck,hospitals
where st_intersects(muntruck.igeom,hospitals.geom) group by muntruck.muncounty,muntruck.munname) as q3
on b.mun = q3.mun and b.county = q3.county
left join(select count(nursinghomes.gid) as nursinghomes, muntruck.munname as mun,muntruck.muncounty as county from muntruck,nursinghomes
where st_intersects(muntruck.igeom,nursinghomes.geom) group by muntruck.muncounty,muntruck.munname) as q4
on b.mun = q4.mun and b.county = q4.county
left join(select count(infra.gid) as infrastructure,muntruck.munname as mun,muntruck.muncounty as county from muntruck,infra
where st_intersects(muntruck.igeom,infra.ggeom) group by muntruck.muncounty,muntruck.munname) as q5
on b.mun = q5.mun and b.county = q5.county
left join(select sum(st_length(geom))/5280 as streamwmi, muntruck.munname as mun,muntruck.muncounty as county from muntruck, streamsw
where st_intersects(muntruck.igeom,streamsw.geom) group by muntruck.muncounty,muntruck.munname) as q6
on b.mun = q6.mun and b.county = q6.county
left join(select sum(st_length(geom))/5280 as streammi, muntruck.munname as mun,muntruck.muncounty as county from muntruck,streams
where st_intersects(muntruck.igeom,streams.geom) group by muntruck.muncounty,muntruck.munname) as q7
on b.mun = q7.mun and b.county = q7.county
left join(select sum(popest*((ST_Area(residentialpopulation.geom)/43560)/acresnew)) as rez, muntruck.munname as mun,muntruck.muncounty as county from muntruck,residentialpopulation
where st_intersects(muntruck.igeom,residentialpopulation.geom) group by muntruck.muncounty,muntruck.munname) as q8
on b.mun = q8.mun and b.county = q8.county;
它运行得非常好,并为每个城市创建了一个包含正确计数的文件。我只是想知道这里是否有多余的代码?它可以更干净,更整洁吗?任何建议将不胜感激
据我了解您的查询,在每个子查询中,您都会计算市政当局与其他一些 table(学校、医院等)的记录。我认为您可以一次完成所有连接,例如:
SELECT
muntruck.munname AS mun,
muntruck.muncounty AS county,
mun.geom as geometry,
COUNT(schools.gid) AS schools,
COUNT(childcare.gid) AS childcares,
COUNT(hospitals.gid) AS hospitals
FROM muntruck
JOIN mun ON (muntruck.munname = mun.mun AND muntruck.muncounty = mun.county)
LEFT JOIN schools ON ST_Intersects(muntruck.igeom, schools.geom)
LEFT JOIN childcare ON ST_Intersects(muntruck.igeom, childcare.geom)
LEFT JOIN hospitals ON ST_Intersects(muntruck.igeom, hospitals.geom)
GROUP BY
muntruck.muncounty,
muntruck.munname,
mun.geom
它看起来更清晰易懂,我认为它产生相同的结果。
此查询创建了道路缓冲区内所有接收器计数的市政摘要报告,并按市政当局分组。
所以基本上输出是
munname school childcare hospitals etc...
"mun1" 3 4 0
"mun2" 1 0 9
select b.mun,b.county,q1.schools,q2.childcares,q3.hospitals,q4.nursinghomes,q5.infrastructure,q6.streamwmi,q7.streammi,q8.rez,b.geometry into roadreport
from (select muntruck.munname as mun,muntruck.muncounty as county,mun.geom as geometry from muntruck,mun
where muntruck.munname = mun.mun and muntruck.muncounty = mun.county) as b
left join(select count(schools.gid) as schools,muntruck.munname as mun,muntruck.muncounty as county from muntruck,schools
where st_intersects(muntruck.igeom,schools.geom) group by muntruck.muncounty,muntruck.munname) as q1
on b.mun = q1.mun and b.county = q1.county
left join(select count(childcare.gid) as childcares,muntruck.munname as mun,muntruck.muncounty as county from muntruck,childcare
where st_intersects(muntruck.igeom,childcare.geom) group by muntruck.muncounty,muntruck.munname) as q2
on b.mun = q2.mun and b.county = q2.county
left join(select count(hospitals.gid) as hospitals, muntruck.munname as mun,muntruck.muncounty as county from muntruck,hospitals
where st_intersects(muntruck.igeom,hospitals.geom) group by muntruck.muncounty,muntruck.munname) as q3
on b.mun = q3.mun and b.county = q3.county
left join(select count(nursinghomes.gid) as nursinghomes, muntruck.munname as mun,muntruck.muncounty as county from muntruck,nursinghomes
where st_intersects(muntruck.igeom,nursinghomes.geom) group by muntruck.muncounty,muntruck.munname) as q4
on b.mun = q4.mun and b.county = q4.county
left join(select count(infra.gid) as infrastructure,muntruck.munname as mun,muntruck.muncounty as county from muntruck,infra
where st_intersects(muntruck.igeom,infra.ggeom) group by muntruck.muncounty,muntruck.munname) as q5
on b.mun = q5.mun and b.county = q5.county
left join(select sum(st_length(geom))/5280 as streamwmi, muntruck.munname as mun,muntruck.muncounty as county from muntruck, streamsw
where st_intersects(muntruck.igeom,streamsw.geom) group by muntruck.muncounty,muntruck.munname) as q6
on b.mun = q6.mun and b.county = q6.county
left join(select sum(st_length(geom))/5280 as streammi, muntruck.munname as mun,muntruck.muncounty as county from muntruck,streams
where st_intersects(muntruck.igeom,streams.geom) group by muntruck.muncounty,muntruck.munname) as q7
on b.mun = q7.mun and b.county = q7.county
left join(select sum(popest*((ST_Area(residentialpopulation.geom)/43560)/acresnew)) as rez, muntruck.munname as mun,muntruck.muncounty as county from muntruck,residentialpopulation
where st_intersects(muntruck.igeom,residentialpopulation.geom) group by muntruck.muncounty,muntruck.munname) as q8
on b.mun = q8.mun and b.county = q8.county;
它运行得非常好,并为每个城市创建了一个包含正确计数的文件。我只是想知道这里是否有多余的代码?它可以更干净,更整洁吗?任何建议将不胜感激
据我了解您的查询,在每个子查询中,您都会计算市政当局与其他一些 table(学校、医院等)的记录。我认为您可以一次完成所有连接,例如:
SELECT
muntruck.munname AS mun,
muntruck.muncounty AS county,
mun.geom as geometry,
COUNT(schools.gid) AS schools,
COUNT(childcare.gid) AS childcares,
COUNT(hospitals.gid) AS hospitals
FROM muntruck
JOIN mun ON (muntruck.munname = mun.mun AND muntruck.muncounty = mun.county)
LEFT JOIN schools ON ST_Intersects(muntruck.igeom, schools.geom)
LEFT JOIN childcare ON ST_Intersects(muntruck.igeom, childcare.geom)
LEFT JOIN hospitals ON ST_Intersects(muntruck.igeom, hospitals.geom)
GROUP BY
muntruck.muncounty,
muntruck.munname,
mun.geom
它看起来更清晰易懂,我认为它产生相同的结果。