raena_analytics_scripts/appsflyer/etl_inappevent_appsflyer.sql

235 lines
8.3 KiB
MySQL
Raw Normal View History

2024-06-24 12:26:08 +00:00
DROP TABLE public.campaign_inappEvent_base_data;
CREATE TABLE public.campaign_inappEvent_base_data AS
SELECT channel,
media_source ,
campaign campaign_name,
adset fb_adset_name,
site_id af_siteid,
ad,
customer_user_id ,
split_part(device_model,'::',1) device_brand,
split_part(device_model,'::',2) device_model,
platform,
install_time::date install_date,
count(CASE WHEN lower(attributed_touch_type)='click' THEN 1 END) total_clicks,
os_version ,
app_version,
A.City,
coalesce(reseller_mobile,profile_phone) AS reseller_mobile,
advertising_id
FROM raena_appsflyer.dw_marketing_inappevent_stats A
LEFT JOIN
(SELECT B.id ,
replace(B.mobile,'+','') AS reseller_mobile
FROM raena_user_management.user B) B ON customer_user_id = B.id
LEFT JOIN
(SELECT DISTINCT profile_objectid,
profile_phone
FROM clevertap.app_installed) C ON A.customer_user_id = C.profile_objectid
--WHERE media_source<>'nan'
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
13,
14,
15,
16,17;
DROP TABLE IF EXISTS public.campaign_inappEvent_base_data_v1;
CREATE TABLE public.campaign_inappEvent_base_data_v1 AS
SELECT DISTINCT A.*,
B.province,
ttl_order,
ttl_amount ,
CASE
WHEN reseller_tier_name_gold>0 THEN 'GOLD'
WHEN reseller_tier_name_silver>0 THEN 'SILVER'
WHEN reseller_tier_name_bronze>0 THEN 'BRONZE'
END highest_Tier ,
reg_date,
reseller_email,
conversion_date
FROM public.campaign_inappEvent_base_data A
LEFT JOIN
(SELECT A.id reseller_id ,
count(DISTINCT B.id) ttl_order,
sum(B.total_amount) AS ttl_amount ,
A.city,
A.province,
A.created_at AS reg_date,
sum(CASE WHEN json_extract_path_text(B.reseller_info,'tierName',TRUE) ='GOLD' THEN 1 END) reseller_tier_name_gold,
sum(CASE WHEN json_extract_path_text(B.reseller_info,'tierName',TRUE) ='SILVER' THEN 1 END) reseller_tier_name_silver,
sum(CASE WHEN json_extract_path_text(B.reseller_info,'tierName',TRUE) ='BRONZE' THEN 1 END) reseller_tier_name_bronze,
min(B.created_at) conversion_date,
A.email reseller_email,
replace(mobile,'+','') AS reseller_mobile
FROM raena_user_management.user A
LEFT JOIN raena_order_management.order B ON A.id = B.reseller_id
AND B.payment_status = 'Paid'
AND is_campaign= 'false'
GROUP BY 1,
4,
5,
6,
11,
12) B ON A.reseller_mobile = B.reseller_mobile;
DROP TABLE IF EXISTS public.campaign_inappEvent_base_data_v2;
CREATE TABLE public.campaign_inappEvent_base_data_v2 AS
SELECT reseller_mobile,
sum(Before_Discount_GM) AS Pre_Disc_GM,
sum(After_discount_GM) AS Post_Disc_GM,
sum(GM_GAP) AS GM_GAP,
sum(Total_Wholesale_price) AS Pre_Disc_Revenue,
sum(a.Total_Payment_Price) AS Post_Disc_Revenue,
sum(Blended_gm_target)/count(1) AS Blended_gm_target,
cast(sum(cast((a.Total_Payment_Price) AS float))/sum(cast((b.Total_Payment_Price)AS float)) AS decimal(10,4)) AS Revenue,
sum(Number_of_resellers) AS No_of_resellers,
sum(Number_of_orders) AS No_of_orders
FROM
(SELECT replace(reseller_mobile,'+','') reseller_mobile,
1 AS id,
cast((sum(((retail_price*quantity)-(seller_margin*quantity))-(cogs*quantity))
/sum(case when retail_price<>0 then (retail_price*quantity)-(seller_margin*quantity) end)) as decimal(10,4)) as Before_Discount_GM,
cast((sum((quantity*discounted_price)-(cogs*quantity))
/sum(case when discounted_price<>0 then quantity*discounted_price end)) as decimal(10,4)) as After_discount_GM,
cast((sum(((retail_price*quantity)-(seller_margin*quantity))-(cogs*quantity))
/sum(case when retail_price<>0 then (retail_price*quantity)-(seller_margin*quantity) end)) as decimal(10,4))-
cast((sum((quantity*discounted_price)-(cogs*quantity))
/sum(case when discounted_price<>0 then quantity*discounted_price end)) as decimal(10,4))
as GM_GAP,
Sum(gm_target)/count(OM_GM_DB_Product_category.sku) Blended_gm_target,
sum((cast(retail_price AS int)*quantity)-(cast(seller_margin AS int)*quantity)) AS Total_Wholesale_price,
sum(quantity*cast(discounted_price AS int)) AS Total_Payment_Price,
count(DISTINCT reseller_id) AS Number_of_resellers,
count(DISTINCT external_id) AS Number_of_orders
FROM OM_GM_DB_Product_category
GROUP BY reseller_mobile) a
LEFT JOIN
(SELECT 1 AS id,
sum(quantity*discounted_price) AS Total_Payment_Price
FROM OM_GM_DB_Product_category) b ON a.id=b.id
WHERE replace(reseller_mobile,'+','') IN
(SELECT DISTINCT reseller_mobile
FROM public.campaign_inappEvent_base_data
WHERE reseller_mobile <> '')
GROUP BY 1
ORDER BY 10 DESC, 1 ASC;
DROP TABLE IF EXISTS public.campaign_inappEvent_base_data_v3;
CREATE TABLE public.campaign_inappEvent_base_data_v3 AS
SELECT DISTINCT replace(reseller_mobile,'+','') reseller_mobile ,
brand_name ,
sku_name ,
sku
FROM
(SELECT reseller_mobile ,
sku_name,
brand_name,
OM_GM_DB_Product_category.sku,
sum(quantity*cast(discounted_price AS int)) AS Total_Payment_Price ,
row_number() over(partition BY reseller_mobile
ORDER BY sum(quantity*cast(discounted_price AS int)) DESC) rnk
FROM OM_GM_DB_Product_category
GROUP BY sku_name,
OM_GM_DB_Product_category.sku,
brand_name,
reseller_mobile) AA
WHERE rnk = 1
AND replace(reseller_mobile,'+','') IN
(SELECT DISTINCT reseller_mobile
FROM public.campaign_inappEvent_base_data
WHERE reseller_mobile <> '');
DROP TABLE IF EXISTS public.launched_total_in_app_campaign;
CREATE TABLE public.launched_total_in_app_campaign AS
SELECT replace(profile_phone,'+','') profile_phone ,
count(1) app_launched
FROM clevertap.app_launched al
WHERE profile_phone IN
(SELECT DISTINCT reseller_mobile
FROM public.campaign_inappEvent_base_data
WHERE reseller_mobile <> '')
GROUP BY profile_phone;
DROP TABLE IF EXISTS public.campaign_inappEvent_base_data_v4 ;
CREATE TABLE public.campaign_inappEvent_base_data_v4 AS
SELECT A.*,
B.Post_Disc_GM*100 Post_discount_gm,
B.Post_Disc_Revenue,
C.brand_name,
C.sku_name,
C.sku,
D.app_launched
FROM public.campaign_inappEvent_base_data_v1 A
LEFT JOIN public.campaign_inappEvent_base_data_v2 B ON replace(A.reseller_mobile,'+','') = replace(B.reseller_mobile,'+','')
LEFT JOIN public.campaign_inappEvent_base_data_v3 C ON replace(A.reseller_mobile,'+','')= replace(C.reseller_mobile,'+','')
LEFT JOIN public.launched_total_in_app_campaign D ON replace(A.reseller_mobile,'+','')= replace(D.profile_phone,'+','');
DROP TABLE IF EXISTS public.campaign_inappEvent_final;
CREATE TABLE public.campaign_inappEvent_final AS
SELECT channel,
media_source,
campaign_name,
fb_adset_name,
af_siteid,
ad,
advertising_id,
customer_user_id ,
device_brand,
device_model,
platform ,
install_date,
total_clicks,
os_version ,
app_version ,
city,
reseller_mobile ,
province ,
ttl_order ,
ttl_amount total_amount,
highest_tier,
reg_date,
reseller_email,
conversion_date,
Post_discount_gm,
post_disc_revenue,
BRAND_NAME ,
SKU_NAME ,
SKU ,
app_launched TOTAL_app_launched,
B.Current_tier
FROM public.campaign_inappEvent_base_data_v4 A
LEFT JOIN
(SELECT mobile ,
reseller_tier_name Current_tier ,
created_at
FROM
(SELECT created_at,
json_extract_path_text(A.reseller_info,'mobile',TRUE) mobile,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
rank() over(partition BY json_extract_path_text(A.reseller_info,'mobile',TRUE)
ORDER BY created_at DESC) rnk
FROM raena_order_management.
ORDER A
ORDER BY created_at DESC) D
WHERE rnk = 1) B ON A.reseller_mobile= B.mobile;