返回select语句的总和SQL

时间:2017-07-25 13:53:22

标签: sql

我创建了一个SELECT语句:

SELECT ARII2.Amount  
FROM 
    AR_Customer ARC2
    Inner JOIN AR_Customer_Site         ARCS2   On ARC2.Customer_Id = ARCS2.Customer_Id AND ARCS2.Customer_Id = ARC2.Customer_Id
    INNER JOIN AR_Customer_System       ARCSYS2  On ARCS2.Customer_Site_Id = ARCSYS2.Customer_Site_Id
    INNER JOIN AR_Branch                ARB2     ON ARB2.Branch_Id = ARC2.Branch_Id
    INNER JOIN AR_Invoice               ARIN2    ON ARIN2.Customer_Site_Id = ARCS2.Customer_Site_Id
    INNER JOIN AR_Invoice_Item          ARII2    ON ARII2.Invoice_Id = ARIN2.Invoice_Id   
    Inner JOIN SY_System                SYSY2   On ARCSYS2.System_Id = SYSY2.System_Id                     
WHERE 
    ARIN2.Invoice_Date > dateadd(year, -1, getdate())
    AND ARC2.Customer_Number = '300000'         
    AND ARII2.[Description] LIKE ('Warranty Credit')
    OR ARII2.[Description] = ('Warranty Credit T')
GROUP BY ARII2.Amount

返回以下结果。

2031.00
1458.98
1272.50
620.00
160.00

我的想法是我可以在我的Amount附近放一个SUM,它会返回5542.48(值的总和)的期望值。

SELECT 
    SUM(ARII2.Amount)  
FROM 
    AR_Customer ARC2
    Inner JOIN AR_Customer_Site         ARCS2   On ARC2.Customer_Id = ARCS2.Customer_Id AND ARCS2.Customer_Id = ARC2.Customer_Id
    INNER JOIN AR_Customer_System       ARCSYS2  On ARCS2.Customer_Site_Id = ARCSYS2.Customer_Site_Id
    INNER JOIN AR_Branch                ARB2     ON ARB2.Branch_Id = ARC2.Branch_Id
    INNER JOIN AR_Invoice               ARIN2    ON ARIN2.Customer_Site_Id = ARCS2.Customer_Site_Id
    INNER JOIN AR_Invoice_Item          ARII2    ON ARII2.Invoice_Id = ARIN2.Invoice_Id   
    Inner JOIN SY_System                SYSY2   On ARCSYS2.System_Id = SYSY2.System_Id                     
WHERE 
    ARIN2.Invoice_Date > dateadd(year, -1, getdate())
    AND ARC2.Customer_Number = '300000'         
    --AND ARIN2.Invoice_Number = '204686'
    AND ARII2.[Description] LIKE ('Warranty Credit')
    OR ARII2.[Description] = ('Warranty Credit T')
GROUP BY ARII2.Amount, ARII2.[Description]

返回了以下不是我要找的结果。

-10155.00
-7294.90
-6362.50
-3100.00
-800.00

一如既往,对此的任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:4)

由于您的GROUP BY,您获得了多行。删除该行,您将只获得一个总和。