创建涉及多个表的Postgres视图

时间:2017-08-11 02:56:37

标签: postgresql

Postgres版本9.4

我已经有了这个涉及两个表的查询,即surigao_stats_2yrevidensapp_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

上面的查询结果如下: enter image description here

我希望有一个视图涉及另外两个具有相同结果where的表格,可以通过municipalitybarangay进行过滤,并将total_bldg,{{1如果total_not_affectedtotal_affected相同,则{},lowmediumhighmunicipality

到目前为止,我已经尝试过这个:

barangay

性能非常慢,我想知道上面提到的值是否与我的查询一起添加。

感谢。

0 个答案:

没有答案