I started writing an aging program for our A/R and started looking for ways to accomplish this within MYSQL.
I came up with this query that accomplished the task
[code]
set @AsOfPeriod="201312";
set @CurrentPeriod=@AsOfPeriod;
set @ThirtyDaysPeriod=PERIOD_ADD(@AsOfPeriod,-1);
set @SixtyDaysPeriod=PERIOD_ADD(@AsOfPeriod,-2);
set @NinetyDaysPeriod=PERIOD_ADD(@AsOfPeriod,-3);
select
artransactions.idealer,
artransactions.dOutStandingBalance as Bal,
min(icloseyear * 100 + iclosemonth) as minPer,
if (min(icloseyear * 100 + iclosemonth)=max(icloseyear * 100 + iclosemonth), "",max(icloseyear * 100 + iclosemonth)) as maxPer,
sum(doutstandingbalance) as SumOutBal,
sum(if ( (iclosemonth + icloseyear * 100)=@CurrentPeriod, dOutStandingBalance,0 )) as "Current",
sum(if ( (iclosemonth + icloseyear * 100)=@ThirtyDaysPeriod, dOutStandingBalance,0 )) as '30Days',
sum(if ( (iclosemonth + icloseyear * 100)=@SixtyDaysPeriod, dOutStandingBalance,0 )) as '60Days',
sum(if ( (icloseyear * 100 + iclosemonth) <= @NinetyDaysPeriod, doutstandingbalance,0)) as "Over 90Days",
@AsOfPeriod ,
@CurrentPeriod,
@ThirtyDaysPeriod,
@SixtyDaysPeriod,
@NinetyDaysPeriod
from artransactions
where artransactions.dOutStandingBalance > 0
group by idealer
order by maxper desc
[/code]
No comments:
Post a Comment