
DROP TABLE raena_analytics.base_analytics_dump;


CREATE TABLE raena_analytics.base_analytics_dump AS
SELECT ts,
       events,
       phone,
       event_props::jsonb->>'CT Session Id' sessionid ,
                                            event_props::jsonb->>'page_name' pagename
FROM clevertap.clevertap_master_data cmd
WHERE ts::date >='2023-01-01'
  AND events IN ('load_home_page',
                 'page_load',
                 'add_to_cart',
                 'begin_checkout',
                 'finish_checkout',
                 'Charged',
                 'page_load_checkout_continue',
                 'page_load_finish_checkout',
                 'confirm_payment',
                 'view_cart',
                 'view_item' )
  AND event_props NOT LIKE '%Focallure%';

DROP TABLE raena_analytics.add_to_cart_base_1;


CREATE TABLE raena_analytics.add_to_cart_base_1 AS
SELECT DISTINCT ts::date,
                phone,
                event_props::jsonb->>'CT Session Id' sessionid ,
                event_props::jsonb->>'screen' pagename,
                event_props::jsonb->>'item_id' sku
FROM clevertap.clevertap_master_data cmd
WHERE ts::date >='2023-01-01'
  AND events ='add_to_cart'
  AND event_props::jsonb->>'item_id' IS NOT NULL ;


DROP TABLE raena_analytics.view_item_base_1;


CREATE TABLE raena_analytics.view_item_base_1 AS
SELECT DISTINCT ts::date,
                phone,
                event_props::jsonb->>'CT Session Id' sessionid ,
                event_props::jsonb->>'screen' pagename,
                event_props::jsonb->>'item_id' sku
FROM clevertap.clevertap_master_data cmd
WHERE ts::date >='2023-01-01'
  AND events ='view_item'
  AND event_props::jsonb->>'item_id' IS NOT NULL ;


DROP TABLE raena_analytics.begin_checkout_base_1;


CREATE TABLE raena_analytics.begin_checkout_base_1 AS
SELECT DISTINCT ts::date,
                phone,
                event_props::jsonb->>'CT Session Id' sessionid ,
                 replace(replace(unnest(regexp_split_to_array(event_props::jsonb ->>'items',',')),']',''),'[','') sku
FROM clevertap.clevertap_master_data cmd
WHERE ts::date >='2023-01-01'
  AND events ='begin_checkout';



drop table raena_analytics.finish_checkout_base_1;

create table raena_analytics.finish_checkout_base_1
as
select A.*,P.sku
from 
(select distinct ts::date, phone,event_props::jsonb->>'CT Session Id' sessionid , 
replace(replace(unnest(regexp_split_to_array(event_props::jsonb ->>'items',',')),']',''),'[','') pagename
from clevertap.clevertap_master_data cmd
where ts::date >='2023-01-01' 
and events ='finish_checkout') A 
left join raena_catalog_management.product P on A.pagename = cast(P.id as varchar) ;

drop table raena_analytics.pagename_homepage_data;

create table raena_analytics.pagename_homepage_data
as
select distinct sessionid ,phone,pagename,ts::date ts
from raena_analytics.base_analytics_dump
where events = 'page_load' and pagename = 'Home';


drop table if exists raena_analytics.pagename_homepage_data_remaining_funnel_stage1;

CREATE TABLE raena_analytics.pagename_homepage_data_remaining_funnel_stage1 AS
SELECT AA.ts,
       AA.pagename ,
       sum(cnt_pageload_home) cnt_pageload_home ,
       count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home,
       sum(cnt_view_item) cnt_view_item ,
       count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item,
       sum(cnt_add_to_cart) cnt_add_to_cart ,
       count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart,
       sum(cnt_view_cart) cnt_view_cart ,
       count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart,
       sum(cnt_begin_checkout) cnt_begin_checkout ,
       count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout,
       sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue ,
       count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue,
       sum(cnt_load_payment_method) cnt_load_payment_method ,
       count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method,
       sum(cnt_confirm_payment) cnt_confirm_payment ,
       count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment
FROM
  (SELECT A.sessionid ,
          A.phone,
          A.pagename,
          A.ts ,
          B.cnt_pageload_home,
          cnt_view_item,
          cnt_add_to_cart,
          cnt_view_cart,
          cnt_begin_checkout,
          cnt_page_load_checkout_continue,
          cnt_load_payment_method,
          cnt_confirm_payment
   FROM raena_analytics.pagename_homepage_data A
   LEFT JOIN
     (SELECT sessionid,
             phone ,
             count(CASE WHEN events = 'page_load'
                   AND pagename= 'Home' THEN sessionid END)cnt_pageload_home,
             count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item ,
             count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart ,
             count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart ,
             count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout ,
             count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue ,
             count(CASE WHEN events = 'page_load'
                   AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method,
             count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment
      FROM raena_analytics.base_analytics_dump
      GROUP BY 1,
               2) B ON A.sessionid = B.sessionid
   AND A.phone = B.phone) AA
GROUP BY 1,
         2;

drop table raena_analytics.offender_funnel_report;

CREATE TABLE raena_analytics.offender_funnel_report AS
SELECT ts,
       pagename,
       C.is_offender ,
       D.name,
       count(phone) count_view_item,
       count(add_to_cart_phone) count_add_to_cart,
       count(begin_checkout_cart) begin_checkout_cart,
       count(finish_checkout_cart) finish_checkout_cart
FROM
  (SELECT DISTINCT A.ts,
                   A.sessionid,
                   A.phone,
                   A.pagename ,
                   A.sku ,
                   B.phone add_to_cart_phone ,
                   C.phone begin_checkout_cart,
                   D.phone finish_checkout_cart
   FROM raena_analytics.view_item_base_1 A
   LEFT JOIN raena_analytics.add_to_cart_base_1 B ON A.sessionid = B.sessionid
   AND A.sku = B.sku
   LEFT JOIN raena_analytics.begin_checkout_base_1 C ON A.sessionid = C.sessionid
   AND A.sku = C.sku
   LEFT JOIN raena_analytics.finish_checkout_base_1 D ON A.sessionid = D.sessionid
   AND A.sku = C.sku) AA
LEFT JOIN raena_catalog_management.product C ON AA.sku = C.sku
LEFT JOIN raena_catalog_management.brand D ON C.brand_id = D.id
GROUP BY 1,
         2,
         3,
         4;

drop table raena_analytics.pagename_Brands_data;

create table raena_analytics.pagename_Brands_data
as
select distinct sessionid ,phone,pagename,ts::date ts
from raena_analytics.base_analytics_dump
where events = 'page_load' and pagename = 'Brands';


drop table if exists raena_analytics.pagename_brand_data_remaining_funnel_stage2;

CREATE TABLE raena_analytics.pagename_brand_data_remaining_funnel_stage2 AS
SELECT AA.ts,
       AA.pagename ,
       sum(cnt_pageload_home) cnt_pageload_home ,
       count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home,
       sum(cnt_view_item) cnt_view_item ,
       count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item,
       sum(cnt_add_to_cart) cnt_add_to_cart ,
       count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart,
       sum(cnt_view_cart) cnt_view_cart ,
       count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart,
       sum(cnt_begin_checkout) cnt_begin_checkout ,
       count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout,
       sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue ,
       count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue,
       sum(cnt_load_payment_method) cnt_load_payment_method ,
       count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method,
       sum(cnt_confirm_payment) cnt_confirm_payment ,
       count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment
FROM
  (SELECT A.sessionid ,
          A.phone,
          A.pagename,
          A.ts ,
          B.cnt_pageload_home,
          cnt_view_item,
          cnt_add_to_cart,
          cnt_view_cart,
          cnt_begin_checkout,
          cnt_page_load_checkout_continue,
          cnt_load_payment_method,
          cnt_confirm_payment
   FROM raena_analytics.pagename_brands_data A
   LEFT JOIN
     (SELECT sessionid,
             phone ,
             count(CASE WHEN events = 'page_load'
                   AND pagename= 'Brands' THEN sessionid END)cnt_pageload_home,
             count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item ,
             count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart ,
             count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart ,
             count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout ,
             count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue ,
             count(CASE WHEN events = 'page_load'
                   AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method,
             count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment
      FROM raena_analytics.base_analytics_dump
      GROUP BY 1,
               2) B ON A.sessionid = B.sessionid
   AND A.phone = B.phone) AA
GROUP BY 1,
         2;



drop table raena_analytics.pagename_search_data;

create table raena_analytics.pagename_search_data
as
select distinct sessionid ,phone,'Search' pagename,ts::date ts
from raena_analytics.base_analytics_dump
where events = 'page_load' and pagename like '%Search%';


drop table if exists raena_analytics.pagename_search_data_remaining_funnel_stage2;

CREATE TABLE raena_analytics.pagename_search_data_remaining_funnel_stage2 AS
SELECT AA.ts,
       AA.pagename ,
       sum(cnt_pageload_home) cnt_pageload_home ,
       count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home,
       sum(cnt_view_item) cnt_view_item ,
       count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item,
       sum(cnt_add_to_cart) cnt_add_to_cart ,
       count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart,
       sum(cnt_view_cart) cnt_view_cart ,
       count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart,
       sum(cnt_begin_checkout) cnt_begin_checkout ,
       count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout,
       sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue ,
       count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue,
       sum(cnt_load_payment_method) cnt_load_payment_method ,
       count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method,
       sum(cnt_confirm_payment) cnt_confirm_payment ,
       count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment
FROM
  (SELECT A.sessionid ,
          A.phone,
          A.pagename,
          A.ts ,
          B.cnt_pageload_home,
          cnt_view_item,
          cnt_add_to_cart,
          cnt_view_cart,
          cnt_begin_checkout,
          cnt_page_load_checkout_continue,
          cnt_load_payment_method,
          cnt_confirm_payment
   FROM raena_analytics.pagename_search_data A
   LEFT JOIN
     (SELECT sessionid,
             phone ,
             count(CASE WHEN events = 'page_load'
                   AND pagename LIKE '%Search%' THEN sessionid END)cnt_pageload_home,
             count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item ,
             count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart ,
             count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart ,
             count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout ,
             count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue ,
             count(CASE WHEN events = 'page_load'
                   AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method,
             count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment
      FROM raena_analytics.base_analytics_dump
      GROUP BY 1,
               2) B ON A.sessionid = B.sessionid
   AND A.phone = B.phone) AA
GROUP BY 1,
         2;


-----CategoryList----



drop table raena_analytics.pagename_CategoryList_data;

create table raena_analytics.pagename_CategoryList_data
as
select distinct sessionid ,phone,pagename,ts::date ts
from raena_analytics.base_analytics_dump
where events = 'page_load' and pagename = 'CategoryList';


drop table if exists raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2;

CREATE TABLE raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2 AS
SELECT AA.ts,
       AA.pagename ,
       sum(cnt_pageload_home) cnt_pageload_home ,
       count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home,
       sum(cnt_view_item) cnt_view_item ,
       count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item,
       sum(cnt_add_to_cart) cnt_add_to_cart ,
       count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart,
       sum(cnt_view_cart) cnt_view_cart ,
       count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart,
       sum(cnt_begin_checkout) cnt_begin_checkout ,
       count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout,
       sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue ,
       count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue,
       sum(cnt_load_payment_method) cnt_load_payment_method ,
       count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method,
       sum(cnt_confirm_payment) cnt_confirm_payment ,
       count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment
FROM
  (SELECT A.sessionid ,
          A.phone,
          A.pagename,
          A.ts ,
          B.cnt_pageload_home,
          cnt_view_item,
          cnt_add_to_cart,
          cnt_view_cart,
          cnt_begin_checkout,
          cnt_page_load_checkout_continue,
          cnt_load_payment_method,
          cnt_confirm_payment
   FROM raena_analytics.pagename_CategoryList_data A
   LEFT JOIN
     (SELECT sessionid,
             phone ,
             count(CASE WHEN events = 'page_load'
                   AND pagename= 'CategoryList' THEN sessionid END)cnt_pageload_home,
             count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item ,
             count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart ,
             count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart ,
             count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout ,
             count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue ,
             count(CASE WHEN events = 'page_load'
                   AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method,
             count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment
      FROM raena_analytics.base_analytics_dump
      GROUP BY 1,
               2) B ON A.sessionid = B.sessionid
   AND A.phone = B.phone) AA
GROUP BY 1,
         2;



-----Cart-----



drop table raena_analytics.pagename_Cart_data;

create table raena_analytics.pagename_Cart_data
as
select distinct sessionid ,phone,pagename,ts::date ts
from raena_analytics.base_analytics_dump
where events = 'page_load' and pagename = 'Cart';


drop table if exists raena_analytics.pagename_Cart_data_remaining_funnel_stage2;

CREATE TABLE raena_analytics.pagename_Cart_data_remaining_funnel_stage2 AS
SELECT AA.ts,
       AA.pagename ,
       sum(cnt_pageload_home) cnt_pageload_home ,
       count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home,
       sum(cnt_view_item) cnt_view_item ,
       count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item,
       sum(cnt_add_to_cart) cnt_add_to_cart ,
       count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart,
       sum(cnt_view_cart) cnt_view_cart ,
       count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart,
       sum(cnt_begin_checkout) cnt_begin_checkout ,
       count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout,
       sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue ,
       count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue,
       sum(cnt_load_payment_method) cnt_load_payment_method ,
       count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method,
       sum(cnt_confirm_payment) cnt_confirm_payment ,
       count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment
FROM
  (SELECT A.sessionid ,
          A.phone,
          A.pagename,
          A.ts ,
          B.cnt_pageload_home,
          cnt_view_item,
          cnt_add_to_cart,
          cnt_view_cart,
          cnt_begin_checkout,
          cnt_page_load_checkout_continue,
          cnt_load_payment_method,
          cnt_confirm_payment
   FROM raena_analytics.pagename_Cart_data A
   INNER JOIN
     (SELECT sessionid,
             phone ,
             count(CASE WHEN events = 'page_load'
                   AND pagename= 'Cart' THEN sessionid END)cnt_pageload_home,
             count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item ,
             count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart ,
             count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart ,
             count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout ,
             count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue ,
             count(CASE WHEN events = 'page_load'
                   AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method,
             count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment
      FROM raena_analytics.base_analytics_dump
      GROUP BY 1,
               2) B ON A.sessionid = B.sessionid
   AND A.phone = B.phone) AA
GROUP BY 1,
         2;

drop table if exists raena_analytics.offender_dashboard_base_table;

CREATE TABLE raena_analytics.offender_dashboard_base_table AS
SELECT gd.transaction_date ,
       category_name ,
       product_type ,
       brand_name ,
       gd.sku ,
       gd.shipping_province ,
       order_recipient ,
       gd.order_placed_by ,
       order_type ,
       tier_name ,
       gd.external_id ,
       sum(quantity) quantity ,
       sum(discounted_price*quantity) payment_price,
       sum(cogs*quantity) total_cogs,
       sum((retail_price*quantity)-(seller_margin*quantity)) total_wholesale_price ,
       C.name channel_name ,
       O.status,
       payment_amount order_level_payment_amount,
       reseller_mobile ,
       reseller_email ,
       CASE
           WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M'
           WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M'
           WHEN max_payment_amount >10000000 THEN '>10M'
       END bucket,
       is_offender,
       sum(CASE WHEN gd.external_id = BB.order_id
           AND gd.sku = BB.sku
           AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount
FROM raena_analytics.gm_dashboard gd
LEFT JOIN raena_order_management.order O ON gd.external_id = O.id
LEFT JOIN raena_order_management.channel C ON O.channel_id = C.id
LEFT JOIN
  (SELECT reseller_id ,
          max(payment_amount) AS max_payment_amount
   FROM
     (SELECT reseller_id,
             date_trunc('month',created_at)::date AS created_date,
             sum(payment_amount) payment_amount
      FROM raena_order_management.order o
      WHERE payment_status = 'Paid'
      GROUP BY 1,
               2) A
   GROUP BY 1) D ON gd.reseller_id = D.reseller_id
LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON gd.external_id = BB.order_id
AND gd.sku = BB.sku
AND (CASE
         WHEN item_type LIKE '%Bundle%' THEN 'Bundle'
         ELSE 'Product'
     END)=BB.product_class
LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON gd.external_id = CC.external_id
AND gd.sku = CC.sku
AND (CASE
         WHEN item_type LIKE '%Bundle%' THEN 'Bundle'
         ELSE 'Product'
     END)=CC.product_class
GROUP BY gd.transaction_date ,
         category_name ,
         product_type ,
         brand_name ,
         gd.sku ,
         gd.shipping_province ,
         order_recipient ,
         gd.order_placed_by ,
         order_type ,
         tier_name ,
         gd.external_id ,
         C.name ,
         O.status,
         payment_amount ,
         reseller_mobile ,
         reseller_email ,
         CASE
             WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M'
             WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M'
             WHEN max_payment_amount >10000000 THEN '>10M'
         END ,
         is_offender;
       
       
drop table if exists raena_analytics.rentention_bucket_wise ;       
       
create table raena_analytics.rentention_bucket_wise 
as
select month_sort , bucket,
       count(distinct case when m0>0 then mobile end) m0_user,
       count(distinct case when m1>0 then mobile end) m1_user,
       count(distinct case when m2>0 then mobile end) m2_user,
       count(distinct case when m3>0 then mobile end) m3_user,
       count(distinct case when m4>0 then mobile end) m4_user,
       count(distinct case when m5>0 then mobile end) m5_user,
       count(distinct case when m6>0 then mobile end) m6_user,
       count(distinct case when gt_m6>0 then mobile end) gt_m6_user,
       count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user,
       count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user,
       count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user,
       count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user,
       count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user,
       count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user,
       count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user,
       count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user
from (       
SELECT mobile , month_sort , bucket,is_offender,sum(M0) M0 , sum(M1) m1,
sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6
FROM
  (SELECT DISTINCT base_1.mobile,
                   date_trunc('MONTH',base_1.created_date)::date  AS month_sort,
                   base_1.created_date,
                   case when max_payment_amount between 0 and 2000000 then '<=2M'
                        when max_payment_amount between 2000001 and 10000000 then '2M-10M'
                        when max_payment_amount >10000000 then '>10M' end bucket,
                        is_offender,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount
                   END AS M0,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount
                   END AS M1,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount
                   END AS M2,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount
                   END AS M3,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount
                   END AS M4,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount
                   END AS M5,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount
                   END AS M6,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount
                   END AS GT_M6
 From
     (SELECT *
      FROM
        (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile,
                         cast(created_at AS Date)::date created_date,
                         payment_amount,
                         row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','')
                                           ORDER BY cast(created_at AS Date)) created_rnk
         FROM raena_order_management.order o
         WHERE payment_status = 'Paid'
          ) a
      WHERE a.created_rnk=1) base_1
   INNER join
     (SELECT mobile,
             date_trunc('month',created_date)::date AS created_date,
             is_offender,
             sum(payment_amount) AS payment_amount
      FROM
        (SELECT DISTINCT reseller_mobile AS mobile,
                         cast(transaction_date  AS Date) created_date,
                         is_offender,
                         discounted_price*quantity payment_amount
         FROM raena_analytics.gm_dashboard gd
         ) A
      GROUP BY 1,
               2,3) base_2 ON base_1.mobile=base_2.mobile
  INNER join
     (SELECT mobile,
             max(payment_amount) AS max_payment_amount
      FROM
        (SELECT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile,
                date_trunc('month',created_at)::date AS created_date,
                sum(payment_amount) payment_amount
         FROM raena_order_management.order o
         WHERE payment_status = 'Paid'
         group by 1,2 ) A
      GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final
     group by 1,2,3,4) BB
     group by 1,2
     order by 1,2;

       
       
drop table if exists raena_analytics.rentention_overall ;       
       
create table raena_analytics.rentention_overall 
as
select month_sort ,
       count(distinct case when m0>0 then mobile end) m0_user,
       count(distinct case when m1>0 then mobile end) m1_user,
       count(distinct case when m2>0 then mobile end) m2_user,
       count(distinct case when m3>0 then mobile end) m3_user,
       count(distinct case when m4>0 then mobile end) m4_user,
       count(distinct case when m5>0 then mobile end) m5_user,
       count(distinct case when m6>0 then mobile end) m6_user,
       count(distinct case when gt_m6>0 then mobile end) gt_m6_user,
       count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user,
       count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user,
       count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user,
       count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user,
       count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user,
       count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user,
       count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user,
       count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user
from (       
SELECT mobile , month_sort,is_offender,sum(M0) M0 , sum(M1) m1,
sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6
FROM
  (SELECT DISTINCT base_1.mobile,
                   date_trunc('MONTH',base_1.created_date)::date  AS month_sort,
                   base_1.created_date,
                   is_offender,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount
                   END AS M0,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount
                   END AS M1,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount
                   END AS M2,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount
                   END AS M3,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount
                   END AS M4,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount
                   END AS M5,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount
                   END AS M6,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount
                   END AS GT_M6
 From
     (SELECT *
      FROM
        (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile,
                         cast(created_at AS Date)::date created_date,
                         payment_amount,
                         row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','')
                                           ORDER BY cast(created_at AS Date)) created_rnk
         FROM raena_order_management.order o
         WHERE payment_status = 'Paid'
          ) a
      WHERE a.created_rnk=1) base_1
   INNER join
     (SELECT mobile,
             date_trunc('month',created_date)::date AS created_date,
             is_offender,
             sum(payment_amount) AS payment_amount
      FROM
        (SELECT DISTINCT reseller_mobile AS mobile,
                         cast(transaction_date  AS Date) created_date,
                         is_offender,
                         discounted_price*quantity payment_amount
         FROM raena_analytics.gm_dashboard gd
         ) A
      GROUP BY 1,
               2,3) base_2 ON base_1.mobile=base_2.mobile
  INNER join
     (SELECT mobile,
             max(payment_amount) AS max_payment_amount
      FROM
        (SELECT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile,
                date_trunc('month',created_at)::date AS created_date,
                sum(payment_amount) payment_amount
         FROM raena_order_management.order o
         WHERE payment_status = 'Paid'
         group by 1,2 ) A
      GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final
     group by 1,2,3) BB
     group by 1
     order by 1;
       
       
    
    
drop table if exists raena_analytics.rentention_brand_wise ;       
       
create table raena_analytics.rentention_brand_wise 
as
select month_sort ,bucket,brand_name,is_offender,
       count(distinct case when m0>0 then mobile end) m0_user,
       count(distinct case when m1>0 then mobile end) m1_user,
       count(distinct case when m2>0 then mobile end) m2_user,
       count(distinct case when m3>0 then mobile end) m3_user,
       count(distinct case when m4>0 then mobile end) m4_user,
       count(distinct case when m5>0 then mobile end) m5_user,
       count(distinct case when m6>0 then mobile end) m6_user,
       count(distinct case when gt_m6>0 then mobile end) gt_m6_user
from (       
SELECT mobile , month_sort ,bucket,brand_name,sum(M0) M0 , sum(M1) m1,
sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6
FROM
  (SELECT DISTINCT base_1.mobile,
                   date_trunc('MONTH',base_1.created_date)::date  AS month_sort,
                   base_1.created_date,
                   case when max_payment_amount between 0 and 2000000 then '<=2M'
                        when max_payment_amount between 2000001 and 10000000 then '2M-10M'
                        when max_payment_amount >10000000 then '>10M' end bucket,
                   base_1.brand_name,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount
                   END AS M0,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount
                   END AS M1,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount
                   END AS M2,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount
                   END AS M3,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount
                   END AS M4,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount
                   END AS M5,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount
                   END AS M6,
                   CASE
                       WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + 
                       date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount
                   END AS GT_M6
 From
     (SELECT distinct *
      FROM
        (SELECT DISTINCT reseller_mobile AS mobile,
                         cast(transaction_date AS Date)::date created_date,
                         brand_name,
                         discounted_price*quantity payment_amount,
                         row_number() over(partition BY reseller_mobile
                                           ORDER BY cast(transaction_date AS Date)) created_rnk
         FROM raena_analytics.gm_dashboard  o
          ) a
      WHERE a.created_rnk=1) base_1
   INNER join
     (SELECT mobile,
             date_trunc('month',created_date)::date AS created_date,
             sum(payment_amount) AS payment_amount
      FROM
        (SELECT DISTINCT reseller_mobile AS mobile,
                         cast(transaction_date AS Date) created_date,
                         discounted_price*quantity payment_amount,
                         row_number() over(partition BY replace(cast(reseller_mobile AS varchar),'"',''), date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date))
                                           ORDER BY date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date))) created_rnk
         FROM raena_analytics.gm_dashboard o
         ) A
      GROUP BY 1,
               2) base_2 ON base_1.mobile=base_2.mobile
  INNER join
     (SELECT mobile,
             max(payment_amount) AS max_payment_amount
      FROM
        (SELECT reseller_mobile AS mobile,
                date_trunc('month',transaction_date)::date AS created_date,
                sum(discounted_price*quantity) payment_amount
         FROM raena_analytics.gm_dashboard o
         group by 1,2 ) A
      GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final
     group by 1,2,3,4) BB
     left join (select distinct name, is_offender from raena_catalog_management.brand ) CC  on BB.brand_name = CC.name 
     group by 1,2,3,4
     order by 1,2,3;
    
    
    drop table raena_analytics.churn_reseller_data;
    
    CREATE TABLE raena_analytics.churn_reseller_data AS
SELECT date_trunc('Month',A.first_transaction_date)::date ,
       CASE
           WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M'
           WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M'
           WHEN max_payment_amount >10000000 THEN '>10M'
       END bucket,
       count(DISTINCT A.mobile) M0_user,
       count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' THEN B.mobile END) churn,
       count(DISTINCT CASE WHEN created_date ='2023-05-01' THEN base_2.mobile END) may_retention,
       count(DISTINCT CASE WHEN created_date ='2023-06-01' THEN base_2.mobile END) jun_retention,
       count(DISTINCT CASE WHEN created_date ='2023-07-01' THEN base_2.mobile END) july_retention,
       count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17'
             AND created_date ='2023-05-01' THEN base_2.mobile END) from_churn_may_retention,
       count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17'
             AND created_date ='2023-06-01' THEN base_2.mobile END) from_churn_jun_retention,
       count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17'
             AND created_date ='2023-07-01' THEN base_2.mobile END) from_churn_july_retention,
       count(DISTINCT CASE WHEN created_date ='2023-05-01'
             AND is_offender = 'true' THEN base_2.mobile END) offender_may_retention,
       count(DISTINCT CASE WHEN created_date ='2023-06-01'
             AND is_offender = 'true' THEN base_2.mobile END) offender_jun_retention,
       count(DISTINCT CASE WHEN created_date ='2023-07-01'
             AND is_offender = 'true' THEN base_2.mobile END) offender_july_retention,
       count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17'
             AND created_date ='2023-05-01'
             AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_may_retention,
       count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17'
             AND created_date ='2023-06-01'
             AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_jun_retention,
       count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17'
             AND created_date ='2023-07-01'
             AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_july_retention
FROM
  (SELECT DISTINCT mobile,
                   created_date first_transaction_date,
                   payment_amount
   FROM
     (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile,
                      cast(created_at AS Date)::date created_date,
                      payment_amount,
                      row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','')
                                        ORDER BY cast(created_at AS Date)::date) created_rnk
      FROM raena_order_management.order o
      WHERE payment_status = 'Paid' ) a
   WHERE a.created_rnk=1) A
LEFT JOIN
  (SELECT DISTINCT mobile,
                   created_date last_transaction_date,
                   payment_amount
   FROM
     (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile,
                      cast(created_at AS Date)::date created_date,
                      payment_amount,
                      row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','')
                                        ORDER BY cast(created_at AS Date)::date DESC) created_rnk
      FROM raena_order_management.order o
      WHERE payment_status = 'Paid'
        AND cast(created_at AS Date)::date <='2023-07-17' ) a
   WHERE a.created_rnk=1) B ON A.mobile = B.mobile
LEFT JOIN
  (SELECT mobile,
          date_trunc('month',created_date)::date AS created_date,
          is_offender,
          sum(payment_amount) AS payment_amount
   FROM
     (SELECT DISTINCT reseller_mobile AS mobile,
                      cast(transaction_date AS Date) created_date,
                      discounted_price*quantity payment_amount,
                      p.is_offender
      FROM raena_analytics.gm_dashboard o
      LEFT JOIN raena_catalog_management.product P ON o.sku = P.sku
      WHERE cast(transaction_date AS Date) >= date_trunc('Month',CURRENT_DATE)::date +interval'-4 Month' ) A
   GROUP BY 1,
            2,
            3) base_2 ON A.mobile=base_2.mobile
LEFT JOIN
  (SELECT mobile,
          max(payment_amount) AS max_payment_amount
   FROM
     (SELECT reseller_mobile AS mobile,
             date_trunc('month',transaction_date)::date AS created_date,
             sum(discounted_price*quantity) payment_amount
      FROM raena_analytics.gm_dashboard o
      GROUP BY 1,
               2) A
   GROUP BY 1) base_3 ON A.mobile=base_3.mobile
GROUP BY 1,
         2;

