之前的累计金额,之后的累计金额

时间:2021-06-06 14:31:41

标签: sql postgresql

table_1

customer_id         month          subscription_price
1               April    2020         49.0
1               May      2020         49.0
1               June     2020         49.0
1               July     2020         49.0
1               February 2021         29.0
1               March    2021         29.0
1               April    2021         29.0
2               January  2020         15.0
2               February 2020         15.0
2               February 2021         30.0

基于上面的table_1,我想把table转换成table_2:

     c_id     month          before  current after cum_sum_before     cum_sum_after
0       1   April    2020   NaN     49.0    49.0        NaN         49+49+49+NaN+29+29+NaN
1       1   May      2020   49.0    49.0    49.0         49            49+49+NaN+29+29+NaN 
2       1   June     2020   49.0    49.0    49.0       49+49              49+NaN+29+29+NaN     
3       1   July     2020   49.0    49.0     NaN       49+49+49              NaN+29+29+NaN
4       1   February 2021   NaN     29.0    29.0       49+49+49                  29+29+NaN
5       1   March    2021   29.0    29.0    29.0     49+49+49+29                    29+NaN
6       1   April    2021   29.0    29.0     NaN    49+49+49+29+29                     NaN

(添加只是为了说明目的,我希望 cum_sum 是整数,而不是文本)。

下面的查询

SELECT customer_id as c_id,month
     ,lag(subscription_price) over (partition by customer_id order by month asc) as before
     ,subscription_price as current
     ,lead(subscription_price) over (partition by customer_id order by month asc) as after
FROM schema.table st

使用 before, current, after 将 table_1 转换为 table_2。 如何创建列累计总和之前和之后的累计总和?

2 个答案:

答案 0 :(得分:0)

尝试使用以下:(之前)

选择 Customer_id, as c_id, 月份 , sum(Convert(int, Subscription_price)) over (partition by customer_id order by month) as Before 从 schema.table st

--升序不需要加ASC,它会自动假设这个顺序,但是,如果你想降序,你需要加DESC。

让我知道这是否有效。

答案 1 :(得分:0)

您似乎想要累积总和,如下所示:

select t.*,
       sum(subscription_price) over (partition by customer_id
                                     order by month
                                     rows between unbounded preceding and 1 preceding
                                    ) as sum_before,
       sum(subscription_price) over (partition by customer_id
                                     order by month
                                     rows between 1 following and unbounded following
                                    ) as sum_before
from t;

注意:这会为第一行和最后一行返回 NULL。为了返回 0,我经常使用算术并省略窗口框架子句:

select t.*,
       (sum(subscription_price) over (partition by customer_id
                                      order by month
                                     ) -
        subscription_price
       ) as sum_before,
       (sum(subscription_price) over (partition by customer_id
                                      order by month desc
                                     ) -
        subscription_price
       ) as sum_before
from t;