使用ROLLUP对行进行求和

时间:2016-02-05 03:10:48

标签: sql 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上有另一行总计。

2 个答案:

答案 0 :(得分:0)

你可以str.split(",").join(' ')

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 ; 函数可用于查询,但不确定数据库是否支持,因为您还没有提到数据库名称。

答案 1 :(得分: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