简体   繁体   中英

SQL - Getting Sum of all month payable while having a WHERE on months

Edited : Thank you every one for your response but it seems it all went out of context. My fault I should have gone into more details.

The query is for Master-Child reportviewer (SQL Server 2008 R2 at backend); where delvoucher has a 1:m relation with dvdetails table. The report is actually an invoice, that display cient_title and deliverydate etc. on top (list) and details of delivery in the inner tablix (Master-Child report scenario). At the bottom, I show sum of all the amount payable for selected month. ( the month is selected through a date time picker placed in same form that also contains my reportviewer control and refreshes report on selected value change). However, I want to get the total payable since beginning of customer minus this month amount ie balance payable). Shall I use UNION? and run a separate query to get SUM(amount) without applying WHERE month is blah blah.

the overall problem is i have to display arrear (sum(payable) - sum(paid)) where paid amount is to be got from another table : Edit Close

My Query

SELECT        
    delvoucher.dvdate, 
    delvoucher.dvno, 
    delvoucher.vehicleno, 
    delvoucher.salesman, 
    dvdetails.[desc], 
    dvdetails.supply, 
    dvdetails.empty, 
    dvdetails.amount, 
    dvdetails.rate, 
    delvoucher.title, 
    customer.currentaddress, 
    customer.phone, 
    customer.code
FROM         
    delvoucher 
    INNER JOIN dvdetails ON delvoucher.id = dvdetails.pid 
    INNER JOIN customer ON delvoucher.title = customer.title
WHERE        
    YEAR(delvoucher.dvdate) = YEAR(@i) 
    AND MONTH(delvoucher.dvdate) = MONTH(@i)

All I want is retrieve SUM(dvdetails.amount) as well, eg

SELECT        
    SUM(dvdetails.amount) , 
    delvoucher.dvdate, 
    delvoucher.dvno, ............

It may be simple but its giving me error. where am i going wrong

You can use GROUP BY statement with SUM() to make a monthly summary like this:

SELECT 
    SUM(dvdetails.amount), 
    YEAR(delvoucher.dvdate), 
    MONTH(delvoucher.dvdate) 
FROM
    delvoucher 
    INNER JOIN dvdetails ON delvoucher.id = dvdetails.pid 
    INNER JOIN customer ON delvoucher.title = customer.title
WHERE
    YEAR(delvoucher.dvdate) = YEAR(@i) 
    AND MONTH(delvoucher.dvdate) = MONTH(@i)
GROUP BY 
    YEAR(delvoucher.dvdate), 
    MONTH(delvoucher.dvdate)

You can read a related question here: SQL query to retrieve SUM in various DATE ranges

And a document of GOUP BY is here: http://www.w3schools.com/sql/sql_groupby.asp

Hope this helps.

@naota has the right answer but in case your looking for the select like this ie to return all rows of delvoucher:

SELECT        
    SUM(dvdetails.amount) , 
    delvoucher.dvdate, 
    delvoucher.dvno, ............

Rather than

SELECT 
    AmountSum = SUM(dvdetails.amount),
    Year = YEAR(delvoucher.dvdate),
    Month = MONTH(delvoucher.dvdate)

Then your query could look like this (untested)

SELECT        
    totals.AmountSum,
    delvoucher.dvdate, 
    delvoucher.dvno, 
    delvoucher.vehicleno, 
    delvoucher.salesman, 
    dvdetails.[desc], 
    dvdetails.supply, 
    dvdetails.empty, 
    dvdetails.amount, 
    dvdetails.rate, 
    delvoucher.title, 
    customer.currentaddress, 
    customer.phone, 
    customer.code
FROM         
    delvoucher 
    INNER JOIN dvdetails ON delvoucher.id = dvdetails.pid 
    INNER JOIN customer ON delvoucher.title = customer.title
    INNER JOIN (   
        SELECT 
            AmountSum = SUM(dvdetails.amount),
            Year = YEAR(delvoucher.dvdate),
            Month = MONTH(delvoucher.dvdate) 
        FROM
            delvoucher 
            INNER JOIN dvdetails ON delvoucher.id = dvdetails.pid 
            INNER JOIN customer ON delvoucher.title = customer.title
        WHERE
            YEAR(delvoucher.dvdate) = YEAR(@i) 
            AND MONTH(delvoucher.dvdate) = MONTH(@i)
        GROUP BY 
            YEAR(delvoucher.dvdate), 
            MONTH(delvoucher.dvdate)
    ) totals ON totals.Year = YEAR(delvoucher.dvdate) AND totals.Month = MONTH(delvoucher.dvdate)
WHERE        
    YEAR(delvoucher.dvdate) = YEAR(@i) 
    AND MONTH(delvoucher.dvdate) = MONTH(@i)

Basically your query inner joins @naota's query joining on year and month

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM