[英]Teradata SQL Cumulative Sum over date from another table
The case In brief;案例简介; the customer subscribes to a monthly service, the renewal date would be any day within the month, not just the month beginning, and he could also renew early or late (before/after the due date) then he consumes a from his quota bundle on daily basis and perhaps he could consume all his bundle quota befroe renewal date.
客户订阅了每月服务,续订日期可以是当月的任何一天,而不仅仅是月初,他也可以提前或延迟(到期日之前/之后)续订,然后他从配额包中消费每天,也许他可以在续订日期之前用完他的所有捆绑配额。 So I need to calculate the
Running Total
for Consumption_Value
based on the service renewal date, not the calendar date I have two tables:所以我需要根据服务续订日期而不是日历日期来计算
Consumption_Value
的Running Total
,我有两个表:
Consumption_T
that holds the info for a subscriber daily consumption on specific offer IDs, like the below sampleConsumption_T
,保存订阅者在特定优惠 ID 上的每日消费信息,如以下示例|Subs_ID|Consumption_Date|Offer_ID|Quota|Consumption_Value|
|-------+----------------+--------+-----+-----------------|
|12345 |22-01-2021 |123 |140 |1 |
|12345 |23-01-2022 |123 |140 |3 |
|12345 |24-01-2023 |123 |140 |5 |
|12345 |25-01-2022 |123 |140 |3 |
|12345 |26-01-2023 |123 |140 |6 |
|12345 |27-01-2024 |123 |140 |4 |
|… |… |… |… |… |
|12345 |15-02-2021 |123 |140 |10 |
|12345 |22-02-2021 |123 |140 |4 |
Renewal_T
is for the renewal dates like the one below:Renewal_T
用于续订日期,如下所示:|Subs_ID|Renewal_Date|Offer_ID|
|-------+------------+--------|
|12345 |22-01-2021 |123 |
|12345 |22-02-2021 |123 |
|12345 |18-03-2022 |123 |
|12345 |19-04-2022 |123 |
|12345 |15-05-2023 |123 |
|12345 |16-06-2023 |123 |
The desired output will be like the below所需的 output 将如下所示
|Subs_ID|Consumption_Date|Offer_ID|Quota|Consumption_Value|Running Total|
|-------+----------------+--------+-----+-----------------+-------------|
|12345 |22-01-2021 |123 |140 |1 |1 |
|12345 |23-01-2022 |123 |140 |3 |4 |
|12345 |24-01-2023 |123 |140 |5 |9 |
|12345 |25-01-2022 |123 |140 |3 |12 |
|12345 |26-01-2023 |123 |140 |6 |18 |
|12345 |27-01-2024 |123 |140 |4 |22 |
|… |… |… |… |… |… |
|12345 |15-02-2021 |123 |140 |10 |140 |
|12345 |22-02-2021 |123 |140 |4 |4 |
The data model is not very clear; model的数据不是很清楚; does the renewal period apply to a Subs_Id , or to Subs_Id + Offer_Id combination?
续订期是否适用于Subs_Id或Subs_Id + Offer_Id组合? Are you sure the years of Consumption_date and Renewal_Date examples are correct (2022, 2023, etc);
您确定 Consumption_date 和 Renewal_Date 示例的年份是否正确(2022、2023 等); these are not coherent.
这些不连贯。 If I assume that the primary key to renewals is Subs_Id+Offer_Id+Renewal_Date, then this would produce what you want:
如果我假设续订的主键是 Subs_Id+Offer_Id+Renewal_Date,那么这将产生您想要的:
select T1.*,
sum(Consumption_Value)
over (partition by T1.Subs_Id, T1.Offer_ID, SubsPeriod.RenewalDate_From
order by Consumption_Date
rows between unbounded preceding and current row) as "Running Total"
from Consumption_T as T1
inner join
(select
Subs_Id
, Offer_Id
, Renewal_Date as RenewalDate_From
, LEAD(Renewal_Date-1,1,date '3000-01-01')
over (partition by Subs_ID, Offer_Id order by Renewal_Date) as RenewalDate_To
from Renewal_T
) SubsPeriod
on SubsPeriod.Subs_Id=T1.Subs_Id
and SubsPeriod.Offer_Id=T1.Offer_Id
and T1.Consumption_Date
between SubsPeriod.RenewalDate_From and SubsPeriod.RenewalDate_To
order by T1.Subs_Id, T1.OffeR_Id, Consumption_date
Provided that the example dates are all 2021.假设示例日期均为 2021 年。
The sub query SubsPeriod derives the renewal periods (with from & to dates, inclusive), and we then join the consumption data to these periods, expecting that the consumption date is within the renewal period.子查询 SubsPeriod 推导出续费周期(包括 from & to 日期),然后我们将消费数据加入这些周期,期望消费日期在续费周期内。 We then calculate a running total for each subs_id, offer_id, renewal period.
然后,我们计算每个 subs_id、offer_id、续订期的运行总计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.