Ad
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
I have a file that have this structure
Account_ID first_payment Last_payment Status
----------------------------------------------------
1 10/15/2021 NA Active
2 09/22/2021 11/22/2021 Canceled
. . . .
I am trying to create a payment record file based on this information, so if a customer is active it will create 1 payment monthly till the next valid current date, and if he is canceled then it will create 1 monthly payment until the cancel date.
So the final result for this file will be
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
Thanks for the help
Ad
Answer
Consider below approach
select Account_ID, payment
from your_table,
unnest(generate_date_array(first_payment, ifnull(last_payment, current_date), interval 1 month)) payment
if applied to sample data in your question
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'
)
output is
Ad
source: stackoverflow.com
Related Questions
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key
Ad