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

Posted

技术标签:

【中文标题】如何根据上个月的计算更新每个月的期初余额?【英文标题】:How can I update the beginning balance for each month based on prior month's calculation? 【发布时间】:2019-10-10 19:30:32 【问题描述】:

我实际上是在尝试在 SQL 中计算滚动总计。我每个月都有几个不同的总额(费用、付款、调整、坏账),需要从上个月的期末余额中减去这些总额。 5 月,期末余额为 28,814,788 美元。考虑到 6 月份的总和(总计 1,030,927 美元),6 月份的期末余额现在应该是 27,783,862 美元(28,814,788 美元 - 830,998 美元)。这 27,783,862 美元现在应该是 7 月份的期初余额。

我编写了以下代码。 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】:

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

首先,我决定通过添加一个列来反映费用、付款、调整和坏账的总和来进行一些整理。这在以后的运行总数中会派上用场。感觉有点“骇人听闻”,但我只是在学习,所以随便吧。

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

最后,我实际上需要从前几个月的运行总数中减去运行(即“开始余额”减去“总和值”),而不是运行(总和)总数,所以我只是修改了上面的表达式,如:

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

【讨论】:

请注意,使用包容性上限意味着您需要在 2 月左右特别小心(取决于您获得月末日期的方式)。使用下一个月的专属上限(例如,WHEN POST_DATE >= '2019-06-01' AND POST_DATE < '2019-07-01')会更容易,如果POST_DATE 也有时间,则可以解决任何问题。您可能会发现使用日历表也会简化事情。 啊,非常感谢,我去看看日历表

以上是关于如何根据上个月的计算更新每个月的期初余额?的主要内容,如果未能解决你的问题,请参考以下文章

ABAP 如何使用select实现多值和输出 比如 十二个月的余额 输出为一年的余额

SSIS - 计算期初和期末余额

如何根据 groupby 计算 12 个月的滚动总和?

从当前行和上一行计算 2 列

Power BI:需要计算最近 3 个月的 MTD 销售额

在 bigquery 中的最后提取日期计算未来 18 个月的预测