I had written a code to create more records from 1 record based on some conditions. For example: If the user purchased a monthly package, the table will create records for each week until the Date Ended.
From this:
Order ID|Date Purchased| Date Ended| Package|
001 | 2019-1-1 | 2019-2-1 | 1Month |
To this:
Order ID|Date Purchased| Date Ended| Package| Date Weekly
001 |2019-1-1 | 2019-2-1 | 1Month | 2019-1-1
001 |2019-1-1 | 2019-2-1 | 1Month | 2019-1-8
001 |2019-1-1 | 2019-2-1 | 1Month | 2019-1-15
001 |2019-1-1 | 2019-2-1 | 1Month | 2019-1-23
The question is how do I perform a Left Join of the 2nd table with the 1st table by Order ID? If there are additional records I wanted to join? For example, Order ID 002, 003 etc. Thanks in advance :)
Here's my code
SELECT
*
FROM
(
SELECT
*,
DATE(PARSE_DATETIME('%Y-%m-%d %H:%M:%S', date_purchased)) AS date_purchased, --Transform datetime to time
DATE(PARSE_DATETIME('%Y-%m-%d %H:%M:%S', date_ended)) AS date_ended
FROM
`database_123`
),UNNEST(GENERATE_DATE_ARRAY(date_purchased, date_ended, INTERVAL 1 WEEK)) AS date_weekly