Brian W Posted February 10, 2009 Share Posted February 10, 2009 Working on a query that will group the total amount that a donor has donated with the donor name. Example query: SELECT d.Pub_Name as Pub_Name, f.Amount as Amount, f.Date as Date, f.ID as ID, f.contact_id as contact_id FROM donors d LEFT JOIN donations f ON f.contact_id = d.contact_id WHERE Amount >= 0 AND Amount <= 9999999 ORDER BY Date DESC I want amount per donor to be summed so I can show the total of all donations given in a time period. I'm thinking I may need to reverse who is on the left of the left-join, but still don't know how to get the sum of all donations associated with that donor without placing a query for donations in my loop of donors. Appreciate any input. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/144636-solved-query-help/ Share on other sites More sharing options...
Maq Posted February 10, 2009 Share Posted February 10, 2009 Why can't you use the aggregate SUM function on amount? Quote Link to comment https://forums.phpfreaks.com/topic/144636-solved-query-help/#findComment-758973 Share on other sites More sharing options...
Brian W Posted February 10, 2009 Author Share Posted February 10, 2009 Well, just as I got the notify someone had replied to my topic, I found the solution. SELECT d.Pub_Name as Pub_Name, sum(f.Amount) as Amount, f.Date as Date, f.ID as ID, f.contact_id as contact_id FROM donations f LEFT JOIN donors d ON d.contact_id = f.contact_id WHERE Amount >= 0 AND Amount <= 9999999 GROUP BY d.contact_id ORDER BY Date DESC A combination of sum and grouping. Thanks Maq for the reply though. I had already tried sum and wasn't getting the results I was hoping for, but now I am with group. Quote Link to comment https://forums.phpfreaks.com/topic/144636-solved-query-help/#findComment-758980 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.