SQL Server 2008 : display total of line items for multiple invoices per customer

时间:2016-08-31 17:45:34

标签: sql-server-2008 group-by sum

This is a very simple question but my brain is fried and I'm having trouble phrasing my search question on google.

I have a query which creates a result set that shows:

customer ID, 
Customer Name,
Line amount

The line amount column displays for an invoice, but the Invoice ID might display on multiple rows because at the Line item level an invoice ID might apply to multiple Items that were sold. All I want is for the Customer ID and Name to be group to show only once, and the total amount of each invoice to display using the line amount column.

This is my query:

SELECT
    SALESTABLE.CUSTACCOUNT as CustomerNumber, 
    CUSTINVOICETRANS.JSDELIVERYNAME as CustomerName,
    SUM(CUSTINVOICETRANS.LINEAMOUNT) as TotalAmount
FROM
    salesline (nolock) 
JOIN
    salestable (nolock) ON salesline.salesid = salestable.salesid
                        AND salesline.salesstatus = '3'
                        AND salesline.dataareaid = '110'
                        AND salesline.custgroup IN ('trade', 'trade npo')
                        AND salesline.itemid NOT LIKE 'freight%'
                        AND salestable.dataareaid = '110'
JOIN
    inventtable (nolock) ON inventtable.itemid = salesline.itemid 
                         AND inventtable.dataareaid = '110'
JOIN
    ECORESPRODUCTtranslation (nolock) ON ECORESPRODUCTtranslation.recid = INVENTTABLE.PRODUCT
JOIN
    CUSTTABLE (nolock) ON SALESTABLE.CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM
                       AND custtable.dataareaid = '110'
JOIN
    DIRPARTYLOCATION (nolock) ON CUSTTABLE.PARTY = DIRPARTYLOCATION.PARTY 
                              AND DIRPARTYLOCATION.ispostaladdress = '1'
JOIN
    DIRPARTYPOSTALADDRESSVIEW (nolock) ON DIRPARTYPOSTALADDRESSVIEW.PARTY = custtable.PARTY  
                                       AND DIRPARTYPOSTALADDRESSVIEW.location = DIRPARTYLOCATION.location
                                       AND DIRPARTYPOSTALADDRESSVIEW.validto = '12/31/2154 23:59:59'
                                       AND DIRPARTYPOSTALADDRESSVIEW.isprimary = '1'
JOIN
    hcmworker (nolock) ON salestable.workersalesresponsible = hcmworker.recid
JOIN
    DIRPARTYTABLE (nolock) ON hcmworker.PERSON = DIRPARTYTABLE.recid
JOIN
    CUSTINVOICETRANS (nolock) ON CUSTINVOICETRANS.inventtransid = salesline.inventtransid
                              AND CUSTINVOICETRANS.dataareaid = '110'
WHERE
    CUSTINVOICETRANS.INVOICEdate >= '8/25/2016' 
    AND CUSTINVOICETRANS.INVOICEdate <= getdate()
GROUP BY
    salestable.CUSTACCOUNT, CUSTINVOICETRANS.JSDELIVERYNAME, CUSTINVOICETRANS.LINEAMOUNT

Thank you

Currently, my result set looks like this:

CustomerNumber  CustomerName        TotalAmount
1000002         *customer name*     15.8000000000000000
1000002         *customer name*     9.8400000000000000
1000002         *customer name*     11.1600000000000000
1000002         *customer name*     89.6400000000000000

1 个答案:

答案 0 :(得分:0)

From what you explained, In the Group BY remove , CUSTINVOICETRANS.LINEAMOUNT. You don't need that column in the Group By when you are summing that value.