Created
March 1, 2024 10:25
-
-
Save FurcyPin/257901aa2772521300a71b41021449dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH price_history_per_shop_per_article AS ( | |
SELECT | |
shop_id, | |
article_id, | |
ARRAY_AGG((SELECT AS STRUCT T.* EXCEPT(shop_id, article_id)) ORDER BY start_datetime) as price_history | |
FROM `supermarket.sell_price_history` T | |
GROUP BY shop_id, article_id | |
) | |
, promotion_history_per_shop_per_article AS ( | |
SELECT | |
shop_id, | |
article_id, | |
ARRAY_AGG((SELECT AS STRUCT T.* EXCEPT(shop_id, article_id)) ORDER BY start_datetime) as promotion_history | |
FROM `supermarket.promotion_history` T | |
GROUP BY shop_id, article_id | |
) | |
, price_and_promotion_history AS ( | |
SELECT | |
shop_id, | |
article_id, | |
price_history, | |
promotion_history, | |
FROM price_history_per_shop_per_article AS price | |
LEFT JOIN promotion_history_per_shop_per_article AS promotion | |
USING (shop_id, article_id) | |
) | |
, price_and_promotion_history_with_next_event_datetime AS ( | |
SELECT | |
shop_id, | |
article_id, | |
ARRAY( | |
SELECT AS STRUCT | |
*, | |
LAG(start_datetime) OVER (ORDER BY start_datetime DESC) as next_event_datetime | |
FROM UNNEST(price_history) | |
ORDER BY start_datetime | |
) as price_history, | |
ARRAY( | |
SELECT AS STRUCT | |
*, | |
LAG(start_datetime) OVER (ORDER BY start_datetime DESC) as next_event_datetime | |
FROM UNNEST(promotion_history) | |
ORDER BY start_datetime | |
) as promotion_history, | |
FROM price_and_promotion_history | |
) | |
, price_and_promotion_events AS ( | |
SELECT | |
* EXCEPT(price_history, promotion_history), | |
ARRAY ( | |
SELECT AS STRUCT | |
sell_price, | |
CAST(NULL as STRING) as promotion_id, | |
NULL as promotion_value, | |
start_datetime, | |
FROM UNNEST(price_history) | |
UNION ALL | |
SELECT AS STRUCT | |
-1 as sell_price, | |
CAST(NULL as STRING) as promotion_id, | |
NULL as promotion_value, | |
end_datetime, | |
FROM UNNEST(price_history) | |
WHERE end_datetime < next_event_datetime OR next_event_datetime IS NULL | |
UNION ALL | |
SELECT AS STRUCT | |
NULL as sell_price, | |
promotion_id, | |
promotion_value, | |
start_datetime, | |
FROM UNNEST(promotion_history) | |
UNION ALL | |
SELECT AS STRUCT | |
NULL as sell_price, | |
"" as promotion_id, | |
-1 as promotion_value, | |
end_datetime, | |
FROM UNNEST(promotion_history) | |
WHERE end_datetime < next_event_datetime OR next_event_datetime IS NULL | |
ORDER BY start_datetime | |
) as events, | |
FROM price_and_promotion_history_with_next_event_datetime | |
) | |
, price_with_promotion_history_before_cleaning AS ( | |
SELECT | |
* EXCEPT(events), | |
ARRAY( | |
SELECT AS STRUCT | |
LAST_VALUE(sell_price IGNORE NULLS) OVER (ORDER BY start_datetime) as sell_price, | |
LAST_VALUE(promotion_id IGNORE NULLS) OVER (ORDER BY start_datetime) as promotion_id, | |
LAST_VALUE(promotion_value IGNORE NULLS) OVER (ORDER BY start_datetime) as promotion_value, | |
start_datetime, | |
LEAD(start_datetime) OVER (ORDER BY start_datetime) as end_datetime | |
FROM UNNEST(events) | |
ORDER BY start_datetime | |
) as price_and_promotion | |
FROM price_and_promotion_events | |
) | |
, price_with_promotion_history AS ( | |
SELECT | |
* EXCEPT(price_and_promotion), | |
ARRAY( | |
SELECT AS STRUCT | |
NULLIF(sell_price, -1) as sell_price, | |
NULLIF(promotion_id, "") as promotion_id, | |
NULLIF(promotion_value, -1) as promotion_value, | |
start_datetime, | |
end_datetime | |
FROM UNNEST(price_and_promotion) | |
WHERE start_datetime < end_datetime | |
) as price_and_promotion | |
FROM price_with_promotion_history_before_cleaning | |
) | |
SELECT | |
shop_id, | |
article_id, | |
sell_price, | |
promotion_id, | |
promotion_value, | |
COALESCE(promotion_value, sell_price) as price_with_promotion_included, | |
start_datetime, | |
end_datetime | |
FROM price_with_promotion_history | |
LEFT JOIN UNNEST(price_and_promotion) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shop_id | article_id | promotion_id | promotion_value | start_datetime | end_datetime | |
---|---|---|---|---|---|---|
dlr29L9xIF | yVxrDmtw | 2GLCMltLyw | 1.99 | 2023-06-22T13:31:46 | 2023-06-27T23:59:59.999000 | |
dlr29L9xIF | yVxrDmtw | 2GLCMltLyw | 2.99 | 2023-06-19T08:58:40 | 2023-06-22T13:31:46 | |
dlr29L9xIF | yVxrDmtw | 2GLCMltLyw | 2.99 | 2023-06-16T11:29:38 | 2023-06-19T08:58:40 | |
dlr29L9xIF | yVxrDmtw | 2GLCMltLyw | 2.99 | 2023-06-14T15:05:31 | 2023-06-16T11:29:38 | |
dlr29L9xIF | yVxrDmtw | -38_P0evh | 2.49 | 2022-05-11T05:03:04 | 2022-05-18T00:00:00 | |
dlr29L9xIF | yVxrDmtw | -38_P0evh | 2.99 | 2022-05-11T00:00:00 | 2022-05-11T05:03:04 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
article_id | shop_id | sell_price | start_datetime | end_datetime | |
---|---|---|---|---|---|
yVxrDmtw | dlr29L9xIF | 3.99 | 2023-06-14T10:46:07 | 2024-02-14T00:59:59 | |
yVxrDmtw | dlr29L9xIF | 3.99 | 2022-05-11T04:20:13 | 2023-06-14T10:46:07 | |
yVxrDmtw | dlr29L9xIF | 3.99 | 2022-05-10T13:23:18 | 2022-05-11T04:20:13 | |
yVxrDmtw | dlr29L9xIF | 3.99 | 2022-05-10T13:08:45 | 2022-05-10T13:23:18 | |
yVxrDmtw | dlr29L9xIF | 2022-05-10T13:08:42 | 2022-05-10T13:08:45 | ||
yVxrDmtw | dlr29L9xIF | 2022-05-10T13:08:40 | 2022-05-10T13:08:42 | ||
yVxrDmtw | dlr29L9xIF | 2022-05-10T13:08:38 | 2022-05-10T13:08:40 | ||
yVxrDmtw | dlr29L9xIF | 2022-05-10T11:53:49 | 2022-05-10T13:08:38 | ||
yVxrDmtw | dlr29L9xIF | 2022-05-10T11:45:19 | 2022-05-10T11:53:49 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment