Postgres版本9.4
我已经有了这个涉及两个表的查询,即surigao_stats_2yr
和evidensapp_surigaobldg
。
SELECT
province,
municipali AS municipality,
brgy_locat AS barangay,
total_bldg,
(total_bldg - count(id)) as total_not_affected,
count(id) AS total_affected,
count(id) FILTER (WHERE gridcode = 1) AS low,
count(id) FILTER (WHERE gridcode = 2) AS medium,
count(id) FILTER (WHERE gridcode = 3) AS high
FROM (
SELECT
pss.province,ps.id, ps.brgy_locat, ps.municipali,
gridcode, count(pss.id) as total_bldg
FROM surigao_stats_2yr ps
INNER JOIN
evidensapp_surigaobldg pss
ON ps.brgy_locat = pss.brgy_locat
AND ps.municipali = pss.municipali
GROUP BY 1, 2,3, 4, 5
)
AS ps_fh GROUP BY 1,2,3, 4
ORDER BY 1 ASC, 2 ASC
我希望有一个视图涉及另外两个具有相同结果where
的表格,可以通过municipality
和barangay
进行过滤,并将total_bldg
,{{1如果total_not_affected
和total_affected
相同,则{},low
,medium
,high
和municipality
。
到目前为止,我已经尝试过这个:
barangay
性能非常慢,我想知道上面提到的值是否与我的查询一起添加。
感谢。