366 lines
13 KiB
Bash
366 lines
13 KiB
Bash
|
#!/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
|
||
|
|