Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Query not summing values by year

Why isn't this SQL in query designer summing the donations by family by year?

SELECT [Family Information].ID, Donations.[Date of Donation], Sum(Donations.[Donation Amount]) AS [SumOfDonation Amount]
FROM [Family Information] RIGHT JOIN Donations ON [Family Information].ID = Donations.[Family Name]
GROUP BY [Family Information].ID, Donations.[Date of Donation]
ORDER BY [Family Information].ID, Donations.[Date of Donation];
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Main source of problem: you are grouping not by year but by date (ie with resolution of teh second or less)

Suggested solution:
Not sure how to extract year from date in your own SQL dialect. With the examples at https://msdn.microsoft.com/en-us/library/ms174395.aspx  , your query could be something like
SELECT [Family Information].ID, Donations.[Date of Donation], 
DATENAME(year,  Donations.[Date of Donation]) AS DonYear,
Sum(Donations.[Donation Amount]) AS [SumOfDonation Amount]
FROM [Family Information] RIGHT JOIN Donations ON [Family Information].ID = Donations.[Family Name]
GROUP BY [Family Information].ID, DATENAME(year,  Donations.[Date of Donation])
ORDER BY [Family Information].ID, DATENAME(year,  Donations.[Date of Donation]) , Donations.[Date of Donation]; 

Open in new window