2017-07-18 92 views
0

我創建這個查詢顯示0,即使沒有排

SELECT 
    CASE 
     WHEN (c.designation <> '') THEN UPPER(c.designation) 
     ELSE 'CATEGORIE INCONNU' 
    END AS designation, 
    CASE 
     WHEN COUNT(lg) > 0 THEN SUM (lg.montant_categorie_maintenance) 
     ELSE 0 
    END AS valeur 
FROM sch_gparc.maint_categorie_maintenance c 
LEFT JOIN sch_gparc.maint_lg_categorie_maintenance lg 
    ON lg.id_categorie = c.id 
LEFT JOIN sch_gparc.maint_maintenance m 
    ON lg.id_maintenance = m.id 
WHERE EXTRACT(MONTH FROM m.date_fin::DATE) = EXTRACT(MONTH FROM '01/04/2017'::DATE) 
    AND EXTRACT(YEAR FROM m.date_fin::DATE) = EXTRACT(YEAR FROM '01/04/2017'::DATE) 
    AND m.type_maintenance = 'PREVENTIVE' 
GROUP BY c.designation 

此查詢的目的是要顯示所有c.designation,如果沒有記錄顯示0

問題是:它不顯示任何內容(當我執行左連接時沒有記錄)。

編輯:所需的數據

| designation | value  | 
| MOTEUR  |  0   | 
| DIVERS  |  0   | 

什麼是現在顯示:沒有。

+0

小心不要在WHERE中用NULL禁止謂詞限制左連接表。這樣你可以將LEFT JOIN變成INNER。將'EXTRACT(MONTH FROM m.date_fin :: DATE)= EXTRACT(MONTH FROM '01/04/2017':: DATE)''移動到'ON' – Serg

+0

@將條件從WHERE移動到ON '是失去了查詢的好行爲,這意味着查詢將顯示錯誤的結果 –

+0

這是否意味着您需要INNER JOIN?然後返回沒有行是完全可以的。 – Serg

回答

0

「問題」是,當你分組時,你失去了沒有任何東西可以分組的行。

我通常只是將聚合查詢返回到我最初選擇的表中。

SELECT 
    s.designation, 
    COALESCE(s.valeur, 0) -- coalesce the NULL rows to zero 
FROM sch_gparc.maint_categorie_maintenance cat 
LEFT JOIN (
    SELECT 
     c.id, 
     CASE 
      WHEN (c.designation <> '') THEN UPPER(c.designation) 
      ELSE 'CATEGORIE INCONNU' 
     END AS designation, 
     SUM (lg.montant_categorie_maintenance) AS valeur 
    FROM sch_gparc.maint_categorie_maintenance c 
    LEFT JOIN sch_gparc.maint_lg_categorie_maintenance lg 
     ON lg.id_categorie = c.id 
    LEFT JOIN sch_gparc.maint_maintenance m 
     ON (
      lg.id_maintenance = m.id 
      AND EXTRACT(MONTH FROM m.date_fin::DATE) = EXTRACT(MONTH FROM '01/04/2017'::DATE) 
      AND EXTRACT(YEAR FROM m.date_fin::DATE) = EXTRACT(YEAR FROM '01/04/2017'::DATE) 
      AND m.type_maintenance = 'PREVENTIVE' 
     ) 
    GROUP BY c.designation 
) s ON (s.id = cat.id) 
+0

給出的結果是inccorect(當您使用條件'm.type_maintenance ='PREVENTIVE''時)結果在每行中必須爲0 –

+0

那麼,沒有測試數據就很難產生工作查詢。結果如何不正確? – Sevanteri

0

我已嘗試以下步驟之一,它的工作:

select c.designation , coalesce(gm.valeur,0) as valeur 
from sch_gparc.maint_categorie_maintenance c 
left join (SELECT 
    CASE 
     WHEN (c.designation <> '') THEN UPPER(c.designation) 
     ELSE 'CATEGORIE INCONNU' 
    END AS designation, 
    CASE 
     WHEN COUNT(m.id) > 0 THEN SUM (lg.montant_categorie_maintenance) 
     ELSE 0 
    END AS valeur 
FROM sch_gparc.maint_categorie_maintenance c 
LEFT JOIN sch_gparc.maint_lg_categorie_maintenance lg 
    ON lg.id_categorie = c.id 
LEFT JOIN sch_gparc.maint_maintenance m 
    ON lg.id_maintenance = m.id 

WHERE 
(EXTRACT(MONTH FROM m.date_fin::DATE) = EXTRACT(MONTH FROM '01/05/2017'::DATE) 
    AND EXTRACT(YEAR FROM m.date_fin::DATE) = EXTRACT(YEAR FROM '01/05/2017'::DATE) 
    AND m.type_maintenance = 'PREVENTIVE') or m.id is null 
GROUP BY 1)gm on gm.designation = UPPER(c.designation) 

希望有所幫助你。

+0

非常感謝 –