If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > selective sum?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-25-09, 05:49
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
selective sum?

Just to briefly outline; in our application, Users can send Greetings to each other on behalf of a selected Charity. A Greeting may include an optional Gift, and if a Gift is included, they may add an additional donation of their choosing.

I've got the following report query that pulls out a list of Charities with the total number of greetings sent on their behalf, along with the total value of Gifts and Donations made to them:

Code:
SELECT
  c.idCharities, c.charityName,
  COUNT(g.idGreetings) AS totalGreetings,
  SUM(gi.giftValue) AS giftValueTotal,
  SUM(g.giftDonationAmount) AS giftDonationTotal
FROM Greetings g
  INNER JOIN Charities c ON g.idCharities = c.idCharities
  LEFT JOIN Gifts gi ON g.idGifts = gi.idGifts
WHERE
  (g.sendDate > c.lastPaymentDate)
  OR c.lastPaymentDate IS NULL
GROUP BY g.idCharities
ORDER BY c.charityName
I need to build on this: I need to add a total for Gift Aid.

There is another field in table Greetings; "giftAid", which will be 1 or 0.

If it's 1, I need to add together the gift value for any gift attached to that greeting, and any donation amount that may have been included, and multiply that by a fixed number - currently 0.282 - to find out the total of Gift Aid that should be claimed.

So I need to add something like:

Code:
SUM (
(
gi.giftValue WHERE g.giftAid = 1
+
g.giftDonationAmount WHERE g.giftAid = 1
)* 0.282
) AS totalGiftAid
... but clearly that's not working; I have no idea, however, how it should be structured. Can anyone help?
Reply With Quote
  #2 (permalink)  
Old 06-25-09, 09:00
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
I don't clearly understand what you want to acheive, so consider this a SWAG but:
Code:
Sum (CASE WHEN 1 = g.giftAid
   THEN i.giftValue * 0.282 END
) AS totalGiftAid
At least it should give you some clues if it isn't what you want.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On