connot使用GROUP BY

时间:2016-04-27 09:29:31

标签: sql sql-server database join subquery

您好我试图写一个查询,其中包括计算2个值的总和,第二个和是第一个值与另一个值之间相乘的结果。请有人帮忙解决这个问题吗? (对不起我的英语,我是frensh开发者):

    SELECT ISNULL(CONVERT(VARCHAR,CONVERT(date,MARE_DAT_CRE,103)),'Total') AS Dat
       , SUM  (MARE_CAUTIONNEMENT) AS HT
       ,  SUM ( MARE_CAUTIONNEMENT * ( SELECT DISTINCT LCF_TAUXTVA 
            FROM F_LIGNECOMFOU 
            INNER JOIN F_AFFAIRES ON LCF_CODE_AFF = AF_CODE_AFFAIRE 
            INNER JOIN F_LOT ON LT_AFFAIRE = AF_CODE_AFFAIRE
            INNER JOIN F_COMMANDEFOU ON CF_NUMERO = LCF_CF_NUMERO
            INNER JOIN F_P_FOURNISSEUR ON CF_IDENT_FO = FOU_IDENT
            WHERE AF_CODE_AFFAIRE = '15065-00' AND LT_IDENT =  500002200 AND FOU_IDENT = 500000838 ) ) FROM F_AVENANT_RETENUE INNER JOIN F_MARCHE_AVENANT ON MAAV_IDENT = MARE_MAAV_IDENT INNER JOIN F_MARCHE_TRAVAUX ON MATR_IDENT = MAAV_MATR_IDENT INNER JOIN F_AFFAIRES ON AF_CODE_AFFAIRE = MATR_AF_IDENT INNER JOIN F_LOT ON LT_AFFAIRE  = AF_CODE_AFFAIRE INNER JOIN F_AVENANT_COTATION ON AVCO_IDENT = MARE_AVCO_IDENT WHERE AF_CODE_AFFAIRE = '15065-00' AND LT_IDENT =  500002200 AND MATR_FOU_IDENT = 500000838 AND MARE_CAUTIONNEMENT IS NOT NULL
GROUP BY MARE_DAT_CRE WITH ROLLUP  

1 个答案:

答案 0 :(得分:0)

您可以尝试在联接中粘贴该子选择。现在你还没有使用表别名所以我不确定哪些表包含字段AF_CODE_AFFAIRE,LT_IDENT和MATR_FOU_IDENT所以你必须添加表别名;

SELECT ISNULL(CONVERT(VARCHAR, CONVERT(DATE, MARE_DAT_CRE, 103)), 'Total') AS Dat
    ,SUM(MARE_CAUTIONNEMENT) AS HT
    ,SUM(MARE_CAUTIONNEMENT) * SUM(new.LCF_TAUXTVA)
FROM F_AVENANT_RETENUE
INNER JOIN F_MARCHE_AVENANT ON MAAV_IDENT = MARE_MAAV_IDENT
INNER JOIN F_MARCHE_TRAVAUX ON MATR_IDENT = MAAV_MATR_IDENT
INNER JOIN F_AFFAIRES ON AF_CODE_AFFAIRE = MATR_AF_IDENT
INNER JOIN F_LOT ON LT_AFFAIRE = AF_CODE_AFFAIRE
INNER JOIN F_AVENANT_COTATION ON AVCO_IDENT = MARE_AVCO_IDENT
LEFT JOIN (
    SELECT DISTINCT LCF_TAUXTVA
    FROM F_LIGNECOMFOU
    INNER JOIN F_AFFAIRES ON LCF_CODE_AFF = AF_CODE_AFFAIRE
    INNER JOIN F_LOT ON LT_AFFAIRE = AF_CODE_AFFAIRE
    INNER JOIN F_COMMANDEFOU ON CF_NUMERO = LCF_CF_NUMERO
    INNER JOIN F_P_FOURNISSEUR ON CF_IDENT_FO = FOU_IDENT
    ) new ON AF_CODE_AFFAIRE = new.AF_CODE_AFFAIRE
    AND LT_IDENT = new.LT_IDENT
    AND MATR_FOU_IDENT = new.MATR_FOU_IDENT
WHERE AF_CODE_AFFAIRE = '15065-00'
    AND LT_IDENT = 500002200
    AND MATR_FOU_IDENT = 500000838
    AND MARE_CAUTIONNEMENT IS NOT NULL
GROUP BY ISNULL(CONVERT(VARCHAR, CONVERT(DATE, MARE_DAT_CRE, 103)), 'Total')
WITH ROLLUP