2016-04-27 64 views
1

你好,我試圖寫一個查詢,它包括計算2個值的總和,第二個和是第一個值和另一個值之間的乘法結果。有人可以幫助解決這個問題嗎? (請原諒我的英文,即時通訊開發者):connot執行一個聚合函數......與GROUP BY

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 
+0

您是否曾嘗試將組添加到與您的非聚合列相同? GROUP BY ISNULL(CONVERT(VARCHAR,CONVERT(DATE,MARE_DAT_CRE,103)),'Total')WITH ROLLUP –

+0

我也使用GROUP BY和ROLLUP子句,但它沒有顯示 –

+0

是@RichBenner,但它沒有顯示在我的問題上: –

回答

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 
+0

謝謝@豐富的benner,我可以通過使用左加入來解決它,我很高興^^謝謝 –

+0

高興地幫助:)。如果這個答案已經爲你排序,隨時接受和upvote。 –

相關問題