简体   繁体   English

SQL 将多行的两个查询合二为一

[英]SQL merge two queries with multiple rows in one

I made two separate requests on a view, each result has several lines.我对一个视图提出了两个单独的请求,每个结果都有几行。 You can see that one of the two columns of the result is the same.您可以看到结果的两列之一是相同的。

I would like to know how to merge the two queries to obtain a single table with the 3 columns, even if some results are NULL.我想知道如何合并这两个查询以获得具有 3 列的单个表,即使某些结果是 NULL。 the aim being to group several results in order to obtain a general progress result.目的是将几个结果分组以获得一般的进度结果。

Thanks.谢谢。

first query第一次查询

SELECT appui.pa, COUNT(appui.gid) AS appui_non_lance

FROM genie_civil.v_appui appui

JOIN suivi.v_contour_aerien aer ON aer.id = appui.contour_etude

WHERE num_appui LIKE 'E%'

AND etat_appui != 'Non utilisé'

AND type_travaux != 'artère neuve'

AND avancement_etude_app = 'non renseigné'

AND date_envoi_pv IS NULL

AND date_retour_pv IS NULL

AND etat_pv = '--Non Renseigné--'

AND aer.date_depot_erdf IS NULL

AND aer.etat_etude_erdf = 'NC'

AND aer.etat_affaire IS NULL

AND date_com IS NULL

AND valid_com IS NULL

--AND date_envoi_executant_app IS NULL

AND date_retour_tvx_appui IS NULL

AND aer.etat_aat = '--Non renseigné--'

AND date_tfx IS NULL

AND valid_tfx IS NULL

AND aer.date_envoi_d2 IS NULL

GROUP BY appui.pa;

second query第二次查询

 SELECT appui.pa, COUNT(appui.gid) AS appui_en_releve

FROM genie_civil.v_appui appui

JOIN suivi.v_contour_aerien aer ON aer.id = appui.contour_etude

WHERE num_appui LIKE 'E%'

AND etat_appui != 'Non utilisé'

AND avancement_etude_app IN('à relever', 'en cours relevé', 'à contrôler relevé')

AND date_envoi_pv IS NULL

AND date_retour_pv IS NULL

AND etat_pv = '--Non Renseigné--'

AND date_com IS NULL

AND valid_com IS NULL

--AND date_envoi_executant_app IS NULL

AND date_retour_tvx_appui IS NULL

AND aer.date_depot_erdf IS NULL

AND aer.etat_etude_erdf ='NC'

AND aer.etat_affaire IS NULL

AND aer.etat_aat = '--Non renseigné--'

AND date_tfx IS NULL

AND valid_tfx IS NULL

AND aer.date_envoi_d2 IS NULL

GROUP BY appui.pa;

You are probably looking for UNION ALL .您可能正在寻找UNION ALL More information is in the Postgres documentation:更多信息在 Postgres 文档中:

https://www.postgresql.org/docs/8.3/queries-union.html https://www.postgresql.org/docs/8.3/queries-union.html

Please use below query,请使用以下查询,


SELECT appui.pa, COUNT(appui.gid) AS appui_non_lance 
FROM genie_civil.v_appui appui
JOIN suivi.v_contour_aerien aer ON aer.id = appui.contour_etude
WHERE num_appui LIKE 'E%'
AND etat_appui != 'Non utilisé' AND type_travaux != 'artère neuve' AND avancement_etude_app = 'non renseigné'
AND date_envoi_pv IS NULL AND date_retour_pv IS NULL AND etat_pv = '--Non Renseigné--'
AND aer.date_depot_erdf IS NULL AND aer.etat_etude_erdf = 'NC' AND aer.etat_affaire IS NULL
AND date_com IS NULL AND valid_com IS NULL
--AND date_envoi_executant_app IS NULL
AND date_retour_tvx_appui IS NULL AND aer.etat_aat = '--Non renseigné--' AND date_tfx IS NULL
AND valid_tfx IS NULL AND aer.date_envoi_d2 IS NULL 
GROUP BY appui.pa

UNION ALL

SELECT appui.pa, COUNT(appui.gid) AS appui_en_releve FROM genie_civil.v_appui appui
JOIN suivi.v_contour_aerien aer ON aer.id = appui.contour_etude
WHERE num_appui LIKE 'E%' 
AND etat_appui != 'Non utilisé' AND avancement_etude_app IN('à relever', 'en cours relevé', 'à contrôler relevé')
AND date_envoi_pv IS NULL AND date_retour_pv IS NULL AND etat_pv = '--Non Renseigné--' AND date_com IS NULL
AND valid_com IS NULL 
--AND date_envoi_executant_app IS NULL
AND date_retour_tvx_appui IS NULL AND aer.date_depot_erdf IS NULL AND aer.etat_etude_erdf ='NC'
AND aer.etat_affaire IS NULL AND aer.etat_aat = '--Non renseigné--'  AND date_tfx IS NULL 
AND valid_tfx IS NULL AND aer.date_envoi_d2 IS NULL
GROUP BY appui.pa;

I think you're just looking to have two separate counts.我认为您只是希望有两个单独的计数。 This approach is best because you only scan the table once.这种方法是最好的,因为您只扫描表一次。 I've moved the conditions common to both of the queries into the where clause while the others are part of the respective case expressions.我已将两个查询共有的条件移到where子句中,而其他条件则是各自case表达式的一部分。

SELECT appui.pa,
    COUNT(CASE WHEN
            type_travaux != 'artère neuve'
        AND avancement_etude_app = 'non renseigné'
        AND aer.date_depot_erdf IS NULL
        AND aer.etat_etude_erdf = 'NC'
    THEN 1 END) AS appui_non_lance,
    COUNT(CASE WHEN
            etat_appui != 'Non utilisé'
        AND avancement_etude_app IN ('à relever', 'en cours relevé', 'à contrôler relevé')
    THEN 1 END) AS appui_en_releve
FROM genie_civil.v_appui appui INNER JOIN suivi.v_contour_aerien aer
    ON aer.id = appui.contour_etude
WHERE
        num_appui LIKE 'E%'
    AND etat_appui != 'Non utilisé'
    AND etat_pv = '--Non Renseigné--'
    AND date_envoi_pv IS NULL
    AND date_retour_pv IS NULL
    AND aer.etat_affaire IS NULL
    AND date_com IS NULL
    AND valid_com IS NULL
    --AND date_envoi_executant_app IS NULL
    AND aer.etat_aat = '--Non renseigné--'
    AND date_tfx IS NULL
    AND date_retour_tvx_appui IS NULL
    AND valid_tfx IS NULL
    AND aer.date_envoi_d2 IS NULL
GROUP BY appui.pa;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM