2017-02-14 96 views
0

這裏是我的query on SQL Fiddle返回所有記錄與COUNT = 0

更新我希望所有的記錄,即使列nbDemandes是0,每個nomLoiRRQSAE)。

因此,實際結果:

nomTypeDemande | nomLoi | nbDemandes 
--------------- -------- ----------- 
Chef équipe  RRQ   2 
Mandat Projet  SAE   4 
PO    RRQ   5 
PO    SAE   1 

通緝的結果:

nomTypeDemande | nomLoi | nbDemandes 
--------------- -------- ----------- 
Chef équipe  RRQ   2 
Chef équipe  SAE   0 
Mandat Projet  RRQ   0 
Mandat Projet  SAE   4 
PO    RRQ   5 
PO    SAE   1 

非常感謝再次幫助我:)

回答

1

你的第一個 INNER JOIN s到一個 RIGHT JOIN替換/ LEFT JOIN

SELECT normesTypesDemande.choix AS nomTypeDemande, 
    normesLois.choix AS nomLoi, 
    COUNT(*) as nbDemandes 
FROM ((gestionDemandes.typeNormes 
    RIGHT JOIN gestionDemandes.normesLois 
     ON typeNormes.loi = normesLois.id) 
    LEFT JOIN gestionDemandes.normesTypesDemande 
     ON typeNormes.typeDemande = normesTypesDemande.id) 
    LEFT JOIN gestionDemandes.demandes 
     ON typeNormes.demande = demandes.id 
GROUP BY normesTypesDemande.choix, normesLois.choix 
ORDER BY normesTypesDemande.choix 

因爲那INNER JOIN和沒有相應的行數爲Comité - SAE例如,INNER JOIN將過濾SAE行。

它更改爲 RIGHT JOIN將確保沒有從 normesLois數據將被排除在外,你應該有你的 0計數。

更新:

哦,不是最優雅的,但這裏是解決方案:

SELECT nomTypeDemande 
    , choix 
    , sum(nbDemandes) AS nbDemandes 
FROM (
    SELECT r.nomTypeDemande 
     , nl.choix 
     , CASE 
      WHEN r.nomLoi = nl.choix 
       THEN sum(r.nbDemandes) 
      ELSE 0 
      END AS nbDemandes 
    FROM (
     SELECT normesTypesDemande.choix AS nomTypeDemande 
      , normesLois.choix AS nomLoi 
      , COUNT(typeNormes.id) AS nbDemandes 
     FROM normesLois 
     FULL JOIN typeNormes 
      ON typeNormes.loi = normesLois.id 
     FULL JOIN normesTypesDemande 
      ON typeNormes.typeDemande = normesTypesDemande.id 
     GROUP BY normesTypesDemande.choix 
      , normesLois.choix 
     ) r 
    CROSS JOIN normesLois nl 
    GROUP BY r.nomTypeDemande 
     , nl.choix 
     , r.nomLoi 
    ) r 
GROUP BY nomTypeDemande 
    , choix 
ORDER BY nomTypeDemande 
    , choix 
+0

喜先生,我已經試過這一點,但不是差在所有... :( – Patix80

+0

@ Patix80你現在可以重試? –

+0

是的,我再次嘗試,但同樣的結果......有沒有(0),例如? – Patix80

0

據我猜你指望demandés和分類。 問題是如果沒有特定類別的需求,它們將不會被計入。

該解決方案可以是左加入上demandés,因此所有[normesLois]和[normesTypesDemande]將被退回,即使沒有該類型demandés。

SELECT normesTypesDemande.choix AS nomTypeDemande, 
     normesLois.choix AS nomLoi, 
     COUNT(typeNormes.id) as nbDemandes 
FROM gestionDemandes.typeNormes 
INNER JOIN gestionDemandes.normesLois ON typeNormes.loi = normesLois.id 
INNER JOIN gestionDemandes.normesTypesDemande ON typeNormes.typeDemande = normesTypesDemande.id 
LEFT JOIN gestionDemandes.demandes ON typeNormes.demande = demandes.id 
GROUP BY normesTypesDemande.choix, normesLois.choix 
ORDER BY normesTypesDemande.choix 

PS:你並不真正需要的是在額外parentesis加入

+0

感謝回答這麼快,爲PS :)。但返回的記錄沒有差異。 – Patix80

+0

@ Patix80也許我們在這裏錯過了一個細節。如果你發佈一個可驗證的例子,你可以得到更加自信的答案。考慮使用[SQLfiddle](http://sqlfiddle.com/)爲例。再試一次,只用LEFT加入,也許你誤會了normesLois]和[normesTypesDemande]匹配 – jean

+0

好的謝謝你的訣竅。我會試試這個... – Patix80

0

你應該內更改爲從右加入 嘗試下面的查詢。

SELECT 
      T3.choix AS nomTypeDemande, 
      T2.choix AS nomLoi, 
      COUNT(T1.id) as nbDemandes 
    FROM gestionDemandes.typeNormes T1 
      RIGHT JOIN gestionDemandes.normesLois T2 ON T1.loi = T2.id 
      RIGHT JOIN gestionDemandes.normesTypesDemande T3 ON T1.typeDemande = T3.id 
      INNER JOIN gestionDemandes.demandes T4 ON T1.demande = T4.id 
    GROUP BY T3.choix, T2.choix 
    ORDER BY T3.choix 
+0

我試過了你的請求,但想要的結果不是它應該的是... – Patix80