0

EDIT: The original post follows, but its a bit long and wordy. This edit presents a simplified question.

I'm trying to SUM 1 column multiple times; from what I've found, my options are either CASE or (SELECT). I am trying to SUM based on a date range and I can't figure out if CASE allows that.

 table.number  |  table.date  
   2        2014/12/18
   2        2014/12/19
   3        2015/01/11
   3        2015/01/12
   7        2015/02/04
   7        2015/02/05

As separate queries, it would look like this:

SELECT SUM(number) as alpha FROM table WHERE date >= 2014/12/01 AND date<= DATE_ADD (2014/12/01, INTERVAL 4 WEEKS)
SELECT SUM(number) as beta FROM table WHERE date >= 2014/12/29 AND date<= DATE_ADD (2014/12/01, INTERVAL 4 WEEKS)
SELECT SUM(number) as gamma FROM table WHERE date >= 2014/01/19 AND date<= DATE_ADD (2014/12/01, INTERVAL 4 WEEKS)

Looking for result set

alpha   |   beta   | gamma
  2          6         14

ORIGINAL: I'm trying to return SUM of payments that will be due within my budgeting time frame (4 weeks) for the current budgeting period and 2 future periods. Some students pay every 4 weeks, others every 12. Here are the relevant fields in my tables:

client.name |   client.ppid |  client.last_payment
john            |   1       |   12/01/14
jack            |   2       |   11/26/14
jane            |   3       |    10/27/14       



pay_profile.id  | pay_profile.price | pay_profile.interval  (in weeks)
        1                   140                     4
        2                   399                     4
        3                   1                       12

pay_history.name    |   pay_history.date  | pay_history.amount 
john                |       12/02/14      |        140
jerry               |   more historical   |       data


budget.period_start |
        12/01/14

I think the most efficient way of doing this is:

1.)SUM all students who pay every 4 weeks as base_pay

2.)SUM all students who pay every 12 weeks and whose DATEADD(client.last_payment, INTERVAL pay_profile.interval WEEKS) is >= budget.period_start and <= DATEADD(budget.period_start, INTERVAL 28 DAYS) as accounts_receivable

3.) As the above step will miss people who've already paid in this budgeting period (as this updates their last_payment dating, putting them out of the range specified in #2), I'll also need to SUM pay_history.date for the range above as well. paid_in_full

4.) repeat step 2 above, adjusting the range and column name for future periods (i.e. accounts_receivable_2

5.) use php to SUM base_pay, accounts_receivable, and pay_history, repeating the process for future periods.

I'm guessing the easiest way would be to use CASE, which I've not done before. Here was my best guess, which fails due to a sytax error. I assuming I can use DATE_ADD in the WHEN statement.

SELECT 
CASE 
    DATE_ADD(client.last_payment, INTERVAL pay_profile.interval WEEK) >= budget.period_start 
    AND 
    DATE_ADD(client.last_payment, INTERVAL pay_profile.interval WEEK) <=                                                           
    DATE_ADD(budget.period_start,INTERVAL 28 DAY) THEN SUM(pay_profile.price) as base_pay
FROM client
LEFT OUTER JOIN pay_profile ON client.ppid = pay_profile.ppid 
LEFT OUTER JOIN budget ON client.active = 1
WHERE
client.active = 1 

Thanks.

3
  • You lost me. Consider providing proper DDLs and a desired result set.
    – Strawberry
    Dec 6, 2014 at 10:52
  • Does the edit clear it up?
    – Kale
    Dec 6, 2014 at 17:16
  • For me? No, it doesn't.
    – Strawberry
    Dec 6, 2014 at 17:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.