first commit

This commit is contained in:
Shariar Imtiaz 2024-06-24 18:26:08 +06:00
commit 742b8d03c0
214 changed files with 109095 additions and 0 deletions

BIN
.etlTransaction_job.sh.swp Normal file

Binary file not shown.

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

BIN
.swp Normal file

Binary file not shown.

147
AM_ACQ/AM_Dashboard Normal file
View File

@ -0,0 +1,147 @@
#!/bin/bash
echo "
--------------------------Revenue Leakage -------------------------
drop table if exists om_brand_type_last_month;
create table om_brand_type_last_month
as
select distinct brand_name,
case when brand_name in ('HISTOIRE NATURELLE','INGRID','W DRESSROOM','FORENCOS','DEWYCEL','GLUTANEX','BELLFLOWER','ONE THING','BEAUSTA')
then 'EL\/PL' else 'NA' end as "EL\/PL Type",
case
when After_discount_GM <= '5.00' then 'offender 1'
when After_discount_GM between 5 and 10 then 'offender 2'
when After_discount_GM between 10 and 20 then 'DDB1'
when After_discount_GM between 20 and 30 then 'DDB2'
when After_discount_GM > 30 then 'DDB3' else 'NA' end as brand_type
from (
select brand_name ,
cast((sum((quantity*discounted_price)-(cogs*quantity))*100/sum(quantity*discounted_price)) as decimal(10,4)) as After_discount_GM
from OM_GM_DB_Product_category
where date_part('month',transaction_date)=EXTRACT(month FROM CURRENT_DATE - '1 month'::interval) and
date_part('year',transaction_date)=EXTRACT(year FROM CURRENT_DATE - '1 month'::interval)
group by 1
) order by 2;
drop table if exists om_seller_type_last_month;
create table om_seller_type_last_month
as
select reseller_email,case when After_discount_GM between 0 and 10.00 then 'Single digit Seller' when After_discount_GM >= 10.00 then 'Double Digit Seller' else 'NA' end as Seller_type
from (
select reseller_email,
cast((sum((quantity*discounted_price)-(cogs*quantity))*100/sum(quantity*discounted_price)) as decimal(10,4)) as After_discount_GM
from OM_GM_DB_Product_category
where date_part('month',transaction_date)=EXTRACT(month FROM CURRENT_DATE - '1 month'::interval) and
date_part('year',transaction_date)=EXTRACT(year FROM CURRENT_DATE - '1 month'::interval)
and reseller_email notnull
group by 1
);
drop table if exists om_seller_type_last_month_10M;
create table om_seller_type_last_month_10M
as
select distinct reseller_email,case when Last_month_rev > 10000000 then 'REV GT 10M' else 'REV LT 10M' end as Last_Month_Rev_Flag
from (
select reseller_email,sum(Payment_Price) Last_month_rev
from GM_GROWTH_TAB1
where date_part('month',created_date)=EXTRACT(month FROM CURRENT_DATE - '1 month'::interval) and
date_part('year',created_date)=EXTRACT(year FROM CURRENT_DATE - '1 month'::interval) and GM_GROWTH_TAB1.brand_name!='All'
and reseller_email notnull
group by 1
) ;
drop table if exists om_sku_weighted_avg;
create table om_sku_weighted_avg
as
select sku,sum(Numerator)/sum(quantity) as Weighted_avg
from (
select sku,(effective_wholesale_price)*quantity as Numerator,quantity,
row_number() over (partition by sku order by created_date desc) as R,cast(created_date as date)
from (
select sso.product_class,parent_sku as sku,sso.effective_wholesale_price,sso.quantity,cast(sso.created_at as date) as created_date
from raena_order_management.sales_sub_order sso
where price_type='NEGOTIATED_PRICE' --and sku='AVS019'
)
) where R<=5
group by 1
order by 1;
drop table if exists public.om_dormant_sellers_filter_base;
create table public.om_dormant_sellers_filter_base
as
select distinct a.reseller_email,flag_3,flag_2,flag_1,flag_0
from (
select distinct "email\ id" as reseller_email
from AM_REVENUE_LEAKAGE_LIST
inner join raena_user_management.user b on AM_REVENUE_LEAKAGE_LIST."email\ id" = b.email
left join GM_GROWTH_TAB1 on GM_GROWTH_TAB1.reseller_email=AM_REVENUE_LEAKAGE_LIST."email\ id"
) a
left join (
select distinct reseller_email,'MTD' as flag_0
from GM_GROWTH_TAB1
where cast(created_date as date)>= cast(date_add('month', 0, date_trunc('month', current_date)) as date) and GM_GROWTH_TAB1.brand_name!='All'
order by 1
) h on a.reseller_email=h.reseller_email
left join (
select distinct reseller_email,'one_month' as flag_1
from GM_GROWTH_TAB1
where cast(created_date as date)>= cast(date_add('month', -1, date_trunc('month', current_date)) as date)
and cast(created_date as date)<= cast(date_add('month', 0, date_trunc('month', current_date)) as date) and GM_GROWTH_TAB1.brand_name!='All'
order by 1
) b on a.reseller_email=b.reseller_email
left join (
select distinct reseller_email,'2nd_month' as flag_2
from GM_GROWTH_TAB1
where cast(created_date as date)>= cast(date_add('month', -3, date_trunc('month', current_date)) as date)
and cast(created_date as date)<= cast(date_add('month', 0, date_trunc('month', current_date)) as date) and GM_GROWTH_TAB1.brand_name!='All'
order by 1
) c on a.reseller_email=c.reseller_email
left join (
select distinct reseller_email,'3rd_month' as flag_3
from GM_GROWTH_TAB1
where cast(created_date as date)>= cast(date_add('month', -6, date_trunc('month', current_date)) as date)
and cast(created_date as date)<= cast(date_add('month', 0, date_trunc('month', current_date)) as date) and GM_GROWTH_TAB1.brand_name!='All'
order by 1
) d on a.reseller_email=d.reseller_email;
drop table if exists public.om_dormant_sellers_filter;
create table public.om_dormant_sellers_filter
as
select distinct reseller_email,Dornant_flag
from (
select distinct reseller_email, case when flag_3 is null then 'M-6' end as Dornant_flag
from public.om_dormant_sellers_filter_base
union
select distinct reseller_email, case when flag_2 is null then 'M-3' end as Dornant_flag
from public.om_dormant_sellers_filter_base
union
select distinct reseller_email, case when flag_1 is null then 'M-1' end as Dornant_flag
from public.om_dormant_sellers_filter_base
union
select distinct reseller_email, case when flag_0 is null then 'MTD' end as Dornant_flag
from public.om_dormant_sellers_filter_base
union
select distinct reseller_email, case when flag_3 is null and flag_2 is null and flag_1 is null and flag_0 is null then 'Never Transacted in Last 6 Months' end as Dornant_flag
from public.om_dormant_sellers_filter_base
)
--where reseller_email='ziyyags@gmail.com'
order by 1;
" > /home/ec2-user/cronjob/AM_ACQ/AM_dashboard.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f/home/ec2-user/cronjob/AM_ACQ/AM_dashboard.sql > AM_Dashboard.log

9
AM_ACQ/AM_Dashboard.log Normal file
View File

@ -0,0 +1,9 @@
DROP TABLE
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
DROP TABLE

143
AM_ACQ/AM_dashboard.sql Normal file
View File

@ -0,0 +1,143 @@
--------------------------Revenue Leakage -------------------------
drop table if exists om_brand_type_last_month;
create table om_brand_type_last_month
as
select distinct brand_name,
case when brand_name in ('HISTOIRE NATURELLE','INGRID','W DRESSROOM','FORENCOS','DEWYCEL','GLUTANEX','BELLFLOWER','ONE THING','BEAUSTA')
then 'EL\/PL' else 'NA' end as EL/PL Type,
case
when After_discount_GM <= '5.00' then 'offender 1'
when After_discount_GM between 5 and 10 then 'offender 2'
when After_discount_GM between 10 and 20 then 'DDB1'
when After_discount_GM between 20 and 30 then 'DDB2'
when After_discount_GM > 30 then 'DDB3' else 'NA' end as brand_type
from (
select brand_name ,
cast((sum((quantity*discounted_price)-(cogs*quantity))*100/sum(quantity*discounted_price)) as decimal(10,4)) as After_discount_GM
from OM_GM_DB_Product_category
where date_part('month',transaction_date)=EXTRACT(month FROM CURRENT_DATE - '1 month'::interval) and
date_part('year',transaction_date)=EXTRACT(year FROM CURRENT_DATE - '1 month'::interval)
group by 1
) order by 2;
drop table if exists om_seller_type_last_month;
create table om_seller_type_last_month
as
select reseller_email,case when After_discount_GM between 0 and 10.00 then 'Single digit Seller' when After_discount_GM >= 10.00 then 'Double Digit Seller' else 'NA' end as Seller_type
from (
select reseller_email,
cast((sum((quantity*discounted_price)-(cogs*quantity))*100/sum(quantity*discounted_price)) as decimal(10,4)) as After_discount_GM
from OM_GM_DB_Product_category
where date_part('month',transaction_date)=EXTRACT(month FROM CURRENT_DATE - '1 month'::interval) and
date_part('year',transaction_date)=EXTRACT(year FROM CURRENT_DATE - '1 month'::interval)
and reseller_email notnull
group by 1
);
drop table if exists om_seller_type_last_month_10M;
create table om_seller_type_last_month_10M
as
select distinct reseller_email,case when Last_month_rev > 10000000 then 'REV GT 10M' else 'REV LT 10M' end as Last_Month_Rev_Flag
from (
select reseller_email,sum(Payment_Price) Last_month_rev
from GM_GROWTH_TAB1
where date_part('month',created_date)=EXTRACT(month FROM CURRENT_DATE - '1 month'::interval) and
date_part('year',created_date)=EXTRACT(year FROM CURRENT_DATE - '1 month'::interval) and GM_GROWTH_TAB1.brand_name!='All'
and reseller_email notnull
group by 1
) ;
drop table if exists om_sku_weighted_avg;
create table om_sku_weighted_avg
as
select sku,sum(Numerator)/sum(quantity) as Weighted_avg
from (
select sku,(effective_wholesale_price)*quantity as Numerator,quantity,
row_number() over (partition by sku order by created_date desc) as R,cast(created_date as date)
from (
select sso.product_class,parent_sku as sku,sso.effective_wholesale_price,sso.quantity,cast(sso.created_at as date) as created_date
from raena_order_management.sales_sub_order sso
where price_type='NEGOTIATED_PRICE' --and sku='AVS019'
)
) where R<=5
group by 1
order by 1;
drop table if exists public.om_dormant_sellers_filter_base;
create table public.om_dormant_sellers_filter_base
as
select distinct a.reseller_email,flag_3,flag_2,flag_1,flag_0
from (
select distinct email id as reseller_email
from AM_REVENUE_LEAKAGE_LIST
inner join raena_user_management.user b on AM_REVENUE_LEAKAGE_LIST.email id = b.email
left join GM_GROWTH_TAB1 on GM_GROWTH_TAB1.reseller_email=AM_REVENUE_LEAKAGE_LIST.email id
) a
left join (
select distinct reseller_email,'MTD' as flag_0
from GM_GROWTH_TAB1
where cast(created_date as date)>= cast(date_add('month', 0, date_trunc('month', current_date)) as date) and GM_GROWTH_TAB1.brand_name!='All'
order by 1
) h on a.reseller_email=h.reseller_email
left join (
select distinct reseller_email,'one_month' as flag_1
from GM_GROWTH_TAB1
where cast(created_date as date)>= cast(date_add('month', -1, date_trunc('month', current_date)) as date)
and cast(created_date as date)<= cast(date_add('month', 0, date_trunc('month', current_date)) as date) and GM_GROWTH_TAB1.brand_name!='All'
order by 1
) b on a.reseller_email=b.reseller_email
left join (
select distinct reseller_email,'2nd_month' as flag_2
from GM_GROWTH_TAB1
where cast(created_date as date)>= cast(date_add('month', -3, date_trunc('month', current_date)) as date)
and cast(created_date as date)<= cast(date_add('month', 0, date_trunc('month', current_date)) as date) and GM_GROWTH_TAB1.brand_name!='All'
order by 1
) c on a.reseller_email=c.reseller_email
left join (
select distinct reseller_email,'3rd_month' as flag_3
from GM_GROWTH_TAB1
where cast(created_date as date)>= cast(date_add('month', -6, date_trunc('month', current_date)) as date)
and cast(created_date as date)<= cast(date_add('month', 0, date_trunc('month', current_date)) as date) and GM_GROWTH_TAB1.brand_name!='All'
order by 1
) d on a.reseller_email=d.reseller_email;
drop table if exists public.om_dormant_sellers_filter;
create table public.om_dormant_sellers_filter
as
select distinct reseller_email,Dornant_flag
from (
select distinct reseller_email, case when flag_3 is null then 'M-6' end as Dornant_flag
from public.om_dormant_sellers_filter_base
union
select distinct reseller_email, case when flag_2 is null then 'M-3' end as Dornant_flag
from public.om_dormant_sellers_filter_base
union
select distinct reseller_email, case when flag_1 is null then 'M-1' end as Dornant_flag
from public.om_dormant_sellers_filter_base
union
select distinct reseller_email, case when flag_0 is null then 'MTD' end as Dornant_flag
from public.om_dormant_sellers_filter_base
union
select distinct reseller_email, case when flag_3 is null and flag_2 is null and flag_1 is null and flag_0 is null then 'Never Transacted in Last 6 Months' end as Dornant_flag
from public.om_dormant_sellers_filter_base
)
--where reseller_email='ziyyags@gmail.com'
order by 1;

View File

@ -0,0 +1,75 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
INSERT 0 1453
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

19
DB_funnel_new_users.log Normal file
View File

@ -0,0 +1,19 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
INSERT 0 599642
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

910
Final_GM_v1.sql Normal file
View File

@ -0,0 +1,910 @@
DROP TABLE IF EXISTS public.manual_bundle_sku_data ;
CREATE TABLE public.manual_bundle_sku_data AS
SELECT upper(bundle_sku) Parent_sku ,
Upper(A.sku) child_sku ,
A.quantity child_quantity,
'Bundle' parent_sku_class,
cast(B.retail_price AS decimal(22,2)) bundle_retail_price,
cast(json_extract_path_text(C.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) AS decimal(22,2)) bronze_price,
cast(json_extract_path_text(C.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) AS decimal(22,2)) silver_price,
cast(json_extract_path_text(C.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price,
cast(C.retail_price AS decimal(22,2)) child_retail_price
FROM bundle_data_manual_new A
INNER JOIN raena_catalog_management.product B ON upper(A.bundle_sku) = upper(B.sku)
INNER JOIN raena_catalog_management.product C ON upper(A.sku) = upper(C.sku);
DROP TABLE IF EXISTS public.order_level_data;
CREATE TABLE public.order_level_data AS
SELECT external_id,
transaction_date AS transaction_date,
discount_amount,
shipping_cost,
A.coupon_code,
reseller_tier_name,
CASE
WHEN flash_sale_id IS NOT NULL THEN 'Flash'
END Product_type,
order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount,
coupon_applied_on
FROM
(SELECT A.id AS external_id ,
(A.created_at) AS transaction_date ,
A.discount_amount,
applied_shipping_amount shipping_cost,
A.coupon_code,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
flash_sale_id,
loyalty_discount AS order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount
FROM raena_order_management.order A
WHERE payment_status='Paid'
AND cast(A.created_at AS date) >='2022-05-24'
AND is_campaign = 'false') A
LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code;
DROP TABLE IF EXISTS public.base_netsuite_stage1;
CREATE TABLE public.base_netsuite_stage1 AS
SELECT DISTINCT transaction_date,
A.external_id,
B.country,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_sku
ELSE B.sku
END sku,
CASE
WHEN B.parent_item_id = F.id THEN F.sku
ELSE B.sku
END parent_sku,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_quantity*B.quantity
ELSE B.quantity
END quantity,
CASE
WHEN B.parent_item_id = F.id THEN F.quantity
ELSE B.quantity
END parent_quantity,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_retail_price
ELSE B.retail_price
END retail_price,
CASE
WHEN B.parent_item_id = F.id THEN F.retail_price
ELSE B.retail_price
END parent_retail_price,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'BRONZE' THEN bronze_price
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'SILVER' THEN silver_price
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'GOLD' THEN gold_price
ELSE B.wholesale_price
END wholesale_price,
CASE
WHEN parent_sku LIKE 'BAZ%' THEN B.wholesale_price
WHEN B.parent_item_id = F.id THEN F.wholesale_price
ELSE B.wholesale_price
END AS parent_wholesale_price,
CASE
WHEN B.parent_item_id = F.id THEN B.retail_price*B.quantity*F.discount_price / sum(CASE WHEN B.parent_item_id = F.id THEN B.retail_price*B.quantity END) over(partition BY external_id ,F.sku)
ELSE B.discount_price
END discount_price,
coupon_applied_on,
A.discount_amount,
CASE
WHEN B.parent_item_id IS NULL
AND cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN 0
ELSE B.dynamic_price
END Dynamic_price ,
CASE
WHEN B.parent_item_id = F.id THEN F.dynamic_price*F.quantity
WHEN B.parent_item_id IS NULL
AND cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN B.dynamic_price*B.quantity
END AS parent_dynamic_price,
B.payment_price,
payment_amount,
B.product_class,
coalesce(F.product_class,B.product_class) parent_product_class,
CASE
WHEN A.product_type = 'Flash'
AND B.sku LIKE 'BAZ%' THEN 'Flash Bundle'
WHEN A.product_type = 'Flash'
AND B.sku NOT LIKE 'BAZ%' THEN 'Flash'
WHEN B.sku LIKE 'BAZ%' THEN 'Bundle'
ELSE 'Regular'
END AS product_type_class,
F.id,
A.order_loyalty_discount,
B.loyalty_discount,
reseller_tier_name
FROM public.order_level_data A
LEFT JOIN raena_order_management.order_item B ON A.external_id = B.order_id
LEFT JOIN public.manual_bundle_sku_data D ON cast(B.sku AS varchar) = cast(D.parent_sku AS varchar)
LEFT JOIN
(SELECT id ,
order_id ,
retail_price,
Sku ,
quantity,
wholesale_price,
discount_price,
dynamic_price,
payment_price,
product_class,
loyalty_discount
FROM raena_order_management.order_item
WHERE product_class = 'Bundle'
AND cast(created_at AS date) >='2022-05-24') F ON B.parent_item_id = F.id
ORDER BY 1,
2;
DROP TABLE IF EXISTS public.loyalty_point_calculation1;
CREATE TABLE public.loyalty_point_calculation1 AS
SELECT *,
sum(order_loyalty_discount)over(partition BY external_id)/count(external_id)over(partition BY external_id) total_order_loyalty_discount ,
sum(loyalty_discount) over(partition BY external_id) total_sku_loyalty_discount
FROM public.base_netsuite_stage1
WHERE order_loyalty_discount >0;
DROP TABLE IF EXISTS public.final_loyalty_point;
CREATE TABLE public.final_loyalty_point AS
SELECT A.*,
CASE
WHEN sum(gold) over (partition BY external_id)>0
AND reseller_tier_name = 'GOLD' THEN gold*total_order_loyalty_discount/sum(gold) over (partition BY external_id)
WHEN sum(SILVER) over (partition BY external_id)>0
AND reseller_tier_name = 'SILVER' THEN SILVER*total_order_loyalty_discount/sum(SILVER) over (partition BY external_id)
WHEN sum(BRONZE) over (partition BY external_id)>0
AND reseller_tier_name = 'BRONZE' THEN bronze*total_order_loyalty_discount/sum(BRONZE) over (partition BY external_id)
END Final_loyalty_point
FROM public.loyalty_point_calculation1 A
LEFT JOIN loyalty_discount B ON A.sku = B.sku
WHERE total_order_loyalty_discount-total_sku_loyalty_discount NOT BETWEEN -10 AND 10;
DROP TABLE IF EXISTS public.base_netsuite_stage2 ;
CREATE TABLE public.base_netsuite_stage2 AS
SELECT transaction_date,
A.external_id,
A.sku,
country,
parent_sku,
quantity,
parent_quantity,
retail_price,
parent_retail_price,
CASE
WHEN parent_sku LIKE 'BAZ%'
AND id IS NULL THEN cast(wholesale_price*(parent_wholesale_price/quantity)/sum(wholesale_price)over(partition BY A.external_id ,parent_sku) AS decimal(22,2))
ELSE wholesale_price
END wholesale_price,
parent_wholesale_price,
discount_price,
coupon_applied_on,
discount_amount,
dynamic_price,
parent_dynamic_price,
payment_price,
payment_amount,
product_class,
parent_product_class,
product_type_class,
id,
CASE
WHEN cast(transaction_date AS date)< '2022-02-28'
AND A.external_id = B.external_id THEN B.final_loyalty_point
ELSE A.loyalty_discount
END loyalty_discount
FROM public.base_netsuite_stage1 A
LEFT JOIN
(SELECT external_id ,
sku ,
final_loyalty_point
FROM public.final_loyalty_point) B ON A.external_id = B.external_id
AND A.sku = B.sku
WHERE product_class <> 'Bundle';
DROP TABLE IF EXISTS public.base_netsuite_stage3 ;
CREATE TABLE public.base_netsuite_stage3 AS
SELECT A.* ,
CASE
WHEN dynamic_price>0 THEN dynamic_price
ELSE wholesale_price
END final_wholesale_price,
CASE
WHEN coupon_applied_on <> 'Cart'
AND discount_price>0 THEN retail_price*quantity*discount_amount/sum(CASE WHEN discount_price>0 THEN retail_price*quantity END)over(partition BY A.external_id)
WHEN coupon_applied_on <> 'Cart'
AND discount_price=0 THEN 0
WHEN coupon_applied_on ='Cart'
AND discount_price=0 THEN retail_price*quantity*discount_amount/sum(retail_price*quantity)over(partition BY A.external_id)
ELSE discount_price
END Final_discount ,
retail_price*quantity-(CASE
WHEN dynamic_price>0 THEN dynamic_price
ELSE wholesale_price
END)*quantity AS seller_margin
FROM public.base_netsuite_stage2 A ;
DROP TABLE IF EXISTS public.base_netsuite_stage4 ;
CREATE TABLE public.base_netsuite_stage4 AS
SELECT A.* ,
CASE
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount>0
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(retail_price AS decimal(22,2))*quantity*(ttl_retail_price-(parent_retail_price*parent_quantity)))/(ttl_retail_price)
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount=0
AND (parent_dynamic_price =0
OR parent_dynamic_price IS NULL)
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(final_wholesale_price AS decimal(22,2))*quantity*(ttl_wholesale_price-(parent_wholesale_price*parent_quantity)))/ttl_wholesale_price
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount=0
AND parent_dynamic_price>0
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(final_wholesale_price AS decimal(22,2))*quantity*(ttl_wholesale_price-ttl_parent_dynamic_price))/ttl_wholesale_price
END AS additional_discount,
CASE
WHEN final_discount >0 THEN cast(final_discount AS decimal(22,2))-cast(seller_margin AS decimal(22,2))
END effective_coupon_discount
FROM public.base_netsuite_stage3 A
LEFT JOIN
(SELECT external_id ,
parent_sku ,
round(sum(retail_price*quantity))ttl_retail_price,
round(sum(final_wholesale_price*quantity))ttl_wholesale_price,
min(CASE WHEN parent_dynamic_price>0 THEN parent_dynamic_price END)ttl_parent_dynamic_price
FROM public.base_netsuite_stage3
WHERE parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle')
GROUP BY external_id ,
parent_sku) B ON A.external_id = B.external_id
AND A.parent_sku = B.parent_sku;
DELETE
FROM public.base_netsuite_final
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.order_level_data);
INSERT INTO public.base_netsuite_final
SELECT A.external_id,
A.transaction_date ,
A.discount_amount order_discount_amount,
A.shipping_cost ,
coupon_code ,
A.coupon_applied_on ,
reseller_tier_name tier,
B.sku product_sku,
B.quantity ,
retail_price,
CASE
WHEN A.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku THEN (seller_margin+diff)/B.quantity
ELSE B.seller_margin/B.quantity
END seller_margin,
((coalesce(retail_price,0)*quantity) -(coalesce(CASE WHEN A.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku THEN (seller_margin+diff)ELSE seller_margin END ,0)) -coalesce(CASE WHEN A.external_id= 'OD1641992277895310REG' THEN 180000 ELSE effective_coupon_discount END,0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price,
cast(final_discount AS decimal(22,2))/quantity coupon_discount,
CASE
WHEN A.external_id= 'OD1641992277895310REG' THEN 1200
ELSE effective_coupon_discount/quantity
END effective_coupon_discount ,
loyalty_discount/quantity loyalty_discount,
coalesce(additional_discount,0)/quantity additional_discount,
CASE
WHEN (parent_product_class = 'Bundle'
AND product_type_class = 'Flash')
OR product_type_class = 'Flash Bundle' THEN 'Flash Bundle'
WHEN (parent_product_class = 'Flash'
OR product_type_class = 'Flash') THEN 'Flash'
WHEN (parent_product_class = 'Bundle'
OR product_type_class = 'Bundle')THEN 'Bundle'
ELSE 'Regular'
END item_type,
diff
FROM public.order_level_data A
LEFT JOIN public.base_netsuite_stage4 B ON A.external_id = B.external_id
LEFT JOIN
(SELECT external_id ,
sku ,
parent_sku ,
seller_margin * diff/sum(seller_margin)over(partition BY external_id) diff
FROM public.base_netsuite_stage4 A
INNER JOIN public.order_check_table B ON A.external_id = B.order_id)C ON B.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku ;
DROP TABLE IF EXISTS public.gm_dimensions_stage1;
CREATE TABLE public.gm_dimensions_stage1 AS
SELECT A.id AS external_id ,
shipping_province,
reseller_id ,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
discount_type,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
C.sku ,
C.name sku_name,
brand_name ,
shipping_to,
product_type ,
category_name,
order_placed_by
FROM raena_order_management.order A
LEFT JOIN raena_order_management.discount_coupon B ON A.coupon_code = B.coupon_code
LEFT JOIN raena_order_management.order_item C ON A.id = C.order_id
WHERE payment_status ='Paid'
AND A.is_campaign = 'false'
AND cast(A.created_at AS date) >='2022-05-24';
DELETE
FROM public.GM_dashboard
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.gm_dimensions_stage1);
INSERT INTO public.GM_dashboard
SELECT A.external_id ,
B.transaction_date+interval'7 Hours' transaction_date,
B.order_discount_amount ,
B.shipping_cost ,
A.shipping_province,
B.coupon_code ,
A.discount_type ,
B.coupon_applied_on ,
A.brand_name,
A.category_name ,
A.product_type ,
A.sku sku,
A.sku_name ,
A.reseller_name ,
A.reseller_email ,
A.reseller_mobile ,
reseller_tier_name tier_name,
reseller_id ,
B.quantity ,
B.retail_price ,
B.seller_margin ,
B.discounted_price ,
B.additional_discount ,
B.loyalty_discount ,
B.effective_coupon_discount,
B.item_type ,
order_placed_by ,
min( case when A.sku = C.sku
AND cast(transaction_date AS date) = Cast(created_at AS date)
then coalesce(cogs_promo,cogs_non_promo) end) cogs,
shipping_to order_recipient,
CASE
WHEN brand_name IN ('LUXCRIME',
'SKINTIFIC',
'TRUEVE',
'SANIYE',
'BEAUDELAB',
'BRASOV',
'FACE REPUBLIC',
'SKIN 1004',
'PREMIERE BEAUTE',
'ALLURA',
'LIPLAPIN',
'ROUNDLAB',
'FACE FLUX',
'DOLLGORAE',
'SKINUA',
'PUREFORET',
'SKINTIFIC',
'OHMYSKIN',
'FEAT FOR SKIN',
'SECONDATE',
'KYND',
'PURNAMA',
'BASE',
'LAVIE LASH',
'REI SKIN',
'USTRAA',
'BRUNBRUN PARISGLOWINC',
'SOONHAN',
'THE YEON',
'MIXSOON',
'KOSE COSMEPORT') THEN 'High GM'
WHEN brand_name IN ('W DRESSROOM',
'BEAUSTA',
'Dewycel',
'GLUTANEX',
'HISTOIRE NATURELLE',
'FORENCOS',
'BELLFLOWER',
'MAXCLINIC') THEN 'EL/PL'
END brand_type ,
Customer_type,
BB.gm_target
FROM public.gm_dimensions_stage1 A
INNER JOIN public.base_netsuite_final B ON A.external_id =B.external_id
AND A.sku=B.product_sku
LEFT JOIN
(SELECT sku ,
min(cogs_non_promo) cogs_non_promo,
min(cogs_promo) cogs_promo,
--cogs ,
--sku_cogs_type ,
Cast(created_at AS date) created_at
FROM public.sku_cogs_audit
group by sku ,
Cast(created_at AS date) ) C ON A.sku = C.sku
AND cast(transaction_date AS date) = Cast(created_at AS date)
left join (SELECT DISTINCT id order_id,
CASE
WHEN customer_id IS NOT NULL
AND order_placed_by = 'admin' THEN 'Offline Dropshipper'
WHEN customer_id IS NOT NULL
AND order_placed_by <> 'admin' THEN 'Online Dropshipper'
WHEN customer_id IS NULL
AND order_placed_by <> 'admin' THEN 'Online Reseller'
WHEN customer_id IS NULL
AND order_placed_by = 'admin' THEN 'Offline Reseller'
END Customer_type
FROM raena_order_management.order) AA on A.external_id = AA.order_id
left join (
select DISTINCT SKU,gm_target,t.name as tierName
from raena_gross_margin_management.gross_margin_config AA
LEFT JOIN raena_user_management.tier t
ON cast(AA.gm_target_tier as TEXT) =cast(t.id as TEXT)
) BB
on A.sku = BB.sku
and A.reseller_tier_name =BB.tierName
GROUP BY A.external_id ,
B.transaction_date+interval'7 Hours' ,
B.order_discount_amount ,
B.shipping_cost ,
A.shipping_province,
B.coupon_code ,
A.discount_type ,
B.coupon_applied_on ,
A.brand_name,
A.category_name ,
A.product_type ,
A.sku ,
A.sku_name ,
A.reseller_name ,
A.reseller_email ,
A.reseller_mobile ,
reseller_tier_name ,
reseller_id ,
B.quantity ,
B.retail_price ,
B.seller_margin ,
B.discounted_price ,
B.additional_discount ,
B.loyalty_discount ,
B.effective_coupon_discount,
B.item_type ,
order_placed_by,
shipping_to,
CASE
WHEN brand_name IN ('LUXCRIME',
'SKINTIFIC',
'TRUEVE',
'SANIYE',
'BEAUDELAB',
'BRASOV',
'FACE REPUBLIC',
'SKIN 1004',
'PREMIERE BEAUTE',
'ALLURA',
'LIPLAPIN',
'ROUNDLAB',
'FACE FLUX',
'DOLLGORAE',
'SKINUA',
'PUREFORET',
'SKINTIFIC',
'OHMYSKIN',
'FEAT FOR SKIN',
'SECONDATE',
'KYND',
'PURNAMA',
'BASE',
'LAVIE LASH',
'REI SKIN',
'USTRAA',
'BRUNBRUN PARISGLOWINC',
'SOONHAN',
'THE YEON',
'MIXSOON',
'KOSE COSMEPORT') THEN 'High GM'
WHEN brand_name IN ('W DRESSROOM',
'BEAUSTA',
'Dewycel',
'GLUTANEX',
'HISTOIRE NATURELLE',
'FORENCOS',
'BELLFLOWER',
'MAXCLINIC') THEN 'EL/PL'
END,Customer_type,
BB.gm_target ;
DROP TABLE IF EXISTS public.business_dimensions_stage1;
CREATE TABLE public.business_dimensions_stage1 AS
SELECT A.id AS external_id ,
reseller_id,
customer_id,
cart_id,
A.flash_sale_id,
shipping_to,
payment_status,
A.status,
A.stock_type,
order_placed_by,
order_warehouse,
cast(channel_id as text) channel_id ,
medium,
marketplace,
provider,
coalesce(cast(B.tier_id as varchar),cast(C.tier_id as varchar)) tier_id ,
json_extract_path_text(A.reseller_info,'city',TRUE) reseller_city,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
json_extract_path_text(A.reseller_info,'store',TRUE) reseller_store,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
shipping_address_line1 ,
shipping_address_line2 ,
shipping_pincode ,
shipping_district,
shipping_city ,
shipping_province ,
is_bank_transfer ,
is_campaign ,
discount_type
FROM raena_order_management.order A
left join public.tier_name B on A.id = B.id
left join raena_user_management.user C on cast(A.reseller_id as varchar) = cast(C.id as varchar)
left join raena_order_management.discount_coupon D on A.coupon_code = D.coupon_code
where cast(A.created_at AS date) >='2022-05-24';
DELETE
FROM public.business_report
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.business_dimensions_stage1);
Insert into public.business_report
SELECT A.external_id,
cast(B.transaction_date as date) created_date ,
B.order_discount_amount,
B.shipping_cost ,
coupon_code ,
D.name brand_name,
C.product_type,
E.name category_name,
C.name sku_name,
B.product_sku sku,
reseller_id,
customer_id,
cart_id,
A.flash_sale_id,
shipping_to,
payment_status,
status,
A.stock_type,
order_placed_by,
order_warehouse,
channel_id,
medium,
marketplace,
provider,
reseller_tier_name tier,
reseller_city,
reseller_name,
reseller_email,
reseller_mobile,
reseller_store,
shipping_address_line1 ,
shipping_address_line2 ,
shipping_pincode ,
shipping_district,
shipping_city ,
shipping_province ,
is_bank_transfer ,
is_campaign ,
C.Country,
B.quantity ,
B.retail_price,
seller_margin,
discounted_price,
loyalty_discount,
effective_coupon_discount ,
additional_discount,
item_type
FROM public.business_dimensions_stage1 A
LEFT JOIN public.base_netsuite_final B ON A.external_id = B.external_id
left join raena_catalog_management.product C on B.product_sku = C.sku
left join raena_catalog_management.brand D on C.brand_id = D.id
left join raena_catalog_management.category E on C.category_id = E.id;
DELETE
FROM public.OM_GM_DB_Product_category
WHERE external_Id IN
(SELECT DISTINCT external_id
FROM public.GM_dashboard
WHERE transaction_date >='2022-05-24');
INSERT INTO OM_GM_DB_Product_category
SELECT A.external_id ,
transaction_date,
concat(left(TO_char(transaction_date,'month'),3),date_part('year',transaction_date)) AS Time,
order_discount_amount ,
shipping_cost ,
coupon_code ,
discount_type ,
coupon_applied_on ,
D.brand_name,
D.category_name ,
D.product_type ,
A.sku,
D.name sku_name ,
reseller_name ,
reseller_email ,
reseller_mobile ,
tier_name ,
reseller_id ,
quantity ,
retail_price ,
seller_margin ,
discounted_price ,
additional_discount,
item_type ,
order_placed_by ,
A.cogs,
shipping_province ,
CASE
WHEN A.external_id = C.external_id THEN 'Yes'
ELSE 'No'
END Flag ,
CASE
WHEN tier_name='GOLD' THEN gold_price
WHEN tier_name ='SILVER' THEN silver_price
WHEN tier_name ='BRONZE' THEN bronze_price
ELSE retail_price-seller_margin
END wholesale_price,
order_recipient,
brand_type,
Customer_type,gm_target
FROM public.GM_dashboard A
LEFT JOIN
(SELECT DISTINCT external_id
FROM
(SELECT external_id,
discounted_price ,
sku ,
(((retail_price-seller_margin)*quantity) -(cogs*quantity))/((retail_price-seller_margin)*quantity) Pre_discount,
CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/(discounted_price*quantity) ELSE 0 END Post_discount
FROM public.GM_dashboard) A
WHERE Post_discount>Pre_discount) C ON A.external_id = C.external_id
LEFT JOIN
(SELECT DISTINCT Sku ,
A.name,
A.product_type,
C.name category_name ,
B.name brand_name
FROM raena_catalog_management.product A
LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id
LEFT JOIN raena_catalog_management.category c ON A.category_id=C.id) D ON A.sku = D.sku
LEFT JOIN pricing_sheet P ON A.sku=p.skucode
WHERE A.cogs IS NOT NULL
AND transaction_date >='2022-05-24';
--Business Dashboard
DELETE
FROM public.GM_GROWTH_TAB1
WHERE created_date >='2022-05-24';
INSERT INTO public.GM_GROWTH_TAB1
SELECT created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
channel_name,
reseller_name,reseller_email,
count(DISTINCT external_id) AS Number_of_Orders ,
sum(retail_price*quantity) RSP ,
sum((retail_price*quantity)-(seller_margin*quantity))wholesale_price ,
sum(discounted_price*quantity) AS Payment_Price ,
sum(effective_coupon_discount*quantity) AS Effective_Coupon_Discount,
sum(loyalty_discount*quantity)Total_Loyalty_point,
sum(additional_discount*quantity)additional_discount,
sum(seller_margin*quantity)seller_margin,
sum(discounted_price*quantity)/count(DISTINCT external_id) AS AOV,
sum(quantity) AS quantity
FROM
(SELECT DISTINCT a.created_date,
'All' AS brand_name,
'All' AS product_type,
'All' AS category_name,
'All' AS sku,
'All' AS sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
b.channel_name,
reseller_name,reseller_email,
external_id,
retail_price,
quantity,
seller_margin,
discounted_price,
effective_coupon_discount,
loyalty_discount,
additional_discount,
row_number() over (partition BY external_id
ORDER BY external_id) AS R
FROM public.business_report a
LEFT JOIN
(SELECT DISTINCT A.id ,
channel_id ,
D.name channel_name
FROM raena_order_management.order A
LEFT JOIN
(SELECT id,
name
FROM raena_order_management.channel) D ON A.channel_id = D.id
WHERE cast(created_at AS date) >= '2022-05-24'
AND channel_id IS NOT NULL) b ON a.external_id=b.id
WHERE cast(is_campaign AS varchar)='false'
AND payment_status='Paid'
AND created_date >= '2022-05-24'
UNION SELECT DISTINCT a.created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
b.channel_name,
reseller_name,reseller_email,
external_id,
retail_price,
quantity,
seller_margin,
discounted_price,
effective_coupon_discount,
loyalty_discount,
additional_discount,
row_number() over (partition BY external_id
ORDER BY external_id) AS R
FROM public.business_report a
LEFT JOIN
(SELECT DISTINCT A.id ,
channel_id ,
D.name channel_name
FROM raena_order_management.order A
LEFT JOIN
(SELECT id,
name
FROM raena_order_management.channel) D ON A.channel_id = D.id
WHERE cast(created_at AS date) >= '2022-05-24'
AND channel_id IS NOT NULL) b ON a.external_id=b.id
WHERE cast(is_campaign AS varchar)='false'
AND payment_status='Paid'
AND created_date >= '2022-05-24')
GROUP BY created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
channel_name,
reseller_name,reseller_email;
--Metric Trend
DROP TABLE IF EXISTS public.GM_GROWTH_TAB2;
CREATE TABLE public.GM_GROWTH_TAB2 AS
SELECT 'year' AS frequency,
cast(date_part('year',created_date) AS varchar) AS time,
cast(date_part('year',created_date) AS int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'quarter' AS frequency,
To_char(created_date,'quarter') AS month_name,
date_part('quarter',created_date) as sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'month' AS frequency,
To_char(created_date,'month') AS month_name,
date_part('month',created_date) as sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'week' AS frequency,
To_char(created_date,'week') AS month_name,
cast(left(To_char(created_date,'week'),1) as int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'day' AS frequency,
cast(date_part('day',created_date) as varchar) AS month_name,
cast(date_part('day',created_date) AS int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
ORDER BY 3;

Binary file not shown.

1040
Gm_dashboard/2q Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,865 @@
DROP TABLE IF EXISTS public.manual_bundle_sku_data ;
CREATE TABLE public.manual_bundle_sku_data AS
SELECT upper(bundle_sku) Parent_sku ,
Upper(A.sku) child_sku ,
A.quantity child_quantity,
'Bundle' parent_sku_class,
cast(B.retail_price AS decimal(22,2)) bundle_retail_price,
cast(json_extract_path_text(C.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) AS decimal(22,2)) bronze_price,
cast(json_extract_path_text(C.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) AS decimal(22,2)) silver_price,
cast(json_extract_path_text(C.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price,
cast(C.retail_price AS decimal(22,2)) child_retail_price
FROM bundle_data_manual_new A
INNER JOIN raena_catalog_management.product B ON upper(A.bundle_sku) = upper(B.sku)
INNER JOIN raena_catalog_management.product C ON upper(A.sku) = upper(C.sku);
DROP TABLE IF EXISTS public.order_level_data;
CREATE TABLE public.order_level_data AS
SELECT external_id,
transaction_date AS transaction_date,
discount_amount,
shipping_cost,
A.coupon_code,
reseller_tier_name,
CASE
WHEN flash_sale_id IS NOT NULL THEN 'Flash'
END Product_type,
order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount,
coupon_applied_on
FROM
(SELECT A.id AS external_id ,
(A.created_at) AS transaction_date ,
A.discount_amount,
applied_shipping_amount shipping_cost,
A.coupon_code,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
flash_sale_id,
loyalty_discount AS order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount
FROM raena_order_management.order A
WHERE payment_status='Paid'
AND cast(A.created_at AS date) >='2022-05-01'
AND is_campaign = 'false') A
LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code;
DROP TABLE IF EXISTS public.base_netsuite_stage1_V1;
CREATE TABLE public.base_netsuite_stage1_V1 AS
SELECT DISTINCT transaction_date,
A.external_id,
CASE
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_sku
WHEN B.id = F.sales_sub_order_id THEN F.sku
ELSE B.parent_sku
END sku,
B.parent_sku parent_sku,
CASE
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_quantity*B.quantity
WHEN B.id = F.sales_sub_order_id THEN F.quantity
ELSE B.quantity
END quantity,
B.quantity parent_quantity,
CASE
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_retail_price
WHEN B.id = F.sales_sub_order_id THEN F.retail_price
ELSE B.retail_price
END retail_price,
B.retail_price parent_retail_price,
CASE
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'BRONZE' THEN bronze_price
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'SILVER' THEN silver_price
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'GOLD' THEN gold_price
when B.id = F.sales_sub_order_id THEN F.effective_wholesale_price
ELSE B.effective_wholesale_price
END effective_wholesale_price,
B.effective_wholesale_price parent_wholesale_price,
CASE
WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount
ELSE B.coupon_discount
END discount_price,
coupon_applied_on,
A.discount_amount,
0 Dynamic_price ,
0 parent_dynamic_price,
case when B.id = F.sales_sub_order_id THEN F.payment_amount
else B.payment_amount end payment_price,
A.payment_amount,
B.product_class,
coalesce(F.product_class,B.product_class) parent_product_class,
CASE
WHEN A.product_type = 'Flash'
AND B.parent_sku LIKE 'BAZ%' THEN 'Flash Bundle'
WHEN A.product_type = 'Flash'
AND B.parent_sku NOT LIKE 'BAZ%' THEN 'Flash'
WHEN B.parent_sku LIKE 'BAZ%' THEN 'Bundle'
ELSE 'Regular'
END AS product_type_class,
F.sales_sub_order_id,
A.order_loyalty_discount,
B.loyalty_discount,
reseller_tier_name
FROM public.order_level_data A
LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id
LEFT JOIN public.manual_bundle_sku_data D ON cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
LEFT JOIN
(SELECT id ,
sales_sub_order_id ,
retail_price,
Sku ,
quantity,
effective_wholesale_price,
coupon_discount,
payment_amount,
product_class,
loyalty_discount
FROM raena_order_management.sales_sub_order_parent_child ssopc
WHERE product_class = 'Bundle'
AND cast(created_at AS date)>='2022-05-01') F ON B.id = F.sales_sub_order_id
ORDER BY 1,2;
DROP TABLE IF EXISTS public.loyalty_point_calculation1;
CREATE TABLE public.loyalty_point_calculation1 AS
SELECT *,
sum(order_loyalty_discount)over(partition BY external_id)/count(external_id)over(partition BY external_id) total_order_loyalty_discount ,
sum(loyalty_discount) over(partition BY external_id) total_sku_loyalty_discount
FROM public.base_netsuite_stage1
WHERE order_loyalty_discount >0;
DROP TABLE IF EXISTS public.final_loyalty_point;
CREATE TABLE public.final_loyalty_point AS
SELECT A.*,
CASE
WHEN sum(gold) over (partition BY external_id)>0
AND reseller_tier_name = 'GOLD' THEN gold*total_order_loyalty_discount/sum(gold) over (partition BY external_id)
WHEN sum(SILVER) over (partition BY external_id)>0
AND reseller_tier_name = 'SILVER' THEN SILVER*total_order_loyalty_discount/sum(SILVER) over (partition BY external_id)
WHEN sum(BRONZE) over (partition BY external_id)>0
AND reseller_tier_name = 'BRONZE' THEN bronze*total_order_loyalty_discount/sum(BRONZE) over (partition BY external_id)
END Final_loyalty_point
FROM public.loyalty_point_calculation1 A
LEFT JOIN loyalty_discount B ON A.sku = B.sku
WHERE total_order_loyalty_discount-total_sku_loyalty_discount NOT BETWEEN -10 AND 10;
DROP TABLE IF EXISTS public.base_netsuite_stage2_v1 ;
CREATE TABLE public.base_netsuite_stage2_v1 AS
SELECT transaction_date,
A.external_id,
A.sku,
parent_sku,
quantity,
parent_quantity,
retail_price,
parent_retail_price,
effective_wholesale_price,
parent_wholesale_price,
discount_price,
coupon_applied_on,
discount_amount,
dynamic_price,
parent_dynamic_price,
payment_price,
payment_amount,
product_class,
parent_product_class,
product_type_class,
sales_sub_order_id,
CASE
WHEN cast(transaction_date AS date)< '2022-02-28'
AND A.external_id = B.external_id THEN B.final_loyalty_point
ELSE A.loyalty_discount
END loyalty_discount
FROM public.base_netsuite_stage1_V1 A
LEFT JOIN
(SELECT external_id ,
sku ,
final_loyalty_point
FROM public.final_loyalty_point) B ON A.external_id = B.external_id
AND A.sku = B.sku;
DROP TABLE IF EXISTS public.base_netsuite_stage3_v1 ;
CREATE TABLE public.base_netsuite_stage3_v1 AS
SELECT A.* ,
CASE
WHEN dynamic_price>0 THEN dynamic_price
ELSE effective_wholesale_price
END final_wholesale_price,
CASE
WHEN coupon_applied_on <> 'Cart'
AND discount_price>0 THEN retail_price*quantity*discount_amount/sum(CASE WHEN discount_price>0 THEN retail_price*quantity END)over(partition BY A.external_id)
WHEN coupon_applied_on <> 'Cart'
AND discount_price=0 THEN 0
WHEN coupon_applied_on ='Cart'
AND discount_price=0 THEN retail_price*quantity*discount_amount/sum(retail_price*quantity)over(partition BY A.external_id)
ELSE discount_price
END Final_discount ,
retail_price*quantity-(CASE
WHEN dynamic_price>0 THEN dynamic_price
ELSE effective_wholesale_price
END)*quantity AS seller_margin
FROM public.base_netsuite_stage2_v1 A ;
DROP TABLE IF EXISTS public.base_netsuite_stage4_v1 ;
CREATE TABLE public.base_netsuite_stage4_v1 AS
SELECT A.* ,
CASE
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount>0
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(retail_price AS decimal(22,2))*quantity*(ttl_retail_price-(parent_retail_price*parent_quantity)))/(ttl_retail_price)
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount=0
AND (parent_dynamic_price =0
OR parent_dynamic_price IS NULL)
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(final_wholesale_price AS decimal(22,2))*quantity*(ttl_wholesale_price-(parent_wholesale_price*parent_quantity)))/ttl_wholesale_price
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount=0
AND parent_dynamic_price>0
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(final_wholesale_price AS decimal(22,2))*quantity*(ttl_wholesale_price-ttl_parent_dynamic_price))/ttl_wholesale_price
END AS additional_discount,
CASE
WHEN final_discount >0 THEN cast(final_discount AS decimal(22,2))-cast(seller_margin AS decimal(22,2))
END effective_coupon_discount
FROM public.base_netsuite_stage3_v1 A
LEFT JOIN
(SELECT external_id ,
parent_sku ,
round(sum(retail_price*quantity))ttl_retail_price,
round(sum(final_wholesale_price*quantity))ttl_wholesale_price,
min(CASE WHEN parent_dynamic_price>0 THEN parent_dynamic_price END)ttl_parent_dynamic_price
FROM public.base_netsuite_stage3_v1
WHERE parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle')
GROUP BY external_id ,
parent_sku) B ON A.external_id = B.external_id
AND A.parent_sku = B.parent_sku;
DELETE
FROM public.base_netsuite_final
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.order_level_data);
INSERT INTO public.base_netsuite_final
SELECT A.external_id,
A.transaction_date ,
A.discount_amount order_discount_amount,
A.shipping_cost ,
coupon_code ,
A.coupon_applied_on ,
reseller_tier_name tier,
B.sku product_sku,
B.quantity ,
retail_price,
CASE
WHEN A.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku THEN (seller_margin+diff)/B.quantity
ELSE B.seller_margin/B.quantity
END seller_margin,
((coalesce(retail_price,0)*quantity)
-(coalesce(CASE WHEN A.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku THEN (seller_margin+diff)ELSE seller_margin END ,0))
-coalesce(CASE WHEN A.external_id= 'OD1641992277895310REG' THEN 180000 ELSE effective_coupon_discount END,0)
-(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price,
cast(final_discount AS decimal(22,2))/quantity coupon_discount,
CASE
WHEN A.external_id= 'OD1641992277895310REG' THEN 1200
ELSE effective_coupon_discount/quantity
END effective_coupon_discount ,
loyalty_discount/quantity loyalty_discount,
coalesce(additional_discount,0)/quantity additional_discount,
CASE
WHEN (parent_product_class = 'Bundle'
AND product_type_class = 'Flash')
OR product_type_class = 'Flash Bundle' THEN 'Flash Bundle'
WHEN (parent_product_class = 'Flash'
OR product_type_class = 'Flash') THEN 'Flash'
WHEN (parent_product_class = 'Bundle'
OR product_type_class = 'Bundle')THEN 'Bundle'
ELSE 'Regular'
END item_type,
diff
FROM public.order_level_data A
LEFT JOIN public.base_netsuite_stage4_v1 B ON A.external_id = B.external_id
LEFT JOIN
(SELECT external_id ,
sku ,
parent_sku ,
seller_margin * diff/sum(seller_margin)over(partition BY external_id) diff
FROM public.base_netsuite_stage4_v1 A
INNER JOIN public.order_check_table B ON A.external_id = B.order_id)C ON B.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku ;
DROP TABLE IF EXISTS public.gm_dimensions_stage1;
CREATE TABLE public.gm_dimensions_stage1 AS
SELECT A.id AS external_id ,
shipping_province,
reseller_id ,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
discount_type,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
C.sku ,
shipping_to,
C.name sku_name,
brand_name ,
product_type ,
category_name,
order_placed_by
FROM raena_order_management.order A
LEFT JOIN raena_order_management.discount_coupon B ON A.coupon_code = B.coupon_code
LEFT JOIN raena_order_management.order_item C ON A.id = C.order_id
WHERE payment_status ='Paid'
AND A.is_campaign = 'false'
AND cast(A.created_at AS date) >='2022-05-01';
DELETE
FROM public.GM_dashboard
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.gm_dimensions_stage1);
INSERT INTO public.GM_dashboard
SELECT A.external_id ,
B.transaction_date+interval'7 Hours' transaction_date,
B.order_discount_amount ,
B.shipping_cost ,
A.shipping_province,
B.coupon_code ,
A.discount_type ,
B.coupon_applied_on ,
A.brand_name,
A.category_name ,
A.product_type ,
A.sku sku,
A.sku_name ,
A.reseller_name ,
A.reseller_email ,
A.reseller_mobile ,
reseller_tier_name tier_name,
reseller_id ,
B.quantity ,
B.retail_price ,
B.seller_margin ,
B.discounted_price ,
B.additional_discount ,
B.loyalty_discount ,
B.effective_coupon_discount,
B.item_type ,
order_placed_by ,
min( case when A.sku = C.sku
AND cast(transaction_date AS date) = Cast(created_at AS date)
then coalesce(cogs_promo,cogs_non_promo) end) cogs,
shipping_to order_recipient ,
CASE
WHEN brand_name IN ('LUXCRIME',
'SKINTIFIC',
'TRUEVE',
'SANIYE',
'BEAUDELAB',
'BRASOV',
'FACE REPUBLIC',
'SKIN1004',
'PREMIERE BEAUTE',
'ALLURA',
'LIPLAPIN',
'ROUNDLAB',
'FACE FLUX',
'DOLLGORAE',
'SKINUA',
'PUREFORET',
'SKINTIFIC',
'OHMYSKIN',
'FEAT FOR SKIN',
'SECONDATE',
'KYND',
'PURNAMA',
'BASE',
'LAVIE LASH',
'REI SKIN',
'USTRAA',
'BRUNBRUN PARISGLOWINC',
'SOONHAN',
'THE YEON',
'MIXSOON',
'KOSE COSMEPORT') THEN 'High GM'
WHEN brand_name IN ('W DRESSROOM',
'BEAUSTA',
'Dewycel',
'GLUTANEX',
'HISTOIRE NATURELLE',
'FORENCOS',
'BELLFLOWER',
'MAXCLINIC') THEN 'EL/PL'
END brand_type ,
Customer_type,
BB.gm_target
--min(CASE WHEN A.sku = C.sku
-- AND cast(transaction_date AS date) = Cast(created_at AS date)
-- AND sku_cogs_type= 'COGST_PROMO' THEN cogs WHEN A.sku = C.sku
-- AND cast(transaction_date AS date) = Cast(created_at AS date)
-- AND sku_cogs_type= 'COGST_NON_PROMO' THEN cogs END) cogs
FROM public.gm_dimensions_stage1 A
INNER JOIN public.base_netsuite_final B ON A.external_id =B.external_id
AND A.sku=B.product_sku
LEFT JOIN
(SELECT sku ,
min(cogs_non_promo) cogs_non_promo,
min(cogs_promo) cogs_promo,
--cogs ,
--sku_cogs_type ,
Cast(created_at AS date) created_at
FROM public.sku_cogs_audit
group by sku ,
Cast(created_at AS date) ) C ON A.sku = C.sku
AND cast(transaction_date AS date) = Cast(created_at AS date)
left join (SELECT DISTINCT id order_id,
CASE
WHEN customer_id IS NOT NULL
AND order_placed_by = 'admin' THEN 'Offline Dropshipper'
WHEN customer_id IS NOT NULL
AND order_placed_by <> 'admin' THEN 'Online Dropshipper'
WHEN customer_id IS NULL
AND order_placed_by <> 'admin' THEN 'Online Reseller'
WHEN customer_id IS NULL
AND order_placed_by = 'admin' THEN 'Offline Reseller'
END Customer_type
FROM raena_order_management.order) AA on A.external_id = AA.order_id
left join (
select DISTINCT SKU,gm_target,t.name as tierName
from raena_gross_margin_management.gross_margin_config AA
LEFT JOIN raena_user_management.tier t
ON cast(AA.gm_target_tier as TEXT) =cast(t.id as TEXT)
) BB
on A.sku = BB.sku
and A.reseller_tier_name =BB.tierName
GROUP BY A.external_id ,
B.transaction_date+interval'7 Hours' ,
B.order_discount_amount ,
B.shipping_cost ,
A.shipping_province,
B.coupon_code ,
A.discount_type ,
B.coupon_applied_on ,
A.brand_name,
A.category_name ,
A.product_type ,
A.sku ,
A.sku_name ,
A.reseller_name ,
A.reseller_email ,
A.reseller_mobile ,
reseller_tier_name ,
reseller_id ,
B.quantity ,
B.retail_price ,
B.seller_margin ,
B.discounted_price ,
B.additional_discount ,
B.loyalty_discount ,
B.effective_coupon_discount,
B.item_type ,
order_placed_by,
shipping_to,
case WHEN brand_name IN ('LUXCRIME',
'SKINTIFIC',
'TRUEVE',
'SANIYE',
'BEAUDELAB',
'BRASOV',
'FACE REPUBLIC',
'SKIN 1004',
'PREMIERE BEAUTE',
'ALLURA',
'LIPLAPIN',
'ROUNDLAB',
'FACE FLUX',
'DOLLGORAE',
'SKINUA',
'PUREFORET',
'SKINTIFIC',
'OHMYSKIN',
'FEAT FOR SKIN',
'SECONDATE',
'KYND',
'PURNAMA',
'BASE',
'LAVIE LASH',
'REI SKIN',
'USTRAA',
'BRUNBRUN PARISGLOWINC',
'SOONHAN',
'THE YEON',
'MIXSOON',
'KOSE COSMEPORT') THEN 'High GM'
WHEN brand_name IN ('W DRESSROOM',
'BEAUSTA',
'Dewycel',
'GLUTANEX',
'HISTOIRE NATURELLE',
'FORENCOS',
'BELLFLOWER',
'MAXCLINIC') THEN 'EL/PL'
END,
Customer_type,
BB.gm_target ;
DROP TABLE IF EXISTS public.business_dimensions_stage1;
CREATE TABLE public.business_dimensions_stage1 AS
SELECT A.id AS external_id ,
reseller_id,
customer_id,
cart_id,
A.flash_sale_id,
shipping_to,
payment_status,
A.status,
A.stock_type,
order_placed_by,
order_warehouse,
cast(channel_id as text) channel_id ,
medium,
marketplace,
provider,
coalesce(cast(B.tier_id as varchar),cast(C.tier_id as varchar)) tier_id ,
json_extract_path_text(A.reseller_info,'city',TRUE) reseller_city,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
json_extract_path_text(A.reseller_info,'store',TRUE) reseller_store,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
shipping_address_line1 ,
shipping_address_line2 ,
shipping_pincode ,
shipping_district,
shipping_city ,
shipping_province ,
is_bank_transfer ,
is_campaign ,
discount_type,
shipping_coupon_discount
FROM raena_order_management.order A
left join public.tier_name B on A.id = B.id
left join raena_user_management.user C on cast(A.reseller_id as varchar) = cast(C.id as varchar)
left join raena_order_management.discount_coupon D on A.coupon_code = D.coupon_code
where cast(A.created_at AS date) >='2022-05-01';
DELETE
FROM public.business_report
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.business_dimensions_stage1);
Insert into public.business_report
SELECT A.external_id,
cast(B.transaction_date as date) created_date ,
B.order_discount_amount,
B.shipping_cost ,
coupon_code ,
D.name brand_name,
C.product_type,
E.name category_name,
C.name sku_name,
B.product_sku sku,
reseller_id,
customer_id,
cart_id,
A.flash_sale_id,
shipping_to,
payment_status,
status,
A.stock_type,
order_placed_by,
order_warehouse,
channel_id,
medium,
marketplace,
provider,
reseller_tier_name tier,
reseller_city,
reseller_name,
reseller_email,
reseller_mobile,
reseller_store,
shipping_address_line1 ,
shipping_address_line2 ,
shipping_pincode ,
shipping_district,
shipping_city ,
shipping_province ,
is_bank_transfer ,
is_campaign ,
C.Country,
B.quantity ,
B.retail_price,
seller_margin,
discounted_price,
loyalty_discount,
effective_coupon_discount ,
additional_discount,
item_type
FROM public.business_dimensions_stage1 A
LEFT JOIN public.base_netsuite_final B ON A.external_id = B.external_id
left join raena_catalog_management.product C on B.product_sku = C.sku
left join raena_catalog_management.brand D on C.brand_id = D.id
left join raena_catalog_management.category E on C.category_id = E.id;
DELETE
FROM public.OM_GM_DB_Product_category
WHERE external_Id IN
(SELECT DISTINCT external_id
FROM public.GM_dashboard
WHERE transaction_date >='2022-05-01');
INSERT INTO OM_GM_DB_Product_category
SELECT A.external_id ,
transaction_date,
concat(left(TO_char(transaction_date,'month'),3),date_part('year',transaction_date)) AS Time,
order_discount_amount ,
shipping_cost ,
coupon_code ,
discount_type ,
coupon_applied_on ,
D.brand_name,
D.category_name ,
D.product_type ,
A.sku,
D.name sku_name ,
reseller_name ,
reseller_email ,
reseller_mobile ,
tier_name ,
reseller_id ,
quantity ,
retail_price ,
seller_margin ,
discounted_price ,
additional_discount,
item_type ,
order_placed_by ,
A.cogs,
shipping_province ,
CASE
WHEN A.external_id = C.external_id THEN 'Yes'
ELSE 'No'
END Flag ,
CASE
WHEN tier_name='GOLD' THEN gold_price
WHEN tier_name ='SILVER' THEN silver_price
WHEN tier_name ='BRONZE' THEN bronze_price
ELSE retail_price-seller_margin
END wholesale_price,
order_recipient,
brand_type,
Customer_type,
gm_target
FROM public.GM_dashboard A
LEFT JOIN
(SELECT DISTINCT external_id
FROM
(SELECT external_id,
discounted_price ,
sku ,
(((retail_price-seller_margin)*quantity) -(cogs*quantity))/((retail_price-seller_margin)*quantity) Pre_discount,
CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/(discounted_price*quantity) ELSE 0 END Post_discount
FROM public.GM_dashboard) A
WHERE Post_discount>Pre_discount) C ON A.external_id = C.external_id
LEFT JOIN
(SELECT DISTINCT Sku ,
A.name,
A.product_type,
C.name category_name ,
B.name brand_name
FROM raena_catalog_management.product A
LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id
LEFT JOIN raena_catalog_management.category c ON A.category_id=C.id) D ON A.sku = D.sku
LEFT JOIN pricing_sheet P ON A.sku=p.skucode
WHERE A.cogs IS NOT NULL
AND transaction_date >='2022-05-01';
--Business Dashboard
DELETE
FROM public.GM_GROWTH_TAB1
WHERE created_date >='2022-05-01';
INSERT INTO public.GM_GROWTH_TAB1
SELECT created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
channel_name,
reseller_name,reseller_email,
count(DISTINCT external_id) AS Number_of_Orders ,
sum(retail_price*quantity) RSP ,
sum((retail_price*quantity)-(seller_margin*quantity))wholesale_price ,
sum(discounted_price*quantity) AS Payment_Price ,
sum(effective_coupon_discount*quantity) AS Effective_Coupon_Discount,
sum(loyalty_discount*quantity)Total_Loyalty_point,
sum(additional_discount*quantity)additional_discount,
sum(seller_margin*quantity)seller_margin,
sum(discounted_price*quantity)/count(DISTINCT external_id) AS AOV,
sum(quantity) AS quantity
FROM
(SELECT DISTINCT a.created_date,
'All' AS brand_name,
'All' AS product_type,
'All' AS category_name,
'All' AS sku,
'All' AS sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
b.channel_name,
reseller_name,reseller_email,
external_id,
retail_price,
quantity,
seller_margin,
discounted_price,
effective_coupon_discount,
loyalty_discount,
additional_discount,
row_number() over (partition BY external_id
ORDER BY external_id) AS R
FROM public.business_report a
LEFT JOIN
(SELECT DISTINCT A.id ,
channel_id ,
D.name channel_name
FROM raena_order_management.order A
LEFT JOIN
(SELECT id,
name
FROM raena_order_management.channel) D ON A.channel_id = D.id
WHERE cast(created_at AS date) >= '2022-05-01'
AND channel_id IS NOT NULL) b ON a.external_id=b.id
WHERE cast(is_campaign AS varchar)='false'
AND payment_status='Paid'
AND created_date >= '2022-05-01'
UNION SELECT DISTINCT a.created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
b.channel_name,
reseller_name,reseller_email,
external_id,
retail_price,
quantity,
seller_margin,
discounted_price,
effective_coupon_discount,
loyalty_discount,
additional_discount,
row_number() over (partition BY external_id
ORDER BY external_id) AS R
FROM public.business_report a
LEFT JOIN
(SELECT DISTINCT A.id ,
channel_id ,
D.name channel_name
FROM raena_order_management.order A
LEFT JOIN
(SELECT id,
name
FROM raena_order_management.channel) D ON A.channel_id = D.id
WHERE cast(created_at AS date) >= '2022-05-01'
AND channel_id IS NOT NULL) b ON a.external_id=b.id
WHERE cast(is_campaign AS varchar)='false'
AND payment_status='Paid'
AND created_date >= '2022-05-01')
GROUP BY created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
channel_name,
reseller_name,reseller_email;
--Metric Trend
DROP TABLE IF EXISTS public.GM_GROWTH_TAB2;
CREATE TABLE public.GM_GROWTH_TAB2 AS
SELECT 'year' AS frequency,
cast(date_part('year',created_date) AS varchar) AS time,
cast(date_part('year',created_date) AS int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'quarter' AS frequency,
To_char(created_date,'quarter') AS month_name,
date_part('quarter',created_date) as sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'month' AS frequency,
To_char(created_date,'month') AS month_name,
date_part('month',created_date) as sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'week' AS frequency,
To_char(created_date,'week') AS month_name,
cast(left(To_char(created_date,'week'),1) as int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'day' AS frequency,
cast(date_part('day',created_date) as varchar) AS month_name,
cast(date_part('day',created_date) AS int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
ORDER BY 3;

1937
Gm_dashboard/Final_GM_v2.sql Normal file

File diff suppressed because it is too large Load Diff

38
Gm_dashboard/OOS_code.log Normal file
View File

@ -0,0 +1,38 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

View File

@ -0,0 +1,47 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
?column?
----------
1
(1 row)
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DELETE 0
INSERT 0 5217

View File

@ -0,0 +1,32 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DELETE 25781
INSERT 0 23943
DROP TABLE
SELECT
DELETE 27102
INSERT 0 25161
DROP TABLE
SELECT
DELETE 28492
INSERT 0 24742
DELETE 27119
INSERT 0 25178
DELETE 28643
INSERT 0 26809
DROP TABLE
SELECT

View File

@ -0,0 +1,53 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
max
----------------------------
2022-07-20 17:46:33.571848
(1 row)
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DELETE 1655
INSERT 0 1655
DROP TABLE
SELECT
DELETE 1651
INSERT 0 1651
DROP TABLE
SELECT
DELETE 1718
INSERT 0 1718
DELETE 1657
INSERT 0 1657
DELETE 1903
INSERT 0 1903
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,881 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
DROP TABLE IF EXISTS public.manual_bundle_sku_data ;
CREATE TABLE public.manual_bundle_sku_data AS
SELECT upper(bundle_sku) Parent_sku ,
Upper(A.sku) child_sku ,
A.quantity child_quantity,
'Bundle' parent_sku_class,
cast(B.retail_price AS decimal(22,2)) bundle_retail_price,
cast(json_extract_path_text(C.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) AS decimal(22,2)) bronze_price,
cast(json_extract_path_text(C.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) AS decimal(22,2)) silver_price,
cast(json_extract_path_text(C.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price,
cast(C.retail_price AS decimal(22,2)) child_retail_price
FROM bundle_data_manual_new A
INNER JOIN raena_catalog_management.product B ON upper(A.bundle_sku) = upper(B.sku)
INNER JOIN raena_catalog_management.product C ON upper(A.sku) = upper(C.sku);
DROP TABLE IF EXISTS public.order_level_data;
CREATE TABLE public.order_level_data AS
SELECT external_id,
transaction_date AS transaction_date,
discount_amount,
shipping_cost,
A.coupon_code,
reseller_tier_name,
CASE
WHEN flash_sale_id IS NOT NULL THEN 'Flash'
END Product_type,
order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount,
coupon_applied_on
FROM
(SELECT A.id AS external_id ,
(A.created_at) AS transaction_date ,
A.discount_amount,
applied_shipping_amount shipping_cost,
A.coupon_code,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
flash_sale_id,
loyalty_discount AS order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount
FROM raena_order_management.order A
WHERE payment_status='Paid'
AND cast(A.created_at AS date) >='$reportDate'
AND is_campaign = 'false') A
LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code;
DROP TABLE IF EXISTS public.base_netsuite_stage1_V1;
CREATE TABLE public.base_netsuite_stage1_V1 AS
SELECT DISTINCT transaction_date,
A.external_id,
CASE
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_sku
WHEN B.id = F.sales_sub_order_id THEN F.sku
ELSE B.parent_sku
END sku,
B.parent_sku parent_sku,
CASE
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_quantity*B.quantity
WHEN B.id = F.sales_sub_order_id THEN F.quantity
ELSE B.quantity
END quantity,
B.quantity parent_quantity,
CASE
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_retail_price
WHEN B.id = F.sales_sub_order_id THEN F.retail_price
ELSE B.retail_price
END retail_price,
B.retail_price parent_retail_price,
CASE
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'BRONZE' THEN bronze_price
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'SILVER' THEN silver_price
WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'GOLD' THEN gold_price
when B.id = F.sales_sub_order_id THEN F.effective_wholesale_price
ELSE B.effective_wholesale_price
END effective_wholesale_price,
B.effective_wholesale_price parent_wholesale_price,
CASE
WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount
ELSE B.coupon_discount
END discount_price,
coupon_applied_on,
A.discount_amount,
0 Dynamic_price ,
0 parent_dynamic_price,
case when B.id = F.sales_sub_order_id THEN F.payment_amount
else B.payment_amount end payment_price,
A.payment_amount,
B.product_class,
coalesce(F.product_class,B.product_class) parent_product_class,
CASE
WHEN A.product_type = 'Flash'
AND B.parent_sku LIKE 'BAZ%' THEN 'Flash Bundle'
WHEN A.product_type = 'Flash'
AND B.parent_sku NOT LIKE 'BAZ%' THEN 'Flash'
WHEN B.parent_sku LIKE 'BAZ%' THEN 'Bundle'
ELSE 'Regular'
END AS product_type_class,
F.sales_sub_order_id,
A.order_loyalty_discount,
B.loyalty_discount,
reseller_tier_name
FROM public.order_level_data A
LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id
LEFT JOIN public.manual_bundle_sku_data D ON cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
LEFT JOIN
(SELECT id ,
sales_sub_order_id ,
retail_price,
Sku ,
quantity,
effective_wholesale_price,
coupon_discount,
payment_amount,
product_class,
loyalty_discount
FROM raena_order_management.sales_sub_order_parent_child ssopc
WHERE product_class = 'Bundle'
AND cast(created_at AS date)>='$reportDate') F ON B.id = F.sales_sub_order_id
ORDER BY 1,2;
DROP TABLE IF EXISTS public.loyalty_point_calculation1;
CREATE TABLE public.loyalty_point_calculation1 AS
SELECT *,
sum(order_loyalty_discount)over(partition BY external_id)/count(external_id)over(partition BY external_id) total_order_loyalty_discount ,
sum(loyalty_discount) over(partition BY external_id) total_sku_loyalty_discount
FROM public.base_netsuite_stage1
WHERE order_loyalty_discount >0;
DROP TABLE IF EXISTS public.final_loyalty_point;
CREATE TABLE public.final_loyalty_point AS
SELECT A.*,
CASE
WHEN sum(gold) over (partition BY external_id)>0
AND reseller_tier_name = 'GOLD' THEN gold*total_order_loyalty_discount/sum(gold) over (partition BY external_id)
WHEN sum(SILVER) over (partition BY external_id)>0
AND reseller_tier_name = 'SILVER' THEN SILVER*total_order_loyalty_discount/sum(SILVER) over (partition BY external_id)
WHEN sum(BRONZE) over (partition BY external_id)>0
AND reseller_tier_name = 'BRONZE' THEN bronze*total_order_loyalty_discount/sum(BRONZE) over (partition BY external_id)
END Final_loyalty_point
FROM public.loyalty_point_calculation1 A
LEFT JOIN loyalty_discount B ON A.sku = B.sku
WHERE total_order_loyalty_discount-total_sku_loyalty_discount NOT BETWEEN -10 AND 10;
DROP TABLE IF EXISTS public.base_netsuite_stage2_v1 ;
CREATE TABLE public.base_netsuite_stage2_v1 AS
SELECT transaction_date,
A.external_id,
A.sku,
parent_sku,
quantity,
parent_quantity,
retail_price,
parent_retail_price,
effective_wholesale_price,
parent_wholesale_price,
discount_price,
coupon_applied_on,
discount_amount,
dynamic_price,
parent_dynamic_price,
payment_price,
payment_amount,
product_class,
parent_product_class,
product_type_class,
sales_sub_order_id,
CASE
WHEN cast(transaction_date AS date)< '2022-02-28'
AND A.external_id = B.external_id THEN B.final_loyalty_point
ELSE A.loyalty_discount
END loyalty_discount
FROM public.base_netsuite_stage1_V1 A
LEFT JOIN
(SELECT external_id ,
sku ,
final_loyalty_point
FROM public.final_loyalty_point) B ON A.external_id = B.external_id
AND A.sku = B.sku;
DROP TABLE IF EXISTS public.base_netsuite_stage3_v1 ;
CREATE TABLE public.base_netsuite_stage3_v1 AS
SELECT A.* ,
CASE
WHEN dynamic_price>0 THEN dynamic_price
ELSE effective_wholesale_price
END final_wholesale_price,
CASE
WHEN coupon_applied_on <> 'Cart'
AND discount_price>0 THEN retail_price*quantity*discount_amount/sum(CASE WHEN discount_price>0 THEN retail_price*quantity END)over(partition BY A.external_id)
WHEN coupon_applied_on <> 'Cart'
AND discount_price=0 THEN 0
WHEN coupon_applied_on ='Cart'
AND discount_price=0 THEN retail_price*quantity*discount_amount/sum(retail_price*quantity)over(partition BY A.external_id)
ELSE discount_price
END Final_discount ,
retail_price*quantity-(CASE
WHEN dynamic_price>0 THEN dynamic_price
ELSE effective_wholesale_price
END)*quantity AS seller_margin
FROM public.base_netsuite_stage2_v1 A ;
DROP TABLE IF EXISTS public.base_netsuite_stage4_v1 ;
CREATE TABLE public.base_netsuite_stage4_v1 AS
SELECT A.* ,
CASE
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount>0
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(retail_price AS decimal(22,2))*quantity*(ttl_retail_price-(parent_retail_price*parent_quantity)))/(ttl_retail_price)
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount=0
AND (parent_dynamic_price =0
OR parent_dynamic_price IS NULL)
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(final_wholesale_price AS decimal(22,2))*quantity*(ttl_wholesale_price-(parent_wholesale_price*parent_quantity)))/ttl_wholesale_price
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount=0
AND parent_dynamic_price>0
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(final_wholesale_price AS decimal(22,2))*quantity*(ttl_wholesale_price-ttl_parent_dynamic_price))/ttl_wholesale_price
END AS additional_discount,
CASE
WHEN final_discount >0 THEN cast(final_discount AS decimal(22,2))-cast(seller_margin AS decimal(22,2))
END effective_coupon_discount
FROM public.base_netsuite_stage3_v1 A
LEFT JOIN
(SELECT external_id ,
parent_sku ,
round(sum(retail_price*quantity))ttl_retail_price,
round(sum(final_wholesale_price*quantity))ttl_wholesale_price,
min(CASE WHEN parent_dynamic_price>0 THEN parent_dynamic_price END)ttl_parent_dynamic_price
FROM public.base_netsuite_stage3_v1
WHERE parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle')
GROUP BY external_id ,
parent_sku) B ON A.external_id = B.external_id
AND A.parent_sku = B.parent_sku;
DELETE
FROM public.base_netsuite_final
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.order_level_data);
INSERT INTO public.base_netsuite_final
SELECT A.external_id,
A.transaction_date ,
A.discount_amount order_discount_amount,
A.shipping_cost ,
coupon_code ,
A.coupon_applied_on ,
reseller_tier_name tier,
B.sku product_sku,
B.quantity ,
retail_price,
CASE
WHEN A.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku THEN (seller_margin+diff)/B.quantity
ELSE B.seller_margin/B.quantity
END seller_margin,
((coalesce(retail_price,0)*quantity)
-(coalesce(CASE WHEN A.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku THEN (seller_margin+diff)ELSE seller_margin END ,0))
-coalesce(CASE WHEN A.external_id= 'OD1641992277895310REG' THEN 180000 ELSE effective_coupon_discount END,0)
-(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price,
cast(final_discount AS decimal(22,2))/quantity coupon_discount,
CASE
WHEN A.external_id= 'OD1641992277895310REG' THEN 1200
ELSE effective_coupon_discount/quantity
END effective_coupon_discount ,
loyalty_discount/quantity loyalty_discount,
coalesce(additional_discount,0)/quantity additional_discount,
CASE
WHEN (parent_product_class = 'Bundle'
AND product_type_class = 'Flash')
OR product_type_class = 'Flash Bundle' THEN 'Flash Bundle'
WHEN (parent_product_class = 'Flash'
OR product_type_class = 'Flash') THEN 'Flash'
WHEN (parent_product_class = 'Bundle'
OR product_type_class = 'Bundle')THEN 'Bundle'
ELSE 'Regular'
END item_type,
diff
FROM public.order_level_data A
LEFT JOIN public.base_netsuite_stage4_v1 B ON A.external_id = B.external_id
LEFT JOIN
(SELECT external_id ,
sku ,
parent_sku ,
seller_margin * diff/sum(seller_margin)over(partition BY external_id) diff
FROM public.base_netsuite_stage4_v1 A
INNER JOIN public.order_check_table B ON A.external_id = B.order_id)C ON B.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku ;
DROP TABLE IF EXISTS public.gm_dimensions_stage1;
CREATE TABLE public.gm_dimensions_stage1 AS
SELECT A.id AS external_id ,
shipping_province,
reseller_id ,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
discount_type,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
C.sku ,
shipping_to,
C.name sku_name,
brand_name ,
product_type ,
category_name,
order_placed_by
FROM raena_order_management.order A
LEFT JOIN raena_order_management.discount_coupon B ON A.coupon_code = B.coupon_code
LEFT JOIN raena_order_management.order_item C ON A.id = C.order_id
WHERE payment_status ='Paid'
AND A.is_campaign = 'false'
AND cast(A.created_at AS date) >='$reportDate';
DELETE
FROM public.GM_dashboard
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.gm_dimensions_stage1);
INSERT INTO public.GM_dashboard
SELECT A.external_id ,
B.transaction_date+interval'7 Hours' transaction_date,
B.order_discount_amount ,
B.shipping_cost ,
A.shipping_province,
B.coupon_code ,
A.discount_type ,
B.coupon_applied_on ,
A.brand_name,
A.category_name ,
A.product_type ,
A.sku sku,
A.sku_name ,
A.reseller_name ,
A.reseller_email ,
A.reseller_mobile ,
reseller_tier_name tier_name,
reseller_id ,
B.quantity ,
B.retail_price ,
B.seller_margin ,
B.discounted_price ,
B.additional_discount ,
B.loyalty_discount ,
B.effective_coupon_discount,
B.item_type ,
order_placed_by ,
min( case when A.sku = C.sku
AND cast(transaction_date AS date) = Cast(created_at AS date)
then coalesce(cogs_promo,cogs_non_promo) end) cogs,
shipping_to order_recipient ,
CASE
WHEN brand_name IN ('LUXCRIME',
'SKINTIFIC',
'TRUEVE',
'SANIYE',
'BEAUDELAB',
'BRASOV',
'FACE REPUBLIC',
'SKIN1004',
'PREMIERE BEAUTE',
'ALLURA',
'LIPLAPIN',
'ROUNDLAB',
'FACE FLUX',
'DOLLGORAE',
'SKINUA',
'PUREFORET',
'SKINTIFIC',
'OHMYSKIN',
'FEAT FOR SKIN',
'SECONDATE',
'KYND',
'PURNAMA',
'BASE',
'LAVIE LASH',
'REI SKIN',
'USTRAA',
'BRUNBRUN PARISGLOWINC',
'SOONHAN',
'THE YEON',
'MIXSOON',
'KOSE COSMEPORT') THEN 'High GM'
WHEN brand_name IN ('W DRESSROOM',
'BEAUSTA',
'Dewycel',
'GLUTANEX',
'HISTOIRE NATURELLE',
'FORENCOS',
'BELLFLOWER',
'MAXCLINIC') THEN 'EL/PL'
END brand_type ,
Customer_type,
BB.gm_target
--min(CASE WHEN A.sku = C.sku
-- AND cast(transaction_date AS date) = Cast(created_at AS date)
-- AND sku_cogs_type= 'COGST_PROMO' THEN cogs WHEN A.sku = C.sku
-- AND cast(transaction_date AS date) = Cast(created_at AS date)
-- AND sku_cogs_type= 'COGST_NON_PROMO' THEN cogs END) cogs
FROM public.gm_dimensions_stage1 A
INNER JOIN public.base_netsuite_final B ON A.external_id =B.external_id
AND A.sku=B.product_sku
LEFT JOIN
(SELECT sku ,
min(cogs_non_promo) cogs_non_promo,
min(cogs_promo) cogs_promo,
--cogs ,
--sku_cogs_type ,
Cast(created_at AS date) created_at
FROM public.sku_cogs_audit
group by sku ,
Cast(created_at AS date) ) C ON A.sku = C.sku
AND cast(transaction_date AS date) = Cast(created_at AS date)
left join (SELECT DISTINCT id order_id,
CASE
WHEN customer_id IS NOT NULL
AND order_placed_by = 'admin' THEN 'Offline Dropshipper'
WHEN customer_id IS NOT NULL
AND order_placed_by <> 'admin' THEN 'Online Dropshipper'
WHEN customer_id IS NULL
AND order_placed_by <> 'admin' THEN 'Online Reseller'
WHEN customer_id IS NULL
AND order_placed_by = 'admin' THEN 'Offline Reseller'
END Customer_type
FROM raena_order_management.order) AA on A.external_id = AA.order_id
left join (
select DISTINCT SKU,gm_target,t.name as tierName
from raena_gross_margin_management.gross_margin_config AA
LEFT JOIN raena_user_management.tier t
ON cast(AA.gm_target_tier as TEXT) =cast(t.id as TEXT)
) BB
on A.sku = BB.sku
and A.reseller_tier_name =BB.tierName
GROUP BY A.external_id ,
B.transaction_date+interval'7 Hours' ,
B.order_discount_amount ,
B.shipping_cost ,
A.shipping_province,
B.coupon_code ,
A.discount_type ,
B.coupon_applied_on ,
A.brand_name,
A.category_name ,
A.product_type ,
A.sku ,
A.sku_name ,
A.reseller_name ,
A.reseller_email ,
A.reseller_mobile ,
reseller_tier_name ,
reseller_id ,
B.quantity ,
B.retail_price ,
B.seller_margin ,
B.discounted_price ,
B.additional_discount ,
B.loyalty_discount ,
B.effective_coupon_discount,
B.item_type ,
order_placed_by,
shipping_to,
case WHEN brand_name IN ('LUXCRIME',
'SKINTIFIC',
'TRUEVE',
'SANIYE',
'BEAUDELAB',
'BRASOV',
'FACE REPUBLIC',
'SKIN 1004',
'PREMIERE BEAUTE',
'ALLURA',
'LIPLAPIN',
'ROUNDLAB',
'FACE FLUX',
'DOLLGORAE',
'SKINUA',
'PUREFORET',
'SKINTIFIC',
'OHMYSKIN',
'FEAT FOR SKIN',
'SECONDATE',
'KYND',
'PURNAMA',
'BASE',
'LAVIE LASH',
'REI SKIN',
'USTRAA',
'BRUNBRUN PARISGLOWINC',
'SOONHAN',
'THE YEON',
'MIXSOON',
'KOSE COSMEPORT') THEN 'High GM'
WHEN brand_name IN ('W DRESSROOM',
'BEAUSTA',
'Dewycel',
'GLUTANEX',
'HISTOIRE NATURELLE',
'FORENCOS',
'BELLFLOWER',
'MAXCLINIC') THEN 'EL/PL'
END,
Customer_type,
BB.gm_target ;
DROP TABLE IF EXISTS public.business_dimensions_stage1;
CREATE TABLE public.business_dimensions_stage1 AS
SELECT A.id AS external_id ,
reseller_id,
customer_id,
cart_id,
A.flash_sale_id,
shipping_to,
payment_status,
A.status,
A.stock_type,
order_placed_by,
order_warehouse,
cast(channel_id as text) channel_id ,
medium,
marketplace,
provider,
coalesce(cast(B.tier_id as varchar),cast(C.tier_id as varchar)) tier_id ,
json_extract_path_text(A.reseller_info,'city',TRUE) reseller_city,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
json_extract_path_text(A.reseller_info,'store',TRUE) reseller_store,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
shipping_address_line1 ,
shipping_address_line2 ,
shipping_pincode ,
shipping_district,
shipping_city ,
shipping_province ,
is_bank_transfer ,
is_campaign ,
discount_type,
shipping_coupon_discount
FROM raena_order_management.order A
left join public.tier_name B on A.id = B.id
left join raena_user_management.user C on cast(A.reseller_id as varchar) = cast(C.id as varchar)
left join raena_order_management.discount_coupon D on A.coupon_code = D.coupon_code
where cast(A.created_at AS date) >='$reportDate';
DELETE
FROM public.business_report
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.business_dimensions_stage1);
Insert into public.business_report
SELECT A.external_id,
cast(B.transaction_date as date) created_date ,
B.order_discount_amount,
B.shipping_cost ,
coupon_code ,
D.name brand_name,
C.product_type,
E.name category_name,
C.name sku_name,
B.product_sku sku,
reseller_id,
customer_id,
cart_id,
A.flash_sale_id,
shipping_to,
payment_status,
status,
A.stock_type,
order_placed_by,
order_warehouse,
channel_id,
medium,
marketplace,
provider,
reseller_tier_name tier,
reseller_city,
reseller_name,
reseller_email,
reseller_mobile,
reseller_store,
shipping_address_line1 ,
shipping_address_line2 ,
shipping_pincode ,
shipping_district,
shipping_city ,
shipping_province ,
is_bank_transfer ,
is_campaign ,
C.Country,
B.quantity ,
B.retail_price,
seller_margin,
discounted_price,
loyalty_discount,
effective_coupon_discount ,
additional_discount,
item_type
FROM public.business_dimensions_stage1 A
LEFT JOIN public.base_netsuite_final B ON A.external_id = B.external_id
left join raena_catalog_management.product C on B.product_sku = C.sku
left join raena_catalog_management.brand D on C.brand_id = D.id
left join raena_catalog_management.category E on C.category_id = E.id;
DELETE
FROM public.OM_GM_DB_Product_category
WHERE external_Id IN
(SELECT DISTINCT external_id
FROM public.GM_dashboard
WHERE transaction_date >='$reportDate');
INSERT INTO OM_GM_DB_Product_category
SELECT A.external_id ,
transaction_date,
concat(left(TO_char(transaction_date,'month'),3),date_part('year',transaction_date)) AS Time,
order_discount_amount ,
shipping_cost ,
coupon_code ,
discount_type ,
coupon_applied_on ,
D.brand_name,
D.category_name ,
D.product_type ,
A.sku,
D.name sku_name ,
reseller_name ,
reseller_email ,
reseller_mobile ,
tier_name ,
reseller_id ,
quantity ,
retail_price ,
seller_margin ,
discounted_price ,
additional_discount,
item_type ,
order_placed_by ,
A.cogs,
shipping_province ,
CASE
WHEN A.external_id = C.external_id THEN 'Yes'
ELSE 'No'
END Flag ,
CASE
WHEN tier_name='GOLD' THEN gold_price
WHEN tier_name ='SILVER' THEN silver_price
WHEN tier_name ='BRONZE' THEN bronze_price
ELSE retail_price-seller_margin
END wholesale_price,
order_recipient,
brand_type,
Customer_type,
gm_target
FROM public.GM_dashboard A
LEFT JOIN
(SELECT DISTINCT external_id
FROM
(SELECT external_id,
discounted_price ,
sku ,
(((retail_price-seller_margin)*quantity) -(cogs*quantity))/((retail_price-seller_margin)*quantity) Pre_discount,
CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/(discounted_price*quantity) ELSE 0 END Post_discount
FROM public.GM_dashboard) A
WHERE Post_discount>Pre_discount) C ON A.external_id = C.external_id
LEFT JOIN
(SELECT DISTINCT Sku ,
A.name,
A.product_type,
C.name category_name ,
B.name brand_name
FROM raena_catalog_management.product A
LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id
LEFT JOIN raena_catalog_management.category c ON A.category_id=C.id) D ON A.sku = D.sku
LEFT JOIN pricing_sheet P ON A.sku=p.skucode
WHERE A.cogs IS NOT NULL
AND transaction_date >='$reportDate';
--Business Dashboard
DELETE
FROM public.GM_GROWTH_TAB1
WHERE created_date >='$reportDate';
INSERT INTO public.GM_GROWTH_TAB1
SELECT created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
channel_name,
reseller_name,reseller_email,
count(DISTINCT external_id) AS Number_of_Orders ,
sum(retail_price*quantity) RSP ,
sum((retail_price*quantity)-(seller_margin*quantity))wholesale_price ,
sum(discounted_price*quantity) AS Payment_Price ,
sum(effective_coupon_discount*quantity) AS Effective_Coupon_Discount,
sum(loyalty_discount*quantity)Total_Loyalty_point,
sum(additional_discount*quantity)additional_discount,
sum(seller_margin*quantity)seller_margin,
sum(discounted_price*quantity)/count(DISTINCT external_id) AS AOV,
sum(quantity) AS quantity
FROM
(SELECT DISTINCT a.created_date,
'All' AS brand_name,
'All' AS product_type,
'All' AS category_name,
'All' AS sku,
'All' AS sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
b.channel_name,
reseller_name,reseller_email,
external_id,
retail_price,
quantity,
seller_margin,
discounted_price,
effective_coupon_discount,
loyalty_discount,
additional_discount,
row_number() over (partition BY external_id
ORDER BY external_id) AS R
FROM public.business_report a
LEFT JOIN
(SELECT DISTINCT A.id ,
channel_id ,
D.name channel_name
FROM raena_order_management.order A
LEFT JOIN
(SELECT id,
name
FROM raena_order_management.channel) D ON A.channel_id = D.id
WHERE cast(created_at AS date) >= '$reportDate'
AND channel_id IS NOT NULL) b ON a.external_id=b.id
WHERE cast(is_campaign AS varchar)='false'
AND payment_status='Paid'
AND created_date >= '$reportDate'
UNION SELECT DISTINCT a.created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
b.channel_name,
reseller_name,reseller_email,
external_id,
retail_price,
quantity,
seller_margin,
discounted_price,
effective_coupon_discount,
loyalty_discount,
additional_discount,
row_number() over (partition BY external_id
ORDER BY external_id) AS R
FROM public.business_report a
LEFT JOIN
(SELECT DISTINCT A.id ,
channel_id ,
D.name channel_name
FROM raena_order_management.order A
LEFT JOIN
(SELECT id,
name
FROM raena_order_management.channel) D ON A.channel_id = D.id
WHERE cast(created_at AS date) >= '$reportDate'
AND channel_id IS NOT NULL) b ON a.external_id=b.id
WHERE cast(is_campaign AS varchar)='false'
AND payment_status='Paid'
AND created_date >= '$reportDate')
GROUP BY created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
channel_name,
reseller_name,reseller_email;
--Metric Trend
DROP TABLE IF EXISTS public.GM_GROWTH_TAB2;
CREATE TABLE public.GM_GROWTH_TAB2 AS
SELECT 'year' AS frequency,
cast(date_part('year',created_date) AS varchar) AS time,
cast(date_part('year',created_date) AS int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'quarter' AS frequency,
To_char(created_date,'quarter') AS month_name,
date_part('quarter',created_date) as sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'month' AS frequency,
To_char(created_date,'month') AS month_name,
date_part('month',created_date) as sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'week' AS frequency,
To_char(created_date,'week') AS month_name,
cast(left(To_char(created_date,'week'),1) as int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'day' AS frequency,
cast(date_part('day',created_date) as varchar) AS month_name,
cast(date_part('day',created_date) AS int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
ORDER BY 3;
" > /home/ec2-user/cronjob/Gm_dashboard/Final_GM_v1.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/Gm_dashboard/Final_GM_v1.sql > etlTransaction_job_v1.log

0
OM_New_VS_Existing.log Normal file
View File

35
OM_New_VS_Existing.sh Normal file
View File

@ -0,0 +1,35 @@
#!/bin/bash
echo "
drop table om_New_Existing_flag
create table om_New_Existing_flag
as
select a.*,b.tier
from
select distinct cast(App_install_date as date) as App_install_date,profile_phone,profile_email,case when R_flag=1 then 'New' else 'Existing' end as flag
from
(
select date_trunc('month', App_install_date) - interval '0 month' as App_install_date,profile_phone,profile_email,
row_number() over (partition by profile_phone,date_trunc('month', App_install_date) - interval '0 month' order by date_trunc('month', App_install_date) - interval '0 month') as R,
row_number() over (partition by profile_phone order by date_trunc('month', App_install_date) - interval '0 month') as R_flag
from
(
select cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_install_date,profile_phone,profile_email
from clevertap.app_installed
) where profile_phone!=' '
) where R=1 --and profile_phone in ('6289876543210','6285320252449')
) a
left join
(
select mobile,tier
from (
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,
json_extract_path_text(reseller_info,'tierName',TRUE) as tier,
row_number() over (partition by replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') order by created_at desc) as R_desc
from raena_order_management.order
) where R_desc=1 and mobile notnull
) b on a.profile_phone=b.mobile
" > /home/ec2-user/cronjob/warehouseAnalysis/OM_New_VS_Existing.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z"-f /home/ec2-user/cronjob/warehouseAnalysis/warehouseAnalysis.sql > OM_New_VS_Existing.log

5
OM_Test_Launched.log Normal file
View File

@ -0,0 +1,5 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE

18
OM_Test_launch.sh Normal file
View File

@ -0,0 +1,18 @@
#!/bin/bash
echo "
create table public.om_product_view_reseller_business_metrics_db
as
select distinct --cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date)
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) as App_view_date,
profile_objectid as view_object_id,profile_phone,profile_email
from clevertap.view_item vi
union
select distinct cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) as App_view_date,
profile_objectid as view_object_id,profile_phone,profile_email
from clevertap.view_cart vi
" > /home/ec2-user/cronjob/warehouseAnalysis/OM_Test_Launched.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/warehouseAnalysis/warehouseAnalysis.sql > OM_Test_Launched.log

View File

@ -0,0 +1,19 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
INSERT 0 534956
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

178
OOS/DB_funnel_new_users.sh Normal file
View File

@ -0,0 +1,178 @@
#!/bin/bash
echo "
--------------------------Revenue Leakage -------------------------
drop table if exists om_clevertap_install_jan_may;
create table om_clevertap_install_jan_may
as
select App_install_date,email,phone,user_id
from (
select *,row_number() over (partition by user_id order by app_install_date) as R
from
(
SELECT '2022-04-01' as App_install_date,email,phone,email as user_id FROM public.clevertap_april_csv
union
SELECT '2022-03-30' as App_install_date,email,phone,email as user_id FROM public.clevertap_march_csv where phone not in (select distinct profile_phone from clevertap.app_installed)
union
select cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_install_date,profile_email,profile_phone,
case when profile_email=' ' then profile_objectid else profile_email end as user_id
from clevertap.app_installed
)
) where R=1 ;--and App_install_date>='2022-01-01'
drop table if exists om_clevertap_install_jan_may_2;
create table om_clevertap_install_jan_may_2
as
select user_id,email,phone,to_char(app_install_date,'month') as month,date_part(day,app_install_date) as day,
address_line1,address_line2,city,province,app_install_date
from (
select a.user_id,a.email,phone,case when app_install_date='2022-03-30' or app_install_date='2022-04-01' then cast(b.created_at as date) else app_install_date end as app_install_date,
coalesce(address_line1,customer_address_line1) as address_line1,coalesce(address_line2,customer_address_line2) as address_line2,
coalesce(city,customer_city) as city,coalesce(province,customer_province) as province
from om_clevertap_install_jan_may a
left join (
select mobile,email,created_at,address_line1,address_line2,city,province
from (
select email,replace(mobile,'+','') as mobile,cast(created_at as date) as created_at,address_line1,address_line2,city,province,
row_number() over (partition by email order by created_at) as R
from raena_user_management.user
)
where R=1 and email notnull) b on a.email=b.email and a.app_install_date<=b.created_at
left join (
select distinct user_id,email,address_line1 as customer_address_line1,address_line2 as customer_address_line2,city as customer_city,province as customer_province,
row_number() over (partition by email order by created_at) as R,cast(created_at as date) as created_at
from raena_user_management.customer
) c on a.email=c.email and c.R=1 and a.app_install_date<=b.created_at
);
--select count(*) from om_clevertap_install_jan_may_2 --494190
drop table if exists om_clevertap_install_jan_may_3;
create table om_clevertap_install_jan_may_3
as
select a.*,cast(b.created_at as date) as transaction_date,transacted_email
--case when cast(b.created_at as date) isnull then 'Never transacted'
--when cast(b.created_at as date)>=app_install_date and cast(b.created_at as date)<=cast(dateadd(day,30,app_install_date) as date) then 'Yes' else 'No' end as transacted_flag
from om_clevertap_install_jan_may_2 a
left join (select json_extract_path_text(reseller_info,'email',TRUE) as transacted_email ,min(created_at) as created_at
from raena_order_management.order where payment_status='Paid' and cast(is_archived as varchar)='false' group by 1 ) b
on a.email=transacted_email;
----select count(*) from om_clevertap_install_jan_may_3 --495191
drop table if exists om_clevertap_install_jan_may_final;
create table om_clevertap_install_jan_may_final
as
select *,case when email notnull and rtrim(ltrim(email)) != '' then 'Yes' else 'No' end as email_flag,
case when phone notnull and rtrim(ltrim(phone)) != '' then 'Yes' else 'No' end as phone_flag,
case when address_line1 notnull and rtrim(ltrim(address_line1)) != '' then 'Yes' else 'No' end as address_flag,
case when transacted_email notnull and rtrim(ltrim(transacted_email)) != '' and transaction_date>=app_install_date and transaction_date<=cast(dateadd(day,30,app_install_date) as date)
then 'Yes' else 'No' end as transacted_flag
from om_clevertap_install_jan_may_3 ;
--order data
drop table if exists om_clevertap_install_jan_may_order;
create table om_clevertap_install_jan_may_order
as
select * from om_clevertap_install_jan_may_final where transacted_email notnull and rtrim(ltrim(transacted_email)) != ''; ;
Insert into Test_1_week_om_clevertap_install_jan_may_final
select *,current_date-1 as Report_date
from om_clevertap_install_jan_may_final;
drop table if exists public.om_reseller_info;
create table public.om_reseller_info
as
select distinct json_extract_path_text(reseller_info,'mobile',TRUE) as reseller_mobile
,json_extract_path_text(reseller_info,'email',TRUE) as reseller_email,reseller_id
from raena_order_management.order
where id like '%DSF';
drop table if exists public.OM_Order_id_payment_id;
CREATE TABLE public.OM_Order_id_payment_id AS WITH NS AS
(SELECT 1 AS n
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
UNION ALL SELECT 10
UNION ALL SELECT 11
UNION ALL SELECT 12
UNION ALL SELECT 13
UNION ALL SELECT 14
UNION ALL SELECT 15
UNION ALL SELECT 16
UNION ALL SELECT 17
UNION ALL SELECT 18
UNION ALL SELECT 19
UNION ALL SELECT 20
UNION ALL SELECT 21
UNION ALL SELECT 22
UNION ALL SELECT 23
UNION ALL SELECT 24
UNION ALL SELECT 25)
SELECT id,
max(created_at) created_at,
replace(cast(substring(cast(TRIM(SPLIT_PART(B.order_ids , ',', NS.n))AS varchar),2,length(cast(TRIM(SPLIT_PART(B.order_ids , ',', NS.n))AS varchar))-2) AS varchar),'“','') AS order_id
FROM NS
INNER JOIN raena_order_management.payment B ON NS.n <= REGEXP_COUNT(B.order_ids , ',') + 1
GROUP BY 1,
3;
drop table if exists OM_Events_user_type;
create table OM_Events_user_type
as
select clevertap.clevertap_master_data.*,
CASE WHEN date_trunc('Month',clevertap.clevertap_master_data.ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',clevertap.clevertap_master_data.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
WHEN date_trunc('Month',clevertap.clevertap_master_data.ts)::date-date_trunc('Month',first_install_date)::date>= 30
AND date_trunc('Month',clevertap.clevertap_master_data.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation'
WHEN date_trunc('Month',clevertap.clevertap_master_data.ts)::date-date_trunc('Month',first_install_date)::date>=30
AND date_trunc('Month',clevertap.clevertap_master_data.ts)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing'
else 'Na' end AS New_existing_flag
from clevertap.clevertap_master_data
left join user_type_table flag ON replace(clevertap.clevertap_master_data.phone,'+','')=flag.reseller_mobile
where events in ('add_to_cart','view_item','dsfpage_pdp_view' ,'dsfpage_submit','pdp_setmargin','dsfpage_chatwithseller','dsfpage_buynow',
'share_whatsapp','share_generic','share_facebook','share_tiktok','share_instagram','video_page_play','reviews_show_all','video_page_productclick',
'video_page_instagram','video_page_whatsapp','video_page_generic','video_page_tiktok','video_page_facebook','video_page_play');
drop table if exists public.om_order_brand_type;
create table public.om_order_brand_type
as
select distinct sso.order_id,coalesce(ssopc.sku,parent_sku) as sku,
case when b.name in ('HISTOIRE NATURELLE','INGRID','W DRESSROOM','FORENCOS','DEWYCEL','GLUTANEX','BELLFLOWER','ONE THING','BEAUSTA') then 'EL/PL'
when b.name in ('LUXCRIME','SKINTIFIC','TRUEVE','SANIYE','BEAUDELAB', 'BRASOV', 'FACE REPUBLIC', 'SKIN1004', 'PREMIERE BEAUTE', 'ALLURA', 'LIPLAPIN', 'ROUNDLAB',
'FACE FLUX','DOLLGORAE','SKINUA', 'PUREFORET','SKINTIFIC', 'OHMYSKIN', 'FEAT FOR SKIN', 'SECONDATE', 'KYND', 'PURNAMA', 'BASE',
'LAVIE LASH', 'REI SKIN', 'USTRAA', 'BRUNBRUN PARISGLOWINC', 'SOONHAN', 'THE YEON', 'MIXSOON','KOSE COSMEPORT') THEN 'High GM'
end Brand_type,
b.name as brand_name
from raena_order_management.sales_sub_order sso
left join raena_order_management.sales_sub_order_parent_child ssopc on sso.id=ssopc.sales_sub_order_id
inner join raena_catalog_management.product p on coalesce(ssopc.sku,parent_sku)=p.sku
inner join raena_catalog_management.brand b on p.brand_id=b.id;
" > /home/ec2-user/cronjob/OOS/DB_funnel_new_users.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f/home/ec2-user/cronjob/OOS/DB_funnel_new_users.sql > DB_funnel_new_users.log

174
OOS/DB_funnel_new_users.sql Normal file
View File

@ -0,0 +1,174 @@
--------------------------Revenue Leakage -------------------------
drop table if exists om_clevertap_install_jan_may;
create table om_clevertap_install_jan_may
as
select App_install_date,email,phone,user_id
from (
select *,row_number() over (partition by user_id order by app_install_date) as R
from
(
SELECT '2022-04-01' as App_install_date,email,phone,email as user_id FROM public.clevertap_april_csv
union
SELECT '2022-03-30' as App_install_date,email,phone,email as user_id FROM public.clevertap_march_csv where phone not in (select distinct profile_phone from clevertap.app_installed)
union
select cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_install_date,profile_email,profile_phone,
case when profile_email=' ' then profile_objectid else profile_email end as user_id
from clevertap.app_installed
)
) where R=1 ;--and App_install_date>='2022-01-01'
drop table if exists om_clevertap_install_jan_may_2;
create table om_clevertap_install_jan_may_2
as
select user_id,email,phone,to_char(app_install_date,'month') as month,date_part(day,app_install_date) as day,
address_line1,address_line2,city,province,app_install_date
from (
select a.user_id,a.email,phone,case when app_install_date='2022-03-30' or app_install_date='2022-04-01' then cast(b.created_at as date) else app_install_date end as app_install_date,
coalesce(address_line1,customer_address_line1) as address_line1,coalesce(address_line2,customer_address_line2) as address_line2,
coalesce(city,customer_city) as city,coalesce(province,customer_province) as province
from om_clevertap_install_jan_may a
left join (
select mobile,email,created_at,address_line1,address_line2,city,province
from (
select email,replace(mobile,'+','') as mobile,cast(created_at as date) as created_at,address_line1,address_line2,city,province,
row_number() over (partition by email order by created_at) as R
from raena_user_management.user
)
where R=1 and email notnull) b on a.email=b.email and a.app_install_date<=b.created_at
left join (
select distinct user_id,email,address_line1 as customer_address_line1,address_line2 as customer_address_line2,city as customer_city,province as customer_province,
row_number() over (partition by email order by created_at) as R,cast(created_at as date) as created_at
from raena_user_management.customer
) c on a.email=c.email and c.R=1 and a.app_install_date<=b.created_at
);
--select count(*) from om_clevertap_install_jan_may_2 --494190
drop table if exists om_clevertap_install_jan_may_3;
create table om_clevertap_install_jan_may_3
as
select a.*,cast(b.created_at as date) as transaction_date,transacted_email
--case when cast(b.created_at as date) isnull then 'Never transacted'
--when cast(b.created_at as date)>=app_install_date and cast(b.created_at as date)<=cast(dateadd(day,30,app_install_date) as date) then 'Yes' else 'No' end as transacted_flag
from om_clevertap_install_jan_may_2 a
left join (select json_extract_path_text(reseller_info,'email',TRUE) as transacted_email ,min(created_at) as created_at
from raena_order_management.order where payment_status='Paid' and cast(is_archived as varchar)='false' group by 1 ) b
on a.email=transacted_email;
----select count(*) from om_clevertap_install_jan_may_3 --495191
drop table if exists om_clevertap_install_jan_may_final;
create table om_clevertap_install_jan_may_final
as
select *,case when email notnull and rtrim(ltrim(email)) != '' then 'Yes' else 'No' end as email_flag,
case when phone notnull and rtrim(ltrim(phone)) != '' then 'Yes' else 'No' end as phone_flag,
case when address_line1 notnull and rtrim(ltrim(address_line1)) != '' then 'Yes' else 'No' end as address_flag,
case when transacted_email notnull and rtrim(ltrim(transacted_email)) != '' and transaction_date>=app_install_date and transaction_date<=cast(dateadd(day,30,app_install_date) as date)
then 'Yes' else 'No' end as transacted_flag
from om_clevertap_install_jan_may_3 ;
--order data
drop table if exists om_clevertap_install_jan_may_order;
create table om_clevertap_install_jan_may_order
as
select * from om_clevertap_install_jan_may_final where transacted_email notnull and rtrim(ltrim(transacted_email)) != ''; ;
Insert into Test_1_week_om_clevertap_install_jan_may_final
select *,current_date-1 as Report_date
from om_clevertap_install_jan_may_final;
drop table if exists public.om_reseller_info;
create table public.om_reseller_info
as
select distinct json_extract_path_text(reseller_info,'mobile',TRUE) as reseller_mobile
,json_extract_path_text(reseller_info,'email',TRUE) as reseller_email,reseller_id
from raena_order_management.order
where id like '%DSF';
drop table if exists public.OM_Order_id_payment_id;
CREATE TABLE public.OM_Order_id_payment_id AS WITH NS AS
(SELECT 1 AS n
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
UNION ALL SELECT 10
UNION ALL SELECT 11
UNION ALL SELECT 12
UNION ALL SELECT 13
UNION ALL SELECT 14
UNION ALL SELECT 15
UNION ALL SELECT 16
UNION ALL SELECT 17
UNION ALL SELECT 18
UNION ALL SELECT 19
UNION ALL SELECT 20
UNION ALL SELECT 21
UNION ALL SELECT 22
UNION ALL SELECT 23
UNION ALL SELECT 24
UNION ALL SELECT 25)
SELECT id,
max(created_at) created_at,
replace(cast(substring(cast(TRIM(SPLIT_PART(B.order_ids , ',', NS.n))AS varchar),2,length(cast(TRIM(SPLIT_PART(B.order_ids , ',', NS.n))AS varchar))-2) AS varchar),'','') AS order_id
FROM NS
INNER JOIN raena_order_management.payment B ON NS.n <= REGEXP_COUNT(B.order_ids , ',') + 1
GROUP BY 1,
3;
drop table if exists OM_Events_user_type;
create table OM_Events_user_type
as
select clevertap.clevertap_master_data.*,
CASE WHEN date_trunc('Month',clevertap.clevertap_master_data.ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',clevertap.clevertap_master_data.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
WHEN date_trunc('Month',clevertap.clevertap_master_data.ts)::date-date_trunc('Month',first_install_date)::date>= 30
AND date_trunc('Month',clevertap.clevertap_master_data.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation'
WHEN date_trunc('Month',clevertap.clevertap_master_data.ts)::date-date_trunc('Month',first_install_date)::date>=30
AND date_trunc('Month',clevertap.clevertap_master_data.ts)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing'
else 'Na' end AS New_existing_flag
from clevertap.clevertap_master_data
left join user_type_table flag ON replace(clevertap.clevertap_master_data.phone,'+','')=flag.reseller_mobile
where events in ('add_to_cart','view_item','dsfpage_pdp_view' ,'dsfpage_submit','pdp_setmargin','dsfpage_chatwithseller','dsfpage_buynow',
'share_whatsapp','share_generic','share_facebook','share_tiktok','share_instagram','video_page_play','reviews_show_all','video_page_productclick',
'video_page_instagram','video_page_whatsapp','video_page_generic','video_page_tiktok','video_page_facebook','video_page_play');
drop table if exists public.om_order_brand_type;
create table public.om_order_brand_type
as
select distinct sso.order_id,coalesce(ssopc.sku,parent_sku) as sku,
case when b.name in ('HISTOIRE NATURELLE','INGRID','W DRESSROOM','FORENCOS','DEWYCEL','GLUTANEX','BELLFLOWER','ONE THING','BEAUSTA') then 'EL/PL'
when b.name in ('LUXCRIME','SKINTIFIC','TRUEVE','SANIYE','BEAUDELAB', 'BRASOV', 'FACE REPUBLIC', 'SKIN1004', 'PREMIERE BEAUTE', 'ALLURA', 'LIPLAPIN', 'ROUNDLAB',
'FACE FLUX','DOLLGORAE','SKINUA', 'PUREFORET','SKINTIFIC', 'OHMYSKIN', 'FEAT FOR SKIN', 'SECONDATE', 'KYND', 'PURNAMA', 'BASE',
'LAVIE LASH', 'REI SKIN', 'USTRAA', 'BRUNBRUN PARISGLOWINC', 'SOONHAN', 'THE YEON', 'MIXSOON','KOSE COSMEPORT') THEN 'High GM'
end Brand_type,
b.name as brand_name
from raena_order_management.sales_sub_order sso
left join raena_order_management.sales_sub_order_parent_child ssopc on sso.id=ssopc.sales_sub_order_id
inner join raena_catalog_management.product p on coalesce(ssopc.sku,parent_sku)=p.sku
inner join raena_catalog_management.brand b on p.brand_id=b.id;

38
OOS/OOS_code.log Normal file
View File

@ -0,0 +1,38 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

427
OOS/OOS_code.sh Normal file
View File

@ -0,0 +1,427 @@
#!/bin/bash
echo "
--------------------------OOS -------------------------
drop table if exists public.om_oos_soldout_1;
create table public.om_oos_soldout_1
as
select snp_date_time,sku_code,inventory
from (
select snp_date,snp_time,snp_date_time,sku_code,inventory,
row_number() over (partition by sku_code,cast(snp_date_time as date) order by snp_date_time desc,inventory desc) as R
from (
select distinct snp_date,snp_time,dateadd(hour,7,cast(cast(snp_date as varchar) || ' ' || cast(snp_time as varchar) as datetime)) as snp_date_time,sku_code,inventory
from public.inventory_snapshot
)
) where R=1;
--age
drop table if exists public.om_oos_soldout_2;
create table public.om_oos_soldout_2
as
select *, datediff(day,cast(snp_date_time as date),current_date) as age
from public.om_oos_soldout_1;
drop table if exists public.om_oos_soldout_2_med;
create table public.om_oos_soldout_2_med
as
select *, case when age between 1 and 3 then '<=3' when age between 4 and 7 then '4-7' when age between 8 and 15 then '8-15'
when age > 15 then 'GT 15' end as age_group
from public.om_oos_soldout_2
where age>0;
drop table if exists public.om_oos_soldout_3;
create table public.om_oos_soldout_3
as
select a.*,b.inventory as Maximum_occurred_inventory
from public.om_oos_soldout_2_med a
inner join (
select inventory,age_group,sku_code
from (
select inventory,age_group,sku_code,row_number() over (partition by age_group,sku_code order by Max_inventory desc) as R
from (
select inventory,age_group,sku_code,count(*) as Max_inventory
from public.om_oos_soldout_2_med
group by inventory,age_group,sku_code order by 4 desc,1
)
) where R=1 order by 2,3
) b on a.sku_code=b.sku_code and a.age_group=b.age_group ;
drop table if exists public.om_oos_soldout_3_med;
create table public.om_oos_soldout_3_med
as
select a.*,b.\"<=3 Maximum_occurred_inventory\",\"4-7 Maximum_occurred_inventory\",\"8-15 Maximum_occurred_inventory\",\"GT 15 Maximum_occurred_inventory\"
from public.om_oos_soldout_3 a
inner join (
select distinct sku_code,
sum(case when Age_group in ('<=3') then Maximum_occurred_inventory end)as \"<=3 Maximum_occurred_inventory\",
sum(case when Age_group in ('4-7') then Maximum_occurred_inventory end) as \"4-7 Maximum_occurred_inventory\",
sum(case when Age_group in ('8-15') then Maximum_occurred_inventory end) as \"8-15 Maximum_occurred_inventory\",
sum(case when Age_group in ('GT 15') then Maximum_occurred_inventory end )as \"GT 15 Maximum_occurred_inventory\"
from public.om_oos_soldout_3
group by 1 order by sku_code
) b on a.sku_code=b.sku_code ;
drop table if exists public.om_oos_soldout_3_med_2;
create table public.om_oos_soldout_3_med_2
as
select a.*,b.sold_out_fl
from public.om_oos_soldout_3 a
inner join (
select sku_code,sold_out_fl
from (
select *,row_number() over (partition by sku_code order by sold_out_fl) as R
from (
select distinct sku_code,
case when ((age=16 or age=17 or age=18 or age=19) and inventory=0) and (\"8-15 Maximum_occurred_inventory\"=0) and \"4-7 Maximum_occurred_inventory\"=0 and \"<=3 Maximum_occurred_inventory\"=0 then '1.GT 15 and sold out'
when (age=8 and inventory=0) and \"4-7 Maximum_occurred_inventory\"=0 and \"<=3 Maximum_occurred_inventory\"=0 then '2.8-15 and sold out'
when ((age=4 or age=5 or age=6) and inventory=0) and \"<=3 Maximum_occurred_inventory\"=0 then '3.4-7 and sold out'
when (age=1 and inventory=0) then '4.<=3 and sold out' else '5.stocked' end as sold_out_fl--,count(distinct sku_code)
from public.om_oos_soldout_3_med
order by sku_code,age
)
) where R=1
) b on a.sku_code=b.sku_code ;
drop table if exists public.om_oos_soldout_4;
create table public.om_oos_soldout_4
as
select a.*,b.brand_name,b.\"EL/PL Flag\",b.give_away,b.active_status
from public.om_oos_soldout_3_med_2 a
inner join
(
select distinct b.name as brand_name,a.sku,case when moqremark = 'NOT ORDER GIVE AWAY ITEM' then 'Yes' else 'No' end give_away,
CASE
WHEN a.is_archived = 'true' THEN 'Permanatly_delisted'
WHEN a.is_delisted = 'true' THEN 'delisted'
WHEN a.is_archived = 'false'
AND a.is_delisted = 'false' THEN 'Active'
END active_status,
case when brand_name in ('HISTOIRE NATURELLE','INGRID','W DRESSROOM','FORENCOS','DEWYCEL','GLUTANEX','BELLFLOWER','ONE THING','BEAUSTA') then 'EL/PL' else 'NA' end as \"EL/PL Flag\"
from raena_catalog_management.product a
left join raena_catalog_management.brand b on a.brand_id=b.id
left join public.demand_forecasting_configs c on a.sku=c.sku
) b on a.sku_code=b.sku ;
drop table if exists public.om_oos_soldout_5;
create table public.om_oos_soldout_5
as
select a.*,coalesce(b.sku_type,'Not Moving') as sku_type,coalesce(b.new_sku_flag,'No') as new_sku_flag,
cikarang_runrate,surabaya_runrate,semarang_runrate,medan_runrate,makassar_runrate,samarinda_runrate,total_runrate,c.gold_price,d.runrate as \"90_days_runrate\"
from public.om_oos_soldout_4 a
left join public.demand_forecast_dashboard_final b on a.sku_code=b.sku
LEFT JOIN
(SELECT distinct sku ,
case when is_slash_price='true'
then json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)
else json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end as gold_price
FROM raena_catalog_management.product p) c on a.sku_code=c.sku
left join
( select sku,sum(oi.quantity)/count(distinct cast(o.created_at as date)) as runrate
from raena_order_management.order o
inner join raena_order_management.order_item oi on o.id=oi.order_id
where cast(o.created_at as date)>=cast((current_date-90) as date) and o.payment_status='Paid' and cast(o.is_archived as varchar)='false'
group by sku
) d on a.sku_code=d.sku
;
/*drop table if exists public.om_oos_soldout_5;
create table public.om_oos_soldout_5
as
select a.*,coalesce(b.sku_type,'Not Moving') as sku_type,coalesce(b.new_sku_flag,'No') as new_sku_flag,
cikarang_runrate,surabaya_runrate,semarang_runrate,medan_runrate,makassar_runrate,samarinda_runrate,total_runrate,c.gold_price,d.runrate as \"90_days_runrate\"
from public.om_oos_soldout_4 a
left join public.demand_forecast_dashboard_final b on a.sku_code=b.sku
LEFT JOIN
(SELECT distinct sku ,
case when is_slash_price='true'
then json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)
else json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end as gold_price
FROM raena_catalog_management.product p) c on a.sku_code=c.sku
left join
( select distinct sku,total_runrate as runrate from public.demand_forecast_dashboard_final
) d on a.sku_code=d.sku; */
drop table if exists public.om_oos_soldout_6;
create table public.om_oos_soldout_6
as
select distinct cast(a.snp_date_time as date) as snp_date_time,a.sku_code,\"90_days_runrate\",a.gold_price,OOS_days,a.sold_out_fl,
a.active_status,a.brand_name,\"EL/PL Flag\",a.give_away,a.sku_type,a.new_sku_flag,c.total_runrate,d.avg_remaining_LM_RV,d.avg_All_LM_RV,e.PO_Ordered_quantity
from public.om_oos_soldout_5 a
inner join (
select sku_code,sold_out_fl,max(case when inventory=0 and sold_out_fl='1.GT 15 and sold out' and age>15 then age
when inventory=0 and sold_out_fl='2.8-15 and sold out' and age between 8 and 15 then age
when inventory=0 and sold_out_fl='3.4-7 and sold out' and age between 4 and 7 then age
when inventory=0 and sold_out_fl='4.<=3 and sold out' and age <=3 then age
end) as OOS_days
from public.om_oos_soldout_5 where sold_out_fl not like '%stocked'
group by 1,2
) b on a.sku_code=b.sku_code
left join public.demand_forecast_dashboard_final c on a.sku_code=c.sku
left join (
select sku,avg(remaining_shipping_fee)/avg(remaining_payment_amount) as avg_remaining_LM_RV,avg(shipping_fee)/avg(payment_amount) as avg_All_LM_RV
from (
select distinct sku,case when (actual_warehouse!=expected_warehouse) or expected_warehouse isnull and payment_amount notnull then payment_amount end as remaining_payment_amount,
case when (actual_warehouse!=expected_warehouse) or expected_warehouse isnull and payment_amount notnull then \"Actual Shipping Fee\" end as remaining_shipping_fee,
payment_amount,\"Actual Shipping Fee\" as shipping_fee
from (
select distinct sku,actual_warehouse,expected_warehouse,A.payment_amount,
case when A.sub_order_shipping_id=sb.reference_id and A.order_id=sb.order_id then ((sb.shipping_amount)/(sb.order_weight))*(sb.final_weight)
when A.order_id = D.id and (A.sub_order_shipping_id= D.id or A.sub_order_shipping_id is null) then D.shipping_amount
when A.sub_order_shipping_id=C.id then C.shipping_amount end \"Actual Shipping Fee\"
from public.rate_sheet_wise_expected_warehouse A
left join raena_order_management.sales_sub_order_shipment C on A.sub_order_shipping_id=C.id
left join raena_order_management.order D on A.order_id = D.id and (A.sub_order_shipping_id= D.id or A.sub_order_shipping_id is null)
left join public.OM_Logistic_final_shipping_base sb on A.sub_order_shipping_id=sb.reference_id and A.order_id=sb.order_id
where A.created_date>=(current_date-90) and A.is_campaign='false'
)
) where payment_amount!=0
group by 1
) d on a.sku_code=d.sku
left join (
select raena_code sku,
coalesce(sum(case when warehouse_id = 'CGK WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Makassar WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Medan WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Samarinda WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Semarang WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Surabaya WHS' then COALESCE(quantity,0) end),0) PO_Ordered_quantity
from raena_erp_management.inbound_order A
left join raena_erp_management.inbound_order_sku B on A.id = B.orderIdId
where cast(expected_arrival_date as date) > current_date -1 and received_time is null
group by raena_code
) e on a.sku_code=e.sku;
-------------------restock
drop table if exists public.om_oos_restock_1;
create table public.om_oos_restock_1
as
select report_date,a.sku,overall_qty_required_per_day as restock,sku_type,brand_name,give_away,new_sku_flag,active_status,
coalesce(cikarang_project_quantity_to_order,0) as cikarang_project_quantity_to_order,coalesce(surabaya_project_quantity_to_order,0) as surabaya_project_quantity_to_order,
coalesce(semarang_project_quantity_to_order,0) as semarang_project_quantity_to_order,coalesce(medan_project_quantity_to_order,0) as medan_project_quantity_to_order,
coalesce(makassar_project_quantity_to_order,0) as makassar_project_quantity_to_order,coalesce(semarandi_project_quantity_to_order,0) as semarandi_project_quantity_to_order
,case when brand_name in ('HISTOIRE NATURELLE','INGRID','W DRESSROOM','FORENCOS','DEWYCEL','GLUTANEX','BELLFLOWER','ONE THING','BEAUSTA') then 'EL/PL' else 'NA' end as \"EL/PL Flag\",
datediff(day,cast(report_date as date),current_date) as age,c.gold_price
from public.OM_OOS_Base a
LEFT JOIN
(SELECT distinct sku ,
case when is_slash_price='true'
then json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)
else json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end as gold_price
FROM raena_catalog_management.product p) c on a.sku=c.sku;
drop table if exists public.om_oos_restock_1_med;
create table public.om_oos_restock_1_med
as
select *
from public.om_oos_restock_1 unpivot
(cnt for warehouse in (cikarang_project_quantity_to_order,surabaya_project_quantity_to_order,semarang_project_quantity_to_order,medan_project_quantity_to_order,
makassar_project_quantity_to_order,semarandi_project_quantity_to_order));
drop table if exists public.om_oos_restock_2;
create table public.om_oos_restock_2
as
select *,case when age between 1 and 3 then '<=3' when age between 4 and 7 then '4-7' when age between 8 and 15 then '8-15'
when age > 15 then 'GT 15' end as age_group
from public.om_oos_restock_1_med
where age>0;
drop table if exists public.om_oos_restock_3;
create table public.om_oos_restock_3
as
select a.*,b.restock as Maximum_occurred_inventory
from public.om_oos_restock_2 a
inner join (
select restock,age_group,sku,warehouse
from (
select restock,age_group,warehouse,sku,row_number() over (partition by age_group,warehouse,sku order by Max_inventory desc) as R
from (
select cnt as restock,age_group,warehouse,sku,count(*) as Max_inventory
from public.om_oos_restock_2
group by cnt,age_group,warehouse,sku order by 5 desc,1
)
) where R=1 order by 2,3
) b on a.sku=b.sku and a.age_group=b.age_group and a.warehouse=b.warehouse;
drop table if exists public.om_oos_restock_4;
create table public.om_oos_restock_4
as
select a.*,b.\"<=3 Maximum_occurred_inventory\",\"4-7 Maximum_occurred_inventory\",\"8-15 Maximum_occurred_inventory\",\"GT 15 Maximum_occurred_inventory\"
from public.om_oos_restock_3 a
inner join (
select distinct sku,warehouse,
sum(case when Age_group in ('<=3') then Maximum_occurred_inventory end)as \"<=3 Maximum_occurred_inventory\",
sum(case when Age_group in ('4-7') then Maximum_occurred_inventory end) as \"4-7 Maximum_occurred_inventory\",
sum(case when Age_group in ('8-15') then Maximum_occurred_inventory end) as \"8-15 Maximum_occurred_inventory\",
sum(case when Age_group in ('GT 15') then Maximum_occurred_inventory end )as \"GT 15 Maximum_occurred_inventory\"
from public.om_oos_restock_3
group by 1,2 order by sku
) b on a.sku=b.sku and a.warehouse=b.warehouse;
drop table if exists public.om_oos_restock_5;
create table public.om_oos_restock_5
as
select a.*,b.sold_out_fl,c.runrate as \"90_days_runrate\"
from public.om_oos_restock_4 a
inner join (
select sku,sold_out_fl,warehouse
from (
select *,row_number() over (partition by sku order by sold_out_fl) as R
from (
select distinct sku,warehouse,
case when ((age=16) and restock=0) and (\"8-15 Maximum_occurred_inventory\"=0) and \"4-7 Maximum_occurred_inventory\"=0 and \"<=3 Maximum_occurred_inventory\"=0 then '1.GT 15 and sold out'
when (age=8 and restock=0) and \"4-7 Maximum_occurred_inventory\"=0 and \"<=3 Maximum_occurred_inventory\"=0 then '2.8-15 and sold out'
when ((age=4) and restock=0) and \"<=3 Maximum_occurred_inventory\"=0 then '3.4-7 and sold out'
when (age=1 and restock=0) then '4.<=3 and sold out' else '5.stocked' end as sold_out_fl--,count(distinct sku_code)
from public.om_oos_restock_4
order by sku,age
)
) where R=1
) b on a.sku=b.sku and a.warehouse=b.warehouse
left join (
select sku,sum(oi.quantity)/count(distinct cast(o.created_at as date)) as runrate
from raena_order_management.order o
inner join raena_order_management.order_item oi on o.id=oi.order_id
where cast(o.created_at as date)>=cast((current_date-90) as date) and o.payment_status='Paid' and cast(o.is_archived as varchar)='false'
group by sku
) c on a.sku=c.sku;
drop table if exists public.weight_sku_table;
CREATE TABLE public.weight_sku_table AS
SELECT sku,
CASE
WHEN weight>=volume_weight THEN weight
ELSE volume_weight
END weight
FROM
(SELECT a.sku,
weight,
(height*width*LENGTH)/6000 AS volume_weight
FROM raena_catalog_management.product a
WHERE CLASS<>'Bundle') A;
drop table if exists public.om_shipping_fee_sku_base_stage2;
CREATE TABLE public.om_shipping_fee_sku_base_stage2 AS
SELECT A.*,
B.weight*Quantity sku_weight,
CASE
WHEN (B.weight*Quantity)<1.3 THEN 1
WHEN (B.weight*Quantity)>=1.3
AND (ABS((B.weight*Quantity)) - FLOOR(ABS((B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((B.weight*Quantity))+1
ELSE FLOOR((B.weight*Quantity))
END AS final_weight
FROM (
select sku,actual_warehouse,expected_warehouse,payment_amount,quantity,sub_order_shipping_id,missed_inventory,
coalesce(expected_JTR_shipping_fee,Expected_shipping_fee,\"Actual Shipping Fee\") as Expected_shipping_fee,\"Actual Shipping Fee\"
from (
select distinct A.sku,actual_warehouse,expected_warehouse,A.payment_amount,quantity,A.sub_order_shipping_id,missed_inventory,
case when A.sub_order_shipping_id=sb.reference_id and A.order_id=sb.order_id then ((sb.shipping_amount)/(sb.order_weight))*(sb.final_weight)
when A.order_id = D.id and (A.sub_order_shipping_id= D.id or A.sub_order_shipping_id is null) then D.shipping_amount
when A.sub_order_shipping_id=C.id then C.shipping_amount end \"Actual Shipping Fee\",
public.rate_sheet_wise_expected_warehouse_expected_shipping_final.Expected_shipping_fee,jsf.expected_JTR_shipping_fee
from public.rate_sheet_wise_expected_warehouse A
left join raena_order_management.sales_sub_order_shipment C on A.sub_order_shipping_id=C.id
left join raena_order_management.order D on A.order_id = D.id and (A.sub_order_shipping_id= D.id or A.sub_order_shipping_id is null)
left join public.OM_Logistic_final_shipping_base sb on A.sub_order_shipping_id=sb.reference_id and A.order_id=sb.order_id
left join public.rate_sheet_wise_expected_warehouse_expected_shipping_final on A.sub_order_shipping_id=public.rate_sheet_wise_expected_warehouse_expected_shipping_final.sub_order_shipping_id
left join public.om_shipment_jtr_shipping_fee_base_11 jsf on A.sub_order_shipping_id=jsf.sub_order_shipping_id
where A.created_date>=(current_date-90) and A.is_campaign='false'
)
) A
LEFT JOIN public.weight_sku_table B ON A.sku = B.sku ;
drop table if exists public.om_shipping_fee_sku_base_stage3;
CREATE TABLE public.om_shipping_fee_sku_base_stage3 AS
SELECT *,
(sku_weight*\"Actual Shipping Fee\")/sum(sku_weight) over(partition BY sub_order_shipping_id) new_shipment_amount,
(sku_weight*Expected_shipping_fee)/sum(sku_weight) over(partition BY sub_order_shipping_id) new_expected_shipment_amount
FROM public.om_shipping_fee_sku_base_stage2;
drop table if exists public.om_oos_restock_6;
create table public.om_oos_restock_6
as
select distinct a.report_date,a.sku,a.sku_type,a.brand_name,a.give_away,a.new_sku_flag,a.active_status,a.\"el/pl flag\",a.age,a.gold_price,a.cnt,a.age_group,a.sold_out_fl,\"90_days_runrate\",
b.restock_days,c.total_runrate,d.avg_remaining_LM_RV,d.avg_All_LM_RV
from public.om_oos_restock_5 a
inner join (
select sku,sold_out_fl,max(case when restock=0 and sold_out_fl='1.GT 15 and sold out' and age>15 then age
when restock=0 and sold_out_fl='2.8-15 and sold out' and age between 8 and 15 then age
when restock=0 and sold_out_fl='3.4-7 and sold out' and age between 4 and 7 then age
when restock=0 and sold_out_fl='4.<=3 and sold out' and age <=3 then age
end) as restock_days
from public.om_oos_restock_5 where sold_out_fl not like '%stocked'
group by 1,2
) b on a.sku=b.sku
left join public.demand_forecast_dashboard_final c on a.sku=c.sku
left join (
select sku,avg(shipping_fee)/avg(payment_amount) as avg_remaining_LM_RV,avg(new_expected_shipment_amount)/avg(payment_amount) as avg_All_LM_RV
from (
select sku,
new_expected_shipment_amount,
payment_amount,\"Actual Shipping Fee\" as shipping_fee
from (
select sku,actual_warehouse,expected_warehouse,
(payment_amount/quantity) as payment_amount,missed_inventory,(new_shipment_amount/quantity) as \"Actual Shipping Fee\",
(new_expected_shipment_amount/quantity) as new_expected_shipment_amount
from public.om_shipping_fee_sku_base_stage3 --where sku='HAN020'
)
) where payment_amount!=0
group by 1
) d on a.sku=d.sku;
" > /home/ec2-user/cronjob/OOS/OOS_code.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/OOS/OOS_code.sql

423
OOS/OOS_code.sql Normal file
View File

@ -0,0 +1,423 @@
--------------------------OOS -------------------------
drop table if exists public.om_oos_soldout_1;
create table public.om_oos_soldout_1
as
select snp_date_time,sku_code,inventory
from (
select snp_date,snp_time,snp_date_time,sku_code,inventory,
row_number() over (partition by sku_code,cast(snp_date_time as date) order by snp_date_time desc,inventory desc) as R
from (
select distinct snp_date,snp_time,dateadd(hour,7,cast(cast(snp_date as varchar) || ' ' || cast(snp_time as varchar) as datetime)) as snp_date_time,sku_code,inventory
from public.inventory_snapshot
)
) where R=1;
--age
drop table if exists public.om_oos_soldout_2;
create table public.om_oos_soldout_2
as
select *, datediff(day,cast(snp_date_time as date),current_date) as age
from public.om_oos_soldout_1;
drop table if exists public.om_oos_soldout_2_med;
create table public.om_oos_soldout_2_med
as
select *, case when age between 1 and 3 then '<=3' when age between 4 and 7 then '4-7' when age between 8 and 15 then '8-15'
when age > 15 then 'GT 15' end as age_group
from public.om_oos_soldout_2
where age>0;
drop table if exists public.om_oos_soldout_3;
create table public.om_oos_soldout_3
as
select a.*,b.inventory as Maximum_occurred_inventory
from public.om_oos_soldout_2_med a
inner join (
select inventory,age_group,sku_code
from (
select inventory,age_group,sku_code,row_number() over (partition by age_group,sku_code order by Max_inventory desc) as R
from (
select inventory,age_group,sku_code,count(*) as Max_inventory
from public.om_oos_soldout_2_med
group by inventory,age_group,sku_code order by 4 desc,1
)
) where R=1 order by 2,3
) b on a.sku_code=b.sku_code and a.age_group=b.age_group ;
drop table if exists public.om_oos_soldout_3_med;
create table public.om_oos_soldout_3_med
as
select a.*,b."<=3 Maximum_occurred_inventory","4-7 Maximum_occurred_inventory","8-15 Maximum_occurred_inventory","GT 15 Maximum_occurred_inventory"
from public.om_oos_soldout_3 a
inner join (
select distinct sku_code,
sum(case when Age_group in ('<=3') then Maximum_occurred_inventory end)as "<=3 Maximum_occurred_inventory",
sum(case when Age_group in ('4-7') then Maximum_occurred_inventory end) as "4-7 Maximum_occurred_inventory",
sum(case when Age_group in ('8-15') then Maximum_occurred_inventory end) as "8-15 Maximum_occurred_inventory",
sum(case when Age_group in ('GT 15') then Maximum_occurred_inventory end )as "GT 15 Maximum_occurred_inventory"
from public.om_oos_soldout_3
group by 1 order by sku_code
) b on a.sku_code=b.sku_code ;
drop table if exists public.om_oos_soldout_3_med_2;
create table public.om_oos_soldout_3_med_2
as
select a.*,b.sold_out_fl
from public.om_oos_soldout_3 a
inner join (
select sku_code,sold_out_fl
from (
select *,row_number() over (partition by sku_code order by sold_out_fl) as R
from (
select distinct sku_code,
case when ((age=16 or age=17 or age=18 or age=19) and inventory=0) and ("8-15 Maximum_occurred_inventory"=0) and "4-7 Maximum_occurred_inventory"=0 and "<=3 Maximum_occurred_inventory"=0 then '1.GT 15 and sold out'
when (age=8 and inventory=0) and "4-7 Maximum_occurred_inventory"=0 and "<=3 Maximum_occurred_inventory"=0 then '2.8-15 and sold out'
when ((age=4 or age=5 or age=6) and inventory=0) and "<=3 Maximum_occurred_inventory"=0 then '3.4-7 and sold out'
when (age=1 and inventory=0) then '4.<=3 and sold out' else '5.stocked' end as sold_out_fl--,count(distinct sku_code)
from public.om_oos_soldout_3_med
order by sku_code,age
)
) where R=1
) b on a.sku_code=b.sku_code ;
drop table if exists public.om_oos_soldout_4;
create table public.om_oos_soldout_4
as
select a.*,b.brand_name,b."EL/PL Flag",b.give_away,b.active_status
from public.om_oos_soldout_3_med_2 a
inner join
(
select distinct b.name as brand_name,a.sku,case when moqremark = 'NOT ORDER GIVE AWAY ITEM' then 'Yes' else 'No' end give_away,
CASE
WHEN a.is_archived = 'true' THEN 'Permanatly_delisted'
WHEN a.is_delisted = 'true' THEN 'delisted'
WHEN a.is_archived = 'false'
AND a.is_delisted = 'false' THEN 'Active'
END active_status,
case when brand_name in ('HISTOIRE NATURELLE','INGRID','W DRESSROOM','FORENCOS','DEWYCEL','GLUTANEX','BELLFLOWER','ONE THING','BEAUSTA') then 'EL/PL' else 'NA' end as "EL/PL Flag"
from raena_catalog_management.product a
left join raena_catalog_management.brand b on a.brand_id=b.id
left join public.demand_forecasting_configs c on a.sku=c.sku
) b on a.sku_code=b.sku ;
drop table if exists public.om_oos_soldout_5;
create table public.om_oos_soldout_5
as
select a.*,coalesce(b.sku_type,'Not Moving') as sku_type,coalesce(b.new_sku_flag,'No') as new_sku_flag,
cikarang_runrate,surabaya_runrate,semarang_runrate,medan_runrate,makassar_runrate,samarinda_runrate,total_runrate,c.gold_price,d.runrate as "90_days_runrate"
from public.om_oos_soldout_4 a
left join public.demand_forecast_dashboard_final b on a.sku_code=b.sku
LEFT JOIN
(SELECT distinct sku ,
case when is_slash_price='true'
then json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)
else json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end as gold_price
FROM raena_catalog_management.product p) c on a.sku_code=c.sku
left join
( select sku,sum(oi.quantity)/count(distinct cast(o.created_at as date)) as runrate
from raena_order_management.order o
inner join raena_order_management.order_item oi on o.id=oi.order_id
where cast(o.created_at as date)>=cast((current_date-90) as date) and o.payment_status='Paid' and cast(o.is_archived as varchar)='false'
group by sku
) d on a.sku_code=d.sku
;
/*drop table if exists public.om_oos_soldout_5;
create table public.om_oos_soldout_5
as
select a.*,coalesce(b.sku_type,'Not Moving') as sku_type,coalesce(b.new_sku_flag,'No') as new_sku_flag,
cikarang_runrate,surabaya_runrate,semarang_runrate,medan_runrate,makassar_runrate,samarinda_runrate,total_runrate,c.gold_price,d.runrate as "90_days_runrate"
from public.om_oos_soldout_4 a
left join public.demand_forecast_dashboard_final b on a.sku_code=b.sku
LEFT JOIN
(SELECT distinct sku ,
case when is_slash_price='true'
then json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)
else json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end as gold_price
FROM raena_catalog_management.product p) c on a.sku_code=c.sku
left join
( select distinct sku,total_runrate as runrate from public.demand_forecast_dashboard_final
) d on a.sku_code=d.sku; */
drop table if exists public.om_oos_soldout_6;
create table public.om_oos_soldout_6
as
select distinct cast(a.snp_date_time as date) as snp_date_time,a.sku_code,"90_days_runrate",a.gold_price,OOS_days,a.sold_out_fl,
a.active_status,a.brand_name,"EL/PL Flag",a.give_away,a.sku_type,a.new_sku_flag,c.total_runrate,d.avg_remaining_LM_RV,d.avg_All_LM_RV,e.PO_Ordered_quantity
from public.om_oos_soldout_5 a
inner join (
select sku_code,sold_out_fl,max(case when inventory=0 and sold_out_fl='1.GT 15 and sold out' and age>15 then age
when inventory=0 and sold_out_fl='2.8-15 and sold out' and age between 8 and 15 then age
when inventory=0 and sold_out_fl='3.4-7 and sold out' and age between 4 and 7 then age
when inventory=0 and sold_out_fl='4.<=3 and sold out' and age <=3 then age
end) as OOS_days
from public.om_oos_soldout_5 where sold_out_fl not like '%stocked'
group by 1,2
) b on a.sku_code=b.sku_code
left join public.demand_forecast_dashboard_final c on a.sku_code=c.sku
left join (
select sku,avg(remaining_shipping_fee)/avg(remaining_payment_amount) as avg_remaining_LM_RV,avg(shipping_fee)/avg(payment_amount) as avg_All_LM_RV
from (
select distinct sku,case when (actual_warehouse!=expected_warehouse) or expected_warehouse isnull and payment_amount notnull then payment_amount end as remaining_payment_amount,
case when (actual_warehouse!=expected_warehouse) or expected_warehouse isnull and payment_amount notnull then "Actual Shipping Fee" end as remaining_shipping_fee,
payment_amount,"Actual Shipping Fee" as shipping_fee
from (
select distinct sku,actual_warehouse,expected_warehouse,A.payment_amount,
case when A.sub_order_shipping_id=sb.reference_id and A.order_id=sb.order_id then ((sb.shipping_amount)/(sb.order_weight))*(sb.final_weight)
when A.order_id = D.id and (A.sub_order_shipping_id= D.id or A.sub_order_shipping_id is null) then D.shipping_amount
when A.sub_order_shipping_id=C.id then C.shipping_amount end "Actual Shipping Fee"
from public.rate_sheet_wise_expected_warehouse A
left join raena_order_management.sales_sub_order_shipment C on A.sub_order_shipping_id=C.id
left join raena_order_management.order D on A.order_id = D.id and (A.sub_order_shipping_id= D.id or A.sub_order_shipping_id is null)
left join public.OM_Logistic_final_shipping_base sb on A.sub_order_shipping_id=sb.reference_id and A.order_id=sb.order_id
where A.created_date>=(current_date-90) and A.is_campaign='false'
)
) where payment_amount!=0
group by 1
) d on a.sku_code=d.sku
left join (
select raena_code sku,
coalesce(sum(case when warehouse_id = 'CGK WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Makassar WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Medan WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Samarinda WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Semarang WHS' then COALESCE(quantity,0) end),0) +
coalesce(sum(case when warehouse_id = 'Surabaya WHS' then COALESCE(quantity,0) end),0) PO_Ordered_quantity
from raena_erp_management.inbound_order A
left join raena_erp_management.inbound_order_sku B on A.id = B.orderIdId
where cast(expected_arrival_date as date) > current_date -1 and received_time is null
group by raena_code
) e on a.sku_code=e.sku;
-------------------restock
drop table if exists public.om_oos_restock_1;
create table public.om_oos_restock_1
as
select report_date,a.sku,overall_qty_required_per_day as restock,sku_type,brand_name,give_away,new_sku_flag,active_status,
coalesce(cikarang_project_quantity_to_order,0) as cikarang_project_quantity_to_order,coalesce(surabaya_project_quantity_to_order,0) as surabaya_project_quantity_to_order,
coalesce(semarang_project_quantity_to_order,0) as semarang_project_quantity_to_order,coalesce(medan_project_quantity_to_order,0) as medan_project_quantity_to_order,
coalesce(makassar_project_quantity_to_order,0) as makassar_project_quantity_to_order,coalesce(semarandi_project_quantity_to_order,0) as semarandi_project_quantity_to_order
,case when brand_name in ('HISTOIRE NATURELLE','INGRID','W DRESSROOM','FORENCOS','DEWYCEL','GLUTANEX','BELLFLOWER','ONE THING','BEAUSTA') then 'EL/PL' else 'NA' end as "EL/PL Flag",
datediff(day,cast(report_date as date),current_date) as age,c.gold_price
from public.OM_OOS_Base a
LEFT JOIN
(SELECT distinct sku ,
case when is_slash_price='true'
then json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)
else json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end as gold_price
FROM raena_catalog_management.product p) c on a.sku=c.sku;
drop table if exists public.om_oos_restock_1_med;
create table public.om_oos_restock_1_med
as
select *
from public.om_oos_restock_1 unpivot
(cnt for warehouse in (cikarang_project_quantity_to_order,surabaya_project_quantity_to_order,semarang_project_quantity_to_order,medan_project_quantity_to_order,
makassar_project_quantity_to_order,semarandi_project_quantity_to_order));
drop table if exists public.om_oos_restock_2;
create table public.om_oos_restock_2
as
select *,case when age between 1 and 3 then '<=3' when age between 4 and 7 then '4-7' when age between 8 and 15 then '8-15'
when age > 15 then 'GT 15' end as age_group
from public.om_oos_restock_1_med
where age>0;
drop table if exists public.om_oos_restock_3;
create table public.om_oos_restock_3
as
select a.*,b.restock as Maximum_occurred_inventory
from public.om_oos_restock_2 a
inner join (
select restock,age_group,sku,warehouse
from (
select restock,age_group,warehouse,sku,row_number() over (partition by age_group,warehouse,sku order by Max_inventory desc) as R
from (
select cnt as restock,age_group,warehouse,sku,count(*) as Max_inventory
from public.om_oos_restock_2
group by cnt,age_group,warehouse,sku order by 5 desc,1
)
) where R=1 order by 2,3
) b on a.sku=b.sku and a.age_group=b.age_group and a.warehouse=b.warehouse;
drop table if exists public.om_oos_restock_4;
create table public.om_oos_restock_4
as
select a.*,b."<=3 Maximum_occurred_inventory","4-7 Maximum_occurred_inventory","8-15 Maximum_occurred_inventory","GT 15 Maximum_occurred_inventory"
from public.om_oos_restock_3 a
inner join (
select distinct sku,warehouse,
sum(case when Age_group in ('<=3') then Maximum_occurred_inventory end)as "<=3 Maximum_occurred_inventory",
sum(case when Age_group in ('4-7') then Maximum_occurred_inventory end) as "4-7 Maximum_occurred_inventory",
sum(case when Age_group in ('8-15') then Maximum_occurred_inventory end) as "8-15 Maximum_occurred_inventory",
sum(case when Age_group in ('GT 15') then Maximum_occurred_inventory end )as "GT 15 Maximum_occurred_inventory"
from public.om_oos_restock_3
group by 1,2 order by sku
) b on a.sku=b.sku and a.warehouse=b.warehouse;
drop table if exists public.om_oos_restock_5;
create table public.om_oos_restock_5
as
select a.*,b.sold_out_fl,c.runrate as "90_days_runrate"
from public.om_oos_restock_4 a
inner join (
select sku,sold_out_fl,warehouse
from (
select *,row_number() over (partition by sku order by sold_out_fl) as R
from (
select distinct sku,warehouse,
case when ((age=16) and restock=0) and ("8-15 Maximum_occurred_inventory"=0) and "4-7 Maximum_occurred_inventory"=0 and "<=3 Maximum_occurred_inventory"=0 then '1.GT 15 and sold out'
when (age=8 and restock=0) and "4-7 Maximum_occurred_inventory"=0 and "<=3 Maximum_occurred_inventory"=0 then '2.8-15 and sold out'
when ((age=4) and restock=0) and "<=3 Maximum_occurred_inventory"=0 then '3.4-7 and sold out'
when (age=1 and restock=0) then '4.<=3 and sold out' else '5.stocked' end as sold_out_fl--,count(distinct sku_code)
from public.om_oos_restock_4
order by sku,age
)
) where R=1
) b on a.sku=b.sku and a.warehouse=b.warehouse
left join (
select sku,sum(oi.quantity)/count(distinct cast(o.created_at as date)) as runrate
from raena_order_management.order o
inner join raena_order_management.order_item oi on o.id=oi.order_id
where cast(o.created_at as date)>=cast((current_date-90) as date) and o.payment_status='Paid' and cast(o.is_archived as varchar)='false'
group by sku
) c on a.sku=c.sku;
drop table if exists public.weight_sku_table;
CREATE TABLE public.weight_sku_table AS
SELECT sku,
CASE
WHEN weight>=volume_weight THEN weight
ELSE volume_weight
END weight
FROM
(SELECT a.sku,
weight,
(height*width*LENGTH)/6000 AS volume_weight
FROM raena_catalog_management.product a
WHERE CLASS<>'Bundle') A;
drop table if exists public.om_shipping_fee_sku_base_stage2;
CREATE TABLE public.om_shipping_fee_sku_base_stage2 AS
SELECT A.*,
B.weight*Quantity sku_weight,
CASE
WHEN (B.weight*Quantity)<1.3 THEN 1
WHEN (B.weight*Quantity)>=1.3
AND (ABS((B.weight*Quantity)) - FLOOR(ABS((B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((B.weight*Quantity))+1
ELSE FLOOR((B.weight*Quantity))
END AS final_weight
FROM (
select sku,actual_warehouse,expected_warehouse,payment_amount,quantity,sub_order_shipping_id,missed_inventory,
coalesce(expected_JTR_shipping_fee,Expected_shipping_fee,"Actual Shipping Fee") as Expected_shipping_fee,"Actual Shipping Fee"
from (
select distinct A.sku,actual_warehouse,expected_warehouse,A.payment_amount,quantity,A.sub_order_shipping_id,missed_inventory,
case when A.sub_order_shipping_id=sb.reference_id and A.order_id=sb.order_id then ((sb.shipping_amount)/(sb.order_weight))*(sb.final_weight)
when A.order_id = D.id and (A.sub_order_shipping_id= D.id or A.sub_order_shipping_id is null) then D.shipping_amount
when A.sub_order_shipping_id=C.id then C.shipping_amount end "Actual Shipping Fee",
public.rate_sheet_wise_expected_warehouse_expected_shipping_final.Expected_shipping_fee,jsf.expected_JTR_shipping_fee
from public.rate_sheet_wise_expected_warehouse A
left join raena_order_management.sales_sub_order_shipment C on A.sub_order_shipping_id=C.id
left join raena_order_management.order D on A.order_id = D.id and (A.sub_order_shipping_id= D.id or A.sub_order_shipping_id is null)
left join public.OM_Logistic_final_shipping_base sb on A.sub_order_shipping_id=sb.reference_id and A.order_id=sb.order_id
left join public.rate_sheet_wise_expected_warehouse_expected_shipping_final on A.sub_order_shipping_id=public.rate_sheet_wise_expected_warehouse_expected_shipping_final.sub_order_shipping_id
left join public.om_shipment_jtr_shipping_fee_base_11 jsf on A.sub_order_shipping_id=jsf.sub_order_shipping_id
where A.created_date>=(current_date-90) and A.is_campaign='false'
)
) A
LEFT JOIN public.weight_sku_table B ON A.sku = B.sku ;
drop table if exists public.om_shipping_fee_sku_base_stage3;
CREATE TABLE public.om_shipping_fee_sku_base_stage3 AS
SELECT *,
(sku_weight*"Actual Shipping Fee")/sum(sku_weight) over(partition BY sub_order_shipping_id) new_shipment_amount,
(sku_weight*Expected_shipping_fee)/sum(sku_weight) over(partition BY sub_order_shipping_id) new_expected_shipment_amount
FROM public.om_shipping_fee_sku_base_stage2;
drop table if exists public.om_oos_restock_6;
create table public.om_oos_restock_6
as
select distinct a.report_date,a.sku,a.sku_type,a.brand_name,a.give_away,a.new_sku_flag,a.active_status,a."el/pl flag",a.age,a.gold_price,a.cnt,a.age_group,a.sold_out_fl,"90_days_runrate",
b.restock_days,c.total_runrate,d.avg_remaining_LM_RV,d.avg_All_LM_RV
from public.om_oos_restock_5 a
inner join (
select sku,sold_out_fl,max(case when restock=0 and sold_out_fl='1.GT 15 and sold out' and age>15 then age
when restock=0 and sold_out_fl='2.8-15 and sold out' and age between 8 and 15 then age
when restock=0 and sold_out_fl='3.4-7 and sold out' and age between 4 and 7 then age
when restock=0 and sold_out_fl='4.<=3 and sold out' and age <=3 then age
end) as restock_days
from public.om_oos_restock_5 where sold_out_fl not like '%stocked'
group by 1,2
) b on a.sku=b.sku
left join public.demand_forecast_dashboard_final c on a.sku=c.sku
left join (
select sku,avg(shipping_fee)/avg(payment_amount) as avg_remaining_LM_RV,avg(new_expected_shipment_amount)/avg(payment_amount) as avg_All_LM_RV
from (
select sku,
new_expected_shipment_amount,
payment_amount,"Actual Shipping Fee" as shipping_fee
from (
select sku,actual_warehouse,expected_warehouse,
(payment_amount/quantity) as payment_amount,missed_inventory,(new_shipment_amount/quantity) as "Actual Shipping Fee",
(new_expected_shipment_amount/quantity) as new_expected_shipment_amount
from public.om_shipping_fee_sku_base_stage3 --where sku='HAN020'
)
) where payment_amount!=0
group by 1
) d on a.sku=d.sku;

2
OOS/sample.log Normal file
View File

@ -0,0 +1,2 @@
DROP TABLE
SELECT

22
OOS/sample.sh Normal file
View File

@ -0,0 +1,22 @@
#!/bin/bash
echo "
--------------------------Revenue Leakage -------------------------
drop table public.om_dormant_sellers_filter;
create table public.om_dormant_sellers_filter
as
select reseller_email,'' as brand_name,'' as sku,Dornant_flag
from public.om_dormant_sellers_filter_base_1
where reseller_email notnull and reseller_email!=''
union
select reseller_email,brand_name,'' as sku,month_cohort
from public.OM_Reseller_brand_month_filter
where reseller_email notnull and reseller_email!='';
" > /home/ec2-user/cronjob/OOS/sample.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f/home/ec2-user/cronjob/OOS/sample.sql > sample.log

18
OOS/sample.sql Normal file
View File

@ -0,0 +1,18 @@
--------------------------Revenue Leakage -------------------------
drop table public.om_dormant_sellers_filter;
create table public.om_dormant_sellers_filter
as
select reseller_email,'' as brand_name,'' as sku,Dornant_flag
from public.om_dormant_sellers_filter_base_1
where reseller_email notnull and reseller_email!=''
union
select reseller_email,brand_name,'' as sku,month_cohort
from public.OM_Reseller_brand_month_filter
where reseller_email notnull and reseller_email!='';

1
OOS_code.log Normal file
View File

@ -0,0 +1 @@
DROP TABLE

0
README.md Normal file
View File

0
a.out Normal file
View File

75
am_recommendation.log Normal file
View File

@ -0,0 +1,75 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
INSERT 0 1453
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

View File

View File

@ -0,0 +1,16 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

295
appsflyer/etl_appsflyer.sh Normal file
View File

@ -0,0 +1,295 @@
!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
DROP TABLE if exists public.campaign_base_data_V3;
CREATE TABLE public.campaign_base_data_V3 AS
SELECT DISTINCT channel,
media_source ,
campaign_type,
campaign campaign_name,
fb_adset_name,
af_siteid,
ad_id,
A.user_id customer_user_id ,
device_brand,
device_model,
platform,
cast(install_time AS date) install_date,
sum(impressions) impressions,
sum(clicks) total_clicks,
sum(install) total_install,
os_version ,
app_version,
A.City_id,
B.profile_phone phone,
install_time
FROM (select distinct * from raena_appsflyer.dw_marketing_install_stats) A
LEFT JOIN
(SELECT DISTINCT profile_objectid,
profile_phone
FROM clevertap.app_installed) B ON A.user_id = B.profile_objectid
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
16,
17,
18,
19,
20;
DROP TABLE IF EXISTS public.campaign_base_data_V4;
CREATE TABLE public.campaign_base_data_V4 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,
reseller_mobile,
conversion_date
FROM public.campaign_base_data_V3 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,
A.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 phone = replace(B.reseller_mobile,'+','');
DROP TABLE IF EXISTS public.reseller_post_gm;
CREATE TABLE public.reseller_post_gm AS
SELECT reseller_name ,
reseller_email ,
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 reseller_name ,
reseller_email ,
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_name ,
reseller_email ,
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 phone
FROM public.campaign_base_data_V4
WHERE phone <> '')
GROUP BY 1,
2,
3
ORDER BY 10 DESC, 2 ASC;
DROP TABLE IF EXISTS public.top_brand_reseller;
CREATE TABLE public.top_brand_reseller AS
SELECT DISTINCT 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 phone
FROM public.campaign_base_data_V4
WHERE phone <> '');
DROP TABLE IF EXISTS public.launched_total;
CREATE TABLE public.launched_total AS
SELECT profile_phone ,
count(1) app_launched
FROM clevertap.app_launched al
WHERE profile_phone IN
(SELECT DISTINCT phone
FROM public.campaign_base_data_V4
WHERE phone <> '')
GROUP BY profile_phone;
DROP TABLE IF EXISTS public.campaign_base_data_v5;
CREATE TABLE public.campaign_base_data_v5 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_base_data_V4 A
LEFT JOIN public.reseller_post_gm B ON A.reseller_mobile = B.reseller_mobile
LEFT JOIN public.top_brand_reseller C ON A.reseller_mobile= C.reseller_mobile
LEFT JOIN public.launched_total D ON A.phone= D.profile_phone;
DROP TABLE IF EXISTS public.campaign_base_data_final_v2;
CREATE TABLE public.campaign_base_data_final_v2 AS
SELECT channel,
media_source,
campaign_type,
campaign_name,
fb_adset_name,
af_siteid,
ad_id ,
customer_user_id ,
device_brand,
device_model,
platform ,
install_date,
install_time ,
impressions,
total_clicks,
total_install,
os_version ,
app_version ,
city_id ,
phone ,
province ,
ttl_order ,
ttl_amount total_amount,
highest_tier,
reg_date,
reseller_email,
conversion_date,
Post_discount_gm,
BRAND_NAME ,
SKU_NAME ,
SKU ,
app_launched TOTAL_app_launched, B.Current_tier
FROM public.campaign_base_data_v5 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;
DROP TABLE IF EXISTS public.campaign_base_data_final_unistall;
CREATE TABLE public.campaign_base_data_final_unistall AS
SELECT distinct city_id ,
cast(install_time AS date) install_date ,
install_time ,
cast(uninstall_time AS date) uninstall_date ,
uninstall_time ,
media_source,
channel,
campaign,
fb_adset_name,
af_siteid,
device_brand,
device_model,
platform,
ad_id,
install,
uninstall
FROM raena_appsflyer.dw_marketing_uninstall_stats;
" > /home/ec2-user/cronjob/appsflyer/etl_appsflyer.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/appsflyer/etl_appsflyer.sql > etl_appsflyer.log

279
appsflyer/etl_appsflyer.sql Normal file
View File

@ -0,0 +1,279 @@
DROP TABLE if exists public.campaign_base_data_V3;
CREATE TABLE public.campaign_base_data_V3 AS
SELECT DISTINCT channel,
media_source ,
campaign_type,
campaign campaign_name,
fb_adset_name,
af_siteid,
ad_id,
A.user_id customer_user_id ,
device_brand,
device_model,
platform,
cast(install_time AS date) install_date,
sum(impressions) impressions,
sum(clicks) total_clicks,
sum(install) total_install,
os_version ,
app_version,
A.City_id,
B.profile_phone phone,
install_time
FROM (select distinct * from raena_appsflyer.dw_marketing_install_stats) A
LEFT JOIN
(SELECT DISTINCT profile_objectid,
profile_phone
FROM clevertap.app_installed) B ON A.user_id = B.profile_objectid
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
16,
17,
18,
19,
20;
DROP TABLE IF EXISTS public.campaign_base_data_V4;
CREATE TABLE public.campaign_base_data_V4 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,
reseller_mobile,
conversion_date
FROM public.campaign_base_data_V3 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,
A.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 phone = replace(B.reseller_mobile,'+','');
DROP TABLE IF EXISTS public.reseller_post_gm;
CREATE TABLE public.reseller_post_gm AS
SELECT reseller_name ,
reseller_email ,
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 reseller_name ,
reseller_email ,
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_name ,
reseller_email ,
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 phone
FROM public.campaign_base_data_V4
WHERE phone <> '')
GROUP BY 1,
2,
3
ORDER BY 10 DESC, 2 ASC;
DROP TABLE IF EXISTS public.top_brand_reseller;
CREATE TABLE public.top_brand_reseller AS
SELECT DISTINCT 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 phone
FROM public.campaign_base_data_V4
WHERE phone <> '');
DROP TABLE IF EXISTS public.launched_total;
CREATE TABLE public.launched_total AS
SELECT profile_phone ,
count(1) app_launched
FROM clevertap.app_launched al
WHERE profile_phone IN
(SELECT DISTINCT phone
FROM public.campaign_base_data_V4
WHERE phone <> '')
GROUP BY profile_phone;
DROP TABLE IF EXISTS public.campaign_base_data_v5;
CREATE TABLE public.campaign_base_data_v5 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_base_data_V4 A
LEFT JOIN public.reseller_post_gm B ON A.reseller_mobile = B.reseller_mobile
LEFT JOIN public.top_brand_reseller C ON A.reseller_mobile= C.reseller_mobile
LEFT JOIN public.launched_total D ON A.phone= D.profile_phone;
DROP TABLE IF EXISTS public.campaign_base_data_final_v2;
CREATE TABLE public.campaign_base_data_final_v2 AS
SELECT channel,
media_source,
campaign_type,
campaign_name,
fb_adset_name,
af_siteid,
ad_id ,
customer_user_id ,
device_brand,
device_model,
platform ,
install_date,
install_time ,
impressions,
total_clicks,
total_install,
os_version ,
app_version ,
city_id ,
phone ,
province ,
ttl_order ,
ttl_amount total_amount,
highest_tier,
reg_date,
reseller_email,
conversion_date,
Post_discount_gm,
BRAND_NAME ,
SKU_NAME ,
SKU ,
app_launched TOTAL_app_launched, B.Current_tier
FROM public.campaign_base_data_v5 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;
DROP TABLE IF EXISTS public.campaign_base_data_final_unistall;
CREATE TABLE public.campaign_base_data_final_unistall AS
SELECT distinct city_id ,
cast(install_time AS date) install_date ,
install_time ,
cast(uninstall_time AS date) uninstall_date ,
uninstall_time ,
media_source,
channel,
campaign,
fb_adset_name,
af_siteid,
device_brand,
device_model,
platform,
ad_id,
install,
uninstall
FROM raena_appsflyer.dw_marketing_uninstall_stats;

View File

@ -0,0 +1,249 @@
!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
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;
" > /home/ec2-user/cronjob/appsflyer/etl_inappevent_appsflyer.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/appsflyer/etl_inappevent_appsflyer.sql

View File

@ -0,0 +1,234 @@
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;

View File

@ -0,0 +1,22 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
DROP TABLE

View File

@ -0,0 +1,24 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

View File

@ -0,0 +1,349 @@
drop table if exists public.brand_promo_dashboard_table ;
create table public.brand_promo_dashboard_table
as
select BB.name brand_name, type promo_type, sku ,'' free_sku, startdate , enddate, promo , Moq ,mov
from public.brand_promo_master AA
left join raena_catalog_management.brand BB on AA.brandid = BB.id
union
select BB.name brand_name,type ,
case when promoselection = 'ANY' then concat('ANY (', promoquantity_1)
when promosku_2 ='(' then concat(promosku_1,promoquantity_1)
else concat(concat(promosku_1,promoquantity_1),concat('+',concat(promosku_2,promoquantity_2))) end Promo_sku,
case when freeselection = 'ANY' then concat('ANY (', freequantity_1)
when freeselection = 'SAME' then (case when promosku_2 ='(' then concat(promosku_1,freequantity_1)
else concat(concat(promosku_1,freequantity_1),concat('+',concat(promosku_2,freequantity_2))) end)
when freesku_2 ='(' then concat(freesku_1,freequantity_1)
else concat(concat(freesku_1,freequantity_1),concat('+',concat(freesku_2,freequantity_2)))
end free_sku,
startdate,
enddate,
'' promo,
'' Moq,
'' mov
from
(
select id , name , brandid ,
type,
promobrand,
concat(split_part(promosku,';',1),'(') promosku_1,
concat(split_part(promosku,';',2),'(') promosku_2,
concat(split_part(promoquantity,';',1),')') promoquantity_1,
concat(split_part(promoquantity,';',2),')') promoquantity_2,
promoselection,
freebrand,
concat(split_part(freesku,';',1),'(') freesku_1,
concat(split_part(freesku,';',2),'(') freesku_2,
concat(split_part(freequantity,';',1),')') freequantity_1,
concat(split_part(freequantity,';',2),')') freequantity_2,
freeselection,
startdate,
enddate
from brand_promo_master A
left join buyngetx_promo B on A.id = B.promo_id
left join buyngetx_free C on A.id = C.promo_id
where type = 'BUY_N_GET_X'
order by 1) AA
left join raena_catalog_management.brand BB on AA.brandid = BB.id
order by startdate ;
drop table if exists public.clevertap_conversion_base;
--cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date)
create table public.clevertap_conversion_base as
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
1 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.app_launched
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
2 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.view_item
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
3 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.view_cart
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
4 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.begin_checkout
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
5 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.finish_checkout
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
6 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.charged;
select count(1),max(transaction_date) from public.clevertap_conversion_base;
drop table if exists public.clevertap_checkout_base;
create table public.clevertap_checkout_base as
SELECT distinct profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
4 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.begin_checkout
UNION ALL
SELECT distinct profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
5 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.finish_checkout;
select count(1) , max(transaction_date) from public.clevertap_checkout_base;
drop table if exists public.order_base;
create table public.order_base
as
SELECT min((cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date))) AS transaction_date ,
profile_phone
FROM clevertap.app_installed
group by profile_phone;
drop table if exists public.usder_base;
create table public.usder_base
as
SELECT A.name ,
A.id ,
B.name tier_name,
A.mobile ,
A.email,
A.province,
A.area_id
FROM raena_user_management.user A
LEFT JOIN raena_user_management.tier B ON A.tier_id = B.id;
DROP TABLE IF EXISTS public.order_clevertab_stage1;
CREATE TABLE public.order_clevertab_stage1
as
SELECT A.transaction_date ,
tier_name,
name Reseller_name,
replace(mobile,'+','') mobile,
email ,
province,
area_id,
id reseller_id
FROM public.order_base A
LEFT join public.usder_base B ON A.profile_phone= replace(mobile,'+','')
ORDER BY id ;
DROP TABLE IF EXISTS public.user_type_stage;
CREATE TABLE public.user_type_stage AS
SELECT A.transaction_date,
profile_phone,
profile_email,
B.Reseller_name,
CASE
WHEN cast(A.transaction_date as date) between B.transaction_date and dateadd(day,30,B.transaction_date) THEN 'New'
ELSE 'Existing'
end user_type,
B.tier_name,
province,
area_id,
count(CASE WHEN eventname = 'begin_checkout' THEN coalesce(profile_phone,profile_email) END) dst_begin_checkout,
count(CASE WHEN eventname = 'finish_checkout' THEN coalesce(profile_phone,profile_email) END) dst_finish_checkout
FROM public.clevertap_checkout_base A
LEFT JOIN public.order_clevertab_stage1 B ON profile_phone =B.mobile
WHERE eventname IN ('begin_checkout',
'finish_checkout')
GROUP BY A.transaction_date,
profile_phone,
profile_email,
B.Reseller_name,
CASE
WHEN cast(A.transaction_date as date) between B.transaction_date and dateadd(day,30,B.transaction_date) THEN 'New'
ELSE 'Existing'
end,
B.tier_name,
province,
area_id
ORDER BY 1 ;
drop table if exists public.final_clevertab_stage;
create table public.final_clevertab_stage as
select transaction_date,
profile_phone,
profile_email,
Reseller_name,
User_type,
tier_name,
products,
province,
area_id,
cart_id,
current_date-transaction_date as age,
sum(dst_begin_checkout) dst_begin_checkout,
sum(dst_finish_checkout) dst_finish_checkout
from public.user_type_stage A
left join (select user_id ,products,replace(mobile,'+','') mobile, email ,A.id cart_id , rank() over(partition by user_id order by A.created_at desc) rnk
from raena_cart_management.cart A left join raena_user_management.user B on A.user_id = B.id
where cleared = 'false'
order by mobile)B on A.profile_phone = B.mobile and B.rnk= 1
group by transaction_date,
profile_phone,
profile_email,
Reseller_name,
User_type,
tier_name,
cart_id,
products,
province,
area_id,
current_date-transaction_date ;
select count(1) , max(transaction_date) from public.final_clevertab_stage;
DROP TABLE IF EXISTS public.sku_wholesale_price ;
CREATE TABLE public.sku_wholesale_price AS
SELECT sku,
case when json_extract_path_text(tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE)='' then '0' else json_extract_path_text(tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) end bronze_price,
case when json_extract_path_text(tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE)='' then '0' else json_extract_path_text(tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) end silver_price,
case when json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)='' then '0' else json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end gold_price,
case when json_extract_path_text(slashed_tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE)='' then '0' else json_extract_path_text(slashed_tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) end slashed_bronze_price,
case when json_extract_path_text(slashed_tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE)='' then '0' else json_extract_path_text(slashed_tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) end slashed_silver_price,
case when json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)='' then '0' else json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end slashed_gold_price,
retail_price ,
cast((height*width*length)/6000 as decimal(22,2)) volume_weight,
weight
FROM raena_catalog_management.product ;
DROP TABLE if exists seq_0_to_1000;
CREATE TABLE seq_0_to_1000 AS
(SELECT row_number() over (
ORDER BY TRUE)::integer - 1 AS i
FROM public.final_clevertab_stage LIMIT 1000);
DROP table if exists public.final_clevertab_sku_stage;
CREATE TABLE public.final_clevertab_sku_stage AS WITH exploded_array AS
(SELECT *,
JSON_EXTRACT_ARRAY_ELEMENT_TEXT(products, seq.i) AS json
FROM public.final_clevertab_stage,
seq_0_to_1000 AS seq
WHERE seq.i < JSON_ARRAY_LENGTH(products))
SELECT transaction_date ,
profile_phone ,
profile_email ,
reseller_name ,
user_type ,
tier_name,
province,
area_id,
cart_id,
age,
dst_begin_checkout ,
dst_finish_checkout ,
json_extract_path_text(json,'sku',TRUE) sku_name,
json_extract_path_text(json,'quantity',TRUE) quantity
FROM exploded_array;
select count(1) , max(transaction_date) from public.final_clevertab_sku_stage;
drop table if exists public.final_clevertab_sku_stage2;
create table public.final_clevertab_sku_stage2
as
SELECT A.*,
CASE WHEN tier_name = 'GOLD' and gold_price<>'' THEN cast(gold_price as decimal(22,4))
WHEN tier_name = 'SILVER' and silver_price<>'' THEN cast(silver_price as decimal(22,4))
else cast(bronze_price as decimal(22,4))
END wholesale_price,
case when volume_weight>weight then volume_weight else weight end sku_weight,
CASE
WHEN ((case when volume_weight>weight then volume_weight else weight end)*Quantity)<1.3 THEN 1
WHEN ((case when volume_weight>weight then volume_weight else weight end)*Quantity)>=1.3
AND (ABS(((case when volume_weight>weight then volume_weight else weight end)*Quantity)) - FLOOR(ABS(((case when volume_weight>weight then volume_weight else weight end)*Quantity)))) BETWEEN 0.3 AND 0.999999
THEN FLOOR(((case when volume_weight>weight then volume_weight else weight end)*Quantity))+1
ELSE FLOOR(((case when volume_weight>weight then volume_weight else weight end)*Quantity))
END AS final_weight,
price
FROM public.final_clevertab_sku_stage a
LEFT JOIN public.sku_wholesale_price b ON a.sku_name = B.sku
left join (select destination_area_id , min(price) price from raena_transport_management.logistic_rate
group by 1) C on A.area_id = C.destination_area_id;
select count(1) , max(transaction_date) from public.final_clevertab_sku_stage2;
DROP TABLE IF EXISTS public.final_clevertab;
CREATE TABLE public.final_clevertab AS
SELECT transaction_date ,
profile_phone ,
profile_email,
reseller_name,
user_type,
tier_name,
province,
cart_id,
age,
dst_begin_checkout,
dst_finish_checkout,
sku_name ,
cast(quantity as int)quantity,
cast(wholesale_price as int) wholesale_price,
final_weight,
price
FROM public.final_clevertab_sku_stage2 ;
select count(1) , max(transaction_date) from public.final_clevertab;

View File

@ -0,0 +1,365 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
drop table if exists public.brand_promo_dashboard_table ;
create table public.brand_promo_dashboard_table
as
select BB.name brand_name, type promo_type, sku ,'' free_sku, startdate , enddate, promo , Moq ,mov
from public.brand_promo_master AA
left join raena_catalog_management.brand BB on AA.brandid = BB.id
union
select BB.name brand_name,type ,
case when promoselection = 'ANY' then concat('ANY (', promoquantity_1)
when promosku_2 ='(' then concat(promosku_1,promoquantity_1)
else concat(concat(promosku_1,promoquantity_1),concat('+',concat(promosku_2,promoquantity_2))) end Promo_sku,
case when freeselection = 'ANY' then concat('ANY (', freequantity_1)
when freeselection = 'SAME' then (case when promosku_2 ='(' then concat(promosku_1,freequantity_1)
else concat(concat(promosku_1,freequantity_1),concat('+',concat(promosku_2,freequantity_2))) end)
when freesku_2 ='(' then concat(freesku_1,freequantity_1)
else concat(concat(freesku_1,freequantity_1),concat('+',concat(freesku_2,freequantity_2)))
end free_sku,
startdate,
enddate,
'' promo,
'' Moq,
'' mov
from
(
select id , name , brandid ,
type,
promobrand,
concat(split_part(promosku,';',1),'(') promosku_1,
concat(split_part(promosku,';',2),'(') promosku_2,
concat(split_part(promoquantity,';',1),')') promoquantity_1,
concat(split_part(promoquantity,';',2),')') promoquantity_2,
promoselection,
freebrand,
concat(split_part(freesku,';',1),'(') freesku_1,
concat(split_part(freesku,';',2),'(') freesku_2,
concat(split_part(freequantity,';',1),')') freequantity_1,
concat(split_part(freequantity,';',2),')') freequantity_2,
freeselection,
startdate,
enddate
from brand_promo_master A
left join buyngetx_promo B on A.id = B.promo_id
left join buyngetx_free C on A.id = C.promo_id
where type = 'BUY_N_GET_X'
order by 1) AA
left join raena_catalog_management.brand BB on AA.brandid = BB.id
order by startdate ;
drop table if exists public.clevertap_conversion_base;
--cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date)
create table public.clevertap_conversion_base as
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
1 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.app_launched
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
2 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.view_item
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
3 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.view_cart
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
4 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.begin_checkout
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
5 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.finish_checkout
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
6 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.charged;
select count(1),max(transaction_date) from public.clevertap_conversion_base;
drop table if exists public.clevertap_checkout_base;
create table public.clevertap_checkout_base as
SELECT distinct profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
4 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.begin_checkout
UNION ALL
SELECT distinct profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
5 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.finish_checkout;
select count(1) , max(transaction_date) from public.clevertap_checkout_base;
drop table if exists public.order_base;
create table public.order_base
as
SELECT min((cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date))) AS transaction_date ,
profile_phone
FROM clevertap.app_installed
group by profile_phone;
drop table if exists public.usder_base;
create table public.usder_base
as
SELECT A.name ,
A.id ,
B.name tier_name,
A.mobile ,
A.email,
A.province,
A.area_id
FROM raena_user_management.user A
LEFT JOIN raena_user_management.tier B ON A.tier_id = B.id;
DROP TABLE IF EXISTS public.order_clevertab_stage1;
CREATE TABLE public.order_clevertab_stage1
as
SELECT A.transaction_date ,
tier_name,
name Reseller_name,
replace(mobile,'+','') mobile,
email ,
province,
area_id,
id reseller_id
FROM public.order_base A
LEFT join public.usder_base B ON A.profile_phone= replace(mobile,'+','')
ORDER BY id ;
DROP TABLE IF EXISTS public.user_type_stage;
CREATE TABLE public.user_type_stage AS
SELECT A.transaction_date,
profile_phone,
profile_email,
B.Reseller_name,
CASE
WHEN cast(A.transaction_date as date) between B.transaction_date and dateadd(day,30,B.transaction_date) THEN 'New'
ELSE 'Existing'
end user_type,
B.tier_name,
province,
area_id,
count(CASE WHEN eventname = 'begin_checkout' THEN coalesce(profile_phone,profile_email) END) dst_begin_checkout,
count(CASE WHEN eventname = 'finish_checkout' THEN coalesce(profile_phone,profile_email) END) dst_finish_checkout
FROM public.clevertap_checkout_base A
LEFT JOIN public.order_clevertab_stage1 B ON profile_phone =B.mobile
WHERE eventname IN ('begin_checkout',
'finish_checkout')
GROUP BY A.transaction_date,
profile_phone,
profile_email,
B.Reseller_name,
CASE
WHEN cast(A.transaction_date as date) between B.transaction_date and dateadd(day,30,B.transaction_date) THEN 'New'
ELSE 'Existing'
end,
B.tier_name,
province,
area_id
ORDER BY 1 ;
drop table if exists public.final_clevertab_stage;
create table public.final_clevertab_stage as
select transaction_date,
profile_phone,
profile_email,
Reseller_name,
User_type,
tier_name,
products,
province,
area_id,
cart_id,
current_date-transaction_date as age,
sum(dst_begin_checkout) dst_begin_checkout,
sum(dst_finish_checkout) dst_finish_checkout
from public.user_type_stage A
left join (select user_id ,products,replace(mobile,'+','') mobile, email ,A.id cart_id , rank() over(partition by user_id order by A.created_at desc) rnk
from raena_cart_management.cart A left join raena_user_management.user B on A.user_id = B.id
where cleared = 'false'
order by mobile)B on A.profile_phone = B.mobile and B.rnk= 1
group by transaction_date,
profile_phone,
profile_email,
Reseller_name,
User_type,
tier_name,
cart_id,
products,
province,
area_id,
current_date-transaction_date ;
select count(1) , max(transaction_date) from public.final_clevertab_stage;
DROP TABLE IF EXISTS public.sku_wholesale_price ;
CREATE TABLE public.sku_wholesale_price AS
SELECT sku,
case when json_extract_path_text(tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE)='' then '0' else json_extract_path_text(tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) end bronze_price,
case when json_extract_path_text(tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE)='' then '0' else json_extract_path_text(tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) end silver_price,
case when json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)='' then '0' else json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end gold_price,
case when json_extract_path_text(slashed_tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE)='' then '0' else json_extract_path_text(slashed_tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) end slashed_bronze_price,
case when json_extract_path_text(slashed_tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE)='' then '0' else json_extract_path_text(slashed_tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) end slashed_silver_price,
case when json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE)='' then '0' else json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) end slashed_gold_price,
retail_price ,
cast((height*width*length)/6000 as decimal(22,2)) volume_weight,
weight
FROM raena_catalog_management.product ;
DROP TABLE if exists seq_0_to_1000;
CREATE TABLE seq_0_to_1000 AS
(SELECT row_number() over (
ORDER BY TRUE)::integer - 1 AS i
FROM public.final_clevertab_stage LIMIT 1000);
DROP table if exists public.final_clevertab_sku_stage;
CREATE TABLE public.final_clevertab_sku_stage AS WITH exploded_array AS
(SELECT *,
JSON_EXTRACT_ARRAY_ELEMENT_TEXT(products, seq.i) AS json
FROM public.final_clevertab_stage,
seq_0_to_1000 AS seq
WHERE seq.i < JSON_ARRAY_LENGTH(products))
SELECT transaction_date ,
profile_phone ,
profile_email ,
reseller_name ,
user_type ,
tier_name,
province,
area_id,
cart_id,
age,
dst_begin_checkout ,
dst_finish_checkout ,
json_extract_path_text(json,'sku',TRUE) sku_name,
json_extract_path_text(json,'quantity',TRUE) quantity
FROM exploded_array;
select count(1) , max(transaction_date) from public.final_clevertab_sku_stage;
drop table if exists public.final_clevertab_sku_stage2;
create table public.final_clevertab_sku_stage2
as
SELECT A.*,
CASE WHEN tier_name = 'GOLD' and gold_price<>'' THEN cast(gold_price as decimal(22,4))
WHEN tier_name = 'SILVER' and silver_price<>'' THEN cast(silver_price as decimal(22,4))
else cast(bronze_price as decimal(22,4))
END wholesale_price,
case when volume_weight>weight then volume_weight else weight end sku_weight,
CASE
WHEN ((case when volume_weight>weight then volume_weight else weight end)*Quantity)<1.3 THEN 1
WHEN ((case when volume_weight>weight then volume_weight else weight end)*Quantity)>=1.3
AND (ABS(((case when volume_weight>weight then volume_weight else weight end)*Quantity)) - FLOOR(ABS(((case when volume_weight>weight then volume_weight else weight end)*Quantity)))) BETWEEN 0.3 AND 0.999999
THEN FLOOR(((case when volume_weight>weight then volume_weight else weight end)*Quantity))+1
ELSE FLOOR(((case when volume_weight>weight then volume_weight else weight end)*Quantity))
END AS final_weight,
price
FROM public.final_clevertab_sku_stage a
LEFT JOIN public.sku_wholesale_price b ON a.sku_name = B.sku
left join (select destination_area_id , min(price) price from raena_transport_management.logistic_rate
group by 1) C on A.area_id = C.destination_area_id;
select count(1) , max(transaction_date) from public.final_clevertab_sku_stage2;
DROP TABLE IF EXISTS public.final_clevertab;
CREATE TABLE public.final_clevertab AS
SELECT transaction_date ,
profile_phone ,
profile_email,
reseller_name,
user_type,
tier_name,
province,
cart_id,
age,
dst_begin_checkout,
dst_finish_checkout,
sku_name ,
cast(quantity as int)quantity,
cast(wholesale_price as int) wholesale_price,
final_weight,
price
FROM public.final_clevertab_sku_stage2 ;
select count(1) , max(transaction_date) from public.final_clevertab;
" > /home/ec2-user/cronjob/clevertap_funnel_reports/clevertap_funnel_reports.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/clevertap_funnel_reports/clevertap_funnel_reports.sql

View File

@ -0,0 +1,12 @@
--------------------------Consignement Business metrics--------------------------
drop table if exists OM_Order_item_Cogs_Promo_dump_Consignment
create table OM_Order_item_Cogs_Promo_dump_Consignment
as
select distinct transaction_date,brand_name,external_id as order_id,a.sku,quantity,cogs as Cogs Per Unit Applied,(cogs*quantity) as Total Cogs,Shipping_province,
case when b.cogs_promo notnull then 'Promo Cogs' when b.cogs_non_promo notnull then 'Non Promo Cogs' end as Cogs Type
from public.GM_dashboard a
left join public.sku_cogs_audit b on a.sku=b.sku and cast(a.transaction_date as date)=cast(b.created_at as date)

20
consignement_code Normal file
View File

@ -0,0 +1,20 @@
#!/bin/bash
echo -e " \n----------- Consignement Metabase Dashboard Code --------------\n"
echo "
--------------------------Consignement Business metrics--------------------------
drop table if exists OM_Order_item_Cogs_Promo_dump_Consignment
create table OM_Order_item_Cogs_Promo_dump_Consignment
as
select distinct transaction_date,brand_name,external_id as order_id,a.sku,quantity,cogs as "Cogs Per Unit Applied",(cogs*quantity) as "Total Cogs",Shipping_province,
case when b.cogs_promo notnull then 'Promo Cogs' when b.cogs_non_promo notnull then 'Non Promo Cogs' end as "Cogs Type"
from public.GM_dashboard a
left join public.sku_cogs_audit b on a.sku=b.sku and cast(a.transaction_date as date)=cast(b.created_at as date)
" > /home/ec2-user/cronjob/consignement/consignement_code.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z"-f /home/ec2-user/cronjob/new_users/consignement/consignement_code.sql > consignement_code.log

0
consignement_code.log Normal file
View File

1
cron.sh Executable file
View File

@ -0,0 +1 @@
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/Final_GM_v1.sql > query.log

View File

@ -0,0 +1,995 @@
--------------------------Order Level Data --------------------------
Drop table if exists public.Gm_final_table;
CREATE TABLE public.Gm_final_table AS
SELECT A.external_id,
cast(transaction_date as date) transaction_date ,
A.order_discount_amount,
A.shipping_cost,
A.shipping_province,
A.coupon_code,
A.discount_type,
A.coupon_applied_on,
A.brand_name,
B.product_type,
A.category_name,
B.name sku_name,
A.reseller_name,
A.reseller_email,
A.reseller_mobile,
A.tier_name ,
A.reseller_id,
A.sku,
A.quantity,
A.retail_price,
A.seller_margin,
A.discounted_price,
A.additional_discount,
A.item_type ,
A.cogs
FROM gm_dashboard A
LEFT JOIN raena_catalog_management.product B ON A.sku = B.sku;
Drop table if exists public.final_order_level_data;
CREATE TABLE public.final_order_level_data AS
SELECT A.*,
CURRENT_DATE,
date_part('Month',CURRENT_DATE) month_of_date,
date_part('Week',CURRENT_DATE) Week_of_date,
date_part('Day',CURRENT_DATE) Day_of_date,
retail_price*quantity AS revenue_on_retail,
(retail_price*quantity)-(seller_margin*quantity) AS revenue_on_wholesale,
(discounted_price*quantity) AS revenue_on_after_discount,
(cogs*quantity) AS total_cogs,
case when retail_price<>0 then cast(((cast(((retail_price*quantity)-(seller_margin*quantity)) AS decimal(22,2))-(cast(cogs*quantity AS decimal(22,2))))/cast(((retail_price*quantity)-(seller_margin*quantity)) AS decimal(22,2)))*100 AS decimal(22,2)) end pre_discount_gm,
case when discounted_price<>0 then cast(((cast((discounted_price*quantity) AS decimal(22,2))-(cast(cogs*quantity AS decimal(22,2))))/cast(((discounted_price*quantity)) AS decimal(22,2)))*100 AS decimal(22,2)) end post_discount_gm,
shipping_city,
shipping_pincode,
shipping_country_code,
order_warehouse
FROM public.Gm_final_table A
LEFT JOIN
(SELECT id ,
shipping_city,
shipping_pincode,
shipping_country_code,
order_warehouse
FROM raena_order_management.order
WHERE cast(created_at AS date) >=dateadd(d,-120,'2023-11-22')) B ON external_id =id
WHERE discounted_price <>0 and cast(transaction_date AS date) between dateadd(d,-120,'2023-11-22') and '2023-11-22';
-------------------------------------------SKU Level Data ---------------------------------
DROP TABLE IF EXISTS public.sku_warehouse_split_stage1;
CREATE TABLE public.sku_warehouse_split_stage1 AS
SELECT sku ,
CASE
WHEN shipping_province IN ('Kalimantan Timur',
'Kalimantan Utara',
'Kalimantan Selatan',
'Kalimantan Tengah',
'Kalimantan Barat') THEN 'Balikpapan - Semarandi'
WHEN shipping_province IN ('DKI Jakarta',
'Banten',
'Jawa Barat') THEN 'Bekasi'
WHEN shipping_province IN ('Sulawesi Selatan',
'Sulawesi Tengah',
'Gorontalo',
'Papua',
'Maluku Utara',
'Sulawesi Utara',
'Maluku',
'Sulawesi Tenggara',
'Papua Barat',
'Sulawesi Barat') THEN 'Makassar'
WHEN shipping_province IN ('Sumatera Utara',
'Sumatera Selatan',
'Riau',
'Sumatera Barat',
'Lampung',
'Aceh',
'Bangka Belitung',
'Jambi',
'Nanggroe Aceh Darussalam (NAD)',
'Kepulauan Riau',
'Bengkulu') THEN 'Pekanbaru - Medan'
WHEN shipping_province IN ('Jawa Tengah',
'DI Yogyakarta') THEN 'Semarang'
WHEN shipping_province IN ('Jawa Timur',
'Bali',
'Nusa Tenggara Timur (NTT)',
'Nusa Tenggara Timur',
'Nusa Tenggara Barat',
'Nusa Tenggara Barat (NTB)') THEN 'Surabaya'
END warehouse,
CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'2023-11-22') AND '2023-11-22' THEN 'L7D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2023-11-22') AND dateadd(d,-7,'2023-11-22') THEN 'L14D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2023-11-22') AND dateadd(d,-14,'2023-11-22') THEN 'L21D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2023-11-22') AND dateadd(d,-21,'2023-11-22') THEN 'L28D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2023-11-22') AND dateadd(d,-28,'2023-11-22') THEN 'L35D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2023-11-22') AND dateadd(d,-35,'2023-11-22') THEN 'L42D'
END Bucket,
Total_revenue,
sum(revenue_on_after_discount) revenue_on_after_discount,
sum(revenue_on_after_discount)*100/Total_revenue AS contribution
FROM public.final_order_level_data A
LEFT JOIN
(SELECT CASE
WHEN shipping_province IN ('Kalimantan Timur',
'Kalimantan Utara',
'Kalimantan Selatan',
'Kalimantan Tengah',
'Kalimantan Barat') THEN 'Balikpapan - Semarandi'
WHEN shipping_province IN ('DKI Jakarta',
'Banten',
'Jawa Barat') THEN 'Bekasi'
WHEN shipping_province IN ('Sulawesi Selatan',
'Sulawesi Tengah',
'Gorontalo',
'Papua',
'Maluku Utara',
'Sulawesi Utara',
'Maluku',
'Sulawesi Tenggara',
'Papua Barat',
'Sulawesi Barat') THEN 'Makassar'
WHEN shipping_province IN ('Sumatera Utara',
'Sumatera Selatan',
'Riau',
'Sumatera Barat',
'Lampung',
'Aceh',
'Bangka Belitung',
'Jambi',
'Nanggroe Aceh Darussalam (NAD)',
'Kepulauan Riau',
'Bengkulu') THEN 'Pekanbaru - Medan'
WHEN shipping_province IN ('Jawa Tengah',
'DI Yogyakarta') THEN 'Semarang'
WHEN shipping_province IN ('Jawa Timur',
'Bali',
'Nusa Tenggara Timur (NTT)',
'Nusa Tenggara Timur',
'Nusa Tenggara Barat',
'Nusa Tenggara Barat (NTB)') THEN 'Surabaya'
END warehouse,
CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'2023-11-22') AND '2023-11-22' THEN 'L7D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2023-11-22') AND dateadd(d,-7,'2023-11-22') THEN 'L14D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2023-11-22') AND dateadd(d,-14,'2023-11-22') THEN 'L21D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2023-11-22') AND dateadd(d,-21,'2023-11-22') THEN 'L28D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2023-11-22') AND dateadd(d,-28,'2023-11-22') THEN 'L35D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2023-11-22') AND dateadd(d,-35,'2023-11-22') THEN 'L42D'
END Bucket,
sum(coalesce (revenue_on_after_discount,0)) Total_revenue
FROM public.final_order_level_data
WHERE cast(transaction_date as date) BETWEEN dateadd(d,-41,'2023-11-22') AND '2023-11-22'
GROUP BY CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'2023-11-22') AND '2023-11-22' THEN 'L7D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2023-11-22') AND dateadd(d,-7,'2023-11-22') THEN 'L14D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2023-11-22') AND dateadd(d,-14,'2023-11-22') THEN 'L21D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2023-11-22') AND dateadd(d,-21,'2023-11-22') THEN 'L28D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2023-11-22') AND dateadd(d,-28,'2023-11-22') THEN 'L35D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2023-11-22') AND dateadd(d,-35,'2023-11-22') THEN 'L42D'
END,
CASE
WHEN shipping_province IN ('Kalimantan Timur',
'Kalimantan Utara',
'Kalimantan Selatan',
'Kalimantan Tengah',
'Kalimantan Barat') THEN 'Balikpapan - Semarandi'
WHEN shipping_province IN ('DKI Jakarta',
'Banten',
'Jawa Barat') THEN 'Bekasi'
WHEN shipping_province IN ('Sulawesi Selatan',
'Sulawesi Tengah',
'Gorontalo',
'Papua',
'Maluku Utara',
'Sulawesi Utara',
'Maluku',
'Sulawesi Tenggara',
'Papua Barat',
'Sulawesi Barat') THEN 'Makassar'
WHEN shipping_province IN ('Sumatera Utara',
'Sumatera Selatan',
'Riau',
'Sumatera Barat',
'Lampung',
'Aceh',
'Bangka Belitung',
'Jambi',
'Nanggroe Aceh Darussalam (NAD)',
'Kepulauan Riau',
'Bengkulu') THEN 'Pekanbaru - Medan'
WHEN shipping_province IN ('Jawa Tengah',
'DI Yogyakarta') THEN 'Semarang'
WHEN shipping_province IN ('Jawa Timur',
'Bali',
'Nusa Tenggara Timur (NTT)',
'Nusa Tenggara Timur',
'Nusa Tenggara Barat',
'Nusa Tenggara Barat (NTB)') THEN 'Surabaya'
END) B ON CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'2023-11-22') AND '2023-11-22' THEN 'L7D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2023-11-22') AND dateadd(d,-7,'2023-11-22') THEN 'L14D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2023-11-22') AND dateadd(d,-14,'2023-11-22') THEN 'L21D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2023-11-22') AND dateadd(d,-21,'2023-11-22') THEN 'L28D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2023-11-22') AND dateadd(d,-28,'2023-11-22') THEN 'L35D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2023-11-22') AND dateadd(d,-35,'2023-11-22') THEN 'L42D'
END=B.bucket
AND CASE
WHEN shipping_province IN ('Kalimantan Timur',
'Kalimantan Utara',
'Kalimantan Selatan',
'Kalimantan Tengah',
'Kalimantan Barat') THEN 'Balikpapan - Semarandi'
WHEN shipping_province IN ('DKI Jakarta',
'Banten',
'Jawa Barat') THEN 'Bekasi'
WHEN shipping_province IN ('Sulawesi Selatan',
'Sulawesi Tengah',
'Gorontalo',
'Papua',
'Maluku Utara',
'Sulawesi Utara',
'Maluku',
'Sulawesi Tenggara',
'Papua Barat',
'Sulawesi Barat') THEN 'Makassar'
WHEN shipping_province IN ('Sumatera Utara',
'Sumatera Selatan',
'Riau',
'Sumatera Barat',
'Lampung',
'Aceh',
'Bangka Belitung',
'Jambi',
'Nanggroe Aceh Darussalam (NAD)',
'Kepulauan Riau',
'Bengkulu') THEN 'Pekanbaru - Medan'
WHEN shipping_province IN ('Jawa Tengah',
'DI Yogyakarta') THEN 'Semarang'
WHEN shipping_province IN ('Jawa Timur',
'Bali',
'Nusa Tenggara Timur (NTT)',
'Nusa Tenggara Timur',
'Nusa Tenggara Barat',
'Nusa Tenggara Barat (NTB)') THEN 'Surabaya'
END=warehouse
WHERE cast(transaction_date as date) BETWEEN dateadd(d,-41,'2023-11-22') AND '2023-11-22'
GROUP BY sku ,
CASE
WHEN shipping_province IN ('Kalimantan Timur',
'Kalimantan Utara',
'Kalimantan Selatan',
'Kalimantan Tengah',
'Kalimantan Barat') THEN 'Balikpapan - Semarandi'
WHEN shipping_province IN ('DKI Jakarta',
'Banten',
'Jawa Barat') THEN 'Bekasi'
WHEN shipping_province IN ('Sulawesi Selatan',
'Sulawesi Tengah',
'Gorontalo',
'Papua',
'Maluku Utara',
'Sulawesi Utara',
'Maluku',
'Sulawesi Tenggara',
'Papua Barat',
'Sulawesi Barat') THEN 'Makassar'
WHEN shipping_province IN ('Sumatera Utara',
'Sumatera Selatan',
'Riau',
'Sumatera Barat',
'Lampung',
'Aceh',
'Bangka Belitung',
'Jambi',
'Nanggroe Aceh Darussalam (NAD)',
'Kepulauan Riau',
'Bengkulu') THEN 'Pekanbaru - Medan'
WHEN shipping_province IN ('Jawa Tengah',
'DI Yogyakarta') THEN 'Semarang'
WHEN shipping_province IN ('Jawa Timur',
'Bali',
'Nusa Tenggara Timur (NTT)',
'Nusa Tenggara Timur',
'Nusa Tenggara Barat',
'Nusa Tenggara Barat (NTB)') THEN 'Surabaya'
END,
CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'2023-11-22') AND '2023-11-22' THEN 'L7D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2023-11-22') AND dateadd(d,-7,'2023-11-22') THEN 'L14D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2023-11-22') AND dateadd(d,-14,'2023-11-22') THEN 'L21D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2023-11-22') AND dateadd(d,-21,'2023-11-22') THEN 'L28D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2023-11-22') AND dateadd(d,-28,'2023-11-22') THEN 'L35D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2023-11-22') AND dateadd(d,-35,'2023-11-22') THEN 'L42D'
END ,
Total_revenue;
DROP TABLE IF EXISTS public.sku_warehouse_split_stage2;
CREATE TABLE public.sku_warehouse_split_stage2 AS
SELECT sku ,
warehouse ,
sum(CASE WHEN bucket = 'L7D' THEN contribution END) L7D_sku_contribution,
sum(CASE WHEN bucket = 'L14D' THEN contribution END) L14D_sku_contribution,
sum(CASE WHEN bucket = 'L21D' THEN contribution END) L21D_sku_contribution,
sum(CASE WHEN bucket = 'L28D' THEN contribution END) L28D_sku_contribution,
sum(CASE WHEN bucket = 'L35D' THEN contribution END) L35D_sku_contribution,
sum(CASE WHEN bucket = 'L42D' THEN contribution END) L42D_sku_contribution,
percentile_cont(0.5) within
GROUP (
ORDER BY contribution) median_contribution,
percentile_cont(0.95) within
GROUP (
ORDER BY contribution) Percentile95_contribution,
coalesce(stddev_samp(contribution),sum(contribution)) sd_contribution
FROM public.sku_warehouse_split_stage1
WHERE warehouse IS NOT NULL
GROUP BY sku,
warehouse ;
DROP TABLE IF EXISTS public.sku_warehouse_split_stage3;
CREATE TABLE public.sku_warehouse_split_stage3 AS
SELECT * , (CASE
WHEN sd_contribution <= 0.1 THEN median_contribution
ELSE Percentile95_contribution
END)sku_qty_split_next_order
FROM public.sku_warehouse_split_stage2;
DROP TABLE IF EXISTS public.sku_warehouse_split_final;
CREATE TABLE public.sku_warehouse_split_final AS
SELECT sku,
warehouse,
l7d_sku_contribution,
L14D_sku_contribution,
L21D_sku_contribution,
L28D_sku_contribution,
L35D_sku_contribution,
L42D_sku_contribution,
median_contribution,
Percentile95_contribution,
sd_contribution,
case when sum(sku_qty_split_next_order)over(partition BY sku)= 0 then 0 else sku_qty_split_next_order*100/sum(sku_qty_split_next_order)over(partition BY sku) end sku_qty_split_next_order_percentage
FROM public.sku_warehouse_split_stage3;
--------------------------------------Warehouse SKu Split----------------------------------
DROP TABLE IF EXISTS public.sku_level_data ;
CREATE TABLE public.sku_level_data AS
SELECT A.sku,
A.name sku_name,
A.is_private,
A.country,
CASE
WHEN A.is_archived = 'true' THEN 'Permanatly_delisted'
WHEN A.is_delisted = 'true' THEN 'delisted'
WHEN A.is_archived = 'false'
AND A.is_delisted = 'false' THEN 'Active'
END active_status,
A.created_at,
B.storage_location storage_type,
A.origin sourcing,
case when C.business_type like '%Consignment%' then 'Consignment' else 'Outright' end ordering_type ,
D.name brand_name ,
A.product_type ,
E.name category_name,
F.cogs ,
CASE
WHEN A.is_slash_price ='true'THEN cast(json_extract_path_text(slashed_tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2))
ELSE cast(json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2))
END effective_wholesale_price,
CASE
WHEN A.origin= 'Local' THEN coalesce(G.bekasi_buffer_time,2)
WHEN A.origin = 'Crossborder' THEN 15
END Bekasi_buffer_lead_time,
CASE
WHEN A.origin= 'Local' THEN coalesce(G.bekasi_lead_time,5)
WHEN A.origin = 'Crossborder' THEN 50
END bekasi_lead_time,
CASE
WHEN A.origin= 'Local' THEN (coalesce(G.bekasi_lead_time,5)+coalesce(G.bekasi_buffer_time,2))
WHEN A.origin = 'Crossborder' THEN 65
END bekasi_total_lead_time,
CASE
WHEN origin= 'Local' THEN 45
WHEN origin = 'Crossborder' THEN 30
END bekasi_inventory_days,
CASE
WHEN origin= 'Local' THEN coalesce(G.semarinda_buffer_time,2)
WHEN origin = 'Crossborder' THEN 15
END Balikpapan_Semarandi_buffer_lead_time,
CASE
WHEN origin= 'Local' THEN coalesce(G.semarinda_lead_time,12)
WHEN origin = 'Crossborder' THEN 50
END Balikpapan_Semarandi_lead_time,
CASE
WHEN origin= 'Local' THEN (coalesce(G.semarinda_lead_time,12)+coalesce(G.semarinda_buffer_time,2))
WHEN origin = 'Crossborder' THEN 65
END Balikpapan_Semarandi_total_lead_time,
CASE
WHEN origin= 'Local' THEN 30
WHEN origin = 'Crossborder' THEN 30
END Balikpapan_Semarandi_inventory_days,
CASE
WHEN origin= 'Local' THEN coalesce(G.pekanbaru_buffer_time,2)
WHEN origin = 'Crossborder' THEN 15
END Pekanbaru_Medan_buffer_lead_time,
CASE
WHEN origin= 'Local' THEN coalesce(G.pekanbaru_lead_time,12)
WHEN origin = 'Crossborder' THEN 50
END Pekanbaru_Medan_lead_time,
CASE
WHEN origin= 'Local' THEN (coalesce(G.pekanbaru_lead_time,12)+coalesce(G.pekanbaru_buffer_time,2))
WHEN origin = 'Crossborder' THEN 65
END Pekanbaru_Medan_total_lead_time,
CASE
WHEN origin= 'Local' THEN 30
WHEN origin = 'Crossborder' THEN 30
END Pekanbaru_Medan_inventory_days,
CASE
WHEN origin= 'Local' THEN coalesce(semarang_buffer_time,2)
WHEN origin = 'Crossborder' THEN 15
END Semarang_buffer_lead_time,
CASE
WHEN origin= 'Local' THEN coalesce(semarang_lead_time,7)
WHEN origin = 'Crossborder' THEN 50
END Semarang_lead_time,
CASE
WHEN origin= 'Local' THEN ( coalesce(semarang_lead_time,7)+coalesce(semarang_buffer_time,2))
WHEN origin = 'Crossborder' THEN 65
END Semarang_total_lead_time,
CASE
WHEN origin= 'Local' THEN 30
WHEN origin = 'Crossborder' THEN 30
END Semarang_inventory_days,
CASE
WHEN origin= 'Local' THEN coalesce(surabaya_buffer_time,2)
WHEN origin = 'Crossborder' THEN 15
END Surabaya_buffer_lead_time,
CASE
WHEN origin= 'Local' THEN coalesce(surabaya_lead_time,7)
WHEN origin = 'Crossborder' THEN 50
END Surabaya_lead_time,
CASE
WHEN origin= 'Local' THEN coalesce(surabaya_lead_time,7)+coalesce(surabaya_buffer_time,2)
WHEN origin = 'Crossborder' THEN 65
END Surabaya_total_lead_time,
CASE
WHEN origin= 'Local' THEN 30
WHEN origin = 'Crossborder' THEN 30
END Surabaya_inventory_days,
CASE
WHEN origin= 'Local' THEN coalesce(makassar_buffer_time,2)
WHEN origin = 'Crossborder' THEN 15
END Makassar_buffer_lead_time,
CASE
WHEN origin= 'Local' THEN coalesce(makassar_lead_time,12)
WHEN origin = 'Crossborder' THEN 50
END Makassar_lead_time,
CASE
WHEN origin= 'Local' THEN (coalesce(makassar_lead_time,12)+coalesce(makassar_buffer_time,2))
WHEN origin = 'Crossborder' THEN 65
END Makassar_total_lead_time,
CASE
WHEN origin= 'Local' THEN 30
WHEN origin = 'Crossborder' THEN 30
END Makassar_inventory_days,
multiple_flag,
G.moq,
moqremark,
case when moqremark = 'NOT ORDER GIVE AWAY ITEM' then 'Yes' else 'No' end give_away
FROM raena_catalog_management.product A
LEFT JOIN public.storage_data B ON A.sku=B.sku
left join public.demand_forecasting_configs G on A.sku= G.sku
LEFT JOIN public.outright_consignment C ON A.sku = C.sku
LEFT JOIN raena_catalog_management.brand D ON A.brand_id = D.id
LEFT JOIN raena_catalog_management.category E ON A.category_id =E.id
LEFT JOIN (select distinct sku sku_code ,cogs_non_promo cogs from public.sku_cogs_audit sca where sku_cogs_action='CREATED') F ON A.sku= F.sku_code ;
---------------------------_basic Calculation -------------------------
DROP TABLE IF EXISTS public.basic_calculation;
CREATE TABLE public.basic_calculation AS
SELECT sku ,
CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2023-11-22') AND '2023-11-22' THEN 'L30D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2023-11-22') AND dateadd(d,-31,'2023-11-22') THEN 'L60D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2023-11-22') AND dateadd(d,-61,'2023-11-22') THEN 'L90D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2023-11-22') AND dateadd(d,-91,'2023-11-22') THEN 'L120D'
END txn_period,
sum(revenue_on_after_discount) revenue,
Total_revenue,
sum(revenue_on_after_discount)*100/Total_revenue AS contribution,
sum(quantity) quantity,
count(DISTINCT transaction_date) days,
cast(cast(sum(quantity) as FLOAT)/cast(count(DISTINCT cast(transaction_date as date) ) as FLOAT) as FLOAT) runrate
FROM public.final_order_level_data A
LEFT JOIN
(SELECT CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2023-11-22') AND '2023-11-22' THEN 'L30D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2023-11-22') AND dateadd(d,-31,'2023-11-22') THEN 'L60D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2023-11-22') AND dateadd(d,-61,'2023-11-22') THEN 'L90D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2023-11-22') AND dateadd(d,-91,'2023-11-22') THEN 'L120D'
END txn_period,
sum(coalesce (revenue_on_after_discount,0)) Total_revenue
FROM public.final_order_level_data
WHERE cast(transaction_date as date) BETWEEN dateadd(d,-120,'2023-11-22') AND '2023-11-22'
GROUP BY CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2023-11-22') AND '2023-11-22' THEN 'L30D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2023-11-22') AND dateadd(d,-31,'2023-11-22') THEN 'L60D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2023-11-22') AND dateadd(d,-61,'2023-11-22') THEN 'L90D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2023-11-22') AND dateadd(d,-91,'2023-11-22') THEN 'L120D'
END) B ON (CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2023-11-22') AND '2023-11-22' THEN 'L30D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2023-11-22') AND dateadd(d,-31,'2023-11-22') THEN 'L60D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2023-11-22') AND dateadd(d,-61,'2023-11-22') THEN 'L90D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2023-11-22') AND dateadd(d,-91,'2023-11-22') THEN 'L120D'
END)=B.txn_period
WHERE cast(transaction_date as date) BETWEEN dateadd(d,-120,'2023-11-22') AND '2023-11-22'
GROUP BY sku,
CASE
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2023-11-22') AND '2023-11-22' THEN 'L30D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2023-11-22') AND dateadd(d,-31,'2023-11-22') THEN 'L60D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2023-11-22') AND dateadd(d,-61,'2023-11-22') THEN 'L90D'
WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2023-11-22') AND dateadd(d,-91,'2023-11-22') THEN 'L120D'
END,
Total_revenue;
select 1;
DROP TABLE IF EXISTS public.basic_calculation_stage1;
CREATE TABLE public.basic_calculation_stage1 AS
SELECT A.sku ,
gold_price ,
CASE
WHEN L120D_revenue = 0
AND L90D_revenue = 0
AND L60D_revenue = 0 THEN 0
ELSE ((CASE WHEN L120D_revenue = 0 THEN 0 ELSE ((L90D_revenue -L120D_revenue)*100/L120D_revenue) END)
+ (CASE WHEN L90D_revenue= 0 THEN 0 ELSE ((L60D_revenue -L90D_revenue)*100/L90D_revenue) END)
+(CASE WHEN L60D_revenue = 0 THEN 0 ELSE ((L30D_revenue -L60D_revenue)*100/L60D_revenue)END)) / (CASE
WHEN L120D_revenue = 0 THEN 0
ELSE 1
END + CASE
WHEN L90D_revenue = 0 THEN 0
ELSE 1
END+CASE
WHEN L60D_revenue = 0 THEN 0
ELSE 1
END)
END AVG_revenue_growth
FROM
(SELECT sku ,
SUM(CASE WHEN txn_period ='L120D' THEN coalesce(revenue,0) ELSE 0 END) L120D_revenue,
SUM(CASE WHEN txn_period ='L90D' THEN coalesce(revenue,0) ELSE 0 END) L90D_revenue,
sum(CASE WHEN txn_period ='L60D' THEN coalesce(revenue,0) ELSE 0 END) L60D_revenue,
sum(CASE WHEN txn_period ='L30D' THEN coalesce(revenue,0) ELSE 0 END) L30D_revenue
FROM public.basic_calculation
GROUP BY sku) A
LEFT JOIN
(SELECT sku ,
json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) gold_price
FROM raena_catalog_management.product p) B ON A.sku = B.sku;
Drop table if exists public.basic_calculation_stage1_v1;
create table public.basic_calculation_stage1_v1 as
select A.*,
abs(contribution-median_contribution) for_max_contribution
from public.basic_calculation A
left join (select sku ,
MEDIAN(contribution) median_contribution
from public.basic_calculation group by sku ) B
on A.sku = B.sku ;
DROP TABLE IF EXISTS public.basic_calculation_stage2;
CREATE TABLE public.basic_calculation_stage2 AS
SELECT A.SKu ,
gold_price,
sum(CASE WHEN txn_period ='L30D' THEN coalesce(revenue,0) ELSE 0 END) L30D_revenue ,
B.AVG_revenue_growth ,
10 AS avg_expected_revenue_growth,
sum(CASE WHEN txn_period ='L30D' THEN coalesce(revenue,0) ELSE 0 END)*(1.10) expected_30D_revenue,
MEDIAN(contribution) median_of_contribution,
coalesce(stddev_samp(contribution),sum(contribution)) sd_of_contribution,
max(contribution) max_contribution ,
max(for_max_contribution) for_max_contribution,
avg(quantity) avg_quantity,
max(quantity) max_quantity,
case when L90D_revenue= 0 and L60D_revenue= 0 and L30D_revenue >0 then 'Yes' else 'No' end New_sku
FROM public.basic_calculation_stage1_v1 A
LEFT JOIN public.basic_calculation_stage1 B ON A.sku=B.sku
left join (select sku ,
coalesce(sum(case when txn_period ='L30D' then coalesce(revenue,0) end),0) as l30D_revenue,
coalesce(sum(case when txn_period ='L60D' then coalesce(revenue,0) end),0) as l60D_revenue,
coalesce(sum(case when txn_period ='L90D' then coalesce(revenue,0) end),0) as l90D_revenue
from public.basic_calculation_stage1_v1
where txn_period <> 'L120D'
group by sku) C On A.sku = C.sku
WHERE txn_period <> 'L120D'
GROUP BY A.SKu,
B.AVG_revenue_growth,
gold_price,
case when L90D_revenue= 0 and L60D_revenue= 0 and L30D_revenue >0 then 'Yes' else 'No' end;
--SELECT *FROM public.basic_calculation_stage2
DROP TABLE IF EXISTS public.basic_calculation_stage3;
/*
CREATE TABLE public.basic_calculation_stage3 AS
SELECT A.SKu,
percentile_cont(0.5) within
GROUP (
ORDER BY runrate) median_runrate,
max(runrate) max_runrate,
sum(CASE WHEN txn_period ='L30D' THEN quantity/days END)last_month_runrate,
CASE
WHEN --max(contribution)>0.02 or
txn_period ='L30D' and max(runrate)>= 30 THEN 'Fast Moving'
WHEN --max(contribution)>0.01 or
txn_period ='L30D' and max(runrate) between 5 and 29 THEN 'Slow Moving'
WHEN --max(contribution)<=0.01 or
txn_period ='L30D' and max(runrate)<5 THEN 'Not Moving'
END sku_type
FROM public.basic_calculation A
WHERE txn_period <> 'L120D'
GROUP BY A.SKu,txn_period;
*/
CREATE TABLE public.basic_calculation_stage3 AS
SELECT A.SKu,
percentile_cont(0.5) within
GROUP (
ORDER BY A.runrate) median_runrate,
max(A.runrate) max_runrate,
sum(CASE WHEN txn_period ='L30D' THEN quantity/days END)last_month_runrate,
CASE
WHEN --max(contribution)>0.02 or
max(B.runrate)> 30 THEN 'Fast Moving'
WHEN --max(contribution)>0.01 or
max(B.runrate)>= 5 THEN 'Slow Moving'
WHEN --max(contribution)<=0.01 or
max(B.runrate)<5 or max(B.runrate) is null THEN 'Not Moving'
END sku_type
FROM public.basic_calculation A
left join (select distinct sku , runrate from public.basic_calculation where txn_period = 'L30D' ) B on A.sku = B.sku
WHERE txn_period <> 'L120D'
GROUP BY A.SKu;
DROP TABLE IF EXISTS public.basic_calculation_stage4;
CREATE TABLE public.basic_calculation_stage4 AS
SELECT A.* ,
CASE
WHEN sd_of_contribution= 0 THEN 0
ELSE (A.for_max_contribution)/sd_of_contribution
END AS max_deviation_contribution,
B.median_runrate,
B.max_runrate,
coalesce(B.last_month_runrate,0)last_month_runrate ,
B.sku_type,
(l30d_revenue/gold_price)*(1+(avg_revenue_growth/100)) AS qty1_median_rev_contribution,
median_runrate*30 qty2_avg_runrate,
avg_quantity qty3_avg_quantity,
expected_30D_revenue/gold_price qty4_expected_revenue_target,
0 buffer
FROM public.basic_calculation_stage2 A
LEFT JOIN public.basic_calculation_stage3 B ON A.sku = B.sku;
--SELECT * FROM public.basic_calculation_stage4;
--select * from public.basic_calculation_stage5
drop table if exists public.basic_calculation_stage5;
CREATE TABLE public.basic_calculation_stage5
as
select sku , MEDIAN(Max_of_qty) Max_of_qty , Avg(Max_of_qty) Avg_of_quantiy
from
(SELECT sku ,
qty1_median_rev_contribution Max_of_qty
FROM public.basic_calculation_stage4
UNION ALL
SELECT sku ,
qty2_avg_runrate
FROM public.basic_calculation_stage4
UNION ALL
SELECT sku ,
qty3_avg_quantity
FROM public.basic_calculation_stage4
UNION ALL
SELECT sku ,
qty4_expected_revenue_target
FROM public.basic_calculation_stage4) A
group by sku;
drop table if exists public.basic_calculation_stage6;
create table public.basic_calculation_stage6
as
select sku,
sum(CASE WHEN B.warehouse ='Balikpapan - Semarandi' THEN sku_qty_split_next_order_percentage/100 END) balikpapan_semarandi_split,
sum(CASE WHEN B.warehouse ='Bekasi' THEN sku_qty_split_next_order_percentage/100 END) Bekasi_split,
sum(CASE WHEN B.warehouse ='Makassar' THEN sku_qty_split_next_order_percentage/100 END) Makassar_split,
sum(CASE WHEN B.warehouse ='Pekanbaru - Medan' THEN sku_qty_split_next_order_percentage/100 END) pekanbaru_medan_split,
sum(CASE WHEN B.warehouse ='Semarang' THEN sku_qty_split_next_order_percentage/100 END) Semarang_split,
sum(CASE WHEN B.warehouse ='Surabaya' THEN sku_qty_split_next_order_percentage/100 END) Surabaya_split
from public.sku_warehouse_split_final B
group by sku;
drop table if exists public.final_basic_calucation_table;
CREATE TABLE public.final_basic_calucation_table AS
SELECT A.sku ,
A.gold_price ,
A.l30d_revenue ,
A.avg_revenue_growth,
A.avg_expected_revenue_growth ,
A.expected_30D_revenue,
A.median_of_contribution median_of_contribution_l90D,
A.sd_of_contribution standard_deviation_of_contribution_l90d,
A.max_deviation_contribution max_standard_deviation_of_contribution_l90d,
A.avg_quantity avg_quantity_sold_l90d,
A.max_quantity max_quantity_sold_l90d,
A.median_runrate median_runrate_l90d,
A.max_runrate max_runrate_l90d,
A.last_month_runrate l30d_runrate,
A.sku_type ,
A.new_sku as new_sku_flag,
A.qty1_median_rev_contribution ,
A.qty2_avg_runrate ,
A.qty3_avg_quantity ,
qty4_expected_revenue_target,
buffer ,
(case when sku_type= 'Fast Moving' then ((Max_of_qty*(1+buffer))/30)
when sku_type = 'Slow Moving' then ((Avg_of_quantiy*(1+buffer))/30)
when sku_type = 'Not Moving' then 0 end) overall_qty_required_per_day,
(case when sku_type= 'Fast Moving' then ((Max_of_qty*(1+buffer))/30)
when sku_type = 'Slow Moving' then ((Avg_of_quantiy*(1+buffer))/30)
when sku_type = 'Not Moving' then 0 end )*balikpapan_semarandi_split balikpapan_semarandi_split,
(case when sku_type= 'Fast Moving' then ((Max_of_qty*(1+buffer))/30)
when sku_type = 'Slow Moving' then ((Avg_of_quantiy*(1+buffer))/30)
when sku_type = 'Not Moving' then 0 end)*Bekasi_split Bekasi_split,
(case when sku_type= 'Fast Moving' then ((Max_of_qty*(1+buffer))/30)
when sku_type = 'Slow Moving' then ((Avg_of_quantiy*(1+buffer))/30)
when sku_type = 'Not Moving' then 0 end )*Makassar_split Makassar_split,
(case when sku_type= 'Fast Moving' then ((Max_of_qty*(1+buffer))/30)
when sku_type = 'Slow Moving' then ((Avg_of_quantiy*(1+buffer))/30)
when sku_type = 'Not Moving' then 0 end )*pekanbaru_medan_split pekanbaru_medan_split,
(case when sku_type= 'Fast Moving' then ((Max_of_qty*(1+buffer))/30)
when sku_type = 'Slow Moving' then ((Avg_of_quantiy*(1+buffer))/30)
when sku_type = 'Not Moving' then 0 end )*Semarang_split Semarang_split,
(case when sku_type= 'Fast Moving' then ((Max_of_qty*(1+buffer))/30)
when sku_type = 'Slow Moving' then ((Avg_of_quantiy*(1+buffer))/30)
when sku_type = 'Not Moving' then 0 end )*Surabaya_split Surabaya_split
FROM public.basic_calculation_stage4 A
LEFT JOIN public.basic_calculation_stage6 B ON A.sku =B.sku
LEFT join public.basic_calculation_stage5 C on A.sku=C.sku;
drop table if exists public.inbound_data;
create table public.inbound_data
as
select raena_code sku,
sum(case when warehouse_id = 'CGK WHS' then COALESCE(quantity,0) end) Cikarang_quantity ,
sum(case when warehouse_id = 'Makassar WHS' then COALESCE(quantity,0) end) Makassar_quantity ,
sum(case when warehouse_id = 'Medan WHS' then COALESCE(quantity,0) end) medan_quantity,
sum(case when warehouse_id = 'Samarinda WHS' then COALESCE(quantity,0) end) Samarinda_quantity ,
sum(case when warehouse_id = 'Semarang WHS' then COALESCE(quantity,0) end) semarang_quantity ,
sum(case when warehouse_id = 'Surabaya WHS' then COALESCE(quantity,0) end) Surabaya_quantity
from (select * from raena_erp_management.inbound_order where cast(expected_arrival_date as date) >'2023-11-22' and received_time is null ) A
left join raena_erp_management.inbound_order_sku B on A.id = B."orderIdId"
group by raena_code;
drop table if exists public.warehouse_in_stock;
create table public.warehouse_in_stock
as
select sku ,
sum(case when F.warehouse_code = 'WH-CGK45' then coalesce(F.stock_limit,0) end) Cikarang_in_stock,
sum(case when F.warehouse_code = 'WH-SUB51' then coalesce(F.stock_limit,0) end) Surabaya_in_stock,
sum(case when F.warehouse_code = 'WH-SRG18' then coalesce(F.stock_limit,0) end) Semarang_in_stock,
sum(case when F.warehouse_code = 'WH-MES07' then coalesce(F.stock_limit,0) end) Medan_in_stock,
sum(case when F.warehouse_code = 'WH-UPG04' then coalesce(F.stock_limit,0) end) Makassar_in_stock,
sum(case when F.warehouse_code = 'WH-AAP02' then coalesce(F.stock_limit,0) end) Samarinda_in_stock
from
raena_catalog_management.product C
left join raena_catalog_management.product_inventory F on C.id = F.product_id
group by sku;
DROP TABLE IF EXISTS public.demand_forecast_dashboard_base;
CREATE TABLE public.demand_forecast_dashboard_base AS
SELECT DISTINCT A.sku ,
A.brand_name ,
case when B.sku_type is not null then B.sku_type else 'Not Moving' end sku_type ,
case when new_sku_flag is not null then new_sku_flag else 'No' end new_sku_flag,
A.sourcing,
A.ordering_type ,
A.active_status,
multiple_flag,
moq,
moqremark,
give_away,
--cast(case when sourcing = 'Local' THEN 14*overall_qty_required_per_day
-- WHEN sourcing = 'Crossborder' THEN 30*overall_qty_required_per_day end as int) overall_qty_required_per_day,
cast(Cikarang_in_stock as int) Cikarang_in_stock,
cast(coalesce(E.Cikarang_quantity,0) as int) Cikarang_quantity_in_transit,
cast((Cikarang_in_stock+ coalesce(E.Cikarang_quantity,0)) as int) total_Cikarang_stock,
cast(l30d_runrate as int) Cikarang_runrate,
cast(CASE
WHEN l30d_runrate = 0 THEN 0 else((Cikarang_in_stock+ coalesce(E.Cikarang_quantity,0))*(1.1)/l30d_runrate)
END as int) Cikarang_Inventory_day_for_future,
cast(bekasi_total_lead_time as int) cikarang_total_lead_time ,
CASE
WHEN CASE
WHEN l30d_runrate = 0 THEN 0 else((Cikarang_in_stock+ coalesce(E.Cikarang_quantity,0))*(1.1)/l30d_runrate)
END<bekasi_total_lead_time THEN 'True'
ELSE 'False'
END cikarang_reorder_flag,
(cast(Bekasi_split*bekasi_inventory_days as int)-case when moq is not null and multiple_flag is not null then mod(cast(Bekasi_split*bekasi_inventory_days as int),cast(moq as int)) else 0 end ) cikarang_project_quantity_to_order,
cast(Surabaya_in_stock as int) Surabaya_in_stock,
cast(coalesce(E.Surabaya_quantity,0) as int) Surabaya_quantity_in_transit,
cast((Surabaya_in_stock+coalesce(E.Surabaya_quantity,0)) as int) total_Surabaya_stock,
cast(l30d_runrate as int) Surabaya_runrate,
cast(CASE
WHEN l30d_runrate = 0 THEN 0 else((Surabaya_in_stock+coalesce(E.Surabaya_quantity,0))*(1.1)/l30d_runrate)
END as int) Surabaya_Inventory_day_for_future,
cast(Surabaya_total_lead_time as int) Surabaya_total_lead_time,
CASE
WHEN CASE
WHEN l30d_runrate = 0 THEN 0 else((Surabaya_in_stock+coalesce(E.Surabaya_quantity,0))*(1.1)/l30d_runrate)
END<Surabaya_total_lead_time THEN 'True'
ELSE 'False'
END surabaya_reorder_flag,
(cast(Surabaya_split*Surabaya_inventory_days as int)-case when moq is not null and multiple_flag is not null then mod(cast(Surabaya_split*Surabaya_inventory_days as int),cast(moq as int)) else 0 end ) Surabaya_project_quantity_to_order,
cast(Semarang_in_stock as int) Semarang_in_stock,
cast(coalesce(E.semarang_quantity,0) as int) semarang_quantity_in_transit,
cast(Semarang_in_stock+coalesce(E.semarang_quantity,0) as int) total_semarang_stock,
cast(l30d_runrate as int) Semarang_runrate,
cast(CASE
WHEN l30d_runrate = 0 THEN 0 else((Semarang_in_stock+coalesce(E.semarang_quantity,0))*(1.1)/l30d_runrate)
END as int) semarang_Inventory_day_for_future,
cast(Semarang_total_lead_time as int) Semarang_total_lead_time,
CASE
WHEN CASE
WHEN l30d_runrate = 0 THEN 0 else((Semarang_in_stock+coalesce(E.semarang_quantity,0))*(1.1)/l30d_runrate)
END<Semarang_total_lead_time THEN 'True'
ELSE 'False'
END semarang_reorder_flag,
(cast(Semarang_split*Semarang_inventory_days as int)-case when moq is not null and multiple_flag is not null then mod(cast(Semarang_split*Semarang_inventory_days as int),cast(moq as int)) else 0 end ) Semarang_project_quantity_to_order,
cast(Medan_in_stock as int) Medan_in_stock,
cast(coalesce(E.medan_quantity,0) as int) medan_quantity_in_transit,
cast(Medan_in_stock+coalesce(E.medan_quantity,0) as int) total_medan_stock,
cast(l30d_runrate as int) Medan_runrate ,
cast(CASE
WHEN l30d_runrate = 0 THEN 0 else((Medan_in_stock+coalesce(E.medan_quantity,0))*(1.1)/l30d_runrate)
END as int) medan_Inventory_day_for_future,
cast(Pekanbaru_Medan_total_lead_time as int) Medan_total_lead_time,
CASE
WHEN CASE
WHEN l30d_runrate = 0 THEN 0 else((Medan_in_stock+coalesce(E.medan_quantity,0))*(1.1)/l30d_runrate)
END<Pekanbaru_Medan_total_lead_time THEN 'True'
ELSE 'False'
END medan_reorder_flag,
(cast(pekanbaru_medan_split*Pekanbaru_Medan_inventory_days as int)-case when moq is not null and multiple_flag is not null then mod(cast(pekanbaru_medan_split*Pekanbaru_Medan_inventory_days as int),cast(moq as int))else 0 end ) Medan_project_quantity_to_order,
cast(Makassar_in_stock as int) Makassar_in_stock,
cast(coalesce(E.Makassar_quantity,0) as int) Makassar_quantity_in_transit,
cast(Makassar_in_stock+coalesce(E.Makassar_quantity,0) as int) total_Makassar_stock,
cast(l30d_runrate as int) Makassar_runrate,
cast(CASE
WHEN l30d_runrate = 0 THEN 0 else((Makassar_in_stock+coalesce(E.Makassar_quantity,0))*(1.1)/l30d_runrate)
END as int) Makassar_Inventory_day_for_future,
cast(Makassar_total_lead_time as int) Makassar_total_lead_time,
CASE
WHEN CASE
WHEN l30d_runrate = 0 THEN 0 else((Makassar_in_stock+coalesce(E.Makassar_quantity,0))*(1.1)/l30d_runrate)
END<Makassar_total_lead_time THEN 'True'
ELSE 'False'
END makassar_reorder_flag,
(cast(Makassar_split*Makassar_inventory_days as int) -case when moq is not null and multiple_flag is not null then mod(cast(Makassar_split*Makassar_inventory_days as int),cast(moq as int))else 0 end ) Makassar_project_quantity_to_order,
cast(Samarinda_in_stock as int) Samarinda_in_stock,
cast(coalesce(E.Samarinda_quantity,0) as int) Samarinda_quantity_in_transit,
cast(Samarinda_in_stock+coalesce(E.Samarinda_quantity,0) as int) total_Samarinda_stock,
cast(l30d_runrate as int) Samarinda_runrate,
cast(CASE
WHEN l30d_runrate = 0 THEN 0 else((Samarinda_in_stock+coalesce(E.Samarinda_quantity,0))*(1.1)/l30d_runrate)
END as int) Samarinda_Inventory_day_for_future,
cast(Balikpapan_Semarandi_total_lead_time as int) Semarandi_total_lead_time,
CASE
WHEN CASE
WHEN l30d_runrate = 0 THEN 0 else((Samarinda_in_stock+coalesce(E.Samarinda_quantity,0))*(1.1)/l30d_runrate)
END<Balikpapan_Semarandi_total_lead_time THEN 'True'
ELSE 'False'
END semarandi_reorder_flag,
(cast(balikpapan_semarandi_split*Balikpapan_Semarandi_inventory_days as int)-case when moq is not null and multiple_flag is not null then mod(cast(balikpapan_semarandi_split*Balikpapan_Semarandi_inventory_days as int),cast(moq as int)) else 0 end ) semarandi_project_quantity_to_order
FROM public.sku_level_data A
LEFT JOIN public.final_basic_calucation_table B ON A.sku = B.sku
LEFT JOIN public.warehouse_in_stock C ON A.sku = C.sku
LEFT JOIN public.basic_calculation D ON A.sku=D.sku
LEFT JOIN public.inbound_data E ON A.sku = E.sku;
drop table if exists public.demand_forecast_dashboard_final;
create table public.demand_forecast_dashboard_final
as
SELECT *,
cast(cikarang_in_stock+surabaya_in_stock +semarang_in_stock +medan_in_stock +makassar_in_stock +samarinda_in_stock as int) AS total_in_stock ,
cast(cikarang_quantity_in_transit +surabaya_quantity_in_transit +semarang_quantity_in_transit +medan_quantity_in_transit +makassar_quantity_in_transit +samarinda_quantity_in_transit as int) AS total_quantity_in_transit,
cast(total_cikarang_stock +total_surabaya_stock +total_semarang_stock +total_medan_stock + total_makassar_stock +total_samarinda_stock as int) AS total_stock,
cast(cikarang_runrate as int) total_runrate,
cast(CASE
WHEN cikarang_runrate = 0 THEN 0 else((total_cikarang_stock +total_surabaya_stock +total_semarang_stock +total_medan_stock + total_makassar_stock +total_samarinda_stock)*(1.1)/cikarang_runrate)
END as int) total_inventory_day_for_future ,
cast(greatest(cikarang_total_lead_time,surabaya_total_lead_time,semarang_total_lead_time,medan_total_lead_time,makassar_total_lead_time,Semarandi_total_lead_time) as int) AS total_total_lead_time,
CASE
WHEN CASE
WHEN cikarang_runrate = 0 THEN 0 else((total_cikarang_stock +total_surabaya_stock +total_semarang_stock +total_medan_stock + total_makassar_stock +total_samarinda_stock)*(1.1)/cikarang_runrate)
END<greatest(cikarang_total_lead_time,surabaya_total_lead_time,semarang_total_lead_time,medan_total_lead_time,makassar_total_lead_time,Semarandi_total_lead_time) THEN 'True'
ELSE 'False'
END overall_reorder_flag,
(coalesce(cikarang_project_quantity_to_order,0)+coalesce(semarandi_project_quantity_to_order,0)+coalesce(Makassar_project_quantity_to_order,0)
+coalesce(Semarang_project_quantity_to_order,0)+coalesce(Medan_project_quantity_to_order,0)+coalesce(Surabaya_project_quantity_to_order,0)) overall_qty_required_per_day
FROM public.demand_forecast_dashboard_base
WHERE sku NOT LIKE '%BAZ%';
Delete from public.OM_OOS_Base where report_date='2023-11-22';
Insert into public.OM_OOS_Base
SELECT *,
cast(cikarang_in_stock+surabaya_in_stock +semarang_in_stock +medan_in_stock +makassar_in_stock +samarinda_in_stock as int) AS total_in_stock ,
cast(cikarang_quantity_in_transit +surabaya_quantity_in_transit +semarang_quantity_in_transit +medan_quantity_in_transit +makassar_quantity_in_transit +samarinda_quantity_in_transit as int) AS total_quantity_in_transit,
cast(total_cikarang_stock +total_surabaya_stock +total_semarang_stock +total_medan_stock + total_makassar_stock +total_samarinda_stock as int) AS total_stock,
cast(cikarang_runrate as int) total_runrate,
cast(CASE
WHEN cikarang_runrate = 0 THEN 0 else((total_cikarang_stock +total_surabaya_stock +total_semarang_stock +total_medan_stock + total_makassar_stock +total_samarinda_stock)*(1.1)/cikarang_runrate)
END as int) total_inventory_day_for_future ,
cast(greatest(cikarang_total_lead_time,surabaya_total_lead_time,semarang_total_lead_time,medan_total_lead_time,makassar_total_lead_time,Semarandi_total_lead_time) as int) AS total_total_lead_time,
CASE
WHEN CASE
WHEN cikarang_runrate = 0 THEN 0 else((total_cikarang_stock +total_surabaya_stock +total_semarang_stock +total_medan_stock + total_makassar_stock +total_samarinda_stock)*(1.1)/cikarang_runrate)
END<greatest(cikarang_total_lead_time,surabaya_total_lead_time,semarang_total_lead_time,medan_total_lead_time,makassar_total_lead_time,Semarandi_total_lead_time) THEN 'True'
ELSE 'False'
END overall_reorder_flag,
(coalesce(cikarang_project_quantity_to_order,0)+coalesce(semarandi_project_quantity_to_order,0)+coalesce(Makassar_project_quantity_to_order,0)
+coalesce(Semarang_project_quantity_to_order,0)+coalesce(Medan_project_quantity_to_order,0)+coalesce(Surabaya_project_quantity_to_order,0)) overall_qty_required_per_day,
'2023-11-22' as Report_date
FROM public.demand_forecast_dashboard_base
WHERE sku NOT LIKE '%BAZ%';

View File

@ -0,0 +1,401 @@
--------------------------Retention Data --------------------------
Drop table if exists public.DAU_RETENTION;
create table DAU_RETENTION
as
select *
from
(
select distinct base_1.mobile,cast(datepart(year,base_1.created_date) as varchar) as conversion_year,
TO_CHAR(base_1.created_date, 'Month') as conversion_month,
datepart(month,base_1.created_date) as month_sort,base_1.created_date,base_1.bucket,
case when datediff(month,base_1.created_date,base_2.created_date)=0 then base_1.mobile end as M0,
case when datediff(month,base_1.created_date,base_3.created_date)=1 then base_1.mobile end as M1,
case when datediff(month,base_1.created_date,base_3.created_date)=2 then base_1.mobile end as M2,
case when datediff(month,base_1.created_date,base_3.created_date)=3 then base_1.mobile end as M3,
case when datediff(month,base_1.created_date,base_3.created_date)=4 then base_1.mobile end as M4,
case when datediff(month,base_1.created_date,base_3.created_date)=5 then base_1.mobile end as M5,
case when datediff(month,base_1.created_date,base_3.created_date)=6 then base_1.mobile end as M6,
case when datediff(month,base_1.created_date,base_3.created_date)>6 then base_1.mobile end as GT_M6
from
(
select *,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,payment_amount,
row_number() over(partition by replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','')
order by cast(created_at AS Date)) created_rnk
FROM raena_order_management.order
WHERE payment_status = 'Paid'
) a where a.created_rnk=1
) base_1
inner join
(
select mobile,date_trunc('month',created_date) as created_date,sum(payment_amount) as payment_amount
from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,payment_amount,
row_number() over(partition by replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+',''),
date_part(year,cast(created_at AS Date)),date_part(month,cast(created_at AS Date))
order by date_part(year,cast(created_at AS Date)),date_part(month,cast(created_at AS Date))) created_rnk
FROM raena_order_management.order
WHERE payment_status = 'Paid'
)
group by 1,2
) base_2 on base_1.mobile=base_2.mobile
left join
(
select distinct date_trunc('month',created_date) as created_date,active_user
from
(
SELECT DISTINCT cast(to_date(substring(ts,1,8),'yyyymmdd') AS date) created_date,
case when profile_phone <> '' then profile_phone
else profile_objectid end Active_user,
dense_rank() over(partition by (case when profile_phone <> '' then profile_phone
else profile_objectid end) order by date_part('year',cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)),date_part('month',cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)) ) created_rnk_2
FROM analytics.clevertap.app_launched al
WHERE eventname= 'App Launched' and profile_phone <> ''
)
) base_3 on base_1.mobile=base_3.active_user
) bucket_final
Drop table if exists public.INSTALLBASE_REVENUE_RETENTION
create table public.INSTALLBASE_REVENUE_RETENTION
as
select *
from
(
select cast(acquisition_year as varchar) as acquisition_year,acquisition_month,month_sort,bucket_check.bucket,install_base.Active_user,
case when datediff(month,install_base.created_date,conversion_base.created_date)=0 then conversion_base.payment_amount end as M0,
case when datediff(month,install_base.created_date,conversion_base.created_date)=1 then conversion_base.payment_amount end as M1,
case when datediff(month,install_base.created_date,conversion_base.created_date)=2 then conversion_base.payment_amount end as M2,
case when datediff(month,install_base.created_date,conversion_base.created_date)=3 then conversion_base.payment_amount end as M3,
case when datediff(month,install_base.created_date,conversion_base.created_date)=4 then conversion_base.payment_amount end as M4,
case when datediff(month,install_base.created_date,conversion_base.created_date)=5 then conversion_base.payment_amount end as M5,
case when datediff(month,install_base.created_date,conversion_base.created_date)=6 then conversion_base.payment_amount end as M6,
case when datediff(month,install_base.created_date,conversion_base.created_date)>6 then conversion_base.payment_amount end as GT_M6
from
(
select distinct base.Active_user,datepart(year,base.created_date) as acquisition_year,
TO_CHAR(base.created_date, 'Month') as acquisition_month,
datepart(month,base.created_date) as month_sort,base.created_date
from
(
SELECT DISTINCT cast(to_date(substring(ts,1,8),'yyyymmdd') AS date) as created_date,
date_part('month',cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)) month_sort,
case when profile_phone <> '' then profile_phone
else profile_objectid end Active_user,
dense_rank() over(partition by (case when profile_phone <> '' then profile_phone
else profile_objectid end) order by cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)) created_rnk
FROM analytics.clevertap.app_launched al
WHERE eventname= 'App Launched' --and profile_phone in ('628814617694')
) base where created_rnk=1
) install_base
inner join
(
select mobile,created_date,payment_amount from
(
select mobile,created_date,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket,payment_amount,row_number() over (partition by mobile order by created_date) as rnk
from
(
select distinct mobile, date_trunc('month',created_date) as created_date,sum(payment_amount) as payment_amount
from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,
payment_amount
FROM raena_order_management.order
WHERE payment_status = 'Paid'
) group by 1,2
)
)
) conversion_base on install_base.active_user=conversion_base.mobile
inner join
(
select mobile,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket from
(
SELECT replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
sum(payment_amount) as payment_amount
FROM raena_order_management.order
WHERE payment_status = 'Paid'
group by 1
)
) bucket_check on install_base.active_user=bucket_check.mobile
) a
Drop table if exists public.INSTALLBASE_RESELLER_RETENTION
create table public.INSTALLBASE_RESELLER_RETENTION
as
select *
from
(
select cast(acquisition_year as varchar) as acquisition_year,acquisition_month,month_sort,bucket_check.bucket,install_base.Active_user,
case when datediff(month,install_base.created_date,conversion_base.created_date)=0 then install_base.Active_user end as M0,
case when datediff(month,install_base.created_date,conversion_base.created_date)=1 then install_base.Active_user end as M1,
case when datediff(month,install_base.created_date,conversion_base.created_date)=2 then install_base.Active_user end as M2,
case when datediff(month,install_base.created_date,conversion_base.created_date)=3 then install_base.Active_user end as M3,
case when datediff(month,install_base.created_date,conversion_base.created_date)=4 then install_base.Active_user end as M4,
case when datediff(month,install_base.created_date,conversion_base.created_date)=5 then install_base.Active_user end as M5,
case when datediff(month,install_base.created_date,conversion_base.created_date)=6 then install_base.Active_user end as M6,
case when datediff(month,install_base.created_date,conversion_base.created_date)>6 then install_base.Active_user end as GT_M6
from
(
select distinct base.Active_user,datepart(year,base.created_date) as acquisition_year,
TO_CHAR(base.created_date, 'Month') as acquisition_month,
datepart(month,base.created_date) as month_sort,base.created_date
from
(
SELECT DISTINCT cast(to_date(substring(ts,1,8),'yyyymmdd') AS date) as created_date,
date_part('month',cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)) month_sort,
case when profile_phone <> '' then profile_phone
else profile_objectid end Active_user,
dense_rank() over(partition by (case when profile_phone <> '' then profile_phone
else profile_objectid end) order by cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)) created_rnk
FROM analytics.clevertap.app_launched al
WHERE eventname= 'App Launched' --and profile_phone in ('628814617694')
) base where created_rnk=1
) install_base
inner join
(
select mobile,created_date from
(
select mobile,created_date,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket,row_number() over (partition by mobile order by created_date) as rnk
from
(
select distinct mobile, date_trunc('month',created_date) as created_date,sum(payment_amount) as payment_amount
from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,
payment_amount
FROM raena_order_management.order
WHERE payment_status = 'Paid'
) group by 1,2
)
)
) conversion_base on install_base.active_user=conversion_base.mobile
inner join
(
select mobile,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket from
(
SELECT replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
sum(payment_amount) as payment_amount
FROM raena_order_management.order
WHERE payment_status = 'Paid'
group by 1
)
) bucket_check on install_base.active_user=bucket_check.mobile
) a
Drop table if exists public.DAU_RETENTION_BASIS_INSTALL_COHORT
create table public.DAU_RETENTION_BASIS_INSTALL_COHORT
as
select *
from
(
select install_base.*,bucket.bucket
from
(
select distinct a.Active_user,datepart(year,a.created_date) as acquisition_year,
TO_CHAR(a.created_date, 'Month') as acquisition_month,
datepart(month,a.created_date) as month_sort,a.created_date,
case when datediff(month,a.created_date,b.created_date)=0 then a.Active_user end as M0,
case when datediff(month,a.created_date,b.created_date)=1 then a.Active_user end as M1,
case when datediff(month,a.created_date,b.created_date)=2 then a.Active_user end as M2,
case when datediff(month,a.created_date,b.created_date)=3 then a.Active_user end as M3,
case when datediff(month,a.created_date,b.created_date)=4 then a.Active_user end as M4,
case when datediff(month,a.created_date,b.created_date)=5 then a.Active_user end as M5,
case when datediff(month,a.created_date,b.created_date)=6 then a.Active_user end as M6,
case when datediff(month,a.created_date,b.created_date)>6 then a.Active_user end as GT_M6
from
(
select * from
(
SELECT DISTINCT cast(to_date(substring(ts,1,8),'yyyymmdd') AS date) as created_date,
date_part('month',cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)) month_sort,
case when profile_phone <> '' then profile_phone
else profile_objectid end Active_user,
dense_rank() over(partition by (case when profile_phone <> '' then profile_phone
else profile_objectid end) order by cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)) created_rnk
FROM analytics.clevertap.app_launched al
WHERE eventname= 'App Launched' --and profile_phone in ('628814617694')
) base where created_rnk=1 ) a
inner join
(
SELECT DISTINCT cast(to_date(substring(ts,1,8),'yyyymmdd') AS date) created_date,
case when profile_phone <> '' then profile_phone
else profile_objectid end Active_user,
dense_rank() over(partition by (case when profile_phone <> '' then profile_phone
else profile_objectid end) order by date_part('year',cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)),date_part('month',cast(to_date(substring(ts,1,8),'yyyymmdd') AS date)) ) created_rnk_2
FROM analytics.clevertap.app_launched al
WHERE eventname= 'App Launched' --and profile_phone in ('628814617694')
) b on a.Active_user=b.Active_user
) install_base
left join
(
select conversion_base.mobile,date_part(year,created_date) as acquistion_year,TO_CHAR(created_date,'month') as acquistion_month,
bucket_check.bucket
from
(
select mobile,created_date
from
(
select mobile,created_date,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket,row_number() over (partition by mobile order by created_date) as rnk
from
(
select distinct mobile, date_trunc('month',created_date) as created_date,sum(payment_amount) as payment_amount
from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,
payment_amount
FROM raena_order_management.order
WHERE payment_status = 'Paid'
) b group by 1,2
) c
) a where a.rnk=1
)conversion_base
inner join
(
select mobile,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket from
(
SELECT replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
sum(payment_amount) as payment_amount
FROM raena_order_management.order
WHERE payment_status = 'Paid'
group by 1
)
) bucket_check on conversion_base.mobile=bucket_check.mobile
) bucket on install_base.active_user=bucket.mobile and date_part(year,install_base.created_date)=bucket.acquistion_year and TO_CHAR(install_base.created_date,'month')=bucket.acquistion_month
) bucket_final
drop table if exists public.OMREVENUE_RETENTION
create table public.OMREVENUE_RETENTION
as
select * from
(
select distinct base_1.mobile,cast(datepart(year,base_1.created_date) as varchar) as acquisition_year,
TO_CHAR(base_1.created_date, 'Month') as acquisition_month,
datepart(month,base_1.created_date) as month_sort,base_1.created_date,base_1.bucket,
case when datediff(month,base_1.created_date,base_2.created_date)=0 then base_2.payment_amount end as M0,
case when datediff(month,base_1.created_date,base_2.created_date)=1 then base_2.payment_amount end as M1,
case when datediff(month,base_1.created_date,base_2.created_date)=2 then base_2.payment_amount end as M2,
case when datediff(month,base_1.created_date,base_2.created_date)=3 then base_2.payment_amount end as M3,
case when datediff(month,base_1.created_date,base_2.created_date)=4 then base_2.payment_amount end as M4,
case when datediff(month,base_1.created_date,base_2.created_date)=5 then base_2.payment_amount end as M5,
case when datediff(month,base_1.created_date,base_2.created_date)=6 then base_2.payment_amount end as M6,
case when datediff(month,base_1.created_date,base_2.created_date)>6 then base_2.payment_amount end as GT_M6
from
(
select *,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,payment_amount,
row_number() over(partition by replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','')
order by cast(created_at AS Date)) created_rnk
FROM raena_order_management.order
WHERE payment_status = 'Paid'
) a where a.created_rnk=1
) base_1
inner join
(
select mobile,date_trunc('month',created_date) as created_date,sum(payment_amount) as payment_amount
from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,payment_amount,
row_number() over(partition by replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+',''),
date_part(year,cast(created_at AS Date)),date_part(month,cast(created_at AS Date))
order by date_part(year,cast(created_at AS Date)),date_part(month,cast(created_at AS Date))) created_rnk
FROM raena_order_management.order
WHERE payment_status = 'Paid'
)
group by 1,2
) base_2 on base_1.mobile=base_2.mobile
) bucket_final
Drop table if exists public.RESELLER_RETENTION
create table public.RESELLER_RETENTION
as
select * from
(
select distinct base_1.mobile,cast(datepart(year,base_1.created_date) as varchar) as acquisition_year,
TO_CHAR(base_1.created_date, 'Month') as acquisition_month,
datepart(month,base_1.created_date) as month_sort,base_1.created_date,base_1.bucket,
case when datediff(month,base_1.created_date,base_2.created_date)=0 then base_1.mobile end as M0,
case when datediff(month,base_1.created_date,base_2.created_date)=1 then base_1.mobile end as M1,
case when datediff(month,base_1.created_date,base_2.created_date)=2 then base_1.mobile end as M2,
case when datediff(month,base_1.created_date,base_2.created_date)=3 then base_1.mobile end as M3,
case when datediff(month,base_1.created_date,base_2.created_date)=4 then base_1.mobile end as M4,
case when datediff(month,base_1.created_date,base_2.created_date)=5 then base_1.mobile end as M5,
case when datediff(month,base_1.created_date,base_2.created_date)=6 then base_1.mobile end as M6,
case when datediff(month,base_1.created_date,base_2.created_date)>6 then base_1.mobile end as GT_M6
from
(
select *,CASE
WHEN payment_amount < 2000000 THEN '<2M'
WHEN payment_amount BETWEEN 2000001 AND 10000000 THEN '2-10M'
WHEN payment_amount>10000000 THEN '+10M'
END AS Bucket from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,payment_amount,
row_number() over(partition by replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','')
order by cast(created_at AS Date)) created_rnk
FROM raena_order_management.order
WHERE payment_status = 'Paid'
) a where a.created_rnk=1
) base_1
inner join
(
select mobile,date_trunc('month',created_date) as created_date,sum(payment_amount) as payment_amount
from
(
SELECT distinct replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+','') as mobile,
cast(created_at AS Date) created_date,payment_amount,
row_number() over(partition by replace(json_extract_path_text(raena_order_management.order.reseller_info,'mobile',TRUE),'+',''),
date_part(year,cast(created_at AS Date)),date_part(month,cast(created_at AS Date))
order by date_part(year,cast(created_at AS Date)),date_part(month,cast(created_at AS Date))) created_rnk
FROM raena_order_management.order
WHERE payment_status = 'Paid'
)
group by 1,2
) base_2 on base_1.mobile=base_2.mobile
) bucket_final

View File

@ -0,0 +1,84 @@
--------------------------DB Funnel New Users--------------------------
Drop table if exists om_clevertap_install_jan_may
create table om_clevertap_install_jan_may
as
select App_install_date,email,phone,user_id
from (
select *,row_number() over (partition by user_id order by app_install_date) as R
from
(
SELECT '2022-04-01' as App_install_date,email,phone,email as user_id FROM public.clevertap_april_csv
union
SELECT '2022-03-30' as App_install_date,email,phone,email as user_id FROM public.clevertap_march_csv where phone not in (select distinct profile_phone from clevertap.app_installed)
union
select cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_install_date,profile_email,profile_phone,
case when profile_email=' ' then profile_objectid else profile_email end as user_id
from clevertap.app_installed
)
) where R=1
Drop table if exists om_clevertap_install_jan_may_2
create table om_clevertap_install_jan_may_2
as
select user_id,email,phone,to_char(app_install_date,'month') as month,date_part(day,app_install_date) as day,address_line1,address_line2,city,province,app_install_date
from (
select a.user_id,a.email,phone,case when app_install_date='2022-03-30' or app_install_date='2022-04-01' then cast(created_at as date) else app_install_date end as app_install_date,
b.address_line1,address_line2,city,province
from om_clevertap_install_jan_may a
left join (
select mobile,email,created_at,address_line1,address_line2,city,province
from (
select email,replace(mobile,'+','') as mobile,created_at,address_line1,address_line2,city,province,
row_number() over (partition by email order by created_at) as R
from raena_user_management.user
)
where R=1 and email notnull) b on a.email=b.email
)
Drop table if exists om_clevertap_install_jan_may_3
create table om_clevertap_install_jan_may_3
as
select a.*,cast(b.created_at as date) as transaction_date,transacted_email
--case when cast(b.created_at as date) isnull then 'Never transacted'
--when cast(b.created_at as date)>=app_install_date and cast(b.created_at as date)<=cast(dateadd(day,30,app_install_date) as date) then 'Yes' else 'No' end as transacted_flag
from om_clevertap_install_jan_may_2 a
left join (select json_extract_path_text(reseller_info,'email',TRUE) as transacted_email ,min(created_at) as created_at
from raena_order_management.order where payment_status='Paid' and cast(is_archived as varchar)='false' group by 1 ) b
on a.email=transacted_email
Drop table if exists om_clevertap_install_jan_may_final
create table om_clevertap_install_jan_may_final
as
select *,case when email notnull and rtrim(ltrim(email)) != '' then 'Yes' else 'No' end as email_flag,
case when phone notnull and rtrim(ltrim(phone)) != '' then 'Yes' else 'No' end as phone_flag,
case when address_line1 notnull and rtrim(ltrim(address_line1)) != '' then 'Yes' else 'No' end as address_flag,
case when transacted_email notnull and rtrim(ltrim(transacted_email)) != '' and transaction_date>=app_install_date and transaction_date<=cast(dateadd(day,30,app_install_date) as date)
then 'Yes' else 'No' end as transacted_flag
from om_clevertap_install_jan_may_3
--order data
Drop table if exists om_clevertap_install_jan_may_order
create table om_clevertap_install_jan_may_order
as
select * from om_clevertap_install_jan_may_final where transacted_email notnull and rtrim(ltrim(transacted_email)) != ''
--launch
Drop table if exists public.om_app_launched_db_funnel
create table public.om_app_launched_db_funnel
as
select * from (
select distinct b.app_install_date,a.App_launch_date,a.user_id,
case when b.phone isnull then 'New'
when App_launch_date >= app_install_date and App_launch_date < cast(dateadd(day,30,app_install_date) as date) then 'Yes' else 'No' end as flag,
row_number() over (partition by a.User_id order by App_launch_date) as R
from (
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_launch_date,
profile_phone,profile_objectid as launch_profile_objectid ,case when profile_email=' ' then profile_objectid else profile_email end as user_id
from clevertap.app_launched
) a
inner join om_clevertap_install_jan_may_final b on a.user_id=b.user_id
) where flag='Yes' order by user_id

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

31
etlTransaction_job.log Normal file
View File

@ -0,0 +1,31 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DELETE 1042
INSERT 0 1042
DROP TABLE
SELECT
DELETE 1042
INSERT 0 1042
DROP TABLE
SELECT
DELETE 1113
INSERT 0 1113
DELETE 1177
INSERT 0 1177
DELETE 1239
INSERT 0 1239
DROP TABLE

102
etlTransaction_job.sh Normal file
View File

@ -0,0 +1,102 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
DROP TABLE IF EXISTS public.manual_bundle_sku_data ;
CREATE TABLE public.manual_bundle_sku_data AS
SELECT upper(bundle_sku) Parent_sku ,
Upper(A.sku) child_sku ,
A.quantity child_quantity,
'Bundle' parent_sku_class,
cast(B.retail_price AS decimal(22,2)) bundle_retail_price,
cast(json_extract_path_text(C.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) AS decimal(22,2)) bronze_price,
cast(json_extract_path_text(C.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) AS decimal(22,2)) silver_price,
cast(json_extract_path_text(C.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price,
cast(C.retail_price AS decimal(22,2)) child_retail_price
FROM bundle_data_manual_new A
INNER JOIN raena_catalog_management.product B ON upper(A.bundle_sku) = upper(B.sku)
INNER JOIN raena_catalog_management.product C ON upper(A.sku) = upper(C.sku);
DROP TABLE IF EXISTS public.order_level_data;
CREATE TABLE public.order_level_data AS
SELECT external_id,
transaction_date AS transaction_date,
discount_amount,
shipping_cost,
A.coupon_code,
reseller_tier_name,
CASE
WHEN flash_sale_id IS NOT NULL THEN 'Flash'
END Product_type,
order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount,
coupon_applied_on
FROM
(SELECT A.id AS external_id ,
(A.created_at) AS transaction_date ,
A.discount_amount,
applied_shipping_amount shipping_cost,
A.coupon_code,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
flash_sale_id,
loyalty_discount AS order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount
FROM raena_order_management.order A
WHERE payment_status='Paid'
AND cast(A.created_at AS date) >='$reportDate'
AND is_campaign = 'false' ) A
LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code;
DROP TABLE IF EXISTS public.base_netsuite_stage1;
CREATE TABLE public.base_netsuite_stage1 AS
SELECT DISTINCT transaction_date,
A.external_id,
B.country,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_sku
ELSE B.sku
END sku,
CASE
WHEN B.parent_item_id = F.id THEN F.sku
ELSE B.sku
END parent_sku,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_quantity*B.quantity
ELSE B.quantity
END quantity,
CASE
WHEN B.parent_item_id = F.id THEN F.quantity
ELSE B.quantity
END parent_quantity,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_retail_price
ELSE B.retail_price
END retail_price,

30
etlTransaction_job_v2.log Normal file
View File

@ -0,0 +1,30 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DELETE 970
INSERT 0 970
DROP TABLE
SELECT
DELETE 970
INSERT 0 970
DROP TABLE
SELECT
DELETE 993
INSERT 0 993
DELETE 976
INSERT 0 976
DELETE 1171
INSERT 0 1171
DROP TABLE
SELECT
DROP TABLE
SELECT

26
etlTrasnsaction_job.log Normal file
View File

@ -0,0 +1,26 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DELETE 3806
INSERT 0 3806
DROP TABLE
SELECT
DELETE 793
INSERT 0 793
DROP TABLE
SELECT
DELETE 793
INSERT 0 793

925
etltransaction_job_final.sh Normal file
View File

@ -0,0 +1,925 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
DROP TABLE IF EXISTS public.manual_bundle_sku_data ;
CREATE TABLE public.manual_bundle_sku_data AS
SELECT upper(bundle_sku) Parent_sku ,
Upper(A.sku) child_sku ,
A.quantity child_quantity,
'Bundle' parent_sku_class,
cast(B.retail_price AS decimal(22,2)) bundle_retail_price,
cast(json_extract_path_text(C.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) AS decimal(22,2)) bronze_price,
cast(json_extract_path_text(C.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) AS decimal(22,2)) silver_price,
cast(json_extract_path_text(C.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price,
cast(C.retail_price AS decimal(22,2)) child_retail_price
FROM bundle_data_manual_new A
INNER JOIN raena_catalog_management.product B ON upper(A.bundle_sku) = upper(B.sku)
INNER JOIN raena_catalog_management.product C ON upper(A.sku) = upper(C.sku);
DROP TABLE IF EXISTS public.order_level_data;
CREATE TABLE public.order_level_data AS
SELECT external_id,
transaction_date AS transaction_date,
discount_amount,
shipping_cost,
A.coupon_code,
reseller_tier_name,
CASE
WHEN flash_sale_id IS NOT NULL THEN 'Flash'
END Product_type,
order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount,
coupon_applied_on
FROM
(SELECT A.id AS external_id ,
(A.created_at) AS transaction_date ,
A.discount_amount,
applied_shipping_amount shipping_cost,
A.coupon_code,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
flash_sale_id,
loyalty_discount AS order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount
FROM raena_order_management.order A
WHERE payment_status='Paid'
AND cast(A.created_at AS date) >='$reportDate'
AND is_campaign = 'false') A
LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code;
DROP TABLE IF EXISTS public.base_netsuite_stage1;
CREATE TABLE public.base_netsuite_stage1 AS
SELECT DISTINCT transaction_date,
A.external_id,
B.country,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_sku
ELSE B.sku
END sku,
CASE
WHEN B.parent_item_id = F.id THEN F.sku
ELSE B.sku
END parent_sku,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_quantity*B.quantity
ELSE B.quantity
END quantity,
CASE
WHEN B.parent_item_id = F.id THEN F.quantity
ELSE B.quantity
END parent_quantity,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_retail_price
ELSE B.retail_price
END retail_price,
CASE
WHEN B.parent_item_id = F.id THEN F.retail_price
ELSE B.retail_price
END parent_retail_price,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'BRONZE' THEN bronze_price
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'SILVER' THEN silver_price
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar)
AND reseller_tier_name = 'GOLD' THEN gold_price
ELSE B.wholesale_price
END wholesale_price,
CASE
WHEN parent_sku LIKE 'BAZ%' THEN B.wholesale_price
WHEN B.parent_item_id = F.id THEN F.wholesale_price
ELSE B.wholesale_price
END AS parent_wholesale_price,
CASE
WHEN B.parent_item_id = F.id THEN B.retail_price*B.quantity*F.discount_price / sum(CASE WHEN B.parent_item_id = F.id THEN B.retail_price*B.quantity END) over(partition BY external_id ,F.sku)
ELSE B.discount_price
END discount_price,
coupon_applied_on,
A.discount_amount,
CASE
WHEN B.parent_item_id IS NULL
AND cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN 0
ELSE B.dynamic_price
END Dynamic_price ,
CASE
WHEN B.parent_item_id = F.id THEN F.dynamic_price*F.quantity
WHEN B.parent_item_id IS NULL
AND cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN B.dynamic_price*B.quantity
END AS parent_dynamic_price,
B.payment_price,
payment_amount,
B.product_class,
coalesce(F.product_class,B.product_class) parent_product_class,
CASE
WHEN A.product_type = 'Flash'
AND B.sku LIKE 'BAZ%' THEN 'Flash Bundle'
WHEN A.product_type = 'Flash'
AND B.sku NOT LIKE 'BAZ%' THEN 'Flash'
WHEN B.sku LIKE 'BAZ%' THEN 'Bundle'
ELSE 'Regular'
END AS product_type_class,
F.id,
A.order_loyalty_discount,
B.loyalty_discount,
reseller_tier_name
FROM public.order_level_data A
LEFT JOIN raena_order_management.order_item B ON A.external_id = B.order_id
LEFT JOIN public.manual_bundle_sku_data D ON cast(B.sku AS varchar) = cast(D.parent_sku AS varchar)
LEFT JOIN
(SELECT id ,
order_id ,
retail_price,
Sku ,
quantity,
wholesale_price,
discount_price,
dynamic_price,
payment_price,
product_class,
loyalty_discount
FROM raena_order_management.order_item
WHERE product_class = 'Bundle'
AND cast(created_at AS date) >='$reportDate') F ON B.parent_item_id = F.id
ORDER BY 1,
2;
DROP TABLE IF EXISTS public.loyalty_point_calculation1;
CREATE TABLE public.loyalty_point_calculation1 AS
SELECT *,
sum(order_loyalty_discount)over(partition BY external_id)/count(external_id)over(partition BY external_id) total_order_loyalty_discount ,
sum(loyalty_discount) over(partition BY external_id) total_sku_loyalty_discount
FROM public.base_netsuite_stage1
WHERE order_loyalty_discount >0;
DROP TABLE IF EXISTS public.final_loyalty_point;
CREATE TABLE public.final_loyalty_point AS
SELECT A.*,
CASE
WHEN sum(gold) over (partition BY external_id)>0
AND reseller_tier_name = 'GOLD' THEN gold*total_order_loyalty_discount/sum(gold) over (partition BY external_id)
WHEN sum(SILVER) over (partition BY external_id)>0
AND reseller_tier_name = 'SILVER' THEN SILVER*total_order_loyalty_discount/sum(SILVER) over (partition BY external_id)
WHEN sum(BRONZE) over (partition BY external_id)>0
AND reseller_tier_name = 'BRONZE' THEN bronze*total_order_loyalty_discount/sum(BRONZE) over (partition BY external_id)
END Final_loyalty_point
FROM public.loyalty_point_calculation1 A
LEFT JOIN loyalty_discount B ON A.sku = B.sku
WHERE total_order_loyalty_discount-total_sku_loyalty_discount NOT BETWEEN -10 AND 10;
DROP TABLE IF EXISTS public.base_netsuite_stage2 ;
CREATE TABLE public.base_netsuite_stage2 AS
SELECT transaction_date,
A.external_id,
A.sku,
country,
parent_sku,
quantity,
parent_quantity,
retail_price,
parent_retail_price,
CASE
WHEN parent_sku LIKE 'BAZ%'
AND id IS NULL THEN cast(wholesale_price*(parent_wholesale_price/quantity)/sum(wholesale_price)over(partition BY A.external_id ,parent_sku) AS decimal(22,2))
ELSE wholesale_price
END wholesale_price,
parent_wholesale_price,
discount_price,
coupon_applied_on,
discount_amount,
dynamic_price,
parent_dynamic_price,
payment_price,
payment_amount,
product_class,
parent_product_class,
product_type_class,
id,
CASE
WHEN cast(transaction_date AS date)< '2022-02-28'
AND A.external_id = B.external_id THEN B.final_loyalty_point
ELSE A.loyalty_discount
END loyalty_discount
FROM public.base_netsuite_stage1 A
LEFT JOIN
(SELECT external_id ,
sku ,
final_loyalty_point
FROM public.final_loyalty_point) B ON A.external_id = B.external_id
AND A.sku = B.sku
WHERE product_class <> 'Bundle';
DROP TABLE IF EXISTS public.base_netsuite_stage3 ;
CREATE TABLE public.base_netsuite_stage3 AS
SELECT A.* ,
CASE
WHEN dynamic_price>0 THEN dynamic_price
ELSE wholesale_price
END final_wholesale_price,
CASE
WHEN coupon_applied_on <> 'Cart'
AND discount_price>0 THEN retail_price*quantity*discount_amount/sum(CASE WHEN discount_price>0 THEN retail_price*quantity END)over(partition BY A.external_id)
WHEN coupon_applied_on <> 'Cart'
AND discount_price=0 THEN 0
WHEN coupon_applied_on ='Cart'
AND discount_price=0 THEN retail_price*quantity*discount_amount/sum(retail_price*quantity)over(partition BY A.external_id)
ELSE discount_price
END Final_discount ,
retail_price*quantity-(CASE
WHEN dynamic_price>0 THEN dynamic_price
ELSE wholesale_price
END)*quantity AS seller_margin
FROM public.base_netsuite_stage2 A ;
DROP TABLE IF EXISTS public.base_netsuite_stage4 ;
CREATE TABLE public.base_netsuite_stage4 AS
SELECT A.* ,
CASE
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount>0
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(retail_price AS decimal(22,2))*quantity*(ttl_retail_price-(parent_retail_price*parent_quantity)))/(ttl_retail_price)
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount=0
AND (parent_dynamic_price =0
OR parent_dynamic_price IS NULL)
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(final_wholesale_price AS decimal(22,2))*quantity*(ttl_wholesale_price-(parent_wholesale_price*parent_quantity)))/ttl_wholesale_price
WHEN (parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle'))
AND Final_discount=0
AND parent_dynamic_price>0
AND A.external_id = B.external_id
AND A.parent_sku = B.parent_sku THEN (cast(final_wholesale_price AS decimal(22,2))*quantity*(ttl_wholesale_price-ttl_parent_dynamic_price))/ttl_wholesale_price
END AS additional_discount,
CASE
WHEN final_discount >0 THEN cast(final_discount AS decimal(22,2))-cast(seller_margin AS decimal(22,2))
END effective_coupon_discount
FROM public.base_netsuite_stage3 A
LEFT JOIN
(SELECT external_id ,
parent_sku ,
round(sum(retail_price*quantity))ttl_retail_price,
round(sum(final_wholesale_price*quantity))ttl_wholesale_price,
min(CASE WHEN parent_dynamic_price>0 THEN parent_dynamic_price END)ttl_parent_dynamic_price
FROM public.base_netsuite_stage3
WHERE parent_product_class= 'Bundle'
OR product_type_class in('Bundle','Flash Bundle')
GROUP BY external_id ,
parent_sku) B ON A.external_id = B.external_id
AND A.parent_sku = B.parent_sku;
DELETE
FROM public.base_netsuite_final
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.order_level_data);
INSERT INTO public.base_netsuite_final
SELECT A.external_id,
A.transaction_date ,
A.discount_amount order_discount_amount,
A.shipping_cost ,
coupon_code ,
A.coupon_applied_on ,
reseller_tier_name tier,
B.sku product_sku,
B.quantity ,
retail_price,
CASE
WHEN A.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku THEN (seller_margin+diff)/B.quantity
ELSE B.seller_margin/B.quantity
END seller_margin,
((coalesce(retail_price,0)*quantity) -(coalesce(CASE WHEN A.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku THEN (seller_margin+diff)ELSE seller_margin END ,0)) -coalesce(CASE WHEN A.external_id= 'OD1641992277895310REG' THEN 180000 ELSE effective_coupon_discount END,0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price,
cast(final_discount AS decimal(22,2))/quantity coupon_discount,
CASE
WHEN A.external_id= 'OD1641992277895310REG' THEN 1200
ELSE effective_coupon_discount/quantity
END effective_coupon_discount ,
loyalty_discount/quantity loyalty_discount,
coalesce(additional_discount,0)/quantity additional_discount,
CASE
WHEN (parent_product_class = 'Bundle'
AND product_type_class = 'Flash')
OR product_type_class = 'Flash Bundle' THEN 'Flash Bundle'
WHEN (parent_product_class = 'Flash'
OR product_type_class = 'Flash') THEN 'Flash'
WHEN (parent_product_class = 'Bundle'
OR product_type_class = 'Bundle')THEN 'Bundle'
ELSE 'Regular'
END item_type,
diff
FROM public.order_level_data A
LEFT JOIN public.base_netsuite_stage4 B ON A.external_id = B.external_id
LEFT JOIN
(SELECT external_id ,
sku ,
parent_sku ,
seller_margin * diff/sum(seller_margin)over(partition BY external_id) diff
FROM public.base_netsuite_stage4 A
INNER JOIN public.order_check_table B ON A.external_id = B.order_id)C ON B.external_id = C.external_id
AND B.sku = C.sku
AND B.parent_sku = C.parent_sku ;
DROP TABLE IF EXISTS public.gm_dimensions_stage1;
CREATE TABLE public.gm_dimensions_stage1 AS
SELECT A.id AS external_id ,
shipping_province,
reseller_id ,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
discount_type,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
C.sku ,
C.name sku_name,
brand_name ,
shipping_to,
product_type ,
category_name,
order_placed_by
FROM raena_order_management.order A
LEFT JOIN raena_order_management.discount_coupon B ON A.coupon_code = B.coupon_code
LEFT JOIN raena_order_management.order_item C ON A.id = C.order_id
WHERE payment_status ='Paid'
AND A.is_campaign = 'false'
AND cast(A.created_at AS date) >='$reportDate';
DELETE
FROM public.GM_dashboard
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.gm_dimensions_stage1);
INSERT INTO public.GM_dashboard
SELECT A.external_id ,
B.transaction_date+interval'7 Hours' transaction_date,
B.order_discount_amount ,
B.shipping_cost ,
A.shipping_province,
B.coupon_code ,
A.discount_type ,
B.coupon_applied_on ,
A.brand_name,
A.category_name ,
A.product_type ,
A.sku sku,
A.sku_name ,
A.reseller_name ,
A.reseller_email ,
A.reseller_mobile ,
reseller_tier_name tier_name,
reseller_id ,
B.quantity ,
B.retail_price ,
B.seller_margin ,
B.discounted_price ,
B.additional_discount ,
B.loyalty_discount ,
B.effective_coupon_discount,
B.item_type ,
order_placed_by ,
min( case when A.sku = C.sku
AND cast(transaction_date AS date) = Cast(created_at AS date)
then coalesce(cogs_promo,cogs_non_promo) end) cogs,
shipping_to order_recipient,
CASE
WHEN brand_name IN ('LUXCRIME',
'SKINTIFIC',
'TRUEVE',
'SANIYE',
'BEAUDELAB',
'BRASOV',
'FACE REPUBLIC',
'SKIN 1004',
'PREMIERE BEAUTE',
'ALLURA',
'LIPLAPIN',
'ROUNDLAB',
'FACE FLUX',
'DOLLGORAE',
'SKINUA',
'PUREFORET',
'SKINTIFIC',
'OHMYSKIN',
'FEAT FOR SKIN',
'SECONDATE',
'KYND',
'PURNAMA',
'BASE',
'LAVIE LASH',
'REI SKIN',
'USTRAA',
'BRUNBRUN PARISGLOWINC',
'SOONHAN',
'THE YEON',
'MIXSOON',
'KOSE COSMEPORT') THEN 'High GM'
WHEN brand_name IN ('W DRESSROOM',
'BEAUSTA',
'Dewycel',
'GLUTANEX',
'HISTOIRE NATURELLE',
'FORENCOS',
'BELLFLOWER',
'MAXCLINIC') THEN 'EL/PL'
END brand_type ,
Customer_type,
BB.gm_target
FROM public.gm_dimensions_stage1 A
INNER JOIN public.base_netsuite_final B ON A.external_id =B.external_id
AND A.sku=B.product_sku
LEFT JOIN
(SELECT sku ,
min(cogs_non_promo) cogs_non_promo,
min(cogs_promo) cogs_promo,
--cogs ,
--sku_cogs_type ,
Cast(created_at AS date) created_at
FROM public.sku_cogs_audit
group by sku ,
Cast(created_at AS date) ) C ON A.sku = C.sku
AND cast(transaction_date AS date) = Cast(created_at AS date)
left join (SELECT DISTINCT id order_id,
CASE
WHEN customer_id IS NOT NULL
AND order_placed_by = 'admin' THEN 'Offline Dropshipper'
WHEN customer_id IS NOT NULL
AND order_placed_by <> 'admin' THEN 'Online Dropshipper'
WHEN customer_id IS NULL
AND order_placed_by <> 'admin' THEN 'Online Reseller'
WHEN customer_id IS NULL
AND order_placed_by = 'admin' THEN 'Offline Reseller'
END Customer_type
FROM raena_order_management.order) AA on A.external_id = AA.order_id
left join (
select DISTINCT SKU,gm_target,t.name as tierName
from raena_gross_margin_management.gross_margin_config AA
LEFT JOIN raena_user_management.tier t
ON cast(AA.gm_target_tier as TEXT) =cast(t.id as TEXT)
) BB
on A.sku = BB.sku
and A.reseller_tier_name =BB.tierName
GROUP BY A.external_id ,
B.transaction_date+interval'7 Hours' ,
B.order_discount_amount ,
B.shipping_cost ,
A.shipping_province,
B.coupon_code ,
A.discount_type ,
B.coupon_applied_on ,
A.brand_name,
A.category_name ,
A.product_type ,
A.sku ,
A.sku_name ,
A.reseller_name ,
A.reseller_email ,
A.reseller_mobile ,
reseller_tier_name ,
reseller_id ,
B.quantity ,
B.retail_price ,
B.seller_margin ,
B.discounted_price ,
B.additional_discount ,
B.loyalty_discount ,
B.effective_coupon_discount,
B.item_type ,
order_placed_by,
shipping_to,
CASE
WHEN brand_name IN ('LUXCRIME',
'SKINTIFIC',
'TRUEVE',
'SANIYE',
'BEAUDELAB',
'BRASOV',
'FACE REPUBLIC',
'SKIN 1004',
'PREMIERE BEAUTE',
'ALLURA',
'LIPLAPIN',
'ROUNDLAB',
'FACE FLUX',
'DOLLGORAE',
'SKINUA',
'PUREFORET',
'SKINTIFIC',
'OHMYSKIN',
'FEAT FOR SKIN',
'SECONDATE',
'KYND',
'PURNAMA',
'BASE',
'LAVIE LASH',
'REI SKIN',
'USTRAA',
'BRUNBRUN PARISGLOWINC',
'SOONHAN',
'THE YEON',
'MIXSOON',
'KOSE COSMEPORT') THEN 'High GM'
WHEN brand_name IN ('W DRESSROOM',
'BEAUSTA',
'Dewycel',
'GLUTANEX',
'HISTOIRE NATURELLE',
'FORENCOS',
'BELLFLOWER',
'MAXCLINIC') THEN 'EL/PL'
END,Customer_type,
BB.gm_target ;
DROP TABLE IF EXISTS public.business_dimensions_stage1;
CREATE TABLE public.business_dimensions_stage1 AS
SELECT A.id AS external_id ,
reseller_id,
customer_id,
cart_id,
A.flash_sale_id,
shipping_to,
payment_status,
A.status,
A.stock_type,
order_placed_by,
order_warehouse,
cast(channel_id as text) channel_id ,
medium,
marketplace,
provider,
coalesce(cast(B.tier_id as varchar),cast(C.tier_id as varchar)) tier_id ,
json_extract_path_text(A.reseller_info,'city',TRUE) reseller_city,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
json_extract_path_text(A.reseller_info,'store',TRUE) reseller_store,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
shipping_address_line1 ,
shipping_address_line2 ,
shipping_pincode ,
shipping_district,
shipping_city ,
shipping_province ,
is_bank_transfer ,
is_campaign ,
discount_type
FROM raena_order_management.order A
left join public.tier_name B on A.id = B.id
left join raena_user_management.user C on cast(A.reseller_id as varchar) = cast(C.id as varchar)
left join raena_order_management.discount_coupon D on A.coupon_code = D.coupon_code
where cast(A.created_at AS date) >='$reportDate';
DELETE
FROM public.business_report
WHERE external_id IN
(SELECT DISTINCT external_id
FROM public.business_dimensions_stage1);
Insert into public.business_report
SELECT A.external_id,
cast(B.transaction_date as date) created_date ,
B.order_discount_amount,
B.shipping_cost ,
coupon_code ,
D.name brand_name,
C.product_type,
E.name category_name,
C.name sku_name,
B.product_sku sku,
reseller_id,
customer_id,
cart_id,
A.flash_sale_id,
shipping_to,
payment_status,
status,
A.stock_type,
order_placed_by,
order_warehouse,
channel_id,
medium,
marketplace,
provider,
reseller_tier_name tier,
reseller_city,
reseller_name,
reseller_email,
reseller_mobile,
reseller_store,
shipping_address_line1 ,
shipping_address_line2 ,
shipping_pincode ,
shipping_district,
shipping_city ,
shipping_province ,
is_bank_transfer ,
is_campaign ,
C.Country,
B.quantity ,
B.retail_price,
seller_margin,
discounted_price,
loyalty_discount,
effective_coupon_discount ,
additional_discount,
item_type
FROM public.business_dimensions_stage1 A
LEFT JOIN public.base_netsuite_final B ON A.external_id = B.external_id
left join raena_catalog_management.product C on B.product_sku = C.sku
left join raena_catalog_management.brand D on C.brand_id = D.id
left join raena_catalog_management.category E on C.category_id = E.id;
DELETE
FROM public.OM_GM_DB_Product_category
WHERE external_Id IN
(SELECT DISTINCT external_id
FROM public.GM_dashboard
WHERE transaction_date >='$reportDate');
INSERT INTO OM_GM_DB_Product_category
SELECT A.external_id ,
transaction_date,
concat(left(TO_char(transaction_date,'month'),3),date_part('year',transaction_date)) AS Time,
order_discount_amount ,
shipping_cost ,
coupon_code ,
discount_type ,
coupon_applied_on ,
D.brand_name,
D.category_name ,
D.product_type ,
A.sku,
D.name sku_name ,
reseller_name ,
reseller_email ,
reseller_mobile ,
tier_name ,
reseller_id ,
quantity ,
retail_price ,
seller_margin ,
discounted_price ,
additional_discount,
item_type ,
order_placed_by ,
A.cogs,
shipping_province ,
CASE
WHEN A.external_id = C.external_id THEN 'Yes'
ELSE 'No'
END Flag ,
CASE
WHEN tier_name='GOLD' THEN gold_price
WHEN tier_name ='SILVER' THEN silver_price
WHEN tier_name ='BRONZE' THEN bronze_price
ELSE retail_price-seller_margin
END wholesale_price,
order_recipient,
brand_type,
Customer_type,gm_target
FROM public.GM_dashboard A
LEFT JOIN
(SELECT DISTINCT external_id
FROM
(SELECT external_id,
discounted_price ,
sku ,
(((retail_price-seller_margin)*quantity) -(cogs*quantity))/((retail_price-seller_margin)*quantity) Pre_discount,
CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/(discounted_price*quantity) ELSE 0 END Post_discount
FROM public.GM_dashboard) A
WHERE Post_discount>Pre_discount) C ON A.external_id = C.external_id
LEFT JOIN
(SELECT DISTINCT Sku ,
A.name,
A.product_type,
C.name category_name ,
B.name brand_name
FROM raena_catalog_management.product A
LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id
LEFT JOIN raena_catalog_management.category c ON A.category_id=C.id) D ON A.sku = D.sku
LEFT JOIN pricing_sheet P ON A.sku=p.skucode
WHERE A.cogs IS NOT NULL
AND transaction_date >='$reportDate';
--Business Dashboard
DELETE
FROM public.GM_GROWTH_TAB1
WHERE created_date >='$reportDate';
INSERT INTO public.GM_GROWTH_TAB1
SELECT created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
channel_name,
reseller_name,reseller_email,
count(DISTINCT external_id) AS Number_of_Orders ,
sum(retail_price*quantity) RSP ,
sum((retail_price*quantity)-(seller_margin*quantity))wholesale_price ,
sum(discounted_price*quantity) AS Payment_Price ,
sum(effective_coupon_discount*quantity) AS Effective_Coupon_Discount,
sum(loyalty_discount*quantity)Total_Loyalty_point,
sum(additional_discount*quantity)additional_discount,
sum(seller_margin*quantity)seller_margin,
sum(discounted_price*quantity)/count(DISTINCT external_id) AS AOV,
sum(quantity) AS quantity
FROM
(SELECT DISTINCT a.created_date,
'All' AS brand_name,
'All' AS product_type,
'All' AS category_name,
'All' AS sku,
'All' AS sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
b.channel_name,
reseller_name,reseller_email,
external_id,
retail_price,
quantity,
seller_margin,
discounted_price,
effective_coupon_discount,
loyalty_discount,
additional_discount,
row_number() over (partition BY external_id
ORDER BY external_id) AS R
FROM public.business_report a
LEFT JOIN
(SELECT DISTINCT A.id ,
channel_id ,
D.name channel_name
FROM raena_order_management.order A
LEFT JOIN
(SELECT id,
name
FROM raena_order_management.channel) D ON A.channel_id = D.id
WHERE cast(created_at AS date) >= '$reportDate'
AND channel_id IS NOT NULL) b ON a.external_id=b.id
WHERE cast(is_campaign AS varchar)='false'
AND payment_status='Paid'
AND created_date >= '$reportDate'
UNION SELECT DISTINCT a.created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
b.channel_name,
reseller_name,reseller_email,
external_id,
retail_price,
quantity,
seller_margin,
discounted_price,
effective_coupon_discount,
loyalty_discount,
additional_discount,
row_number() over (partition BY external_id
ORDER BY external_id) AS R
FROM public.business_report a
LEFT JOIN
(SELECT DISTINCT A.id ,
channel_id ,
D.name channel_name
FROM raena_order_management.order A
LEFT JOIN
(SELECT id,
name
FROM raena_order_management.channel) D ON A.channel_id = D.id
WHERE cast(created_at AS date) >= '$reportDate'
AND channel_id IS NOT NULL) b ON a.external_id=b.id
WHERE cast(is_campaign AS varchar)='false'
AND payment_status='Paid'
AND created_date >= '$reportDate')
GROUP BY created_date,
brand_name,
product_type,
category_name,
sku,
sku_name,
order_placed_by,
shipping_province,
shipping_to,
status,
channel_name,
reseller_name,reseller_email;
--Metric Trend
DROP TABLE IF EXISTS public.GM_GROWTH_TAB2;
CREATE TABLE public.GM_GROWTH_TAB2 AS
SELECT 'year' AS frequency,
cast(date_part('year',created_date) AS varchar) AS time,
cast(date_part('year',created_date) AS int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'quarter' AS frequency,
To_char(created_date,'quarter') AS month_name,
date_part('quarter',created_date) as sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'month' AS frequency,
To_char(created_date,'month') AS month_name,
date_part('month',created_date) as sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'week' AS frequency,
To_char(created_date,'week') AS month_name,
cast(left(To_char(created_date,'week'),1) as int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'day' AS frequency,
cast(date_part('day',created_date) as varchar) AS month_name,
cast(date_part('day',created_date) AS int) AS sort ,
*
FROM public.GM_GROWTH_TAB1
ORDER BY 3;
" > /home/ec2-user/cronjob/Final_GM_v1.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/Final_GM_v1.sql > etlTransaction_job.log

10
etlwarehouseAnalysis.log Normal file
View File

@ -0,0 +1,10 @@
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE
SELECT

View File

@ -0,0 +1,18 @@
DROP TABLE
SELECT
DELETE 0
INSERT 0 3152
DROP TABLE
SELECT
DROP TABLE
SELECT
DELETE 19170
INSERT 0 19170
DROP TABLE
SELECT
DROP TABLE
SELECT
DELETE 42
INSERT 0 42
DROP TABLE
SELECT

View File

@ -0,0 +1,782 @@
drop table if exists public.homepage_base_stage1_v1;
create table public.homepage_base_stage1_v1
as
select *
FROM clevertap.clevertap_master_data cmd
WHERE events IN ('home_stories',
'home_banner',
'home_category',
'videofeed_videoclick',
'home_brand_grid_view',
'home_brand_Caraousel',
'home_product_Caraousel',
'home_product_infinite')
and ts::date = '2023-01-22' ;
delete from public.homepage_base_stage1
where ts::date = '2023-01-22';
insert into public.homepage_base_stage1
SELECT ts,
events,
name ,
email,
phone,
objectid,
json_extract_path_text(profiledata,'tier') tier,
json_extract_path_text(profiledata,'isguestuser') isguestuser,
split_part(json_extract_path_text(replace(event_props,'\\xa0',' '),'url'),'/',2) collection,
split_part(json_extract_path_text(replace(event_props,'\\xa0',' '),'url'),'/',3) collection_id,
json_extract_path_text(replace(event_props,'\\xa0',' '),'userId') userId,
json_extract_path_text(replace(event_props,'\\xa0',' '),'sectionName') sectionName,
json_extract_path_text(replace(event_props,'\\xa0',' '),'guest_user') guest_user,
json_extract_path_text(replace(event_props,'\\xa0',' '),'tier') tier_id,
json_extract_path_text(replace(event_props,'\\xa0',' '),'categoryName') categoryName,
json_extract_path_text(replace(event_props,'\\xa0',' '),'brand_code') brand_code,
json_extract_path_text(replace(event_props,'\\xa0',' '),'brand_name') brand_name,
case when events ='home_product_infinite' then json_extract_path_text(replace(event_props,'\\xa0',' '),'brand') end brand_id,
case when events ='home_product_Caraousel' then json_extract_path_text(replace(event_props,'\\xa0',' '),'brand')end brand,
json_extract_path_text(replace(event_props,'\\xa0',' '),'productId') productId,
json_extract_path_text(replace(event_props,'\\xa0',' '),'videoId') videoId,
json_extract_path_text(replace(event_props,'\\xa0',' '),'pageName') pageName,
json_extract_path_text(replace(event_props,'\\xa0',' '),'SKU_code') SKU_code
FROM public.homepage_base_stage1_v1;
drop table if exists public.homepage_base;
create table public.homepage_base AS
SELECT 'Day' Frequency,
date_trunc('day',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12
union
SELECT 'Week' Frequency,
date_trunc('Week',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12
union
SELECT 'Month' Frequency,
date_trunc('Month',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12
union
SELECT 'Quarter' Frequency,
date_trunc('quarter',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12
union
SELECT 'Year' Frequency,
date_trunc('Year',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12;
drop table if exists public.pdp_base_stage1_v1;
create table public.pdp_base_stage1_v1
as
select *
FROM clevertap.clevertap_master_data cmd
WHERE events IN ('view_item',
'pdp_buynow',
'add_to_cart',
'pdp_setmargin')
and ts::date = '2023-01-22' ;
delete from public.pdp_base_stage1
where ts::date = '2023-01-22' ;
insert into public.pdp_base_stage1
SELECT ts,
events,
name ,
email,
phone,
objectid,
json_extract_path_text(profiledata,'tier') tier,
json_extract_path_text(profiledata ,'isguestuser') isguestuser,
json_extract_path_text(replace(event_props,'\\xa0',' '),'item_brand') brand_name,
json_extract_path_text(replace(event_props,'\\xa0',' ') ,'item_id') SKU_code,
json_extract_path_text(replace(event_props,'\\xa0',' ') ,'categoryPageId') categoryPageId,
json_extract_path_text(replace(event_props,'\\xa0',' '),'screen') screen
FROM public.pdp_base_stage1_v1;
drop table if exists public.pdp_base;
create table public.pdp_base AS
SELECT 'Day' Frequency,
date_trunc('day',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9
union
SELECT 'Week' Frequency,
date_trunc('Week',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9
union
SELECT 'Month' Frequency,
date_trunc('Month',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9
union
SELECT 'Quarter' Frequency,
date_trunc('quarter',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9
union
SELECT 'Year' Frequency,
date_trunc('Year',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9;
DROP TABLE IF EXISTS public.flash_sale_order;
CREATE TABLE public.flash_sale_order AS
SELECT 'Day' Frequency,
date_trunc('Day',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6
union
SELECT 'Week' Frequency,
date_trunc('Week',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6
union
SELECT 'Month' Frequency,
date_trunc('Month',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6
union
SELECT 'Quarter' Frequency,
date_trunc('Quarter',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6
union
SELECT 'Year' Frequency,
date_trunc('year',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6;
delete from public.flash_base_stage1
where ts::date = '2023-01-22';
insert into public.flash_base_stage1
SELECT ts,
name ,
email,
events,
phone,
objectid,
upper(json_extract_path_text(profiledata,'tier')) tier,
json_extract_path_text(replace(event_props,'\\xa0',' '),'item_brand') brand_name,
json_extract_path_text(replace(event_props,'\\xa0',' ') ,'item_id') SKU_code
FROM clevertap.clevertap_master_data cmd
WHERE events in ('flashsale_carousel_item_press','flashsale_carousel_view_all_press')
and ts::date = '2023-01-22' ;
drop table if exists public.flash_sale_event_table;
create table public.flash_sale_event_table
as
SELECT 'Day' Frequency,
date_trunc('day',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Week' Frequency,
date_trunc('Week',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Month' Frequency,
date_trunc('Month',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Quarter' Frequency,
date_trunc('Quarter',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Quarter' Frequency,
date_trunc('Quarter',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Year' Frequency,
date_trunc('Year',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5;

View File

@ -0,0 +1,797 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
drop table if exists public.homepage_base_stage1_v1;
create table public.homepage_base_stage1_v1
as
select *
FROM clevertap.clevertap_master_data cmd
WHERE events IN ('home_stories',
'home_banner',
'home_category',
'videofeed_videoclick',
'home_brand_grid_view',
'home_brand_Caraousel',
'home_product_Caraousel',
'home_product_infinite')
and ts::date = '$reportDate' ;
delete from public.homepage_base_stage1
where ts::date = '$reportDate';
insert into public.homepage_base_stage1
SELECT ts,
events,
name ,
email,
phone,
objectid,
json_extract_path_text(profiledata,'tier') tier,
json_extract_path_text(profiledata,'isguestuser') isguestuser,
split_part(json_extract_path_text(replace(event_props,'\\\xa0',' '),'url'),'/',2) collection,
split_part(json_extract_path_text(replace(event_props,'\\\xa0',' '),'url'),'/',3) collection_id,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'userId') userId,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'sectionName') sectionName,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'guest_user') guest_user,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'tier') tier_id,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'categoryName') categoryName,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'brand_code') brand_code,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'brand_name') brand_name,
case when events ='home_product_infinite' then json_extract_path_text(replace(event_props,'\\\xa0',' '),'brand') end brand_id,
case when events ='home_product_Caraousel' then json_extract_path_text(replace(event_props,'\\\xa0',' '),'brand')end brand,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'productId') productId,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'videoId') videoId,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'pageName') pageName,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'SKU_code') SKU_code
FROM public.homepage_base_stage1_v1;
drop table if exists public.homepage_base;
create table public.homepage_base AS
SELECT 'Day' Frequency,
date_trunc('day',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12
union
SELECT 'Week' Frequency,
date_trunc('Week',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12
union
SELECT 'Month' Frequency,
date_trunc('Month',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12
union
SELECT 'Quarter' Frequency,
date_trunc('quarter',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12
union
SELECT 'Year' Frequency,
date_trunc('Year',ts)::date AS created_date,
coalesce(tier,C.name) tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
coalesce(isguestuser,guest_user) guest_user,
collection,
collection_id,
coalesce(F.name,categoryName)categoryName,
coalesce(E.name,D.name,brand_name,brand) brand_name,
videoId,
pageName,
SKU_code,
count(CASE WHEN events = 'home_banner' THEN events END) total_home_banner_event,
count(DISTINCT CASE WHEN events = 'home_banner' THEN phone END) total_home_banner_user,
count(CASE WHEN events = 'home_stories' THEN events END) total_home_stories_event,
count(DISTINCT CASE WHEN events = 'home_stories' THEN phone END) total_home_stories_user,
count(CASE WHEN events = 'home_category' THEN events END) total_home_category_event,
count(DISTINCT CASE WHEN events = 'home_category' THEN phone END) total_home_category_user,
count(CASE WHEN events = 'home_brand_grid_view' THEN events END) total_home_brand_grid_view_event,
count(DISTINCT CASE WHEN events = 'home_brand_grid_view' THEN phone END) total_home_brand_grid_view_user,
count(CASE WHEN events = 'videofeed_videoclick' THEN events END) total_videofeed_videoclick_event,
count(DISTINCT CASE WHEN events = 'videofeed_videoclick' THEN phone END) total_videofeed_videoclick_user,
count(CASE WHEN events = 'home_brand_Caraousel' THEN events END) total_home_brand_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_brand_Caraousel' THEN phone END) total_home_brand_Caraousel_user,
count(CASE WHEN events = 'home_product_Caraousel' THEN events END) total_home_product_Caraousel_event,
count(DISTINCT CASE WHEN events = 'home_product_Caraousel' THEN phone END) total_home_product_Caraousel_user,
count(CASE WHEN events = 'home_product_infinite' THEN events END) total_home_product_infinite_event,
count(DISTINCT CASE WHEN events = 'home_product_infinite' THEN phone END) total_home_product_infinite_user
FROM public.homepage_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
LEFT JOIN raena_user_management.tier C ON tier_id= C.id
LEFT JOIN raena_catalog_management.brand D ON brand_code= D.id
LEFT JOIN raena_catalog_management.brand E ON collection_id= E.id
LEFT JOIN raena_catalog_management.category F ON collection_id= F.id
OR brand_id = D.id
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,12;
drop table if exists public.pdp_base_stage1_v1;
create table public.pdp_base_stage1_v1
as
select *
FROM clevertap.clevertap_master_data cmd
WHERE events IN ('view_item',
'pdp_buynow',
'add_to_cart',
'pdp_setmargin')
and ts::date = '$reportDate' ;
delete from public.pdp_base_stage1
where ts::date = '$reportDate' ;
insert into public.pdp_base_stage1
SELECT ts,
events,
name ,
email,
phone,
objectid,
json_extract_path_text(profiledata,'tier') tier,
json_extract_path_text(profiledata ,'isguestuser') isguestuser,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'item_brand') brand_name,
json_extract_path_text(replace(event_props,'\\\xa0',' ') ,'item_id') SKU_code,
json_extract_path_text(replace(event_props,'\\\xa0',' ') ,'categoryPageId') categoryPageId,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'screen') screen
FROM public.pdp_base_stage1_v1;
drop table if exists public.pdp_base;
create table public.pdp_base AS
SELECT 'Day' Frequency,
date_trunc('day',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9
union
SELECT 'Week' Frequency,
date_trunc('Week',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9
union
SELECT 'Month' Frequency,
date_trunc('Month',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9
union
SELECT 'Quarter' Frequency,
date_trunc('quarter',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9
union
SELECT 'Year' Frequency,
date_trunc('Year',ts)::date AS created_date,
tier tier_name,
CASE
WHEN date_trunc('Month',ts)::date = date_trunc('Month',first_install_date)::date
AND date_trunc('Month',ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
ELSE 'existing'
END user_type,
isguestuser guest_user,
brand_name brand_name,
categoryPageId,
screen,
SKU_code,
count(CASE WHEN events = 'view_item' THEN events END) total_view_item_event,
count(DISTINCT CASE WHEN events = 'view_item' THEN phone END) total_view_item_user,
count(CASE WHEN events = 'pdp_buynow' THEN events END) total_pdp_buynow_event,
count(DISTINCT CASE WHEN events = 'pdp_buynow' THEN phone END) total_pdp_buynow_user,
count(CASE WHEN events = 'add_to_cart' THEN events END) total_add_to_cart_event,
count(DISTINCT CASE WHEN events = 'add_to_cart' THEN phone END) total_add_to_cart_user,
count(CASE WHEN events = 'pdp_setmargin' THEN events END) total_pdp_setmargin_event,
count(DISTINCT CASE WHEN events = 'pdp_setmargin' THEN phone END) total_pdp_setmargin_user
FROM public.pdp_base_stage1
LEFT JOIN public.user_type_table ON phone = reseller_mobile
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9;
DROP TABLE IF EXISTS public.flash_sale_order;
CREATE TABLE public.flash_sale_order AS
SELECT 'Day' Frequency,
date_trunc('Day',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6
union
SELECT 'Week' Frequency,
date_trunc('Week',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6
union
SELECT 'Month' Frequency,
date_trunc('Month',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6
union
SELECT 'Quarter' Frequency,
date_trunc('Quarter',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6
union
SELECT 'Year' Frequency,
date_trunc('year',transaction_date)::date AS created_date,
upper(tier_name)tier_name,
sku,
brand_name,
flash_sale_id,
count(DISTINCT external_id) total_orders,
sum(discounted_price*quantity) post_discount_revenue,
sum(quantity) total_quantity,
count(distinct reseller_id) number_of_reseller
FROM
(SELECT external_id ,
sku,
brand_name,
public.gm_dashboard.quantity ,
tier_name,
flash_sale_id,
discounted_price discounted_price,
public.gm_dashboard.reseller_id,
transaction_date::date transaction_date
FROM public.gm_dashboard
INNER JOIN raena_order_management.sales_sub_order ON external_id = order_id and sku=raena_order_management.sales_sub_order.parent_sku
AND flash_sale_id IS NOT NULL) A
GROUP BY 1,
2,
3,
4,
5,
6;
delete from public.flash_base_stage1
where ts::date = '$reportDate';
insert into public.flash_base_stage1
SELECT ts,
name ,
email,
events,
phone,
objectid,
upper(json_extract_path_text(profiledata,'tier')) tier,
json_extract_path_text(replace(event_props,'\\\xa0',' '),'item_brand') brand_name,
json_extract_path_text(replace(event_props,'\\\xa0',' ') ,'item_id') SKU_code
FROM clevertap.clevertap_master_data cmd
WHERE events in ('flashsale_carousel_item_press','flashsale_carousel_view_all_press')
and ts::date = '$reportDate' ;
drop table if exists public.flash_sale_event_table;
create table public.flash_sale_event_table
as
SELECT 'Day' Frequency,
date_trunc('day',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Week' Frequency,
date_trunc('Week',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Month' Frequency,
date_trunc('Month',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Quarter' Frequency,
date_trunc('Quarter',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Quarter' Frequency,
date_trunc('Quarter',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5
union
SELECT 'Year' Frequency,
date_trunc('Year',ts)::date AS created_date,
tier tier_name,
brand_name brand_name,
SKU_code,
count(CASE WHEN events = 'flashsale_carousel_item_press' THEN events END) total_flashsale_carousel_item_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_item_press' THEN phone END) total_flashsale_carousel_item_press_user,
count(CASE WHEN events = 'flashsale_carousel_view_all_press' THEN events END) total_flashsale_carousel_view_all_press_event,
count(DISTINCT CASE WHEN events = 'flashsale_carousel_view_all_press' THEN phone END) total_flashsale_carousel_view_all_press_user
FROM public.flash_base_stage1
GROUP BY 1,
2,
3,
4,
5;
" > /home/ec2-user/cronjob/home_page_pdp_flash/home_page_pdp_flash.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/home_page_pdp_flash/home_page_pdp_flash.sql

73
install_uninsatll_rate.sh Normal file
View File

@ -0,0 +1,73 @@
#!/bin/bash
echo "
create table om_app_install_uninstall_rate_reseller_business_metrics_db
as
select frequency,case when frequency='month' then cast(date_part('year',App_uninstall_date) as varchar)
when frequency='quarter' then cast(date_part('year',App_uninstall_date) as varchar)
when frequency='day' then concat(left(TO_CHAR(App_uninstall_date,'Month'),3),right(cast(date_part('year',App_uninstall_date) as varchar),2))
when frequency='week' then concat(left(TO_CHAR(App_uninstall_date,'Month'),3),right(cast(date_part('year',App_uninstall_date) as varchar),2))
end as upper ,
case when frequency='month' then cast(date_part('year',App_uninstall_date) as varchar)
when frequency='quarter' then cast(date_part('year',App_uninstall_date) as varchar)
when frequency='day' then concat(right(cast(date_part('year',App_uninstall_date) as varchar),2),right(cast(date_part('month',App_uninstall_date) as varchar),1))
when frequency='week' then concat(right(cast(date_part('year',App_uninstall_date) as varchar),2),right(cast(date_part('month',App_uninstall_date) as varchar),1))
end as upper_sort,
"time",sort,flag,tier,sum(uninstall_user) as uninstall_user,sum(install_user) as install_user
--cast(sum(uninstall_user) as float)/cast(sum(install_user) as float) as UnInstall_rate
from
(
select 'year' as frequency,
cast(date_part('year',App_uninstall_date) as varchar) as time,cast(date_part('year',App_uninstall_date) as int) as sort,
App_uninstall_date,om_app_uninstalled_reseller_business_metrics_db.flag,om_app_uninstalled_reseller_business_metrics_db.tier,count(distinct public.om_app_uninstalled_reseller_business_metrics_db.user_id) as uninstall_user,
count(distinct public.om_app_installed_reseller_business_metrics_db.user_id) as install_user
from public.om_app_uninstalled_reseller_business_metrics_db
left join public.om_app_installed_reseller_business_metrics_db
on app_install_date between cast(dateadd(day,-30,App_uninstall_date) as date) and App_uninstall_date
group by 2,3,4,5,6
union
select 'quarter' as frequency,
To_char(App_uninstall_date,'quarter') as time,cast(concat(date_part('year',App_uninstall_date),date_part('quarter',App_uninstall_date)) as int) as sort,
App_uninstall_date,om_app_uninstalled_reseller_business_metrics_db.flag,om_app_uninstalled_reseller_business_metrics_db.tier,count(distinct public.om_app_uninstalled_reseller_business_metrics_db.user_id) as uninstall_user,
count(distinct public.om_app_installed_reseller_business_metrics_db.user_id) as install_user
from public.om_app_uninstalled_reseller_business_metrics_db
left join public.om_app_installed_reseller_business_metrics_db
on app_install_date between cast(dateadd(day,-30,App_uninstall_date) as date) and App_uninstall_date
group by 2,3,4,5,6
union
select 'month' as frequency,
To_char(App_uninstall_date,'month') as time,
date_part('month',App_uninstall_date) as sort,
App_uninstall_date,om_app_uninstalled_reseller_business_metrics_db.flag,om_app_uninstalled_reseller_business_metrics_db.tier,count(distinct public.om_app_uninstalled_reseller_business_metrics_db.user_id) as uninstall_user,
count(distinct public.om_app_installed_reseller_business_metrics_db.user_id) as install_user
from public.om_app_uninstalled_reseller_business_metrics_db
left join public.om_app_installed_reseller_business_metrics_db
on app_install_date between cast(dateadd(day,-30,App_uninstall_date) as date) and App_uninstall_date
group by 2,3,4,5,6
union
select 'week' as frequency,
To_char(App_uninstall_date,'week') as month_name,
cast(left(To_char(App_uninstall_date,'week'),1) as int) as sort,
App_uninstall_date,om_app_uninstalled_reseller_business_metrics_db.flag,om_app_uninstalled_reseller_business_metrics_db.tier,count(distinct public.om_app_uninstalled_reseller_business_metrics_db.user_id) as uninstall_user,
count(distinct public.om_app_installed_reseller_business_metrics_db.user_id) as install_user
from public.om_app_uninstalled_reseller_business_metrics_db
left join public.om_app_installed_reseller_business_metrics_db
on app_install_date between cast(dateadd(day,-30,App_uninstall_date) as date) and App_uninstall_date
group by 2,3,4,5,6
union
select 'day' as frequency,
cast(date_part('day',App_uninstall_date) as varchar) as month_name,
cast(date_part('day',App_uninstall_date) as int) as sort,
App_uninstall_date,om_app_uninstalled_reseller_business_metrics_db.flag,om_app_uninstalled_reseller_business_metrics_db.tier,count(distinct public.om_app_uninstalled_reseller_business_metrics_db.user_id) as uninstall_user,
count(distinct public.om_app_installed_reseller_business_metrics_db.user_id) as install_user
from public.om_app_uninstalled_reseller_business_metrics_db
left join public.om_app_installed_reseller_business_metrics_db
on app_install_date between cast(dateadd(day,-30,App_uninstall_date) as date) and App_uninstall_date
group by 2,3,4,5,6
)
group by 1,2,3,4,5,6,7
order by sort,upper_sort ;
" > /home/ec2-user/cronjob/warehouseAnalysis/install_uninstall_rate.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/warehouseAnalysis/install_uninstall_rate.sql

View File

@ -0,0 +1,118 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
DROP TABLE IF EXISTS public.loyalty_reseller_stage1;
CREATE TABLE public.loyalty_reseller_stage1 AS
SELECT distinct cast(createdat+interval'7 Hours' as date) created_date,
transactionid,
eventtype,
resellerid ,
email,
mobile,
C.name tier_name,
A.status ,
points,
CASE
WHEN lms_orderid LIKE 'OD%' THEN lms_orderid
END order_id,
totalamount
FROM public.lms_transactions A
LEFT JOIN raena_user_management.user B ON A.resellerid = B.id
LEFT JOIN raena_user_management.tier C ON A.tierid = C.id ;
DROP TABLE IF EXISTS public.loyalty_total_orders;
CREATE TABLE public.loyalty_total_orders AS
SELECT cast(A.created_at+interval'7 Hours' as date) AS created_date ,
A.reseller_id ,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
A.payment_status,
A.id order_id,
A.status order_status,
A.payment_amount payment_price
FROM raena_order_management.order A
where A.created_at ::date >='2022-01-01';
drop table if exists public.loyalty_base_1;
create table public.loyalty_base_1
as
select A.created_date ,
A.reseller_id ,
A.reseller_name,
A.reseller_email,
A.reseller_mobile,
A.reseller_tier_name,
A.payment_status,
A.order_id,
A.payment_price,
A.order_status,
B.transactionid,
B.created_date,
B.eventtype,
B.resellerid lm_reseller,
B.email lm_email,
B.mobile lm_mobile,
B.tier_name,
B.status ,
B.order_id lm_orders,
totalamount totalamount,
points loyalty_point
from public.loyalty_total_orders A left join public.loyalty_reseller_stage1 b
on A.order_id = B.order_id
and B.order_id is not null
union
select B.created_date,
B.resellerid,
'Na',
B.email,
B.mobile,
B.tier_name,
'Na',
B.order_id,
0,
'Na',
B.transactionid,
B.created_date,
B.eventtype,
B.resellerid lm_reseller,
B.email lm_email,
B.mobile lm_mobile,
B.tier_name,
B.status ,
B.order_id lm_orders,
totalamount totalamount,
points loyalty_point
from public.loyalty_reseller_stage1 B
where B.order_id is null;
" > /home/ec2-user/cronjob/loyalty_point/loyalty_point.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/loyalty_point/loyalty_point.sql

View File

@ -0,0 +1,98 @@
DROP TABLE IF EXISTS public.loyalty_reseller_stage1;
CREATE TABLE public.loyalty_reseller_stage1 AS
SELECT distinct cast(createdat+interval'7 Hours' as date) created_date,
transactionid,
eventtype,
resellerid ,
email,
mobile,
C.name tier_name,
A.status ,
points,
CASE
WHEN lms_orderid LIKE 'OD%' THEN lms_orderid
END order_id,
totalamount
FROM public.lms_transactions A
LEFT JOIN raena_user_management.user B ON A.resellerid = B.id
LEFT JOIN raena_user_management.tier C ON A.tierid = C.id ;
DROP TABLE IF EXISTS public.loyalty_total_orders;
CREATE TABLE public.loyalty_total_orders AS
SELECT cast(A.created_at+interval'7 Hours' as date) AS created_date ,
A.reseller_id ,
json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
A.payment_status,
A.id order_id,
A.status order_status,
A.payment_amount payment_price
FROM raena_order_management.order A
where A.created_at ::date >='2022-01-01';
drop table if exists public.loyalty_base_1;
create table public.loyalty_base_1
as
select A.created_date ,
A.reseller_id ,
A.reseller_name,
A.reseller_email,
A.reseller_mobile,
A.reseller_tier_name,
A.payment_status,
A.order_id,
A.payment_price,
A.order_status,
B.transactionid,
B.created_date,
B.eventtype,
B.resellerid lm_reseller,
B.email lm_email,
B.mobile lm_mobile,
B.tier_name,
B.status ,
B.order_id lm_orders,
totalamount totalamount,
points loyalty_point
from public.loyalty_total_orders A left join public.loyalty_reseller_stage1 b
on A.order_id = B.order_id
and B.order_id is not null
union
select B.created_date,
B.resellerid,
'Na',
B.email,
B.mobile,
B.tier_name,
'Na',
B.order_id,
0,
'Na',
B.transactionid,
B.created_date,
B.eventtype,
B.resellerid lm_reseller,
B.email lm_email,
B.mobile lm_mobile,
B.tier_name,
B.status ,
B.order_id lm_orders,
totalamount totalamount,
points loyalty_point
from public.loyalty_reseller_stage1 B
where B.order_id is null;

6
mongo_rdash360.log Normal file
View File

@ -0,0 +1,6 @@
DROP TABLE
DROP TABLE
SELECT
DROP TABLE
SELECT
DROP TABLE

View File

@ -0,0 +1,45 @@
#!/bin/bash
echo -e " \n----------- Conversion Report Metabase Dashboard Code --------------\n"
echo "
--------------------------Conversion Report Business metrics--------------------------
drop table if exists public.om_conversion_order_data_1
create table public.om_conversion_order_data_1
as
select distinct cast(created_at as date) as created_date,date_part('year',created_at) as order_year,date_part('month',created_at) as order_month,
a.reseller_id,id as order_id,json_extract_path_text(reseller_info,'email',TRUE) as transacted_email,replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as transacted_mobile,
payment_amount,shipping_to,order_placed_by,b.Monthly_Tier,c.Max_Tier_of_any_month,
rank() over(partition by a.reseller_id order by date_part('year',created_at),date_part('month',created_at)) as R
from raena_order_management.order a
inner join (
select distinct
reseller_id,order_year,order_month,
case when revenue<2000000 then '<2 mn' when revenue>=2000000 and revenue<=10000000 then '2-10 mn' when revenue>10000000 then '10+ mn' end as Monthly_Tier
from (
select date_part('year',created_at) as order_year,date_part('month',created_at) as order_month,reseller_id
,sum(payment_amount) as revenue
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
group by date_part('year',created_at),date_part('month',created_at),reseller_id
) where reseller_id notnull
) b on a.reseller_id=b.reseller_id and date_part('year',a.created_at) =b.order_year and date_part('month',a.created_at)=b.order_month
inner join (
select distinct
reseller_id,case when revenue<2000000 then '<2 mn' when revenue>=2000000 and revenue<=10000000 then '2-10 mn' when revenue>10000000 then '10+ mn' end as Max_Tier_of_any_month
from (
select reseller_id,max(payment_amount) as revenue
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
group by reseller_id
) where reseller_id notnull
) c on a.reseller_id=c.reseller_id
where payment_status='Paid' and cast(is_archived as varchar)='false'
order by 1,3
" > /home/ec2-user/cronjob/new_users/conversion_report.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z"-f /home/ec2-user/cronjob/new_users/conversion_report.sql > /conversion_report.log

View File

@ -0,0 +1,37 @@
--------------------------Conversion Report Business metrics--------------------------
drop table if exists public.om_conversion_order_data_1
create table public.om_conversion_order_data_1
as
select distinct cast(created_at as date) as created_date,date_part('year',created_at) as order_year,date_part('month',created_at) as order_month,
a.reseller_id,id as order_id,json_extract_path_text(reseller_info,'email',TRUE) as transacted_email,replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as transacted_mobile,
payment_amount,shipping_to,order_placed_by,b.Monthly_Tier,c.Max_Tier_of_any_month,
rank() over(partition by a.reseller_id order by date_part('year',created_at),date_part('month',created_at)) as R
from raena_order_management.order a
inner join (
select distinct
reseller_id,order_year,order_month,
case when revenue<2000000 then '<2 mn' when revenue>=2000000 and revenue<=10000000 then '2-10 mn' when revenue>10000000 then '10+ mn' end as Monthly_Tier
from (
select date_part('year',created_at) as order_year,date_part('month',created_at) as order_month,reseller_id
,sum(payment_amount) as revenue
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
group by date_part('year',created_at),date_part('month',created_at),reseller_id
) where reseller_id notnull
) b on a.reseller_id=b.reseller_id and date_part('year',a.created_at) =b.order_year and date_part('month',a.created_at)=b.order_month
inner join (
select distinct
reseller_id,case when revenue<2000000 then '<2 mn' when revenue>=2000000 and revenue<=10000000 then '2-10 mn' when revenue>10000000 then '10+ mn' end as Max_Tier_of_any_month
from (
select reseller_id,max(payment_amount) as revenue
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
group by reseller_id
) where reseller_id notnull
) c on a.reseller_id=c.reseller_id
where payment_status='Paid' and cast(is_archived as varchar)='false'
order by 1,3

View File

@ -0,0 +1,93 @@
#!/bin/bash
echo -e " \n----------- DB Funnel New Users Metabase Dashboard Code --------------\n"
echo "
--------------------------DB Funnel New Users--------------------------
Drop table if exists om_clevertap_install_jan_may
create table om_clevertap_install_jan_may
as
select App_install_date,email,phone,user_id
from (
select *,row_number() over (partition by user_id order by app_install_date) as R
from
(
SELECT '2022-04-01' as App_install_date,email,phone,email as user_id FROM public.clevertap_april_csv
union
SELECT '2022-03-30' as App_install_date,email,phone,email as user_id FROM public.clevertap_march_csv where phone not in (select distinct profile_phone from clevertap.app_installed)
union
select cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_install_date,profile_email,profile_phone,
case when profile_email=' ' then profile_objectid else profile_email end as user_id
from clevertap.app_installed
)
) where R=1
Drop table if exists om_clevertap_install_jan_may_2
create table om_clevertap_install_jan_may_2
as
select user_id,email,phone,to_char(app_install_date,'month') as month,date_part(day,app_install_date) as day,address_line1,address_line2,city,province,app_install_date
from (
select a.user_id,a.email,phone,case when app_install_date='2022-03-30' or app_install_date='2022-04-01' then cast(created_at as date) else app_install_date end as app_install_date,
b.address_line1,address_line2,city,province
from om_clevertap_install_jan_may a
left join (
select mobile,email,created_at,address_line1,address_line2,city,province
from (
select email,replace(mobile,'+','') as mobile,created_at,address_line1,address_line2,city,province,
row_number() over (partition by email order by created_at) as R
from raena_user_management.user
)
where R=1 and email notnull) b on a.email=b.email
)
Drop table if exists om_clevertap_install_jan_may_3
create table om_clevertap_install_jan_may_3
as
select a.*,cast(b.created_at as date) as transaction_date,transacted_email
--case when cast(b.created_at as date) isnull then 'Never transacted'
--when cast(b.created_at as date)>=app_install_date and cast(b.created_at as date)<=cast(dateadd(day,30,app_install_date) as date) then 'Yes' else 'No' end as transacted_flag
from om_clevertap_install_jan_may_2 a
left join (select json_extract_path_text(reseller_info,'email',TRUE) as transacted_email ,min(created_at) as created_at
from raena_order_management.order where payment_status='Paid' and cast(is_archived as varchar)='false' group by 1 ) b
on a.email=transacted_email
Drop table if exists om_clevertap_install_jan_may_final
create table om_clevertap_install_jan_may_final
as
select *,case when email notnull and rtrim(ltrim(email)) != '' then 'Yes' else 'No' end as email_flag,
case when phone notnull and rtrim(ltrim(phone)) != '' then 'Yes' else 'No' end as phone_flag,
case when address_line1 notnull and rtrim(ltrim(address_line1)) != '' then 'Yes' else 'No' end as address_flag,
case when transacted_email notnull and rtrim(ltrim(transacted_email)) != '' and transaction_date>=app_install_date and transaction_date<=cast(dateadd(day,30,app_install_date) as date)
then 'Yes' else 'No' end as transacted_flag
from om_clevertap_install_jan_may_3
--order data
Drop table if exists om_clevertap_install_jan_may_order
create table om_clevertap_install_jan_may_order
as
select * from om_clevertap_install_jan_may_final where transacted_email notnull and rtrim(ltrim(transacted_email)) != ''
--launch
Drop table if exists public.om_app_launched_db_funnel
create table public.om_app_launched_db_funnel
as
select * from (
select distinct b.app_install_date,a.App_launch_date,a.user_id,
case when b.phone isnull then 'New'
when App_launch_date >= app_install_date and App_launch_date < cast(dateadd(day,30,app_install_date) as date) then 'Yes' else 'No' end as flag,
row_number() over (partition by a.User_id order by App_launch_date) as R
from (
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_launch_date,
profile_phone,profile_objectid as launch_profile_objectid ,case when profile_email=' ' then profile_objectid else profile_email end as user_id
from clevertap.app_launched
) a
inner join om_clevertap_install_jan_may_final b on a.user_id=b.user_id
) where flag='Yes' order by user_id
" > /home/ec2-user/cronjob/demandForecasting/db_funnel_code.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z"-f /home/ec2-user/cronjob/new_users/db_funnel_code.sql > dbfunnel_Metabase.log

View File

View File

@ -0,0 +1,171 @@
#!/bin/bash
echo -e " \n----------- Reseller Business metrics Metabase Dashboard Code --------------\n"
echo "
--------------------------Reseller Business metrics--------------------------
Drop table if exists public.om_app_installed_reseller_business_metrics_db
create table public.om_app_installed_reseller_business_metrics_db
as
select App_install_date_1 as App_install_date,profile_objectid,profile_phone,profile_email,User_id,tier,flag,r_f,R
from (
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when App_install_date_1 >= app_install_date and App_install_date_1 < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
row_number() over (partition by User_id,date_trunc('month', App_install_date_1) - interval '0 month'
order by date_trunc('month', App_install_date_1) - interval '0 month') as R_f,
row_number() over (partition by User_id order by App_install_date_1) as R
from (
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_install_date_1,
profile_objectid,profile_phone,profile_email,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
--coalesce(profile_phone,profile_objectid) as User_id
from clevertap.app_installed
) a
left join om_New_Existing_flag b on a.profile_phone=b.profile_phone and b.r_flag=1
) where R_f=1
--order table creation
Drop table if exists public.om_order_reseller_business_metrics_db
create table public.om_order_reseller_business_metrics_db
as
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when created_at >= app_install_date and created_at < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag
from (
select a.*,b.shipping_to
from
(
select replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,cast(created_at as date) as created_at
,count(distinct id) as order_id,sum(payment_amount) as payment_amount
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
group by 1,2
) a
left join
(
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,
row_number() over (partition by replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') order by shipping_to) as R_asc
,shipping_to
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
) b on a.mobile=b.mobile and b.R_asc=1
) a
left join om_New_Existing_flag b on a.mobile=b.profile_phone and b.r_flag=1
--app_uninstalled table creation
Drop table if exists public.om_app_uninstalled_reseller_business_metrics_db
create table public.om_app_uninstalled_reseller_business_metrics_db
as
select * from (
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when app_uninstall_date >= app_install_date and app_uninstall_date < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
row_number() over (partition by User_id,date_trunc('month', App_uninstall_date) - interval '0 month'
order by date_trunc('month', App_uninstall_date) - interval '0 month') as R_f
from (
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_uninstall_date,
profile_phone,profile_objectid as Uninstall_profile_objectid ,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
from clevertap.app_uninstalled
) a
left join om_New_Existing_flag b on a.profile_phone=b.profile_phone and b.r_flag=1
) where R_f=1
--app launched
Drop table if exists public.om_app_launched_reseller_business_metrics_db
create table public.om_app_launched_reseller_business_metrics_db
as
select * from (
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when App_launch_date >= app_install_date and App_launch_date < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
row_number() over (partition by User_id,date_trunc('month', App_launch_date) - interval '0 month'
order by date_trunc('month', App_launch_date) - interval '0 month') as R_f,
row_number() over (partition by User_id order by App_launch_date) as R
from (
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_launch_date,
profile_phone,profile_objectid as launch_profile_objectid ,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
from clevertap.app_launched
) a
left join om_New_Existing_flag b on a.profile_phone=b.profile_phone and b.r_flag=1
) where R_f=1
--view
Drop table if exists public.om_product_view_reseller_business_metrics_db
create table public.om_product_view_reseller_business_metrics_db
as
select * from (
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when App_view_date >= app_install_date and App_view_date < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
row_number() over (partition by User_id,date_trunc('month', App_view_date) - interval '0 month'
order by date_trunc('month', App_view_date) - interval '0 month') as R_f
from(
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_view_date,
profile_objectid as view_object_id,profile_phone,profile_email,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
from clevertap.view_item vi
union
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_view_date,
profile_objectid as view_object_id,profile_phone,profile_email,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
from clevertap.view_cart vi
) a
left join om_New_Existing_flag b on a.profile_phone=b.profile_phone and b.r_flag=1
) where R_f=1
--view conversion
Drop table if exists public.om_order_reseller_business_metrics_db_view_conversion
create table public.om_order_reseller_business_metrics_db_view_conversion
as
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when created_at >= app_install_date and created_at < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag
from (
select a.*,b.shipping_to
from
(
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,cast(created_at as date) as created_at
,ID as order_id, payment_amount
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
) a
left join
(
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,
row_number() over (partition by replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') order by shipping_to) as R_asc
,shipping_to
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
) b on a.mobile=b.mobile and b.R_asc=1
) a
left join om_New_Existing_flag b on a.mobile=b.profile_phone and b.r_flag=1
--Sku order/--Unique sku
Drop table if exists public.om_sku_reseller_business_metrics_db
create table public.om_sku_reseller_business_metrics_db
as
select a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when created_at >= app_install_date and created_at < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag
from (
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,cast(oi.created_at as date) as created_at,
sku
from raena_order_management.order_item oi
inner join raena_order_management.order o on oi.order_id=o.id
where o.payment_status='Paid' and cast(o.is_archived as varchar)='false'
--and replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') !=' '
) a
left join om_New_Existing_flag b on a.mobile=b.profile_phone and b.r_flag=1
Drop table if exists public.OM_Post_disc_reseller_business_db
create table OM_Post_disc_reseller_business_db
as
select transaction_date,coalesce(b.tier,'Not Present') as tier,
case when b.profile_phone isnull then 'New'
when transaction_date >= app_install_date and transaction_date < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
sum(quantity) as quantity,sum(discounted_price) as discounted_price,sum(cogs) as cogs
from OM_GM_DB_Product_category a
left join om_New_Existing_flag b
on replace(a.reseller_mobile,'+','')=b.profile_phone
and b.R_flag=1
group by 1,2,3
" > /home/ec2-user/cronjob/new_users/reseller_business_metrics_code.sql
psql "host=raen-prd-sg-redshift-cluster.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z"-f /home/ec2-user/cronjob/new_users/new_users/reseller_business_metrics_code.sql > reseller_business_metrics_code_Metabase.log

View File

@ -0,0 +1,163 @@
--------------------------Reseller Business metrics--------------------------
Drop table if exists public.om_app_installed_reseller_business_metrics_db
create table public.om_app_installed_reseller_business_metrics_db
as
select App_install_date_1 as App_install_date,profile_objectid,profile_phone,profile_email,User_id,tier,flag,r_f,R
from (
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when App_install_date_1 >= app_install_date and App_install_date_1 < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
row_number() over (partition by User_id,date_trunc('month', App_install_date_1) - interval '0 month'
order by date_trunc('month', App_install_date_1) - interval '0 month') as R_f,
row_number() over (partition by User_id order by App_install_date_1) as R
from (
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_install_date_1,
profile_objectid,profile_phone,profile_email,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
--coalesce(profile_phone,profile_objectid) as User_id
from clevertap.app_installed
) a
left join om_New_Existing_flag b on a.profile_phone=b.profile_phone and b.r_flag=1
) where R_f=1
--order table creation
Drop table if exists public.om_order_reseller_business_metrics_db
create table public.om_order_reseller_business_metrics_db
as
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when created_at >= app_install_date and created_at < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag
from (
select a.*,b.shipping_to
from
(
select replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,cast(created_at as date) as created_at
,count(distinct id) as order_id,sum(payment_amount) as payment_amount
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
group by 1,2
) a
left join
(
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,
row_number() over (partition by replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') order by shipping_to) as R_asc
,shipping_to
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
) b on a.mobile=b.mobile and b.R_asc=1
) a
left join om_New_Existing_flag b on a.mobile=b.profile_phone and b.r_flag=1
--app_uninstalled table creation
Drop table if exists public.om_app_uninstalled_reseller_business_metrics_db
create table public.om_app_uninstalled_reseller_business_metrics_db
as
select * from (
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when app_uninstall_date >= app_install_date and app_uninstall_date < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
row_number() over (partition by User_id,date_trunc('month', App_uninstall_date) - interval '0 month'
order by date_trunc('month', App_uninstall_date) - interval '0 month') as R_f
from (
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_uninstall_date,
profile_phone,profile_objectid as Uninstall_profile_objectid ,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
from clevertap.app_uninstalled
) a
left join om_New_Existing_flag b on a.profile_phone=b.profile_phone and b.r_flag=1
) where R_f=1
--app launched
Drop table if exists public.om_app_launched_reseller_business_metrics_db
create table public.om_app_launched_reseller_business_metrics_db
as
select * from (
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when App_launch_date >= app_install_date and App_launch_date < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
row_number() over (partition by User_id,date_trunc('month', App_launch_date) - interval '0 month'
order by date_trunc('month', App_launch_date) - interval '0 month') as R_f,
row_number() over (partition by User_id order by App_launch_date) as R
from (
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_launch_date,
profile_phone,profile_objectid as launch_profile_objectid ,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
from clevertap.app_launched
) a
left join om_New_Existing_flag b on a.profile_phone=b.profile_phone and b.r_flag=1
) where R_f=1
--view
Drop table if exists public.om_product_view_reseller_business_metrics_db
create table public.om_product_view_reseller_business_metrics_db
as
select * from (
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when App_view_date >= app_install_date and App_view_date < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
row_number() over (partition by User_id,date_trunc('month', App_view_date) - interval '0 month'
order by date_trunc('month', App_view_date) - interval '0 month') as R_f
from(
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_view_date,
profile_objectid as view_object_id,profile_phone,profile_email,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
from clevertap.view_item vi
union
select distinct cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date) as App_view_date,
profile_objectid as view_object_id,profile_phone,profile_email,case when profile_phone=' ' then profile_objectid else profile_phone end as user_id
from clevertap.view_cart vi
) a
left join om_New_Existing_flag b on a.profile_phone=b.profile_phone and b.r_flag=1
) where R_f=1
--view conversion
Drop table if exists public.om_order_reseller_business_metrics_db_view_conversion
create table public.om_order_reseller_business_metrics_db_view_conversion
as
select distinct a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when created_at >= app_install_date and created_at < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag
from (
select a.*,b.shipping_to
from
(
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,cast(created_at as date) as created_at
,ID as order_id, payment_amount
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
) a
left join
(
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,
row_number() over (partition by replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') order by shipping_to) as R_asc
,shipping_to
from raena_order_management.order
where payment_status='Paid' and cast(is_archived as varchar)='false'
) b on a.mobile=b.mobile and b.R_asc=1
) a
left join om_New_Existing_flag b on a.mobile=b.profile_phone and b.r_flag=1
--Sku order/--Unique sku
Drop table if exists public.om_sku_reseller_business_metrics_db
create table public.om_sku_reseller_business_metrics_db
as
select a.*,coalesce(b.tier,'Not Present') as tier,case when b.profile_phone isnull then 'New'
when created_at >= app_install_date and created_at < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag
from (
select distinct replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as mobile,cast(oi.created_at as date) as created_at,
sku
from raena_order_management.order_item oi
inner join raena_order_management.order o on oi.order_id=o.id
where o.payment_status='Paid' and cast(o.is_archived as varchar)='false'
--and replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') !=' '
) a
left join om_New_Existing_flag b on a.mobile=b.profile_phone and b.r_flag=1
Drop table if exists public.OM_Post_disc_reseller_business_db
create table OM_Post_disc_reseller_business_db
as
select transaction_date,coalesce(b.tier,'Not Present') as tier,
case when b.profile_phone isnull then 'New'
when transaction_date >= app_install_date and transaction_date < cast(dateadd(day,30,app_install_date) as date) then 'New' else 'Existing' end as flag,
sum(quantity) as quantity,sum(discounted_price) as discounted_price,sum(cogs) as cogs
from OM_GM_DB_Product_category a
left join om_New_Existing_flag b
on replace(a.reseller_mobile,'+','')=b.profile_phone
and b.R_flag=1
group by 1,2,3

View File

@ -0,0 +1,113 @@
--------------------------Rdash -------------------------
DROP TABLE IF EXISTS raena_analytics.om_mongo_brand_category;
CREATE TABLE raena_analytics.om_mongo_brand_category AS
SELECT rsellviewid,
brand::json->> 'name' AS Brand_name,
arr.item_object::json ->> 'hasChild' AS has_child,
arr.item_object::json ->> 'id' AS actual_categoryid,
arr.item_object::json ->> 'name' AS actual_category_name
FROM raena_analytics.mongo_baseproducts,
jsonb_array_elements(replace(categorypath,'''','')::jsonb) WITH
ORDINALITY arr(item_object, POSITION);
DROP TABLE IF exists raena_analytics.om_mongo_Order_item_details;
CREATE TABLE raena_analytics.om_mongo_Order_item_details AS
SELECT orderid,
orderitemid,
quantity,
originalprice,
rsellviewid,
productsource,
sku
FROM raena_analytics.mongo_orderitems mo
ORDER BY orderid ;
DROP TABLE IF EXISTS raena_analytics.om_mongo_Order_details;
CREATE TABLE raena_analytics.om_mongo_Order_details AS
SELECT createdat,
orderid,
storeinfo::json->> 'name' AS store,
storeinfo::json->> 'storeOwner' AS sellername,
storeinfo::json->> 'storeOwnerPhone' AS Sellerphone,
storename,
marketplaceorderid,
totalamount ,
orderStatus
FROM raena_analytics.mongo_orders;
DROP TABLE IF EXISTS raena_analytics.om_mongo_orders_summary_metabase;
CREATE TABLE raena_analytics.om_mongo_orders_summary_metabase AS
SELECT DISTINCT mod.*,
moid.orderitemid,
moid.quantity,
moid.originalprice,
moid.rsellviewid,
moid.productsource,
moid.sku,
mbc.brand_name,
mbc.has_child,
mbc.actual_categoryid,
mbc.actual_category_name,
row_number() over (partition BY mod.orderid,orderitemid,sku
ORDER BY originalprice DESC) AS R
FROM raena_analytics.om_mongo_Order_details MOD
LEFT JOIN raena_analytics.om_mongo_Order_item_details moid ON MOD.orderid=moid.orderid
LEFT JOIN raena_analytics.om_mongo_brand_category mbc ON moid.rsellviewid=mbc.rsellviewid;
DROP TABLE IF EXISTS raena_analytics.OM_Mongo_orders_dump_metabase;
CREATE TABLE raena_analytics.OM_Mongo_orders_dump_metabase AS
SELECT orderid,
status,
shippingamount,
paymenttype,
createdat,
isarchived,
invoicedata,
estimatedeliverydate,
delivereddate,
pickupdonedate,
actualshippingdate,
daystoship,
currency,
cancelreason,
cancelby,
buyercancelreason,
orderupdatedat,
ordercreatedat,
totalamount,
orderstatus,
paymenttime,
paymentstatus,
storename,
marketplacestoreid,
marketplaceorderid,
sellerbusinessprostoreid,
sellerbusinessproid,
sellerraenaemail,
sellerraenaphonenumber,
sellerraenausername,
sellerraenauserid,
fulfillmentstatus,
marketplaceid,
storeid,
orderref,
storeinfo::json->> 'storeOwner' AS sellername,
storeinfo::json->> 'storeOwnerPhone' AS Sellerphone
FROM raena_analytics.mongo_orders;

View File

@ -0,0 +1,130 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
--------------------------Rdash -------------------------
DROP TABLE IF EXISTS raena_analytics.om_mongo_brand_category;
CREATE TABLE raena_analytics.om_mongo_brand_category AS
SELECT rsellviewid,
brand::json->> 'name' AS Brand_name,
arr.item_object::json ->> 'hasChild' AS has_child,
arr.item_object::json ->> 'id' AS actual_categoryid,
arr.item_object::json ->> 'name' AS actual_category_name
FROM raena_analytics.mongo_baseproducts,
jsonb_array_elements(replace(categorypath,'''','')::jsonb) WITH
ORDINALITY arr(item_object, POSITION);
DROP TABLE IF exists raena_analytics.om_mongo_Order_item_details;
CREATE TABLE raena_analytics.om_mongo_Order_item_details AS
SELECT orderid,
orderitemid,
quantity,
originalprice,
rsellviewid,
productsource,
sku
FROM raena_analytics.mongo_orderitems mo
ORDER BY orderid ;
DROP TABLE IF EXISTS raena_analytics.om_mongo_Order_details;
CREATE TABLE raena_analytics.om_mongo_Order_details AS
SELECT createdat,
orderid,
storeinfo::json->> 'name' AS store,
storeinfo::json->> 'storeOwner' AS sellername,
storeinfo::json->> 'storeOwnerPhone' AS Sellerphone,
storename,
marketplaceorderid,
totalamount ,
orderStatus
FROM raena_analytics.mongo_orders;
DROP TABLE IF EXISTS raena_analytics.om_mongo_orders_summary_metabase;
CREATE TABLE raena_analytics.om_mongo_orders_summary_metabase AS
SELECT DISTINCT mod.*,
moid.orderitemid,
moid.quantity,
moid.originalprice,
moid.rsellviewid,
moid.productsource,
moid.sku,
mbc.brand_name,
mbc.has_child,
mbc.actual_categoryid,
mbc.actual_category_name,
row_number() over (partition BY mod.orderid,orderitemid,sku
ORDER BY originalprice DESC) AS R
FROM raena_analytics.om_mongo_Order_details MOD
LEFT JOIN raena_analytics.om_mongo_Order_item_details moid ON MOD.orderid=moid.orderid
LEFT JOIN raena_analytics.om_mongo_brand_category mbc ON moid.rsellviewid=mbc.rsellviewid;
DROP TABLE IF EXISTS raena_analytics.OM_Mongo_orders_dump_metabase;
CREATE TABLE raena_analytics.OM_Mongo_orders_dump_metabase AS
SELECT orderid,
status,
shippingamount,
paymenttype,
createdat,
isarchived,
invoicedata,
estimatedeliverydate,
delivereddate,
pickupdonedate,
actualshippingdate,
daystoship,
currency,
cancelreason,
cancelby,
buyercancelreason,
orderupdatedat,
ordercreatedat,
totalamount,
orderstatus,
paymenttime,
paymentstatus,
storename,
marketplacestoreid,
marketplaceorderid,
sellerbusinessprostoreid,
sellerbusinessproid,
sellerraenaemail,
sellerraenaphonenumber,
sellerraenausername,
sellerraenauserid,
fulfillmentstatus,
marketplaceid,
storeid,
orderref,
storeinfo::json->> 'storeOwner' AS sellername,
storeinfo::json->> 'storeOwnerPhone' AS Sellerphone
FROM raena_analytics.mongo_orders;
" > /home/ec2-user/cronjob/postgresql/360rdash/360_rdash_etl.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/360rdash/360_rdash_etl.sql

View File

@ -0,0 +1,221 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
DROP TABLE IF EXISTS raena_analytics.Am_dashbaord_base1_table;
CREATE TABLE raena_analytics.Am_dashbaord_base1_table AS
SELECT A.external_id ,
transaction_date::date transaction_date,
reseller_email,
reseller_id,
customer_type ,
coupon_code,
brand_name ,
A.sku,
order_placed_by ,
retail_price ,
seller_margin ,
sum(discounted_price) discounted_price,
SUM(quantity) quantity ,
cogs,
sum(CASE WHEN A.external_id = B.order_id
AND A.sku = B.sku
AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=B.product_class THEN B.new_shipment_amount ELSE C.new_shipment_amount END) Shipping_amount,
shipping_province,
province
FROM raena_analytics.gm_dashboard A
LEFT JOIN raena_analytics.sku_level_shipping_fee_final B ON A.external_id = B.order_id
AND A.sku = B.sku
AND (CASE
WHEN item_type LIKE '%Bundle%' THEN 'Bundle'
ELSE 'Product'
END)=B.product_class
LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final C ON A.external_id = C.external_id
AND A.sku = C.sku
AND (CASE
WHEN item_type LIKE '%Bundle%' THEN 'Bundle'
ELSE 'Product'
END)=C.product_class
LEFT JOIN
(SELECT id ,
province
FROM raena_user_management.user
WHERE province IS NOT NULL) D ON A.reseller_id = cast(D.id AS varchar)
WHERE transaction_date::date BETWEEN CURRENT_DATE-interval'6 months' AND CURRENT_DATE
AND reseller_id IS NOT NULL
GROUP BY A.external_id ,
transaction_date::date,
reseller_id,
reseller_email,
customer_type ,
coupon_code,
brand_name ,
A.sku,
order_placed_by ,
coupon_code ,
retail_price ,
seller_margin,
cogs,
shipping_province,
province;
DROP TABLE IF EXISTS raena_analytics.brand_type_base_table ;
CREATE TABLE raena_analytics.brand_type_base_table AS
SELECT brand_name,
CASE
WHEN post_discount_gm<5 THEN 'Offender1'
WHEN post_discount_gm BETWEEN 5 AND 9.99999 THEN 'Offender2'
WHEN post_discount_gm BETWEEN 10 AND 19.99999 THEN 'DDB1'
WHEN post_discount_gm BETWEEN 20 AND 29.99999 THEN 'DDB2'
WHEN post_discount_gm>30 THEN 'DDB3'
ELSE 'EL/PL'
END brand_type,
CASE
WHEN post_discount_gm <10 THEN 'SDS'
ELSE 'DDS'
END seller_type
FROM
(SELECT brand_name ,
cast((sum((discounted_price*quantity)-(cogs*quantity))*100)/sum(CASE WHEN discounted_price<>0 THEN discounted_price*quantity END) AS decimal(10,4)) post_discount_gm
FROM raena_analytics.Am_dashbaord_base1_table
GROUP BY 1) A;
DROP TABLE IF EXISTS raena_analytics.Am_dashbaord_base2_table;
CREATE TABLE raena_analytics.Am_dashbaord_base2_table AS
SELECT A.* ,
B.brand_type,
B.seller_type,
C.max_td_date,
concat('M-',cast((date_part('year',CURRENT_DATE)-date_part('year',max_td_date))*12+(date_part('Month',CURRENT_DATE)-date_part('Month',max_td_date)) AS varchar)) dom
FROM raena_analytics.Am_dashbaord_base1_table A
LEFT JOIN raena_analytics.brand_type_base_table B ON A.brand_name = B.brand_name
LEFT JOIN
(SELECT reseller_id,
max(transaction_date::date) max_td_date
FROM raena_analytics.gm_dashboard gd
GROUP BY 1) C ON A.reseller_id = C.reseller_id ;
DROP TABLE if exists raena_analytics.dormant_reseller_base;
CREATE TABLE raena_analytics.dormant_reseller_base AS
SELECT DISTINCT reseller_id,
CASE
WHEN Dom = 'M-0' THEN 0
END \"M-0\" ,
CASE
WHEN Dom = 'M-1' THEN 1
END \"M-1\",
CASE
WHEN Dom = 'M-2' THEN 1
END \"M-2\",
CASE
WHEN Dom = 'M-3' THEN 1
END \"M-3\",
CASE
WHEN Dom = 'M-4' THEN 1
END \"M-4\",
CASE
WHEN Dom = 'M-5' THEN 1
END \"M-5\" ,
CASE
WHEN Dom = 'M-6' THEN 1
END \"M-6\"
FROM raena_analytics.Am_dashbaord_base2_table;
UPDATE raena_analytics.dormant_reseller_base
SET \"M-5\" = 1
WHERE \"M-6\" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET \"M-4\" = 1
WHERE \"M-5\" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET \"M-3\" = 1
WHERE \"M-4\" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET \"M-2\" = 1
WHERE \"M-3\" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET \"M-1\" = 1
WHERE \"M-2\" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET \"M-0\" = 1
WHERE \"M-1\" = 1;
DROP TABLE IF EXISTS raena_analytics.final_dormant_base;
CREATE TABLE raena_analytics.final_dormant_base AS
SELECT reseller_id,
'M-0' dormant
FROM raena_analytics.dormant_reseller_base
WHERE \"M-0\" =1
UNION ALL
SELECT reseller_id,
'M-1' dormant
FROM raena_analytics.dormant_reseller_base
WHERE \"M-1\" =1
UNION ALL
SELECT reseller_id,
'M-2' dormant
FROM raena_analytics.dormant_reseller_base
WHERE \"M-2\" =1
UNION ALL
SELECT reseller_id,
'M-3' dormant
FROM raena_analytics.dormant_reseller_base
WHERE \"M-3\" =1
UNION ALL
SELECT reseller_id,
'M-4' dormant
FROM raena_analytics.dormant_reseller_base
WHERE \"M-4\" =1
UNION ALL
SELECT reseller_id,
'M-5' dormant
FROM raena_analytics.dormant_reseller_base
WHERE \"M-5\" =1
UNION ALL
SELECT reseller_id,
'M-6' dormant
FROM raena_analytics.dormant_reseller_base
WHERE \"M-6\" =1;
" > /home/ec2-user/cronjob/postgresql/am_dashboard/am_dashboard.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/am_dashboard/am_dashboard.sql

View File

@ -0,0 +1,204 @@
DROP TABLE IF EXISTS raena_analytics.Am_dashbaord_base1_table;
CREATE TABLE raena_analytics.Am_dashbaord_base1_table AS
SELECT A.external_id ,
transaction_date::date transaction_date,
reseller_email,
reseller_id,
customer_type ,
coupon_code,
brand_name ,
A.sku,
order_placed_by ,
retail_price ,
seller_margin ,
sum(discounted_price) discounted_price,
SUM(quantity) quantity ,
cogs,
sum(CASE WHEN A.external_id = B.order_id
AND A.sku = B.sku
AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=B.product_class THEN B.new_shipment_amount ELSE C.new_shipment_amount END) Shipping_amount,
shipping_province,
province
FROM raena_analytics.gm_dashboard A
LEFT JOIN raena_analytics.sku_level_shipping_fee_final B ON A.external_id = B.order_id
AND A.sku = B.sku
AND (CASE
WHEN item_type LIKE '%Bundle%' THEN 'Bundle'
ELSE 'Product'
END)=B.product_class
LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final C ON A.external_id = C.external_id
AND A.sku = C.sku
AND (CASE
WHEN item_type LIKE '%Bundle%' THEN 'Bundle'
ELSE 'Product'
END)=C.product_class
LEFT JOIN
(SELECT id ,
province
FROM raena_user_management.user
WHERE province IS NOT NULL) D ON A.reseller_id = cast(D.id AS varchar)
WHERE transaction_date::date BETWEEN CURRENT_DATE-interval'6 months' AND CURRENT_DATE
AND reseller_id IS NOT NULL
GROUP BY A.external_id ,
transaction_date::date,
reseller_id,
reseller_email,
customer_type ,
coupon_code,
brand_name ,
A.sku,
order_placed_by ,
coupon_code ,
retail_price ,
seller_margin,
cogs,
shipping_province,
province;
DROP TABLE IF EXISTS raena_analytics.brand_type_base_table ;
CREATE TABLE raena_analytics.brand_type_base_table AS
SELECT brand_name,
CASE
WHEN post_discount_gm<5 THEN 'Offender1'
WHEN post_discount_gm BETWEEN 5 AND 9.99999 THEN 'Offender2'
WHEN post_discount_gm BETWEEN 10 AND 19.99999 THEN 'DDB1'
WHEN post_discount_gm BETWEEN 20 AND 29.99999 THEN 'DDB2'
WHEN post_discount_gm>30 THEN 'DDB3'
ELSE 'EL/PL'
END brand_type,
CASE
WHEN post_discount_gm <10 THEN 'SDS'
ELSE 'DDS'
END seller_type
FROM
(SELECT brand_name ,
cast((sum((discounted_price*quantity)-(cogs*quantity))*100)/sum(CASE WHEN discounted_price<>0 THEN discounted_price*quantity END) AS decimal(10,4)) post_discount_gm
FROM raena_analytics.Am_dashbaord_base1_table
GROUP BY 1) A;
DROP TABLE IF EXISTS raena_analytics.Am_dashbaord_base2_table;
CREATE TABLE raena_analytics.Am_dashbaord_base2_table AS
SELECT A.* ,
B.brand_type,
B.seller_type,
C.max_td_date,
concat('M-',cast((date_part('year',CURRENT_DATE)-date_part('year',max_td_date))*12+(date_part('Month',CURRENT_DATE)-date_part('Month',max_td_date)) AS varchar)) dom
FROM raena_analytics.Am_dashbaord_base1_table A
LEFT JOIN raena_analytics.brand_type_base_table B ON A.brand_name = B.brand_name
LEFT JOIN
(SELECT reseller_id,
max(transaction_date::date) max_td_date
FROM raena_analytics.gm_dashboard gd
GROUP BY 1) C ON A.reseller_id = C.reseller_id ;
DROP TABLE if exists raena_analytics.dormant_reseller_base;
CREATE TABLE raena_analytics.dormant_reseller_base AS
SELECT DISTINCT reseller_id,
CASE
WHEN Dom = 'M-0' THEN 0
END "M-0" ,
CASE
WHEN Dom = 'M-1' THEN 1
END "M-1",
CASE
WHEN Dom = 'M-2' THEN 1
END "M-2",
CASE
WHEN Dom = 'M-3' THEN 1
END "M-3",
CASE
WHEN Dom = 'M-4' THEN 1
END "M-4",
CASE
WHEN Dom = 'M-5' THEN 1
END "M-5" ,
CASE
WHEN Dom = 'M-6' THEN 1
END "M-6"
FROM raena_analytics.Am_dashbaord_base2_table;
UPDATE raena_analytics.dormant_reseller_base
SET "M-5" = 1
WHERE "M-6" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET "M-4" = 1
WHERE "M-5" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET "M-3" = 1
WHERE "M-4" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET "M-2" = 1
WHERE "M-3" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET "M-1" = 1
WHERE "M-2" = 1;
UPDATE raena_analytics.dormant_reseller_base
SET "M-0" = 1
WHERE "M-1" = 1;
DROP TABLE IF EXISTS raena_analytics.final_dormant_base;
CREATE TABLE raena_analytics.final_dormant_base AS
SELECT reseller_id,
'M-0' dormant
FROM raena_analytics.dormant_reseller_base
WHERE "M-0" =1
UNION ALL
SELECT reseller_id,
'M-1' dormant
FROM raena_analytics.dormant_reseller_base
WHERE "M-1" =1
UNION ALL
SELECT reseller_id,
'M-2' dormant
FROM raena_analytics.dormant_reseller_base
WHERE "M-2" =1
UNION ALL
SELECT reseller_id,
'M-3' dormant
FROM raena_analytics.dormant_reseller_base
WHERE "M-3" =1
UNION ALL
SELECT reseller_id,
'M-4' dormant
FROM raena_analytics.dormant_reseller_base
WHERE "M-4" =1
UNION ALL
SELECT reseller_id,
'M-5' dormant
FROM raena_analytics.dormant_reseller_base
WHERE "M-5" =1
UNION ALL
SELECT reseller_id,
'M-6' dormant
FROM raena_analytics.dormant_reseller_base
WHERE "M-6" =1;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,514 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
drop table if exists raena_analytics.clevertap_event_data_vishnu;
create table raena_analytics.clevertap_event_data_vishnu
as
select ts::date ,events, name , email ,phone,event_props::jsonb->>'CT Session Id' sessionid ,
event_props::jsonb->>'item_id' item_id ,
event_props::jsonb->>'isGuestUser' isGuestUser ,
event_props::jsonb->>'CT Source' Source ,
event_props::jsonb->>'id' id ,
event_props::jsonb->>'brandName' brandName ,
event_props::jsonb->>'item_name' item_name ,
event_props::jsonb->>'offenderItems' offenderItems ,
event_props::jsonb->>'items' items ,
event_props::jsonb->>'search_term' search_term ,
event_props::jsonb->>'screen' screen
from clevertap.clevertap_master_data where events in (
'brands_tab_press' ,
'brand_logo_carousel_press',
'App Launched',
'view_item',
'begin_checkout',
'search',
'Charged',
'App Installed',
'view_cart',
'add_to_cart',
'customer_search',
'Notification Clicked',
'Notification Viewed',
'Push Impressions',
'home_category',
'pdp_brandprice',
'order_items_view',
'sidebar_browsebrands',
'confirm_payment',
'finish_checkout',
'loyalty_page_visit',
'home_brand_Caraousel',
'home_brand_grid_view',
'WISHLIST',
'brand_carousel_press',
'continue_shopping',
'home_banner',
'brandpage_image_banner_press',
'skip_login',
'App Uninstalled',
'coupon_applied_successfully',
'coupon_could_not_be_applied',
'view_rewards',
'view_order_checkout',
'coupon_remove',
'homeintent_buyws',
'homepage_image_banner_press',
'home_continue_payment',
'homepage_carousel_category_press',
'flashsale_carousel_item_press',
'flashsale_carousel_view_all_press',
'homeintent_managemp'
)
and ts::date >='2023-04-01';
drop table raena_analytics.hero_sku_l6m;
CREATE TABLE raena_analytics.hero_sku_l6m AS
SELECT DISTINCT brand_name ,
brand_id,
SKU
FROM
(SELECT * ,
sum(contribution) over (partition BY brand_name
ORDER BY rnk) final_rnk
FROM
(SELECT * ,
sum(payment_amount) over (partition BY brand_name) running_sum,
(payment_amount*100)/nullif(sum(payment_amount) over (partition BY brand_name),0) contribution
FROM
(SELECT GD.brand_name ,
gd.sku ,
B.brand_id,
sum(quantity* discounted_price) payment_amount ,
rank() over (partition BY brand_name
ORDER BY sum(quantity* discounted_price) DESC) rnk
FROM raena_analytics.gm_dashboard gd
LEFT JOIN raena_catalog_management.product B ON gd.sku = B.sku
WHERE transaction_date BETWEEN (date_trunc('Month',CURRENT_DATE)::date + interval'-6 Month')::date AND date_trunc('Month',CURRENT_DATE)::date
GROUP BY 1,
2,
3) A) AA) BB
WHERE (final_rnk <=75)
OR contribution >= 75 ;
drop table if exists raena_analytics.whatsapp_campaign_base_table ;
CREATE TABLE raena_analytics.whatsapp_campaign_base_table AS
SELECT DISTINCT brand_name ,
segment_name,
reseller_email,
reseller_mobile ,
reseller_name
from
(select *,row_number()over(partition by brand_name order by segment_number ) brand_limit_user
FROM
(SELECT * ,
rank()over(partition BY brand_name,reseller_email
ORDER BY segment_number) remove_duplicate_user
FROM
(SELECT DISTINCT anchor_brand_name brand_name,
'SEGMENT_PERSONAL_ANCHOR_BRAND' segment_name,
1 segment_number,
reseller_email,
reseller_mobile,
NULL reseller_name
FROM
(SELECT Transaction_date2 year_date,
reseller_email,
reseller_mobile,
bucket revenue_cohort,
Brand_name,
avg_brand_contribution ,
avg_payment_amount avg_brand_revenue,
anchor_brand_name,
avg_anchor_brand_contribution,
avg_anchor_payment_amount avg_anchor_brand_revenue
FROM raena_analytics.reseller_level_anchor_brand_data
WHERE 1=1
AND bucket IS NOT NULL
ORDER BY year_date DESC , avg_anchor_brand_contribution DESC) A
UNION ALL SELECT DISTINCT brand_name brand_name,
'SEGMENT_PARENT_BRAND_FOR_ANCHOR' segment_name,
2 segment_number,
reseller_email,
reseller_mobile,
NULL reseller_namme
FROM
(SELECT Transaction_date2 year_date,
reseller_email,
reseller_mobile,
bucket revenue_cohort,
Brand_name,
avg_brand_contribution ,
avg_payment_amount avg_brand_revenue,
anchor_brand_name,
avg_anchor_brand_contribution,
avg_anchor_payment_amount avg_anchor_brand_revenue
FROM raena_analytics.reseller_level_anchor_brand_data
WHERE 1=1
AND bucket IS NOT NULL
ORDER BY year_date DESC , avg_anchor_brand_contribution DESC) A
UNION ALL SELECT DISTINCT brand_name brand_name,
'SEGMENT_PERSONALIZED_RECOMMENDATIONS' segment_name,
3 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
(SELECT reseller_email,
reseller_mobile,
u.name,
dr.sku,
b.name brand_name,
score match_score,
after_discount post_discount_gm,
stock_type,
pi.current_inventory_ready,
pi.current_inventory_pre_order,
p.name product_name
FROM
(SELECT *
FROM raena_recommendation_engine.daily_recommendation
ORDER BY reseller_email,
score DESC) dr
INNER JOIN
( SELECT *
FROM raena_user_management.user
WHERE status = 'active') u ON u.mobile = dr.reseller_mobile
INNER JOIN
( SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
INNER JOIN
( SELECT product_id,
sum(CASE WHEN stock_type = 'READY_SKU' THEN stock_limit - reserve_stock END) AS current_inventory_ready ,
sum(CASE WHEN stock_type = 'PRE_ORDER' THEN stock_limit - reserve_stock END) AS current_inventory_pre_order
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
INNER JOIN
( SELECT *
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
ORDER BY match_score DESC) A
UNION ALL SELECT DISTINCT name brand_name,
'SEGMENT_PROVINCE_BASED_RECOMMENDATION' segment_name,
4 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
( SELECT shipping_province ,
dr.sku ,
b.name ,
score ,
after_discount ,
stock_type,
pi.current_inventory_ready,
pi.current_inventory_pre_order,
p.name product_name
FROM
(SELECT shipping_province,
sku,
score,
after_discount
FROM raena_recommendation_engine.daily_recommendation_new_user
ORDER BY score DESC, rank) dr
INNER JOIN
(SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
INNER JOIN
(SELECT product_id,
sum(CASE WHEN stock_type = 'READY_SKU' THEN stock_limit - reserve_stock END) AS current_inventory_ready ,
sum(CASE WHEN stock_type = 'PRE_ORDER' THEN stock_limit - reserve_stock END) AS current_inventory_pre_order
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
INNER JOIN
(SELECT id,
name
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
ORDER BY Score DESC) A
INNER JOIN
(SELECT reseller_email,
reseller_mobile,
reseller_name ,
shipping_province,
sku,
brand_name
FROM raena_analytics.gm_dashboard gd
WHERE transaction_date::date >='2023-02-01'
AND reseller_email IS NOT NULL)B ON lower(A.shipping_province) = lower(B.shipping_province)
AND A.name = B.brand_name
AND A.sku = B.sku
UNION ALL SELECT DISTINCT AA.Brand_name brand_name,
'SEGMENT_REVENUE_COHORT_ANCHOR_BRAND' segment_name,
4 segment_number,
reseller_email,
reseller_mobile,
reseller_name
FROM
(SELECT Transaction_date2 year_date,
bucket revenue_cohort,
Brand_name,
avg_brand_contribution ,
avg_payment_amount avg_brand_revenue,
anchor_brand_name,
avg_anchor_brand_contribution,
avg_anchor_payment_amount avg_anchor_brand_revenue
FROM raena_analytics.anchor_brand_data
ORDER BY year_date DESC, avg_anchor_brand_contribution DESC) AA
INNER JOIN
(SELECT brand_name ,
bucket,
reseller_email,
reseller_mobile ,
reseller_name
FROM raena_analytics.gm_dashboard gd
INNER JOIN
(SELECT DISTINCT reseller_id ,
CASE WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '0 to 2M' WHEN max_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' WHEN max_payment_amount >10000000 THEN '>10M' END bucket
FROM
(SELECT reseller_id,
max(payment_amount) max_payment_amount
FROM
(SELECT reseller_id ,
date_trunc('Month',transaction_date)::date ,
sum(discounted_price*quantity) payment_amount
FROM raena_analytics.gm_dashboard gd2
GROUP BY 1,
2) A
GROUP BY 1) B)C ON gd.reseller_id = C.reseller_id
WHERE transaction_date::date BETWEEN '2023-05-01' AND '2023-07-31') BB ON AA.brand_name = BB.brand_name
AND AA.revenue_cohort = BB.bucket
UNION ALL SELECT DISTINCT brand_name,
'SEGMENT_PERSONALIZED_RECOMMENDATIONS_SIMILAR_USER' segment_name,
5 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
(SELECT reseller_email,
mobile Reseller_mobile,
bb.name,
dr.sku,
b.name brand_name,
score match_score,
after_discount post_discount_gm,
stock_type,
pi.current_inventory_ready,
pipreorder.current_inventory_pre_order,
p.name product_name
FROM
(SELECT *
FROM raena_recommendation_engine.daily_similar_user_v2
ORDER BY reseller_email,
score DESC) dr
LEFT JOIN
( SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_ready
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'READY_SKU'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_pre_order,
max(estimated_arrival_date) AS estimated_arrival_date
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'PRE_ORDER'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pipreorder ON pipreorder.product_id = p.id
INNER JOIN
( SELECT id,
name
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
INNER JOIN
( SELECT *
FROM raena_user_management.user) bb ON bb.email = dr.reseller_email
ORDER BY reseller_email,
match_score DESC) AA
UNION ALL SELECT DISTINCT brand_name,
'SEGMENT_PERSONALIZED_RECOMMENDATIONS_SHIPPING_PROVINCE' segment_name,
6 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
(SELECT dr.reseller_email,
mobile Reseller_mobile,
bb.name,
Shipping_Province,
dr.sku,
b.name brand_name,
score match_score,
after_discount post_discount_gm,
stock_type,
pi.current_inventory_ready,
pipreorder.current_inventory_pre_order,
p.name product_name
FROM
(SELECT *
FROM raena_recommendation_engine.daily_recommendation_shipping_province
ORDER BY reseller_email,
score DESC) dr
LEFT JOIN
(SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_ready
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'READY_SKU'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_pre_order,
max(estimated_arrival_date) AS estimated_arrival_date
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'PRE_ORDER'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pipreorder ON pipreorder.product_id = p.id
INNER JOIN
(SELECT id,
name
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
INNER JOIN
(SELECT *
FROM raena_user_management.user) bb ON bb.email = dr.reseller_email
ORDER BY match_score DESC) AA
UNION ALL SELECT DISTINCT brand_name,
'SEGMENT_PERSONALIZED_RECOMMENDATIONS_REVENUE' segment_name,
7 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
(SELECT dr.reseller_email,
mobile Reseller_mobile,
bb.name,
dr.sku,
b.name brand_name,
score match_score,
after_discount post_discount_gm,
stock_type,
pi.current_inventory_ready,
pipreorder.current_inventory_pre_order,
p.name product_name
FROM
( SELECT *
FROM raena_recommendation_engine.daily_recommendation_revenue
ORDER BY reseller_email,
score DESC) dr
LEFT JOIN
(SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_ready
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'READY_SKU'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_pre_order,
max(estimated_arrival_date) AS estimated_arrival_date
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'PRE_ORDER'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pipreorder ON pipreorder.product_id = p.id
INNER JOIN
( SELECT id,
name
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
INNER JOIN
( SELECT *
FROM raena_user_management.user) bb ON bb.email = dr.reseller_email
ORDER BY match_score DESC) A) AA) BB
WHERE remove_duplicate_user =1) CC
where brand_limit_user <=2000;
update raena_analytics.whatsapp_campaign_base_table
set reseller_name = name
from raena_user_management.user
where reseller_email = email ;
update raena_analytics.whatsapp_campaign_base_table
set reseller_name = name
from raena_user_management.user
where reseller_mobile = mobile
and reseller_name is null ;
update raena_analytics.whatsapp_campaign_base_table
set reseller_email = email
from raena_user_management.user
where reseller_mobile = mobile
and reseller_email is null ;
" > /home/ec2-user/cronjob/postgresql/am_recommendation/whatsapp_campaign.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/am_recommendation/whatsapp_campaign.sql

View File

@ -0,0 +1,496 @@
drop table if exists raena_analytics.clevertap_event_data_vishnu;
create table raena_analytics.clevertap_event_data_vishnu
as
select ts::date ,events, name , email ,phone,event_props::jsonb->>'CT Session Id' sessionid ,
event_props::jsonb->>'item_id' item_id ,
event_props::jsonb->>'isGuestUser' isGuestUser ,
event_props::jsonb->>'CT Source' Source ,
event_props::jsonb->>'id' id ,
event_props::jsonb->>'brandName' brandName ,
event_props::jsonb->>'item_name' item_name ,
event_props::jsonb->>'offenderItems' offenderItems ,
event_props::jsonb->>'items' items ,
event_props::jsonb->>'search_term' search_term ,
event_props::jsonb->>'screen' screen
from clevertap.clevertap_master_data where events in (
'brands_tab_press' ,
'brand_logo_carousel_press',
'App Launched',
'view_item',
'begin_checkout',
'search',
'Charged',
'App Installed',
'view_cart',
'add_to_cart',
'customer_search',
'Notification Clicked',
'Notification Viewed',
'Push Impressions',
'home_category',
'pdp_brandprice',
'order_items_view',
'sidebar_browsebrands',
'confirm_payment',
'finish_checkout',
'loyalty_page_visit',
'home_brand_Caraousel',
'home_brand_grid_view',
'WISHLIST',
'brand_carousel_press',
'continue_shopping',
'home_banner',
'brandpage_image_banner_press',
'skip_login',
'App Uninstalled',
'coupon_applied_successfully',
'coupon_could_not_be_applied',
'view_rewards',
'view_order_checkout',
'coupon_remove',
'homeintent_buyws',
'homepage_image_banner_press',
'home_continue_payment',
'homepage_carousel_category_press',
'flashsale_carousel_item_press',
'flashsale_carousel_view_all_press',
'homeintent_managemp'
)
and ts::date >='2023-04-01';
drop table raena_analytics.hero_sku_l6m;
CREATE TABLE raena_analytics.hero_sku_l6m AS
SELECT DISTINCT brand_name ,
brand_id,
SKU
FROM
(SELECT * ,
sum(contribution) over (partition BY brand_name
ORDER BY rnk) final_rnk
FROM
(SELECT * ,
sum(payment_amount) over (partition BY brand_name) running_sum,
(payment_amount*100)/nullif(sum(payment_amount) over (partition BY brand_name),0) contribution
FROM
(SELECT GD.brand_name ,
gd.sku ,
B.brand_id,
sum(quantity* discounted_price) payment_amount ,
rank() over (partition BY brand_name
ORDER BY sum(quantity* discounted_price) DESC) rnk
FROM raena_analytics.gm_dashboard gd
LEFT JOIN raena_catalog_management.product B ON gd.sku = B.sku
WHERE transaction_date BETWEEN (date_trunc('Month',CURRENT_DATE)::date + interval'-6 Month')::date AND date_trunc('Month',CURRENT_DATE)::date
GROUP BY 1,
2,
3) A) AA) BB
WHERE (final_rnk <=75)
OR contribution >= 75 ;
drop table if exists raena_analytics.whatsapp_campaign_base_table ;
CREATE TABLE raena_analytics.whatsapp_campaign_base_table AS
SELECT DISTINCT brand_name ,
segment_name,
reseller_email,
reseller_mobile ,
reseller_name
from
(select *,row_number()over(partition by brand_name order by segment_number ) brand_limit_user
FROM
(SELECT * ,
rank()over(partition BY brand_name,reseller_email
ORDER BY segment_number) remove_duplicate_user
FROM
(SELECT DISTINCT anchor_brand_name brand_name,
'SEGMENT_PERSONAL_ANCHOR_BRAND' segment_name,
1 segment_number,
reseller_email,
reseller_mobile,
NULL reseller_name
FROM
(SELECT Transaction_date2 year_date,
reseller_email,
reseller_mobile,
bucket revenue_cohort,
Brand_name,
avg_brand_contribution ,
avg_payment_amount avg_brand_revenue,
anchor_brand_name,
avg_anchor_brand_contribution,
avg_anchor_payment_amount avg_anchor_brand_revenue
FROM raena_analytics.reseller_level_anchor_brand_data
WHERE 1=1
AND bucket IS NOT NULL
ORDER BY year_date DESC , avg_anchor_brand_contribution DESC) A
UNION ALL SELECT DISTINCT brand_name brand_name,
'SEGMENT_PARENT_BRAND_FOR_ANCHOR' segment_name,
2 segment_number,
reseller_email,
reseller_mobile,
NULL reseller_namme
FROM
(SELECT Transaction_date2 year_date,
reseller_email,
reseller_mobile,
bucket revenue_cohort,
Brand_name,
avg_brand_contribution ,
avg_payment_amount avg_brand_revenue,
anchor_brand_name,
avg_anchor_brand_contribution,
avg_anchor_payment_amount avg_anchor_brand_revenue
FROM raena_analytics.reseller_level_anchor_brand_data
WHERE 1=1
AND bucket IS NOT NULL
ORDER BY year_date DESC , avg_anchor_brand_contribution DESC) A
UNION ALL SELECT DISTINCT brand_name brand_name,
'SEGMENT_PERSONALIZED_RECOMMENDATIONS' segment_name,
3 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
(SELECT reseller_email,
reseller_mobile,
u.name,
dr.sku,
b.name brand_name,
score match_score,
after_discount post_discount_gm,
stock_type,
pi.current_inventory_ready,
pi.current_inventory_pre_order,
p.name product_name
FROM
(SELECT *
FROM raena_recommendation_engine.daily_recommendation
ORDER BY reseller_email,
score DESC) dr
INNER JOIN
( SELECT *
FROM raena_user_management.user
WHERE status = 'active') u ON u.mobile = dr.reseller_mobile
INNER JOIN
( SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
INNER JOIN
( SELECT product_id,
sum(CASE WHEN stock_type = 'READY_SKU' THEN stock_limit - reserve_stock END) AS current_inventory_ready ,
sum(CASE WHEN stock_type = 'PRE_ORDER' THEN stock_limit - reserve_stock END) AS current_inventory_pre_order
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
INNER JOIN
( SELECT *
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
ORDER BY match_score DESC) A
UNION ALL SELECT DISTINCT name brand_name,
'SEGMENT_PROVINCE_BASED_RECOMMENDATION' segment_name,
4 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
( SELECT shipping_province ,
dr.sku ,
b.name ,
score ,
after_discount ,
stock_type,
pi.current_inventory_ready,
pi.current_inventory_pre_order,
p.name product_name
FROM
(SELECT shipping_province,
sku,
score,
after_discount
FROM raena_recommendation_engine.daily_recommendation_new_user
ORDER BY score DESC, rank) dr
INNER JOIN
(SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
INNER JOIN
(SELECT product_id,
sum(CASE WHEN stock_type = 'READY_SKU' THEN stock_limit - reserve_stock END) AS current_inventory_ready ,
sum(CASE WHEN stock_type = 'PRE_ORDER' THEN stock_limit - reserve_stock END) AS current_inventory_pre_order
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
INNER JOIN
(SELECT id,
name
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
ORDER BY Score DESC) A
INNER JOIN
(SELECT reseller_email,
reseller_mobile,
reseller_name ,
shipping_province,
sku,
brand_name
FROM raena_analytics.gm_dashboard gd
WHERE transaction_date::date >='2023-02-01'
AND reseller_email IS NOT NULL)B ON lower(A.shipping_province) = lower(B.shipping_province)
AND A.name = B.brand_name
AND A.sku = B.sku
UNION ALL SELECT DISTINCT AA.Brand_name brand_name,
'SEGMENT_REVENUE_COHORT_ANCHOR_BRAND' segment_name,
4 segment_number,
reseller_email,
reseller_mobile,
reseller_name
FROM
(SELECT Transaction_date2 year_date,
bucket revenue_cohort,
Brand_name,
avg_brand_contribution ,
avg_payment_amount avg_brand_revenue,
anchor_brand_name,
avg_anchor_brand_contribution,
avg_anchor_payment_amount avg_anchor_brand_revenue
FROM raena_analytics.anchor_brand_data
ORDER BY year_date DESC, avg_anchor_brand_contribution DESC) AA
INNER JOIN
(SELECT brand_name ,
bucket,
reseller_email,
reseller_mobile ,
reseller_name
FROM raena_analytics.gm_dashboard gd
INNER JOIN
(SELECT DISTINCT reseller_id ,
CASE WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '0 to 2M' WHEN max_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' WHEN max_payment_amount >10000000 THEN '>10M' END bucket
FROM
(SELECT reseller_id,
max(payment_amount) max_payment_amount
FROM
(SELECT reseller_id ,
date_trunc('Month',transaction_date)::date ,
sum(discounted_price*quantity) payment_amount
FROM raena_analytics.gm_dashboard gd2
GROUP BY 1,
2) A
GROUP BY 1) B)C ON gd.reseller_id = C.reseller_id
WHERE transaction_date::date BETWEEN '2023-05-01' AND '2023-07-31') BB ON AA.brand_name = BB.brand_name
AND AA.revenue_cohort = BB.bucket
UNION ALL SELECT DISTINCT brand_name,
'SEGMENT_PERSONALIZED_RECOMMENDATIONS_SIMILAR_USER' segment_name,
5 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
(SELECT reseller_email,
mobile Reseller_mobile,
bb.name,
dr.sku,
b.name brand_name,
score match_score,
after_discount post_discount_gm,
stock_type,
pi.current_inventory_ready,
pipreorder.current_inventory_pre_order,
p.name product_name
FROM
(SELECT *
FROM raena_recommendation_engine.daily_similar_user_v2
ORDER BY reseller_email,
score DESC) dr
LEFT JOIN
( SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_ready
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'READY_SKU'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_pre_order,
max(estimated_arrival_date) AS estimated_arrival_date
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'PRE_ORDER'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pipreorder ON pipreorder.product_id = p.id
INNER JOIN
( SELECT id,
name
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
INNER JOIN
( SELECT *
FROM raena_user_management.user) bb ON bb.email = dr.reseller_email
ORDER BY reseller_email,
match_score DESC) AA
UNION ALL SELECT DISTINCT brand_name,
'SEGMENT_PERSONALIZED_RECOMMENDATIONS_SHIPPING_PROVINCE' segment_name,
6 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
(SELECT dr.reseller_email,
mobile Reseller_mobile,
bb.name,
Shipping_Province,
dr.sku,
b.name brand_name,
score match_score,
after_discount post_discount_gm,
stock_type,
pi.current_inventory_ready,
pipreorder.current_inventory_pre_order,
p.name product_name
FROM
(SELECT *
FROM raena_recommendation_engine.daily_recommendation_shipping_province
ORDER BY reseller_email,
score DESC) dr
LEFT JOIN
(SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_ready
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'READY_SKU'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_pre_order,
max(estimated_arrival_date) AS estimated_arrival_date
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'PRE_ORDER'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pipreorder ON pipreorder.product_id = p.id
INNER JOIN
(SELECT id,
name
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
INNER JOIN
(SELECT *
FROM raena_user_management.user) bb ON bb.email = dr.reseller_email
ORDER BY match_score DESC) AA
UNION ALL SELECT DISTINCT brand_name,
'SEGMENT_PERSONALIZED_RECOMMENDATIONS_REVENUE' segment_name,
7 segment_number,
reseller_email,
reseller_mobile,
name reseller_name
FROM
(SELECT dr.reseller_email,
mobile Reseller_mobile,
bb.name,
dr.sku,
b.name brand_name,
score match_score,
after_discount post_discount_gm,
stock_type,
pi.current_inventory_ready,
pipreorder.current_inventory_pre_order,
p.name product_name
FROM
( SELECT *
FROM raena_recommendation_engine.daily_recommendation_revenue
ORDER BY reseller_email,
score DESC) dr
LEFT JOIN
(SELECT id,
sku,
stock_type,
name,
brand_id
FROM raena_catalog_management.product
WHERE is_archived = 'false'
AND is_delisted = 'false') p ON p.sku = dr.sku
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_ready
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'READY_SKU'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pi ON pi.product_id = p.id
LEFT JOIN
(SELECT product_id,
sum(stock_limit - reserve_stock) AS current_inventory_pre_order,
max(estimated_arrival_date) AS estimated_arrival_date
FROM raena_catalog_management.product_inventory
WHERE is_archived = 'false'
AND stock_type = 'PRE_ORDER'
AND (stock_limit - reserve_stock) > 0
GROUP BY product_id) pipreorder ON pipreorder.product_id = p.id
INNER JOIN
( SELECT id,
name
FROM raena_catalog_management.brand) b ON b.id = p.brand_id
INNER JOIN
( SELECT *
FROM raena_user_management.user) bb ON bb.email = dr.reseller_email
ORDER BY match_score DESC) A) AA) BB
WHERE remove_duplicate_user =1) CC
where brand_limit_user <=2000;
update raena_analytics.whatsapp_campaign_base_table
set reseller_name = name
from raena_user_management.user
where reseller_email = email ;
update raena_analytics.whatsapp_campaign_base_table
set reseller_name = name
from raena_user_management.user
where reseller_mobile = mobile
and reseller_name is null ;
update raena_analytics.whatsapp_campaign_base_table
set reseller_email = email
from raena_user_management.user
where reseller_mobile = mobile
and reseller_email is null ;

View File

@ -0,0 +1,288 @@
!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
DROP TABLE if exists raena_analytics.campaign_base_data_V3;
CREATE TABLE raena_analytics.campaign_base_data_V3 AS
SELECT DISTINCT channel,
media_source ,
campaign_type,
campaign campaign_name,
fb_adset_name,
af_siteid,
ad_id,
A.user_id customer_user_id ,
device_brand,
device_model,
platform,
cast(install_time AS date) install_date,
sum(impressions) impressions,
sum(clicks) total_clicks,
sum(install) total_install,
os_version ,
app_version,
A.City_id,
B.profile_phone phone,
install_time
FROM (select distinct * from raena_appsflyer.dw_marketing_install_stats) A
LEFT JOIN
(SELECT DISTINCT profile_objectid,
profile_phone
FROM clevertap.app_installed) B ON A.user_id = B.profile_objectid
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
16,
17,
18,
19,
20;
DROP TABLE IF EXISTS raena_analytics.gm_dashboard_appflyer_base;
CREATE TABLE raena_analytics.gm_dashboard_appflyer_base 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;
CREATE TABLE raena_analytics.user_base_appflyer AS
SELECT id,
city,
province,
created_at,
email,
mobile
FROM raena_user_management.user;
DROP TABLE IF EXISTS raena_analytics.campaign_base_data_V4;
CREATE TABLE raena_analytics.campaign_base_data_V4 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,
reseller_mobile,
conversion_date
FROM raena_analytics.campaign_base_data_V3 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,
A.mobile AS reseller_mobile
FROM raena_analytics.user_base_appflyer A
LEFT JOIN raena_analytics.gm_dashboard_appflyer_base B ON cast(A.id AS varchar) = B.reseller_id
GROUP BY 1,
4,
5,
6,
11,
12) B ON phone = replace(B.reseller_mobile,'+','');
DROP TABLE IF EXISTS raena_analytics.reseller_post_gm;
CREATE TABLE raena_analytics.reseller_post_gm 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 phone
FROM raena_analytics.campaign_base_data_V4
WHERE phone <> '') B ON replace(reseller_mobile,'+','')=phone
GROUP BY 1;
DROP TABLE IF EXISTS raena_analytics.top_brand_reseller;
CREATE TABLE raena_analytics.top_brand_reseller AS
SELECT DISTINCT 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)BB
INNER JOIN
(SELECT DISTINCT phone
FROM raena_analytics.campaign_base_data_V4
WHERE phone <> '') CC ON replace(reseller_mobile,'+','') = phone;
DROP TABLE IF EXISTS raena_analytics.launched_total;
CREATE TABLE raena_analytics.launched_total AS
SELECT profile_phone ,
count(1) app_launched
FROM clevertap.app_launched al
INNER JOIN
(SELECT DISTINCT phone
FROM raena_analytics.campaign_base_data_V4
WHERE phone <> '') B ON profile_phone=phone
GROUP BY profile_phone;
DROP TABLE IF EXISTS raena_analytics.campaign_base_data_v5;
CREATE TABLE raena_analytics.campaign_base_data_v5 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_base_data_V4 A
LEFT JOIN raena_analytics.reseller_post_gm B ON A.reseller_mobile = B.reseller_mobile
LEFT JOIN raena_analytics.top_brand_reseller C ON A.reseller_mobile= C.reseller_mobile
LEFT JOIN raena_analytics.launched_total D ON A.phone= D.profile_phone;
DROP TABLE IF EXISTS raena_analytics.campaign_base_data_final_v2;
CREATE TABLE raena_analytics.campaign_base_data_final_v2 AS
SELECT channel,
media_source,
campaign_type,
campaign_name,
fb_adset_name,
af_siteid,
ad_id ,
customer_user_id ,
device_brand,
device_model,
platform ,
install_date,
install_time ,
impressions,
total_clicks,
total_install,
os_version ,
app_version ,
city_id ,
phone ,
province ,
ttl_order ,
ttl_amount total_amount,
highest_tier,
reg_date,
reseller_email,
conversion_date,
Post_discount_gm,
BRAND_NAME ,
SKU_NAME ,
SKU ,
app_launched TOTAL_app_launched, B.Current_tier
FROM raena_analytics.campaign_base_data_v5 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;
DROP TABLE IF EXISTS raena_analytics.campaign_base_data_final_unistall;
CREATE TABLE raena_analytics.campaign_base_data_final_unistall AS
SELECT distinct city_id ,
cast(install_time AS date) install_date ,
install_time ,
cast(uninstall_time AS date) uninstall_date ,
uninstall_time ,
media_source,
channel,
campaign,
fb_adset_name,
af_siteid,
device_brand,
device_model,
platform,
ad_id,
install,
uninstall
FROM raena_appsflyer.dw_marketing_uninstall_stats;
" > /home/ec2-user/cronjob/postgresql/appflyer/appflyer_etl.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/appflyer/appflyer_etl.sql

View File

@ -0,0 +1,273 @@
DROP TABLE if exists raena_analytics.campaign_base_data_V3;
CREATE TABLE raena_analytics.campaign_base_data_V3 AS
SELECT DISTINCT channel,
media_source ,
campaign_type,
campaign campaign_name,
fb_adset_name,
af_siteid,
ad_id,
A.user_id customer_user_id ,
device_brand,
device_model,
platform,
cast(install_time AS date) install_date,
sum(impressions) impressions,
sum(clicks) total_clicks,
sum(install) total_install,
os_version ,
app_version,
A.City_id,
B.profile_phone phone,
install_time
FROM (select distinct * from raena_appsflyer.dw_marketing_install_stats) A
LEFT JOIN
(SELECT DISTINCT profile_objectid,
profile_phone
FROM clevertap.app_installed) B ON A.user_id = B.profile_objectid
GROUP BY 1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
16,
17,
18,
19,
20;
DROP TABLE IF EXISTS raena_analytics.gm_dashboard_appflyer_base;
CREATE TABLE raena_analytics.gm_dashboard_appflyer_base 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;
CREATE TABLE raena_analytics.user_base_appflyer AS
SELECT id,
city,
province,
created_at,
email,
mobile
FROM raena_user_management.user;
DROP TABLE IF EXISTS raena_analytics.campaign_base_data_V4;
CREATE TABLE raena_analytics.campaign_base_data_V4 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,
reseller_mobile,
conversion_date
FROM raena_analytics.campaign_base_data_V3 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,
A.mobile AS reseller_mobile
FROM raena_analytics.user_base_appflyer A
LEFT JOIN raena_analytics.gm_dashboard_appflyer_base B ON cast(A.id AS varchar) = B.reseller_id
GROUP BY 1,
4,
5,
6,
11,
12) B ON phone = replace(B.reseller_mobile,'+','');
DROP TABLE IF EXISTS raena_analytics.reseller_post_gm;
CREATE TABLE raena_analytics.reseller_post_gm 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 phone
FROM raena_analytics.campaign_base_data_V4
WHERE phone <> '') B ON replace(reseller_mobile,'+','')=phone
GROUP BY 1;
DROP TABLE IF EXISTS raena_analytics.top_brand_reseller;
CREATE TABLE raena_analytics.top_brand_reseller AS
SELECT DISTINCT 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)BB
INNER JOIN
(SELECT DISTINCT phone
FROM raena_analytics.campaign_base_data_V4
WHERE phone <> '') CC ON replace(reseller_mobile,'+','') = phone;
DROP TABLE IF EXISTS raena_analytics.launched_total;
CREATE TABLE raena_analytics.launched_total AS
SELECT profile_phone ,
count(1) app_launched
FROM clevertap.app_launched al
INNER JOIN
(SELECT DISTINCT phone
FROM raena_analytics.campaign_base_data_V4
WHERE phone <> '') B ON profile_phone=phone
GROUP BY profile_phone;
DROP TABLE IF EXISTS raena_analytics.campaign_base_data_v5;
CREATE TABLE raena_analytics.campaign_base_data_v5 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_base_data_V4 A
LEFT JOIN raena_analytics.reseller_post_gm B ON A.reseller_mobile = B.reseller_mobile
LEFT JOIN raena_analytics.top_brand_reseller C ON A.reseller_mobile= C.reseller_mobile
LEFT JOIN raena_analytics.launched_total D ON A.phone= D.profile_phone;
DROP TABLE IF EXISTS raena_analytics.campaign_base_data_final_v2;
CREATE TABLE raena_analytics.campaign_base_data_final_v2 AS
SELECT channel,
media_source,
campaign_type,
campaign_name,
fb_adset_name,
af_siteid,
ad_id ,
customer_user_id ,
device_brand,
device_model,
platform ,
install_date,
install_time ,
impressions,
total_clicks,
total_install,
os_version ,
app_version ,
city_id ,
phone ,
province ,
ttl_order ,
ttl_amount total_amount,
highest_tier,
reg_date,
reseller_email,
conversion_date,
Post_discount_gm,
BRAND_NAME ,
SKU_NAME ,
SKU ,
app_launched TOTAL_app_launched, B.Current_tier
FROM raena_analytics.campaign_base_data_v5 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;
DROP TABLE IF EXISTS raena_analytics.campaign_base_data_final_unistall;
CREATE TABLE raena_analytics.campaign_base_data_final_unistall AS
SELECT distinct city_id ,
cast(install_time AS date) install_date ,
install_time ,
cast(uninstall_time AS date) uninstall_date ,
uninstall_time ,
media_source,
channel,
campaign,
fb_adset_name,
af_siteid,
device_brand,
device_model,
platform,
ad_id,
install,
uninstall
FROM raena_appsflyer.dw_marketing_uninstall_stats;

View File

@ -0,0 +1,250 @@
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;

View File

@ -0,0 +1,268 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
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;
" > /home/ec2-user/cronjob/postgresql/appflyer/etl_inappevent_appsflyer.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/appflyer/etl_inappevent_appsflyer.sql

View File

@ -0,0 +1,411 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
drop table if exists raena_analytics.brand_promo_dashboard_table ;
create table raena_analytics.brand_promo_dashboard_table
as
select BB.name brand_name, type promo_type, sku ,'' free_sku, startdate , enddate, cast(coalesce(promo,'0') as float) promo ,cast(coalesce(Moq,'0') as float) moq,cast(coalesce(mov,'0') as float) mov
from raena_analytics.brand_promo_master AA
left join raena_catalog_management.brand BB on AA.brandid = cast(BB.id as varchar)
union
select BB.name brand_name,type ,
case when promoselection = 'ANY' then concat('ANY (', promoquantity_1)
when promosku_2 ='(' then concat(promosku_1,promoquantity_1)
else concat(concat(promosku_1,promoquantity_1),concat('+',concat(promosku_2,promoquantity_2))) end Promo_sku,
case when freeselection = 'ANY' then concat('ANY (', freequantity_1)
when freeselection = 'SAME' then (case when promosku_2 ='(' then concat(promosku_1,freequantity_1)
else concat(concat(promosku_1,freequantity_1),concat('+',concat(promosku_2,freequantity_2))) end)
when freesku_2 ='(' then concat(freesku_1,freequantity_1)
else concat(concat(freesku_1,freequantity_1),concat('+',concat(freesku_2,freequantity_2)))
end free_sku,
startdate,
enddate,
0 promo,
0 Moq,
0 mov
from
(
select id , name , brandid ,
type,
promobrand,
concat(split_part(promosku,';',1),'(') promosku_1,
concat(split_part(promosku,';',2),'(') promosku_2,
concat(split_part(promoquantity,';',1),')') promoquantity_1,
concat(split_part(promoquantity,';',2),')') promoquantity_2,
promoselection,
freebrand,
concat(split_part(freesku,';',1),'(') freesku_1,
concat(split_part(freesku,';',2),'(') freesku_2,
concat(split_part(freequantity,';',1),')') freequantity_1,
concat(split_part(freequantity,';',2),')') freequantity_2,
freeselection,
startdate,
enddate
from raena_analytics.brand_promo_master A
left join raena_analytics.buyngetx_promo B on A.id = B.promo_id
left join raena_analytics.buyngetx_free C on A.id = C.promo_id
where type = 'BUY_N_GET_X'
order by 1) AA
left join raena_catalog_management.brand BB on AA.brandid = cast(BB.id as varchar)
order by startdate ;
DROP TABLE IF EXISTS raena_analytics.clevertap_conversion_base;
--cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date)
CREATE TABLE raena_analytics.clevertap_conversion_base AS
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
1 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.app_launched
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
2 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.view_item
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
3 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.view_cart
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
4 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.begin_checkout
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
5 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.finish_checkout
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
6 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.charged;
SELECT count(1),
max(transaction_date)
FROM raena_analytics.clevertap_conversion_base;
DROP TABLE IF EXISTS raena_analytics.clevertap_checkout_base;
CREATE TABLE raena_analytics.clevertap_checkout_base AS
SELECT DISTINCT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
4 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.begin_checkout
UNION ALL
SELECT DISTINCT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
5 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.finish_checkout;
SELECT count(1) ,
max(transaction_date)
FROM raena_analytics.clevertap_checkout_base;
DROP TABLE IF EXISTS raena_analytics.order_base;
CREATE TABLE raena_analytics.order_base AS
SELECT min((cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date))) AS transaction_date ,
profile_phone
FROM clevertap.app_installed
GROUP BY profile_phone;
DROP TABLE IF EXISTS raena_analytics.usder_base;
CREATE TABLE raena_analytics.usder_base AS
SELECT A.name ,
A.id ,
B.name tier_name,
A.mobile ,
A.email,
A.province,
A.area_id
FROM raena_user_management.user A
LEFT JOIN raena_user_management.tier B ON A.tier_id = B.id;
DROP TABLE IF EXISTS raena_analytics.order_clevertab_stage1;
CREATE TABLE raena_analytics.order_clevertab_stage1 AS
SELECT A.transaction_date ,
tier_name,
name Reseller_name,
replace(mobile,'+','') mobile,
email ,
province,
area_id,
id reseller_id
FROM raena_analytics.order_base A
LEFT JOIN raena_analytics.usder_base B ON A.profile_phone= replace(mobile,'+','')
ORDER BY id ;
DROP TABLE IF EXISTS raena_analytics.user_type_stage;
CREATE TABLE raena_analytics.user_type_stage AS
SELECT A.transaction_date,
profile_phone,
profile_email,
B.Reseller_name,
CASE
WHEN cast(A.transaction_date AS date) BETWEEN B.transaction_date AND B.transaction_date+interval '30 days' THEN 'New'
ELSE 'Existing'
END user_type,
B.tier_name,
province,
area_id,
count(CASE WHEN eventname = 'begin_checkout' THEN coalesce(profile_phone,profile_email) END) dst_begin_checkout,
count(CASE WHEN eventname = 'finish_checkout' THEN coalesce(profile_phone,profile_email) END) dst_finish_checkout
FROM raena_analytics.clevertap_checkout_base A
LEFT JOIN raena_analytics.order_clevertab_stage1 B ON profile_phone =B.mobile
WHERE eventname IN ('begin_checkout',
'finish_checkout')
GROUP BY A.transaction_date,
profile_phone,
profile_email,
B.Reseller_name,
CASE
WHEN cast(A.transaction_date AS date) BETWEEN B.transaction_date AND B.transaction_date+interval '30 days' THEN 'New'
ELSE 'Existing'
END,
B.tier_name,
province,
area_id
ORDER BY 1 ;
DROP TABLE IF EXISTS raena_analytics.final_clevertab_stage;
CREATE TABLE raena_analytics.final_clevertab_stage AS
SELECT transaction_date,
profile_phone,
profile_email,
Reseller_name,
User_type,
tier_name,
products,
province,
area_id,
cart_id,
CURRENT_DATE-transaction_date AS age,
sum(dst_begin_checkout) dst_begin_checkout,
sum(dst_finish_checkout) dst_finish_checkout
FROM raena_analytics.user_type_stage A
LEFT JOIN
(SELECT user_id ,
products,
mobile,
email ,
A.id cart_id ,
rank() over(partition BY user_id
ORDER BY A.created_at DESC) rnk
FROM
(SELECT DISTINCT id ,
products,
created_at ,
user_id
FROM raena_cart_management.cart
WHERE cleared = 'false') A
LEFT JOIN
(SELECT DISTINCT cast(id AS varchar),
replace(mobile,'+','') mobile,
email
FROM raena_user_management.user) B ON A.user_id = B.id)B ON A.profile_phone = B.mobile
AND B.rnk= 1
GROUP BY transaction_date,
profile_phone,
profile_email,
Reseller_name,
User_type,
tier_name,
cart_id,
products,
province,
area_id,
CURRENT_DATE-transaction_date ;
select count(1) , max(transaction_date) from raena_analytics.final_clevertab_stage;
DROP TABLE IF EXISTS raena_analytics.sku_wholesale_price ;
CREATE TABLE raena_analytics.sku_wholesale_price AS
SELECT sku,
CASE
WHEN tier_price::json->>'07030fbe-5801-4318-9e97-fe33fa169894'='' THEN '0'
ELSE tier_price::json->>'07030fbe-5801-4318-9e97-fe33fa169894'
END bronze_price,
CASE
WHEN tier_price::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19'='' THEN '0'
ELSE tier_price::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19'
END silver_price,
CASE
WHEN tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9'='' THEN '0'
ELSE tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9'
END gold_price,
CASE
WHEN slashed_tier_price::json->>'07030fbe-5801-4318-9e97-fe33fa169894'='' THEN '0'
ELSE slashed_tier_price::json->>'07030fbe-5801-4318-9e97-fe33fa169894'
END slashed_bronze_price,
CASE
WHEN slashed_tier_price::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19'='' THEN '0'
ELSE slashed_tier_price::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19'
END slashed_silver_price,
CASE
WHEN slashed_tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9'='' THEN '0'
ELSE slashed_tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9'
END slashed_gold_price,
retail_price ,
cast((height*width*LENGTH)/6000 AS decimal(22,2)) volume_weight,
weight
FROM raena_catalog_management.product ;
DROP table if exists raena_analytics.final_clevertab_sku_stage;
CREATE TABLE raena_analytics.final_clevertab_sku_stage
as
select transaction_date ,
profile_phone ,
profile_email ,
reseller_name ,
user_type ,
tier_name,
province,
area_id,
cart_id,
age,
dst_begin_checkout ,
dst_finish_checkout ,
arr.item_object::json ->> 'sku' sku_name,
arr.item_object::json ->> 'quantity' quantity
from raena_analytics.final_clevertab_stage,
jsonb_array_elements(replace(products,'''','')::jsonb) WITH
ORDINALITY arr(item_object, POSITION)
where products not like '%ANE014%';
select count(1) , max(transaction_date) from raena_analytics.final_clevertab_sku_stage;
drop table if exists raena_analytics.final_clevertab_sku_stage2;
create table raena_analytics.final_clevertab_sku_stage2
as
SELECT A.*,
CASE WHEN tier_name = 'GOLD' and gold_price<>'' THEN cast(gold_price as decimal(22,4))
WHEN tier_name = 'SILVER' and silver_price<>'' THEN cast(silver_price as decimal(22,4))
else cast(bronze_price as decimal(22,4))
END wholesale_price,
case when volume_weight>weight then volume_weight else weight end sku_weight,
CASE
WHEN ((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int))<1.3 THEN 1
WHEN ((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int))>=1.3
AND (ABS(((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int))) - FLOOR(ABS(((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int))))) BETWEEN 0.3 AND 0.999999
THEN FLOOR(((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int)))+1
ELSE FLOOR(((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int)))
END AS final_weight,
price
FROM raena_analytics.final_clevertab_sku_stage a
LEFT JOIN raena_analytics.sku_wholesale_price b ON a.sku_name = B.sku
left join (select destination_area_id , min(price) price from raena_transport_management.logistic_rate
group by 1) C on A.area_id = C.destination_area_id;
select count(1) , max(transaction_date) from raena_analytics.final_clevertab_sku_stage2;
DROP TABLE IF EXISTS raena_analytics.final_clevertab;
CREATE TABLE raena_analytics.final_clevertab AS
SELECT transaction_date ,
profile_phone ,
profile_email,
reseller_name,
user_type,
tier_name,
province,
cart_id,
age,
dst_begin_checkout,
dst_finish_checkout,
sku_name ,
cast(quantity as int)quantity,
cast(wholesale_price as int) wholesale_price,
final_weight,
price
FROM raena_analytics.final_clevertab_sku_stage2 ;
select count(1) , max(transaction_date) from raena_analytics.final_clevertab;
" > /home/ec2-user/cronjob/postgresql/card_abandment/cart_abandment_etl.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/card_abandment/cart_abandment_etl.sql

View File

@ -0,0 +1,395 @@
drop table if exists raena_analytics.brand_promo_dashboard_table ;
create table raena_analytics.brand_promo_dashboard_table
as
select BB.name brand_name, type promo_type, sku ,'' free_sku, startdate , enddate, cast(coalesce(promo,'0') as float) promo ,cast(coalesce(Moq,'0') as float) moq,cast(coalesce(mov,'0') as float) mov
from raena_analytics.brand_promo_master AA
left join raena_catalog_management.brand BB on AA.brandid = cast(BB.id as varchar)
union
select BB.name brand_name,type ,
case when promoselection = 'ANY' then concat('ANY (', promoquantity_1)
when promosku_2 ='(' then concat(promosku_1,promoquantity_1)
else concat(concat(promosku_1,promoquantity_1),concat('+',concat(promosku_2,promoquantity_2))) end Promo_sku,
case when freeselection = 'ANY' then concat('ANY (', freequantity_1)
when freeselection = 'SAME' then (case when promosku_2 ='(' then concat(promosku_1,freequantity_1)
else concat(concat(promosku_1,freequantity_1),concat('+',concat(promosku_2,freequantity_2))) end)
when freesku_2 ='(' then concat(freesku_1,freequantity_1)
else concat(concat(freesku_1,freequantity_1),concat('+',concat(freesku_2,freequantity_2)))
end free_sku,
startdate,
enddate,
0 promo,
0 Moq,
0 mov
from
(
select id , name , brandid ,
type,
promobrand,
concat(split_part(promosku,';',1),'(') promosku_1,
concat(split_part(promosku,';',2),'(') promosku_2,
concat(split_part(promoquantity,';',1),')') promoquantity_1,
concat(split_part(promoquantity,';',2),')') promoquantity_2,
promoselection,
freebrand,
concat(split_part(freesku,';',1),'(') freesku_1,
concat(split_part(freesku,';',2),'(') freesku_2,
concat(split_part(freequantity,';',1),')') freequantity_1,
concat(split_part(freequantity,';',2),')') freequantity_2,
freeselection,
startdate,
enddate
from raena_analytics.brand_promo_master A
left join raena_analytics.buyngetx_promo B on A.id = B.promo_id
left join raena_analytics.buyngetx_free C on A.id = C.promo_id
where type = 'BUY_N_GET_X'
order by 1) AA
left join raena_catalog_management.brand BB on AA.brandid = cast(BB.id as varchar)
order by startdate ;
DROP TABLE IF EXISTS raena_analytics.clevertap_conversion_base;
--cast(left(ts,4) || '-' || right(left(ts,6),2) || '-' || right(left(ts,8),2) as date)
CREATE TABLE raena_analytics.clevertap_conversion_base AS
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
1 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.app_launched
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
2 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.view_item
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
3 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.view_cart
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
4 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.begin_checkout
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
5 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.finish_checkout
UNION ALL
SELECT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
6 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.charged;
SELECT count(1),
max(transaction_date)
FROM raena_analytics.clevertap_conversion_base;
DROP TABLE IF EXISTS raena_analytics.clevertap_checkout_base;
CREATE TABLE raena_analytics.clevertap_checkout_base AS
SELECT DISTINCT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
4 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.begin_checkout
UNION ALL
SELECT DISTINCT profile_objectid,
profile_phone,
profile_email,
profile_platform,
eventname,
5 sort_id,
cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date
FROM clevertap.finish_checkout;
SELECT count(1) ,
max(transaction_date)
FROM raena_analytics.clevertap_checkout_base;
DROP TABLE IF EXISTS raena_analytics.order_base;
CREATE TABLE raena_analytics.order_base AS
SELECT min((cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date))) AS transaction_date ,
profile_phone
FROM clevertap.app_installed
GROUP BY profile_phone;
DROP TABLE IF EXISTS raena_analytics.usder_base;
CREATE TABLE raena_analytics.usder_base AS
SELECT A.name ,
A.id ,
B.name tier_name,
A.mobile ,
A.email,
A.province,
A.area_id
FROM raena_user_management.user A
LEFT JOIN raena_user_management.tier B ON A.tier_id = B.id;
DROP TABLE IF EXISTS raena_analytics.order_clevertab_stage1;
CREATE TABLE raena_analytics.order_clevertab_stage1 AS
SELECT A.transaction_date ,
tier_name,
name Reseller_name,
replace(mobile,'+','') mobile,
email ,
province,
area_id,
id reseller_id
FROM raena_analytics.order_base A
LEFT JOIN raena_analytics.usder_base B ON A.profile_phone= replace(mobile,'+','')
ORDER BY id ;
DROP TABLE IF EXISTS raena_analytics.user_type_stage;
CREATE TABLE raena_analytics.user_type_stage AS
SELECT A.transaction_date,
profile_phone,
profile_email,
B.Reseller_name,
CASE
WHEN cast(A.transaction_date AS date) BETWEEN B.transaction_date AND B.transaction_date+interval '30 days' THEN 'New'
ELSE 'Existing'
END user_type,
B.tier_name,
province,
area_id,
count(CASE WHEN eventname = 'begin_checkout' THEN coalesce(profile_phone,profile_email) END) dst_begin_checkout,
count(CASE WHEN eventname = 'finish_checkout' THEN coalesce(profile_phone,profile_email) END) dst_finish_checkout
FROM raena_analytics.clevertap_checkout_base A
LEFT JOIN raena_analytics.order_clevertab_stage1 B ON profile_phone =B.mobile
WHERE eventname IN ('begin_checkout',
'finish_checkout')
GROUP BY A.transaction_date,
profile_phone,
profile_email,
B.Reseller_name,
CASE
WHEN cast(A.transaction_date AS date) BETWEEN B.transaction_date AND B.transaction_date+interval '30 days' THEN 'New'
ELSE 'Existing'
END,
B.tier_name,
province,
area_id
ORDER BY 1 ;
DROP TABLE IF EXISTS raena_analytics.final_clevertab_stage;
CREATE TABLE raena_analytics.final_clevertab_stage AS
SELECT transaction_date,
profile_phone,
profile_email,
Reseller_name,
User_type,
tier_name,
products,
province,
area_id,
cart_id,
CURRENT_DATE-transaction_date AS age,
sum(dst_begin_checkout) dst_begin_checkout,
sum(dst_finish_checkout) dst_finish_checkout
FROM raena_analytics.user_type_stage A
LEFT JOIN
(SELECT user_id ,
products,
mobile,
email ,
A.id cart_id ,
rank() over(partition BY user_id
ORDER BY A.created_at DESC) rnk
FROM
(SELECT DISTINCT id ,
products,
created_at ,
user_id
FROM raena_cart_management.cart
WHERE cleared = 'false') A
LEFT JOIN
(SELECT DISTINCT cast(id AS varchar),
replace(mobile,'+','') mobile,
email
FROM raena_user_management.user) B ON A.user_id = B.id)B ON A.profile_phone = B.mobile
AND B.rnk= 1
GROUP BY transaction_date,
profile_phone,
profile_email,
Reseller_name,
User_type,
tier_name,
cart_id,
products,
province,
area_id,
CURRENT_DATE-transaction_date ;
select count(1) , max(transaction_date) from raena_analytics.final_clevertab_stage;
DROP TABLE IF EXISTS raena_analytics.sku_wholesale_price ;
CREATE TABLE raena_analytics.sku_wholesale_price AS
SELECT sku,
CASE
WHEN tier_price::json->>'07030fbe-5801-4318-9e97-fe33fa169894'='' THEN '0'
ELSE tier_price::json->>'07030fbe-5801-4318-9e97-fe33fa169894'
END bronze_price,
CASE
WHEN tier_price::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19'='' THEN '0'
ELSE tier_price::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19'
END silver_price,
CASE
WHEN tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9'='' THEN '0'
ELSE tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9'
END gold_price,
CASE
WHEN slashed_tier_price::json->>'07030fbe-5801-4318-9e97-fe33fa169894'='' THEN '0'
ELSE slashed_tier_price::json->>'07030fbe-5801-4318-9e97-fe33fa169894'
END slashed_bronze_price,
CASE
WHEN slashed_tier_price::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19'='' THEN '0'
ELSE slashed_tier_price::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19'
END slashed_silver_price,
CASE
WHEN slashed_tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9'='' THEN '0'
ELSE slashed_tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9'
END slashed_gold_price,
retail_price ,
cast((height*width*LENGTH)/6000 AS decimal(22,2)) volume_weight,
weight
FROM raena_catalog_management.product ;
DROP table if exists raena_analytics.final_clevertab_sku_stage;
CREATE TABLE raena_analytics.final_clevertab_sku_stage
as
select transaction_date ,
profile_phone ,
profile_email ,
reseller_name ,
user_type ,
tier_name,
province,
area_id,
cart_id,
age,
dst_begin_checkout ,
dst_finish_checkout ,
arr.item_object::json ->> 'sku' sku_name,
arr.item_object::json ->> 'quantity' quantity
from raena_analytics.final_clevertab_stage,
jsonb_array_elements(replace(products,'''','')::jsonb) WITH
ORDINALITY arr(item_object, POSITION)
where products not like '%ANE014%';
select count(1) , max(transaction_date) from raena_analytics.final_clevertab_sku_stage;
drop table if exists raena_analytics.final_clevertab_sku_stage2;
create table raena_analytics.final_clevertab_sku_stage2
as
SELECT A.*,
CASE WHEN tier_name = 'GOLD' and gold_price<>'' THEN cast(gold_price as decimal(22,4))
WHEN tier_name = 'SILVER' and silver_price<>'' THEN cast(silver_price as decimal(22,4))
else cast(bronze_price as decimal(22,4))
END wholesale_price,
case when volume_weight>weight then volume_weight else weight end sku_weight,
CASE
WHEN ((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int))<1.3 THEN 1
WHEN ((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int))>=1.3
AND (ABS(((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int))) - FLOOR(ABS(((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int))))) BETWEEN 0.3 AND 0.999999
THEN FLOOR(((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int)))+1
ELSE FLOOR(((case when volume_weight>weight then volume_weight else weight end)*cast(Quantity as int)))
END AS final_weight,
price
FROM raena_analytics.final_clevertab_sku_stage a
LEFT JOIN raena_analytics.sku_wholesale_price b ON a.sku_name = B.sku
left join (select destination_area_id , min(price) price from raena_transport_management.logistic_rate
group by 1) C on A.area_id = C.destination_area_id;
select count(1) , max(transaction_date) from raena_analytics.final_clevertab_sku_stage2;
DROP TABLE IF EXISTS raena_analytics.final_clevertab;
CREATE TABLE raena_analytics.final_clevertab AS
SELECT transaction_date ,
profile_phone ,
profile_email,
reseller_name,
user_type,
tier_name,
province,
cart_id,
age,
dst_begin_checkout,
dst_finish_checkout,
sku_name ,
cast(quantity as int)quantity,
cast(wholesale_price as int) wholesale_price,
final_weight,
price
FROM raena_analytics.final_clevertab_sku_stage2 ;
select count(1) , max(transaction_date) from raena_analytics.final_clevertab;

View File

@ -0,0 +1,186 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
DROP TABLE raena_analytics.google_pay_split;
CREATE TABLE raena_analytics.google_pay_split AS
SELECT * ,
split_part(address ,', ',1) address1,
split_part(address,', ',2) address2,
split_part(address,', ',3) address3,
split_part(address,', ',4) address4,
split_part(address,', ',5) address5,
split_part(address,', ',6) address6,
split_part(address,', ',7) address7,
split_part(address,', ',8) address8,
split_part(address,', ',9) address9,
split_part(address,', ',10) address10,
split_part(address,', ',11) address11
FROM
(SELECT DISTINCT lower(search_city) city,
lower(address) address,
CASE
WHEN search_city=''
AND address='' THEN 1
END flag
FROM raena_crawler_management.google_map_search_results
UNION SELECT DISTINCT lower(current_city) city,
lower(address) address,
CASE
WHEN current_city=''
AND address='' THEN 1
END flag
FROM
(SELECT fbp.profile_id,
name reseller_name,
mobile,
Email,
active_status is_account_active,
current_city,
hometown,
WORK company,
address,
friends number_of_friends,
followers number_of_followers,
following number_of_people_following --,converted_flag
FROM
(SELECT facebook_profile.*
FROM raena_crawler_management.facebook_profile ) fbp
LEFT JOIN
(SELECT DISTINCT coalesce(A.profile_id,B.profile_id) profile_id ,
mobile,
Email
FROM
(SELECT profile_id ,
CASE WHEN detail_type= 'Mobile' THEN value END AS mobile
FROM raena_crawler_management.facebook_profile_detail
WHERE detail_type ='Mobile') A
FULL OUTER JOIN
(SELECT profile_id ,
CASE WHEN detail_type= 'Email' THEN value END AS Email
FROM raena_crawler_management.facebook_profile_detail
WHERE detail_type ='Email') B ON A.profile_id = B.profile_id) fbpd ON fbpd.profile_id = fbp.profile_id) B) A
WHERE flag IS NULL;
DROP TABLE raena_analytics.google_pay_mapping_table_stage;
CREATE TABLE raena_analytics.google_pay_mapping_table_stage AS
SELECT A.* ,
B.city mapping_city
FROM raena_analytics.google_pay_split A
LEFT JOIN
(SELECT DISTINCT lower(city) city
FROM raena_analytics.google_pay_split) B ON 1=1;
DROP TABLE raena_analytics.city_calculated_values;
CREATE TABLE raena_analytics.city_calculated_values AS
SELECT address,
mapping_city calculated_city
FROM
(SELECT *,
CASE
WHEN replace(replace(address1,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value1,
CASE
WHEN replace(replace(address2,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value2,
CASE
WHEN replace(replace(address3,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value3,
CASE
WHEN replace(replace(address4,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value4,
CASE
WHEN replace(replace(address5,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value5,
CASE
WHEN replace(replace(address6,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value6,
CASE
WHEN replace(replace(address7,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value7,
CASE
WHEN replace(replace(address8,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value8,
CASE
WHEN replace(replace(address9,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value9,
CASE
WHEN replace(replace(address10,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value10,
CASE
WHEN replace(replace(address11,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value11,
CASE
WHEN replace(replace(address1,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address2,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address3,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address4,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address5,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address6,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address7,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address8,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address9,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address10,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address11,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END total
FROM raena_analytics.google_pay_mapping_table_stage) A
WHERE total>0 ;
" > /home/ec2-user/cronjob/postgresql/city_mapping_for_online_lead_db.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/city_mapping_for_online_lead_db.sql

View File

@ -0,0 +1,168 @@
DROP TABLE raena_analytics.google_pay_split;
CREATE TABLE raena_analytics.google_pay_split AS
SELECT * ,
split_part(address ,', ',1) address1,
split_part(address,', ',2) address2,
split_part(address,', ',3) address3,
split_part(address,', ',4) address4,
split_part(address,', ',5) address5,
split_part(address,', ',6) address6,
split_part(address,', ',7) address7,
split_part(address,', ',8) address8,
split_part(address,', ',9) address9,
split_part(address,', ',10) address10,
split_part(address,', ',11) address11
FROM
(SELECT DISTINCT lower(search_city) city,
lower(address) address,
CASE
WHEN search_city=''
AND address='' THEN 1
END flag
FROM raena_crawler_management.google_map_search_results
UNION SELECT DISTINCT lower(current_city) city,
lower(address) address,
CASE
WHEN current_city=''
AND address='' THEN 1
END flag
FROM
(SELECT fbp.profile_id,
name reseller_name,
mobile,
Email,
active_status is_account_active,
current_city,
hometown,
WORK company,
address,
friends number_of_friends,
followers number_of_followers,
following number_of_people_following --,converted_flag
FROM
(SELECT facebook_profile.*
FROM raena_crawler_management.facebook_profile ) fbp
LEFT JOIN
(SELECT DISTINCT coalesce(A.profile_id,B.profile_id) profile_id ,
mobile,
Email
FROM
(SELECT profile_id ,
CASE WHEN detail_type= 'Mobile' THEN value END AS mobile
FROM raena_crawler_management.facebook_profile_detail
WHERE detail_type ='Mobile') A
FULL OUTER JOIN
(SELECT profile_id ,
CASE WHEN detail_type= 'Email' THEN value END AS Email
FROM raena_crawler_management.facebook_profile_detail
WHERE detail_type ='Email') B ON A.profile_id = B.profile_id) fbpd ON fbpd.profile_id = fbp.profile_id) B) A
WHERE flag IS NULL;
DROP TABLE raena_analytics.google_pay_mapping_table_stage;
CREATE TABLE raena_analytics.google_pay_mapping_table_stage AS
SELECT A.* ,
B.city mapping_city
FROM raena_analytics.google_pay_split A
LEFT JOIN
(SELECT DISTINCT lower(city) city
FROM raena_analytics.google_pay_split) B ON 1=1;
DROP TABLE raena_analytics.city_calculated_values;
CREATE TABLE raena_analytics.city_calculated_values AS
SELECT address,
mapping_city calculated_city
FROM
(SELECT *,
CASE
WHEN replace(replace(address1,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value1,
CASE
WHEN replace(replace(address2,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value2,
CASE
WHEN replace(replace(address3,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value3,
CASE
WHEN replace(replace(address4,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value4,
CASE
WHEN replace(replace(address5,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value5,
CASE
WHEN replace(replace(address6,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value6,
CASE
WHEN replace(replace(address7,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value7,
CASE
WHEN replace(replace(address8,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value8,
CASE
WHEN replace(replace(address9,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value9,
CASE
WHEN replace(replace(address10,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value10,
CASE
WHEN replace(replace(address11,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END map_value11,
CASE
WHEN replace(replace(address1,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address2,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address3,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address4,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address5,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address6,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address7,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address8,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address9,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address10,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END + CASE
WHEN replace(replace(address11,'kota ',''),'city ','') = replace(replace(mapping_city,'kota ',''),'city ','') THEN 1
ELSE 0
END total
FROM raena_analytics.google_pay_mapping_table_stage) A
WHERE total>0 ;

View File

@ -0,0 +1,95 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
drop table raena_analytics.increase_cogs_alert;
create table raena_analytics.increase_cogs_alert
as
SELECT AA.sku,
brand_name,
new_cogs,
new_cogs_updated_date,
old_cogs,
old_cogs_updated_date,
po_number,
cogs cogs_in_po,
calculated_pkp_cogs po_cogs_with_pkp,
quantity quantity_in_po,
wholesale_gold_price,
wholesale_silver_price,
wholesale_bronze_price,
cast(CEILING(new_cogs/cast((100-gold_gm_target) as bigint)) as bigint)*100 gold_msp,
cast(CEILING(new_cogs/cast((100-silver_gm_target)as bigint)) as bigint)*100 silver_msp,
cast(CEILING(new_cogs/cast((100-bronze_gm_target)as bigint)) as bigint)*100 bronze_msp
FROM
(SELECT sku,
cogs new_cogs,
created_at new_cogs_updated_date,
old_cogs ,
brand_name,
cast(wholesale_gold_price as varchar)wholesale_gold_price ,
cast(wholesale_silver_price as varchar) wholesale_silver_price,
cast(wholesale_bronze_price as varchar)wholesale_bronze_price,
old_created_date old_cogs_updated_date,
cogs-old_cogs diff
from (SELECT A.sku,
A.cogs,
A.old_cogs,
A.created_at,
A.old_created_date,
raena_catalog_management.brand.name brand_name,
raena_catalog_management.product.tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' wholesale_gold_price,
raena_catalog_management.product.tier_price::json->'8eb95d6e-915a-4a91-9c12-fa43db995e19' wholesale_silver_price,
raena_catalog_management.product.tier_price::json->'07030fbe-5801-4318-9e97-fe33fa169894' wholesale_bronze_price
FROM (select sku , cogs , lead(cogs, 1)over ( partition by sku order by created_at desc ) old_cogs ,
created_at ,
lead(created_at , 1 ) over (partition by sku order by created_at desc ) old_created_date
from raena_catalog_management.cogs_audit
order by sku , created_at desc) A
left join raena_catalog_management.product on A.sku=raena_catalog_management.product.sku
LEFT JOIN raena_catalog_management.brand ON raena_catalog_management.product.brand_id=raena_catalog_management.brand.id
)A ) AA
LEFT JOIN
(SELECT B.po_number,
A.sku,
A.received_quantity quantity ,
C.cogs,
C.calculated_pkp_cogs,
A.created_at
FROM raena_erp_management.inbound_grn_sku A
LEFT JOIN raena_erp_management.inbound_order B ON A.po_id=B.id
LEFT JOIN raena_erp_management.inbound_order_sku C ON A.sku_id = C.id
)BB ON AA.sku = BB.sku
AND AA.new_cogs_updated_date::date = BB.created_at::Date
LEFT JOIN
(SELECT sku ,
sum(CASE WHEN tierName='GOLD' THEN gm_target END) gold_gm_target,
sum(CASE WHEN tierName='SILVER' THEN gm_target END) silver_gm_target,
sum(CASE WHEN tierName='BRONZE' THEN gm_target END)bronze_gm_target
FROM
(SELECT DISTINCT SKU,
gm_target,
t.name AS tierName
FROM raena_gross_margin_management.gross_margin_config AA
LEFT JOIN raena_user_management.tier t ON cast(AA.gm_target_tier AS TEXT) =cast(t.id AS TEXT)
where gm_target <100) A
GROUP BY 1) DD ON AA.sku = DD.sku
where diff<>0
ORDER by gold_msp desc;
" > /home/ec2-user/cronjob/postgresql/cogs_alert/increase_cogs_alert.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/cogs_alert/increase_cogs_alert.sql

View File

@ -0,0 +1,78 @@
drop table raena_analytics.increase_cogs_alert;
create table raena_analytics.increase_cogs_alert
as
SELECT AA.sku,
brand_name,
new_cogs,
new_cogs_updated_date,
old_cogs,
old_cogs_updated_date,
po_number,
cogs cogs_in_po,
calculated_pkp_cogs po_cogs_with_pkp,
quantity quantity_in_po,
wholesale_gold_price,
wholesale_silver_price,
wholesale_bronze_price,
cast(CEILING(new_cogs/cast((100-gold_gm_target) as bigint)) as bigint)*100 gold_msp,
cast(CEILING(new_cogs/cast((100-silver_gm_target)as bigint)) as bigint)*100 silver_msp,
cast(CEILING(new_cogs/cast((100-bronze_gm_target)as bigint)) as bigint)*100 bronze_msp
FROM
(SELECT sku,
cogs new_cogs,
created_at new_cogs_updated_date,
old_cogs ,
brand_name,
cast(wholesale_gold_price as varchar)wholesale_gold_price ,
cast(wholesale_silver_price as varchar) wholesale_silver_price,
cast(wholesale_bronze_price as varchar)wholesale_bronze_price,
old_created_date old_cogs_updated_date,
cogs-old_cogs diff
from (SELECT A.sku,
A.cogs,
A.old_cogs,
A.created_at,
A.old_created_date,
raena_catalog_management.brand.name brand_name,
raena_catalog_management.product.tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' wholesale_gold_price,
raena_catalog_management.product.tier_price::json->'8eb95d6e-915a-4a91-9c12-fa43db995e19' wholesale_silver_price,
raena_catalog_management.product.tier_price::json->'07030fbe-5801-4318-9e97-fe33fa169894' wholesale_bronze_price
FROM (select sku , cogs , lead(cogs, 1)over ( partition by sku order by created_at desc ) old_cogs ,
created_at ,
lead(created_at , 1 ) over (partition by sku order by created_at desc ) old_created_date
from raena_catalog_management.cogs_audit
order by sku , created_at desc) A
left join raena_catalog_management.product on A.sku=raena_catalog_management.product.sku
LEFT JOIN raena_catalog_management.brand ON raena_catalog_management.product.brand_id=raena_catalog_management.brand.id
)A ) AA
LEFT JOIN
(SELECT B.po_number,
A.sku,
A.received_quantity quantity ,
C.cogs,
C.calculated_pkp_cogs,
A.created_at
FROM raena_erp_management.inbound_grn_sku A
LEFT JOIN raena_erp_management.inbound_order B ON A.po_id=B.id
LEFT JOIN raena_erp_management.inbound_order_sku C ON A.sku_id = C.id
)BB ON AA.sku = BB.sku
AND AA.new_cogs_updated_date::date = BB.created_at::Date
LEFT JOIN
(SELECT sku ,
sum(CASE WHEN tierName='GOLD' THEN gm_target END) gold_gm_target,
sum(CASE WHEN tierName='SILVER' THEN gm_target END) silver_gm_target,
sum(CASE WHEN tierName='BRONZE' THEN gm_target END)bronze_gm_target
FROM
(SELECT DISTINCT SKU,
gm_target,
t.name AS tierName
FROM raena_gross_margin_management.gross_margin_config AA
LEFT JOIN raena_user_management.tier t ON cast(AA.gm_target_tier AS TEXT) =cast(t.id AS TEXT)
where gm_target <100) A
GROUP BY 1) DD ON AA.sku = DD.sku
where diff<>0
ORDER by gold_msp desc;

View File

@ -0,0 +1,220 @@
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
drop table raena_analytics.pay_on_delivery_payment_dashboard;
CREATE TABLE raena_analytics.pay_on_delivery_payment_dashboard AS
SELECT A.* ,
payment_id,
total_amount,
payment_status,
gateway_expiry,
number_of_day_pending,
payment_link,
case when no_of_retries >=2 then 'Yes' else 'No' end offender_reseller
FROM
(SELECT A.id order_id ,
A.created_at::date order_created_date ,
A.status order_status,
payment_amount,
B.name Am_name,
CASE
WHEN B.email = C.\"emailid \" THEN 'Denny'
ELSE 'Manthan'
END Team,
reseller_info->>'mobile' reseller_mobile,
reseller_info->>'name' reseller_name
FROM raena_order_management.order A
LEFT JOIN raena_user_management.admin_user B ON A.created_by = cast(B.id AS varchar)
LEFT JOIN raena_analytics.denny_acq_team_member C ON B.email = C.\"emailid \"
WHERE A.id LIKE 'PL%') A
LEFT JOIN
(SELECT order_id,
id AS payment_id ,
total_amount,
payment_status,
gateway_expiry,
gateway_expiry::date-CURRENT_DATE::date number_of_day_pending,
CASE
WHEN payment_provider = 'DurianPay' THEN payment_details::jsonb->>'invoice_url'
WHEN payment_provider ='Xendit' THEN concat('https://checkout.xendit.co/web/',payment_provider_code)
END payment_link,
no_of_retries
FROM
(SELECT replace(cast(jsonb_array_elements(order_ids) AS varchar),'\"','') AS order_id ,
*
FROM raena_order_management.payment p) A
WHERE order_id LIKE '%PL%') B ON A.order_id = B.order_id;
DROP TABLE IF EXISTS raena_analytics.fullfillment_base_data ;
CREATE TABLE raena_analytics.fullfillment_base_data AS
SELECT DISTINCT B.order_id ,
A.sku ,
batch,
shipment_id,
quantity,
sum(A.applied_cogs*(CASE WHEN quantity>0 THEN quantity ELSE 1 END))/sum(CASE WHEN quantity>0 THEN quantity ELSE 1 END) applied_cogs,
A.cogs_type
FROM raena_order_management.fulfillment_detail A
LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.shipment_id = B.id
WHERE applied_cogs IS NOT NULL
AND applied_cogs <>0
GROUP BY 1,
2,
3,
4,
5,
7;
DROP TABLE IF EXISTS raena_analytics.cogs_base_data;
CREATE TABLE raena_analytics.cogs_base_data AS
SELECT sku,
cogs,
promo,
(created_at+interval'7 hours')::date created_at,
coalesce(lead((created_at+interval'7 hours')::date-interval'1 day' , 1) over (partition BY sku
ORDER BY sku,created_at),((CURRENT_DATE+interval'7 hours')+interval'1 day'))::date last_date
FROM raena_catalog_management.cogs_audit ca
ORDER BY 1,
4;
DROP TABLE raena_analytics.consignment_base_data ;
CREATE TABLE raena_analytics.consignment_base_data AS
SELECT transaction_date,
paid_status,
Brand_name ,
raena_code,
B.Quantity consignment_quantity,
B.pkp_cogs,
batch_no,
C.order_id ,
applied_cogs ,
cogs_type ,
C.quantity fulfilled_quantity,
C.shipment_id,
D.sku,
D.retail_price,
D.quantity,
D.cogs,
D.effective_cogs,
D.promo,
D.payment_amount
FROM raena_erp_management.inbound_order A
INNER JOIN raena_erp_management.inbound_order_sku B ON A.po_number = B.reference_no
INNER JOIN raena_analytics.fullfillment_base_data C ON B.batch_no = C.batch
AND B.raena_code = C.sku
INNER JOIN
(SELECT A.order_id ,
date_trunc('Month',A.created_at)::date transaction_date,
sales_sub_order_shipment_id,
coalesce(B.sku,A.parent_sku) sku,
coalesce(B.retail_price,A.retail_price)retail_price ,
coalesce(B.quantity,A.quantity) quantity,
coalesce(C.cogs,A.cogs) cogs,
A.effective_cogs ,
C.promo,
coalesce(B.payment_amount,A.payment_amount) payment_amount
FROM raena_order_management.sales_sub_order A
LEFT JOIN raena_order_management.sales_sub_order_parent_child B ON A.id = B.sales_sub_order_id
LEFT JOIN raena_analytics.cogs_base_data C ON B.sku = C.sku
AND B.product_class = 'Bundle'
AND B.created_at::date BETWEEN C.created_at AND C.last_date
WHERE A.created_at::date >='2023-01-01'
ORDER BY 1) D ON C.order_id = D.order_id
AND shipment_id = sales_sub_order_shipment_id
AND C.sku = D.sku
WHERE transaction_date>='2023-01-01';
drop table if exists raena_analytics.clevertap_event_data_vishnu_new;
create table raena_analytics.clevertap_event_data_vishnu_new
as
select ts::date ,events, name , email ,phone,event_props::jsonb->>'CT Session Id' sessionid ,
event_props::jsonb->>'item_id' item_id ,
event_props::jsonb->>'isGuestUser' isGuestUser ,
event_props::jsonb->>'CT Source' Source ,
event_props::jsonb->>'id' id ,
event_props::jsonb->>'brandName' brandName ,
event_props::jsonb->>'item_name' item_name ,
event_props::jsonb->>'offenderItems' offenderItems ,
event_props::jsonb->>'items' items ,
event_props::jsonb->>'search_term' search_term ,
event_props::jsonb->>'screen' screen
from clevertap.clevertap_master_data where events in (
'brands_tab_press' ,
'brand_logo_carousel_press',
'App Launched',
'view_item',
'begin_checkout',
'search',
'Charged',
'App Installed',
'view_cart',
'add_to_cart',
'customer_search',
'Notification Clicked',
'Notification Viewed',
'Push Impressions',
'home_category',
--'pdp_brandprice',
'order_items_view',
--'sidebar_browsebrands',
'confirm_payment',
'finish_checkout',
'loyalty_page_visit',
'home_brand_Caraousel',
'home_brand_grid_view',
'WISHLIST',
'brand_carousel_press',
--'continue_shopping',
'home_banner',
'brandpage_image_banner_press',
'skip_login',
'App Uninstalled','coupon_applied_successfully',
'coupon_could_not_be_applied',
'view_rewards',
'view_order_checkout',
'coupon_remove',
--'homeintent_buyws',
'homepage_image_banner_press',
'home_continue_payment',
'homepage_carousel_category_press',
'flashsale_carousel_item_press',
'flashsale_carousel_view_all_press'--,
--'homeintent_managemp'
)
and event_props not like '%OOREDOO%'
and ts::date between current_date+interval'-100 days' and current_date ;
drop table if exists raena_analytics.clevertap_event_data_vishnu;
create table raena_analytics.clevertap_event_data_vishnu
as
select * from raena_analytics.clevertap_event_data_vishnu_new ;
" > /home/ec2-user/cronjob/postgresql/consignement/consignment_sales_report.sql
psql "host=analytics-db-instance-1.cd7qipz3esdx.ap-southeast-1.rds.amazonaws.com user=dbadmin dbname=analytics port=5432 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/postgresql/consignement/consignment_sales_report.sql

View File

@ -0,0 +1,203 @@
drop table raena_analytics.pay_on_delivery_payment_dashboard;
CREATE TABLE raena_analytics.pay_on_delivery_payment_dashboard AS
SELECT A.* ,
payment_id,
total_amount,
payment_status,
gateway_expiry,
number_of_day_pending,
payment_link,
case when no_of_retries >=2 then 'Yes' else 'No' end offender_reseller
FROM
(SELECT A.id order_id ,
A.created_at::date order_created_date ,
A.status order_status,
payment_amount,
B.name Am_name,
CASE
WHEN B.email = C."emailid " THEN 'Denny'
ELSE 'Manthan'
END Team,
reseller_info->>'mobile' reseller_mobile,
reseller_info->>'name' reseller_name
FROM raena_order_management.order A
LEFT JOIN raena_user_management.admin_user B ON A.created_by = cast(B.id AS varchar)
LEFT JOIN raena_analytics.denny_acq_team_member C ON B.email = C."emailid "
WHERE A.id LIKE 'PL%') A
LEFT JOIN
(SELECT order_id,
id AS payment_id ,
total_amount,
payment_status,
gateway_expiry,
gateway_expiry::date-CURRENT_DATE::date number_of_day_pending,
CASE
WHEN payment_provider = 'DurianPay' THEN payment_details::jsonb->>'invoice_url'
WHEN payment_provider ='Xendit' THEN concat('https://checkout.xendit.co/web/',payment_provider_code)
END payment_link,
no_of_retries
FROM
(SELECT replace(cast(jsonb_array_elements(order_ids) AS varchar),'"','') AS order_id ,
*
FROM raena_order_management.payment p) A
WHERE order_id LIKE '%PL%') B ON A.order_id = B.order_id;
DROP TABLE IF EXISTS raena_analytics.fullfillment_base_data ;
CREATE TABLE raena_analytics.fullfillment_base_data AS
SELECT DISTINCT B.order_id ,
A.sku ,
batch,
shipment_id,
quantity,
sum(A.applied_cogs*(CASE WHEN quantity>0 THEN quantity ELSE 1 END))/sum(CASE WHEN quantity>0 THEN quantity ELSE 1 END) applied_cogs,
A.cogs_type
FROM raena_order_management.fulfillment_detail A
LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.shipment_id = B.id
WHERE applied_cogs IS NOT NULL
AND applied_cogs <>0
GROUP BY 1,
2,
3,
4,
5,
7;
DROP TABLE IF EXISTS raena_analytics.cogs_base_data;
CREATE TABLE raena_analytics.cogs_base_data AS
SELECT sku,
cogs,
promo,
(created_at+interval'7 hours')::date created_at,
coalesce(lead((created_at+interval'7 hours')::date-interval'1 day' , 1) over (partition BY sku
ORDER BY sku,created_at),((CURRENT_DATE+interval'7 hours')+interval'1 day'))::date last_date
FROM raena_catalog_management.cogs_audit ca
ORDER BY 1,
4;
DROP TABLE raena_analytics.consignment_base_data ;
CREATE TABLE raena_analytics.consignment_base_data AS
SELECT transaction_date,
paid_status,
Brand_name ,
raena_code,
B.Quantity consignment_quantity,
B.pkp_cogs,
batch_no,
C.order_id ,
applied_cogs ,
cogs_type ,
C.quantity fulfilled_quantity,
C.shipment_id,
D.sku,
D.retail_price,
D.quantity,
D.cogs,
D.effective_cogs,
D.promo,
D.payment_amount
FROM raena_erp_management.inbound_order A
INNER JOIN raena_erp_management.inbound_order_sku B ON A.po_number = B.reference_no
INNER JOIN raena_analytics.fullfillment_base_data C ON B.batch_no = C.batch
AND B.raena_code = C.sku
INNER JOIN
(SELECT A.order_id ,
date_trunc('Month',A.created_at)::date transaction_date,
sales_sub_order_shipment_id,
coalesce(B.sku,A.parent_sku) sku,
coalesce(B.retail_price,A.retail_price)retail_price ,
coalesce(B.quantity,A.quantity) quantity,
coalesce(C.cogs,A.cogs) cogs,
A.effective_cogs ,
C.promo,
coalesce(B.payment_amount,A.payment_amount) payment_amount
FROM raena_order_management.sales_sub_order A
LEFT JOIN raena_order_management.sales_sub_order_parent_child B ON A.id = B.sales_sub_order_id
LEFT JOIN raena_analytics.cogs_base_data C ON B.sku = C.sku
AND B.product_class = 'Bundle'
AND B.created_at::date BETWEEN C.created_at AND C.last_date
WHERE A.created_at::date >='2023-01-01'
ORDER BY 1) D ON C.order_id = D.order_id
AND shipment_id = sales_sub_order_shipment_id
AND C.sku = D.sku
WHERE transaction_date>='2023-01-01';
drop table if exists raena_analytics.clevertap_event_data_vishnu_new;
create table raena_analytics.clevertap_event_data_vishnu_new
as
select ts::date ,events, name , email ,phone,event_props::jsonb->>'CT Session Id' sessionid ,
event_props::jsonb->>'item_id' item_id ,
event_props::jsonb->>'isGuestUser' isGuestUser ,
event_props::jsonb->>'CT Source' Source ,
event_props::jsonb->>'id' id ,
event_props::jsonb->>'brandName' brandName ,
event_props::jsonb->>'item_name' item_name ,
event_props::jsonb->>'offenderItems' offenderItems ,
event_props::jsonb->>'items' items ,
event_props::jsonb->>'search_term' search_term ,
event_props::jsonb->>'screen' screen
from clevertap.clevertap_master_data where events in (
'brands_tab_press' ,
'brand_logo_carousel_press',
'App Launched',
'view_item',
'begin_checkout',
'search',
'Charged',
'App Installed',
'view_cart',
'add_to_cart',
'customer_search',
'Notification Clicked',
'Notification Viewed',
'Push Impressions',
'home_category',
--'pdp_brandprice',
'order_items_view',
--'sidebar_browsebrands',
'confirm_payment',
'finish_checkout',
'loyalty_page_visit',
'home_brand_Caraousel',
'home_brand_grid_view',
'WISHLIST',
'brand_carousel_press',
--'continue_shopping',
'home_banner',
'brandpage_image_banner_press',
'skip_login',
'App Uninstalled','coupon_applied_successfully',
'coupon_could_not_be_applied',
'view_rewards',
'view_order_checkout',
'coupon_remove',
--'homeintent_buyws',
'homepage_image_banner_press',
'home_continue_payment',
'homepage_carousel_category_press',
'flashsale_carousel_item_press',
'flashsale_carousel_view_all_press'--,
--'homeintent_managemp'
)
and event_props not like '%OOREDOO%'
and ts::date between current_date+interval'-100 days' and current_date ;
drop table if exists raena_analytics.clevertap_event_data_vishnu;
create table raena_analytics.clevertap_event_data_vishnu
as
select * from raena_analytics.clevertap_event_data_vishnu_new ;

Some files were not shown because too many files have changed in this diff Show More