
DROP TABLE raena_analytics.campaign_inappEvent_base_data_base;


CREATE TABLE raena_analytics.campaign_inappEvent_base_data_base 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,
       original_url,
       --coalesce(reseller_mobile,profile_phone) AS reseller_mobile,
       advertising_id
FROM raena_appsflyer.dw_marketing_inappevent_stats A
GROUP BY 1,
         2,
         3,
         4,
         5,
         6,
         7,
         8,
         9,
         10,
         11,
         13,
         14,
         15,
         16,17;


DROP TABLE raena_analytics.campaign_inappEvent_base_data;


CREATE TABLE raena_analytics.campaign_inappEvent_base_data AS
SELECT A.* ,
       coalesce(reseller_mobile,profile_phone) AS reseller_mobile
FROM raena_analytics.campaign_inappEvent_base_data_base A
LEFT JOIN
  (SELECT B.id ,
          replace(B.mobile,'+','') AS reseller_mobile
   FROM raena_user_management.user B) B ON customer_user_id = cast(B.id AS varchar)
LEFT JOIN
  (SELECT DISTINCT profile_objectid,
                   profile_phone
   FROM clevertap.app_installed) C ON A.customer_user_id = C.profile_objectid;


  DROP TABLE IF EXISTS raena_analytics.gm_dashboard_appflyer_base_v1;


CREATE TABLE raena_analytics.gm_dashboard_appflyer_base_v1 AS
SELECT count(DISTINCT external_id) ttl_orders,
       sum(discounted_price*quantity) ttl_amount,
       tier_name,
       cast(transaction_date AS date)transaction_date,
       reseller_id
FROM raena_analytics.gm_dashboard
WHERE is_campaign= 'false'
GROUP BY 3,
         4,
         5;

DROP TABLE IF EXISTS raena_analytics.user_base_appflyer_v1;


CREATE TABLE raena_analytics.user_base_appflyer_v1 AS
SELECT id,
       city,
       province,
       created_at,
       email,
       mobile
FROM raena_user_management.user;


DROP TABLE IF EXISTS raena_analytics.campaign_inappEvent_base_data_v1;

CREATE TABLE raena_analytics.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 raena_analytics.campaign_inappEvent_base_data A
LEFT JOIN
  (SELECT A.id reseller_id ,
          sum(ttl_orders) ttl_order,
          sum(ttl_amount) ttl_amount,
          A.city,
          A.province,
          A.created_at AS reg_date,
          sum(CASE WHEN B.tier_name='GOLD' THEN 1 END) reseller_tier_name_gold,
          sum(CASE WHEN B.tier_name='SILVER' THEN 1 END) reseller_tier_name_silver,
          sum(CASE WHEN B.tier_name='BRONZE' THEN 1 END) reseller_tier_name_bronze,
          min(B.transaction_date) conversion_date,
          A.email reseller_email,
          replace(mobile,'+','') AS reseller_mobile
   FROM raena_analytics.user_base_appflyer_v1  A
   LEFT JOIN raena_analytics.gm_dashboard_appflyer_base_v1 B ON cast(A.id AS varchar) = B.reseller_id
   GROUP BY 1,
            4,
            5,
            6,
            11,
            12) B ON A.reseller_mobile = B.reseller_mobile;

DROP TABLE IF EXISTS raena_analytics.campaign_inappEvent_base_data_v2;

CREATE TABLE raena_analytics.campaign_inappEvent_base_data_v2 AS
SELECT reseller_mobile,
       sum(After_discount_GM) AS Post_Disc_GM,
       sum(a.Total_Payment_Price) AS Post_Disc_Revenue
FROM
  (SELECT reseller_mobile,
          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,
          sum(quantity*cast(discounted_price AS int)) AS Total_Payment_Price
   FROM raena_analytics.OM_GM_DB_Product_category
   GROUP BY reseller_mobile) a
INNER JOIN
  (SELECT DISTINCT reseller_mobile phone
     FROM raena_analytics.campaign_inappEvent_base_data
     WHERE reseller_mobile <> '') B ON replace(reseller_mobile,'+','')=phone
GROUP BY 1;

DROP TABLE IF EXISTS raena_analytics.campaign_inappEvent_base_data_v3;


CREATE TABLE raena_analytics.campaign_inappEvent_base_data_v3 AS
SELECT DISTINCT replace(reseller_mobile,'+','') reseller_mobile ,
                brand_name ,
                sku_name ,
                sku
FROM
  (SELECT *
   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 raena_analytics.OM_GM_DB_Product_category
      GROUP BY sku_name,
               OM_GM_DB_Product_category.sku,
               brand_name,
               reseller_mobile) AA
   WHERE rnk = 1) B
INNER JOIN
  (SELECT DISTINCT reseller_mobile phone
   FROM raena_analytics.campaign_inappEvent_base_data
   WHERE reseller_mobile <> '') C ON replace(reseller_mobile,'+','') = phone;


DROP TABLE IF EXISTS raena_analytics.launched_total_in_app_campaign;


CREATE TABLE raena_analytics.launched_total_in_app_campaign AS
SELECT replace(profile_phone,'+','') profile_phone ,
       count(1) app_launched
FROM clevertap.app_launched al
INNER JOIN
  (SELECT DISTINCT reseller_mobile phone
   FROM raena_analytics.campaign_inappEvent_base_data
   WHERE reseller_mobile <> '') C ON profile_phone = phone
GROUP BY profile_phone;

DROP TABLE IF EXISTS raena_analytics.campaign_inappEvent_base_data_v4 ;

CREATE TABLE raena_analytics.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 raena_analytics.campaign_inappEvent_base_data_v1 A
LEFT JOIN raena_analytics.campaign_inappEvent_base_data_v2 B ON replace(A.reseller_mobile,'+','') = replace(B.reseller_mobile,'+','')
LEFT JOIN raena_analytics.campaign_inappEvent_base_data_v3 C ON replace(A.reseller_mobile,'+','')= replace(C.reseller_mobile,'+','')
LEFT JOIN raena_analytics.launched_total_in_app_campaign D ON replace(A.reseller_mobile,'+','')= replace(D.profile_phone,'+','');

DROP TABLE IF EXISTS raena_analytics.campaign_inappEvent_final;

CREATE TABLE raena_analytics.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,
       original_url
FROM raena_analytics.campaign_inappEvent_base_data_v4 A
LEFT JOIN
  (SELECT reseller_mobile mobile ,
          tier_name Current_tier ,
          transaction_date created_at
   FROM
     (SELECT transaction_date,
             A.reseller_mobile,
             tier_name,
             rank() over(partition BY reseller_mobile
                         ORDER BY transaction_date DESC) rnk
      FROM raena_analytics.gm_dashboard A) D
   WHERE rnk = 1) B ON A.reseller_mobile= B.mobile;

 
