在 SQL 中创建一个存储过程或视图,它将 运行 在 Big Query 中根据首次付款和最后一次付款填充付款历史记录
Creating a stored procedure or view in SQL that will run in Big Query that fills payment history based on first payment and last payment
我有一个具有这种结构的文件
Account_ID first_payment Last_payment Status
----------------------------------------------------
1 10/15/2021 NA Active
2 09/22/2021 11/22/2021 Canceled
. . . .
我正在尝试根据此信息创建付款记录文件,因此如果客户处于活跃状态,它将每月创建 1 笔付款,直到下一个有效的当前日期,如果他被取消,则它将创建 1 个月的付款直到取消日期。
所以这个文件的最终结果将是
Account_id payment
----------------------
1 10/15/2021
1 11/15/2021
1 12/15/2021
1 01/15/2022
2 09/15/2021
2 10/15/2021
2 11/15/2021
感谢您的帮助
考虑以下方法
select Account_ID, payment
from your_table,
unnest(generate_date_array(first_payment, ifnull(last_payment, current_date), interval 1 month)) payment
如果应用于您问题中的示例数据
with your_table as (
select 1 Account_ID, date '2021-10-15' first_payment, cast(null as date) last_payment, 'Active' Status union all
select 2, '2021-09-22', '2021-11-22', 'Canceled'
)
输出是
我有一个具有这种结构的文件
Account_ID first_payment Last_payment Status
----------------------------------------------------
1 10/15/2021 NA Active
2 09/22/2021 11/22/2021 Canceled
. . . .
我正在尝试根据此信息创建付款记录文件,因此如果客户处于活跃状态,它将每月创建 1 笔付款,直到下一个有效的当前日期,如果他被取消,则它将创建 1 个月的付款直到取消日期。
所以这个文件的最终结果将是
Account_id payment
----------------------
1 10/15/2021
1 11/15/2021
1 12/15/2021
1 01/15/2022
2 09/15/2021
2 10/15/2021
2 11/15/2021
感谢您的帮助
考虑以下方法
select Account_ID, payment
from your_table,
unnest(generate_date_array(first_payment, ifnull(last_payment, current_date), interval 1 month)) payment
如果应用于您问题中的示例数据
with your_table as (
select 1 Account_ID, date '2021-10-15' first_payment, cast(null as date) last_payment, 'Active' Status union all
select 2, '2021-09-22', '2021-11-22', 'Canceled'
)
输出是