mysql - CASE w/ DATEADD range to SUM column multiple times for future earnings estimate -
edit: original post follows, bit long , wordy. edit presents simplified question.
i'm trying sum
1 column multiple times; i've found, options either case
or (select)
. trying sum based on date range , 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, this:
select sum(number) alpha table date >= 2014/12/01 , date<= date_add (2014/12/01, interval 4 weeks) select sum(number) beta table date >= 2014/12/29 , date<= date_add (2014/12/01, interval 4 weeks) select sum(number) gamma table date >= 2014/01/19 , date<= date_add (2014/12/01, interval 4 weeks)
looking result set
alpha | beta | gamma 2 6 14
original: i'm trying return sum
of payments due within budgeting time frame (4 weeks) current budgeting period , 2 future periods. students pay every 4 weeks, others every 12. here relevant fields in 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 efficient way of doing is:
1.)sum students pay every 4 weeks base_pay
2.)sum students pay every 12 weeks , dateadd(client.last_payment, interval pay_profile.interval weeks)
>= budget.period_start
, <= dateadd(budget.period_start, interval 28 days)
accounts_receivable
3.) above step miss people who've paid in budgeting period (as updates last_payment dating, putting them out of range specified in #2), i'll need sum pay_history.date range above well. paid_in_full
4.) repeat step 2 above, adjusting range , column name future periods (i.e. accounts_receivable_2
5.) use php sum base_pay
, accounts_receivable
, , pay_history
, repeating process future periods.
i'm guessing easiest way use case, i've not done before. here best guess, fails due sytax error. assuming can use date_add
in when
statement.
select case date_add(client.last_payment, interval pay_profile.interval week) >= budget.period_start , date_add(client.last_payment, interval pay_profile.interval week) <= date_add(budget.period_start,interval 28 day) sum(pay_profile.price) base_pay client left outer join pay_profile on client.ppid = pay_profile.ppid left outer join budget on client.active = 1 client.active = 1
thanks.
Comments
Post a Comment