Reputation: 45
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 i.e. 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, e.g.
SELECT
SUM(dvdetails.amount) ,
delvoucher.dvdate,
delvoucher.dvno, ............
It may be simple but its giving me error. where am i going wrong
Upvotes: 0
Views: 441
Reputation: 18379
@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
Upvotes: 0
Reputation: 4718
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.
Upvotes: 2