Created
February 6, 2025 20:13
-
-
Save meetchandan/a982f8fb3652b478b960f7ecf02e2955 to your computer and use it in GitHub Desktop.
This file contains 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 vsku_category AS ( | |
SELECT DISTINCT vsku, category, 'AE' as country | |
FROM noondwh.instant_instant_order.virtual_sku_item vsi | |
JOIN noonbinimuae.ref.category_AE cae USING(sku) | |
UNION ALL | |
SELECT DISTINCT vsku, category as category_sa, 'SA' as country | |
FROM noondwh.instant_instant_order.virtual_sku_item vsi | |
JOIN noonbinimuae.ref.category_SA cae USING(sku) | |
), | |
vsku_conv AS( | |
SELECT vsku, dt, sum(case when is_added=1 THEN 1 ELSE 0 END) as opt_in, count(1) as total_per_day | |
FROM | |
( | |
SELECT distinct DATE(created_at) as dt, order_nr, vsku, 0 as is_added, JSON_EXTRACT_SCALAR(misc, "$.build") as build | |
FROM noondwh.instant_instant_order.sales_order | |
JOIN UNNEST (JSON_EXTRACT_STRING_ARRAY(misc, '$.analysis.live_related_combos_not_added')) vsku | |
WHERE DATE(created_at) >= PARSE_DATE('%Y%m%d', @DS_START_DATE) | |
AND DATE(created_at) <= PARSE_DATE('%Y%m%d', @DS_END_DATE) | |
AND status_code_order = 'delivered' | |
UNION ALL | |
SELECT distinct DATE(so.created_at) as dt, order_nr, JSON_EXTRACT_SCALAR(soi.misc, '$.parent_vsku') as vsku, 1 as is_added, JSON_EXTRACT_SCALAR(so.misc, "$.build") as build | |
FROM noondwh.instant_instant_order.sales_order_item soi | |
JOIN noondwh.instant_instant_order.sales_order so using (id_sales_order) | |
WHERE JSON_EXTRACT(soi.misc, '$.parent_vsku') IS NOT NULL | |
AND DATE(so.created_at) >= PARSE_DATE('%Y%m%d', @DS_START_DATE) | |
AND DATE(so.created_at) <= PARSE_DATE('%Y%m%d', @DS_END_DATE) | |
AND DATE(soi.created_at) >= PARSE_DATE('%Y%m%d', @DS_START_DATE) | |
AND DATE(soi.created_at) <= PARSE_DATE('%Y%m%d', @DS_END_DATE) | |
AND status_code_order = 'delivered' | |
) | |
GROUP BY 1, 2 | |
), | |
combo_items AS ( | |
SELECT vsku, SUM(qty) items_cnt | |
FROM noondwh.instant_instant_order.virtual_sku_item | |
GROUP BY 1 | |
), | |
order_combo_item_cnt AS ( | |
SELECT DATE(created_at) dt, JSON_EXTRACT_SCALAR(misc, '$.parent_vsku') vsku, count(*) total_items_cnt | |
FROM noondwh.instant_instant_order.sales_order_item | |
WHERE DATE(created_at) >= PARSE_DATE('%Y%m%d', @DS_START_DATE) | |
AND DATE(created_at) <= PARSE_DATE('%Y%m%d', @DS_END_DATE) | |
AND JSON_EXTRACT_SCALAR(misc, '$.parent_vsku') IS NOT NULL | |
GROUP BY 1, 2 | |
), | |
order_combo_cnt AS ( | |
SELECT vsku, dt, (total_items_cnt / items_cnt) combo_sales, | |
FROM order_combo_item_cnt | |
JOIN combo_items USING(vsku) | |
), | |
vsku_details AS ( | |
SELECT vsku, title_en, vsku_ref | |
FROM noondwh.instant_instant_order.virtual_sku | |
) | |
SELECT dt, vsku, title_en as title, vsku_ref, category, country, COALESCE(opt_in, 0) opt_in, COALESCE(total_per_day, 0) total_per_day, COALESCE(ROUND(opt_in * 100 / total_per_day, 2), 0) conv_rate, build | |
FROM vsku_category | |
JOIN vsku_details USING(vsku) | |
LEFT JOIN vsku_conv USING(vsku) | |
WHERE country IS NOT NULL | |
AND COALESCE(total_per_day, 0) > 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment