Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/144636-solved-query-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/144636-solved-query-help/#findComment-758980
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.