如何根据上个月的计算更新每个月的期初余额?

时间:2019-10-10 19:30:32

标签: mysql sql date formula

我本质上是试图计算SQL中的滚动总数。我每个月都有一些不同的总计(费用,付款,调整,坏账),需要从上个月的期末余额中减去这些金额。 5月份的期末余额为28,814,788美元。鉴于6月份的总额(总计1,030,927美元),6月份的期末余额现在应为27,783,862美元(28,814,788美元-830,998美元)。这笔$ 27,783,862美元现在应该是七月的期初余额。

我编写了以下代码。 SELECT子句中的子查询提供了应从6月的总计中减去5月的未偿余额(期末余额28,814,788美元)。

SELECT  
    CASE    WHEN POST_DATE BETWEEN '2019-08-01' AND '2019-08-31' THEN 'August - 2019'
            WHEN POST_DATE BETWEEN '2019-07-01' AND '2019-07-31' THEN 'July - 2019'
            WHEN POST_DATE BETWEEN '2019-06-01' AND '2019-06-30' THEN 'June - 2019'
            ELSE 'Other' END as month_year,
    (SELECT sum(OUTSTANDING_AMT)
    FROM ARPB_TRANSACTIONS
    WHERE POST_DATE BETWEEN '2019-05-01' AND '2019-05-31') as begin_bal,
        sum(CHARGE_AMOUNT)+sum(VOID_AMOUNT) as charge_tot, 
        sum(PAYMENT_AMOUNT) as payment_tot,
        sum(ADJUSTMENT_AMOUNT) as adjust_tot,
        sum(ACTIVE_AR_IN_OUT) as bad_debt
FROM F_ARPB_CUBE_ETR_DETAIL
WHERE POST_DATE BETWEEN '2019-06-01' AND '2019-08-31'
GROUP BY 
    CASE    WHEN POST_DATE BETWEEN '2019-08-01' AND '2019-08-31' THEN 'August - 2019'
            WHEN POST_DATE BETWEEN '2019-07-01' AND '2019-07-31' THEN 'July - 2019'
            WHEN POST_DATE BETWEEN '2019-06-01' AND '2019-06-30' THEN 'June - 2019'
            ELSE 'Other' END

预期结果将是:DESIRED OUTPUT

1 个答案:

答案 0 :(得分:0)

因此,我实际上找到了自己问题的答案。我走了几步。

首先,我决定通过添加一列来反映费用,付款,调整和坏账的总和来稍微组织一下事情。这对于以后的运行总计很有用。感觉有点“ hacky”,但是,我只是在学习,等等。

sum(CHARGE_AMOUNT)+sum(VOID_AMOUNT)+sum(PAYMENT_AMOUNT)+
    sum(ADJUSTMENT_AMOUNT)+sum(ACTIVE_AR_IN_OUT) as sum_of_values 

第二,我创建了一个运行总计。最初这是一个挑战,因为除非我给别名指定为month_year并将其放在FROM子句的子查询中,否则我无法引用月份和年份。

FROM 
    (SELECT 
CASE    WHEN POST_DATE BETWEEN '2019-06-01' AND '2019-06-30' THEN 'June - 2019'
        WHEN POST_DATE BETWEEN '2019-07-01' AND '2019-07-31' THEN 'July - 2019'
        WHEN POST_DATE BETWEEN '2019-08-01' AND '2019-08-31' THEN 'August - 2019'
        ELSE 'Other' END as month_year,
        (SELECT sum(OUTSTANDING_AMT)
    FROM ARPB_TRANSACTIONS
    WHERE POST_DATE BETWEEN '2019-05-01' AND '2019-05-31') as beginning_balance,
    sum(CHARGE_AMOUNT)+sum(VOID_AMOUNT) as charge_tot, 
    sum(PAYMENT_AMOUNT) as payment_tot,
    sum(ADJUSTMENT_AMOUNT) as adjustment_tot,
    sum(ACTIVE_AR_IN_OUT) as bad_debt_tot,
    sum(CHARGE_AMOUNT)+sum(VOID_AMOUNT)+sum(PAYMENT_AMOUNT)+
    sum(ADJUSTMENT_AMOUNT)+sum(ACTIVE_AR_IN_OUT) as sum_of_values 
FROM F_ARPB_CUBE_ETR_DETAIL 
WHERE POST_DATE BETWEEN '2019-06-01' AND '2019-08-31'
GROUP BY CASE   
    WHEN POST_DATE BETWEEN '2019-06-01' AND '2019-06-30' THEN 'June - 2019'
    WHEN POST_DATE BETWEEN '2019-07-01' AND '2019-07-31' THEN 'July - 2019'
    WHEN POST_DATE BETWEEN '2019-08-01' AND '2019-08-31' THEN 'August - 2019'
    ELSE 'Other' END ) as x

第三,这使我可以在运行的总表达式中具体引用“ month_year”:

 SELECT month_year, beginning_balance,charge_tot, payment_tot, adjustment_tot, bad_debt_tot, sum_of_values, SUM(sum_of_values) OVER (ORDER BY month_year desc) as running_total

最后,我实际上需要从前几个月的运行总计中减去运行总和(即“ beginning_balance”减去“ sum_of_values”),因此我只需要修改上面的表达式即可:

SELECT  month_year, beginning_balance,charge_tot, payment_tot, adjustment_tot, bad_debt_tot, sum_of_values, beginning_balance-SUM(sum_of_values) OVER (ORDER BY month_year desc) as running_total

这是最终的查询:

SELECT  month_year, beginning_balance,charge_tot, payment_tot, adjustment_tot, bad_debt_tot, sum_of_values, beginning_balance-SUM(sum_of_values) OVER (ORDER BY month_year desc) as running_total
FROM 
    (SELECT 
CASE    WHEN POST_DATE BETWEEN '2019-06-01' AND '2019-06-30' THEN 'June - 2019'
        WHEN POST_DATE BETWEEN '2019-07-01' AND '2019-07-31' THEN 'July - 2019'
        WHEN POST_DATE BETWEEN '2019-08-01' AND '2019-08-31' THEN 'August - 2019'
        ELSE 'Other' END as month_year,
        (SELECT sum(OUTSTANDING_AMT)
    FROM ARPB_TRANSACTIONS
    WHERE POST_DATE BETWEEN '2019-05-01' AND '2019-05-31') as beginning_balance,
    sum(CHARGE_AMOUNT)+sum(VOID_AMOUNT) as charge_tot, 
    sum(PAYMENT_AMOUNT) as payment_tot,
    sum(ADJUSTMENT_AMOUNT) as adjustment_tot,
    sum(ACTIVE_AR_IN_OUT) as bad_debt_tot,
    sum(CHARGE_AMOUNT)+sum(VOID_AMOUNT)+sum(PAYMENT_AMOUNT)+
    sum(ADJUSTMENT_AMOUNT)+sum(ACTIVE_AR_IN_OUT) as sum_of_values 
FROM F_ARPB_CUBE_ETR_DETAIL 
WHERE POST_DATE BETWEEN '2019-06-01' AND '2019-08-31'
GROUP BY CASE   
    WHEN POST_DATE BETWEEN '2019-06-01' AND '2019-06-30' THEN 'June - 2019'
    WHEN POST_DATE BETWEEN '2019-07-01' AND '2019-07-31' THEN 'July - 2019'
    WHEN POST_DATE BETWEEN '2019-08-01' AND '2019-08-31' THEN 'August - 2019'
    ELSE 'Other' END ) as x
ORDER BY month_year desc 

Final Output