289 lines
8.3 KiB
Bash
289 lines
8.3 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 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
|