2016-02-05 48 views
0

我寫了一個查詢,可以產生以下結果。使用ROLLUP總和行

查詢看起來像

SELECT 
    rlrDivision.strLangue1 AS Division, 
    COUNT(p.pkPoste) AS NumbersToAdd, 
    SUM(p.iNbPostes) AS MoreNumbersToAdd 
FROM 
    poste p 
LEFT JOIN 
    lien_objet_ref_8 lienDiv ON lienDiv.fkObjet = p.pkPoste 
LEFT JOIN 
    r_referentiel rrDivision ON rrDivision.pkReferentiel =  lienDiv.fkReferentiel 
LEFT JOIN 
    r_libelle_ref rlrDivision ON rlrDivision.pkLibelleRef = rrDivision.fkLib1 
LEFT JOIN 
    lien_objet_liste_3 lol3 ON lol3.fkObjet = p.pkPoste AND lol3.`strContexte`='descriptif/type_contrat' 
LEFT JOIN 
    r_liste_editable rle ON lol3.fkListe = rle.pkListeEditable 
LEFT JOIN 
    r_libelle_ref rlr ON rle.fkLibelleRef = rlr.pkLibelleRef 
WHERE 
    p.pkPoste <> 1 
    AND p.iSuppr <> 1 
    AND p.iDernierEtatPoste = 2 
    AND rlr.strLangue1 LIKE 'Full-time%' 
GROUP BY 
    rlrDivision.strLangue1, p.`iNbPostes` WITH ROLLUP 

這幾乎產生正確的結果。但是,我想每個部門只有一行,沒有ROLLUP我有。但是,我沒有所需的總數。所以基本上我想要的是下面的內容,但在NumbersToAdd和MoreNumbersToAdd的總數上有另一行。

+0

什麼是您的RDBMS?它支持['ROLLUP'](https://technet.microsoft.com/en-us/library/ms189305(v = sql.90).aspx)? –

+0

在這裏使用原始sql – 00robinette

+0

@GordonLinoff對不起,我改變了我的別名,以便它現在變得更有意義。 – 00robinette

回答

0

你可以做UNION

SELECT 
rlrDivision.strLangue1 AS Division, 
COUNT(p.pkPoste) AS NumbersToAdd, 
SUM(p.iNbPostes) AS MoreNumbersToAdd 
FROM poste p 
LEFT JOIN lien_objet_ref_8 lienDiv ON lienDiv.fkObjet = p.pkPoste 
LEFT JOIN r_referentiel rrDivision ON rrDivision.pkReferentiel =  lienDiv.fkReferentiel 
LEFT JOIN r_libelle_ref rlrDivision ON rlrDivision.pkLibelleRef = rrDivision.fkLib1 

LEFT JOIN lien_objet_liste_3 lol3 ON lol3.fkObjet = p.pkPoste AND lol3.`strContexte`='descriptif/type_contrat' 
LEFT JOIN r_liste_editable rle ON lol3.fkListe = rle.pkListeEditable 
LEFT JOIN r_libelle_ref rlr ON rle.fkLibelleRef = rlr.pkLibelleRef 
WHERE p.pkPoste <> 1 
AND p.iSuppr <> 1 
AND p.iDernierEtatPoste = 2 
AND rlr.strLangue1 LIKE 'Full-time%' 
GROUP BY rlrDivision.strLangue1 

UNION ALL 

SELECT 'Total' as Division 
    , COUNT(p.pkPoste) as NumbersToAdd 
    , SUM(p.iNbPostes) AS MoreNumbersToAdd 
FROM poste p 
LEFT JOIN lien_objet_ref_8 lienDiv ON lienDiv.fkObjet = p.pkPoste 
LEFT JOIN r_referentiel rrDivision ON rrDivision.pkReferentiel =  lienDiv.fkReferentiel 
LEFT JOIN r_libelle_ref rlrDivision ON rlrDivision.pkLibelleRef = rrDivision.fkLib1 
LEFT JOIN lien_objet_liste_3 lol3 ON lol3.fkObjet = p.pkPoste AND lol3.`strContexte`='descriptif/type_contrat' 
LEFT JOIN r_liste_editable rle ON lol3.fkListe = rle.pkListeEditable 
LEFT JOIN r_libelle_ref rlr ON rle.fkLibelleRef = rlr.pkLibelleRef 
WHERE p.pkPoste <> 1 
AND p.iSuppr <> 1 
AND p.iDernierEtatPoste = 2 
AND rlr.strLangue1 LIKE 'Full-time%' 
GROUP BY rlrDivision.strLangue1 
; 

有可供查詢做總等ROLL UP功能,但不知道你的數據庫會支持,因爲你沒有提到你的數據庫名稱。

+0

@minatvermaj我不知道這是可以接受的sqlPro我得到的錯誤未知列'更多數字添加'在'字段列表' – 00robinette

+0

其實它似乎支持ROLLUP,但又沒有別名 – 00robinette

+0

@ 00robinette我已更新答案。你能檢查一下嗎? – minatverma

0

ROLLUP是正確的方法,但由於大量的工作被用來計算如何正確執行我正在回答我自己的問題。感謝您的意見。

SELECT 
    rlrDivision.strLangue1 AS Division, 
    COUNT(p.pkPoste) AS NumbersToAdd, 
    SUM(p.iNbPostes) AS MoreNumbersToAdd 
    FROM poste p 
    LEFT JOIN lien_objet_ref_8 lienDiv ON lienDiv.fkObjet = p.pkPoste 
    LEFT JOIN r_referentiel rrDivision ON rrDivision.pkReferentiel =     lienDiv.fkReferentiel 
    LEFT JOIN r_libelle_ref rlrDivision ON rlrDivision.pkLibelleRef = rrDivision.fkLib1 
    LEFT JOIN lien_objet_liste_3 lol3 ON lol3.fkObjet = p.pkPoste AND lol3.`strContexte`='descriptif/type_contrat' 
    LEFT JOIN r_liste_editable rle ON lol3.fkListe = rle.pkListeEditable 
    LEFT JOIN r_libelle_ref rlr ON rle.fkLibelleRef = rlr.pkLibelleRef 
    WHERE p.pkPoste <> 1 
    AND p.iSuppr <> 1 
AND p.iDernierEtatPoste = 2 
AND rlr.strLangue1 LIKE 'Full-time%' 
GROUP BY rlrDivision.strLangue1 WITH ROLLUP