From 742b8d03c070ce45dc0465871a425b4ee474a0e0 Mon Sep 17 00:00:00 2001 From: "shariar@raenabeauty.com" Date: Mon, 24 Jun 2024 18:26:08 +0600 Subject: [PATCH] first commit --- .etlTransaction_job.sh.swp | Bin 0 -> 24576 bytes .idea/.gitignore | 8 + .swp | Bin 0 -> 12288 bytes AM_ACQ/AM_Dashboard | 147 + AM_ACQ/AM_Dashboard.log | 9 + AM_ACQ/AM_dashboard.sql | 143 + Am_recommendation/am_recommendation.log | 75 + Am_recommendation/am_recommendation.sql | 2735 ++++++++++++++++ Am_recommendation/etl_am_recommendation.sh | 2751 ++++++++++++++++ DB_funnel_new_users.log | 19 + Final_GM_v1.sql | 910 ++++++ Gm_dashboard/.etl_gmdashboard_new_v4.sh.swo | Bin 0 -> 12288 bytes Gm_dashboard/2q | 1040 ++++++ Gm_dashboard/Final_GM_v1.sql | 865 +++++ Gm_dashboard/Final_GM_v2.sql | 1937 +++++++++++ Gm_dashboard/OOS_code.log | 38 + Gm_dashboard/etlDemandForecasting.log | 47 + Gm_dashboard/etlTransaction_job_v1.log | 32 + Gm_dashboard/etlTransaction_job_v2.log | 53 + Gm_dashboard/etl_gmdashboard_new_v2.sh | 1183 +++++++ Gm_dashboard/etl_gmdashboard_new_v3.sh | 1911 +++++++++++ Gm_dashboard/etl_gmdashboard_new_v5.sh | 1957 +++++++++++ Gm_dashboard/etl_gmdashboard_v1.sh | 881 +++++ OM_New_VS_Existing.log | 0 OM_New_VS_Existing.sh | 35 + OM_Test_Launched.log | 5 + OM_Test_launch.sh | 18 + OOS/DB_funnel_new_users.log | 19 + OOS/DB_funnel_new_users.sh | 178 + OOS/DB_funnel_new_users.sql | 174 + OOS/OOS_code.log | 38 + OOS/OOS_code.sh | 427 +++ OOS/OOS_code.sql | 423 +++ OOS/sample.log | 2 + OOS/sample.sh | 22 + OOS/sample.sql | 18 + OOS_code.log | 1 + README.md | 0 a.out | 0 am_recommendation.log | 75 + appsflyer/etl_appsflayer.log | 0 appsflyer/etl_appsflyer.log | 16 + appsflyer/etl_appsflyer.sh | 295 ++ appsflyer/etl_appsflyer.sql | 279 ++ appsflyer/etl_inappevent_appsflyer.sh | 249 ++ appsflyer/etl_inappevent_appsflyer.sql | 234 ++ clevertap_funnel_reports.log | 22 + .../clevertap_funnel_reports.log | 24 + .../clevertap_funnel_reports.sql | 349 ++ .../etl_cart_conversion.sh | 365 +++ consignement/consignement_code.sql | 12 + consignement_code | 20 + consignement_code.log | 0 cron.sh | 1 + demandForecasting/Demand_forecasting.sql | 995 ++++++ demandForecasting/Retention_Metabase.sql | 401 +++ demandForecasting/db_funnel_code.sql | 84 + demandForecasting/etlDemandForecasting.log | 0 demandForecasting/etlDemandForecasting.sh | 1013 ++++++ .../to_run_etlDemandForecasting.sh | 1008 ++++++ etlTransaction_job.log | 31 + etlTransaction_job.sh | 102 + etlTransaction_job_v2.log | 30 + etlTrasnsaction_job.log | 26 + etltransaction_job_final.sh | 925 ++++++ etlwarehouseAnalysis.log | 10 + home_page_pdp_flash/home_page_pdp_flash.log | 18 + home_page_pdp_flash/home_page_pdp_flash.sql | 782 +++++ .../home_page_pdp_flash_script.sh | 797 +++++ install_uninsatll_rate.sh | 73 + loyalty_point/loyalty_point.sh | 118 + loyalty_point/loyalty_point.sql | 98 + mongo_rdash360.log | 6 + new_users/conversion_report.sh | 45 + new_users/conversion_report.sql | 37 + new_users/db_funnel_code.sh | 93 + new_users/dbfunnel_Metabase.log | 0 new_users/reseller_business_metrics.sh | 171 + new_users/reseller_business_metrics_code.sql | 163 + ...eseller_business_metrics_code_Metabase.log | 0 postgresql/360rdash/360_rdash_etl.sql | 113 + postgresql/360rdash/rdash_etl_cron.sh | 130 + postgresql/am_dashboard/am_dashboard.sh | 221 ++ postgresql/am_dashboard/am_dashboard.sql | 204 ++ .../am_recommendation_etl.sql | 2860 ++++++++++++++++ .../am_recommendation_etl_cron.sh | 2876 ++++++++++++++++ .../am_recommendation/whatsapp_campaign.sh | 514 +++ .../am_recommendation/whatsapp_campaign.sql | 496 +++ postgresql/appflyer/appflyer_etl.sh | 288 ++ postgresql/appflyer/appflyer_etl.sql | 273 ++ .../appflyer/etl_inappevent_appsflyer.sql | 250 ++ postgresql/appflyer/in_app_appsflyer_etl.sh | 268 ++ .../card_abandment/cart_abandment_etl.sh | 411 +++ .../card_abandment/cart_abandment_etl.sql | 395 +++ postgresql/city_mapping_for_online_lead_db.sh | 186 ++ .../city_mapping_for_online_lead_db.sql | 168 + postgresql/cogs_alert/cogs_increase_alert.sh | 95 + postgresql/cogs_alert/increase_cogs_alert.sql | 78 + .../consignement/consignment_sales_report.sh | 220 ++ .../consignement/consignment_sales_report.sql | 203 ++ postgresql/consignement/pwd | 0 .../demand_forecasting/Demand_forecasting.sql | 997 ++++++ .../demand_forecasting_etl.sh | 1016 ++++++ .../distributed_warehouse_etl_cron.sh | 1501 +++++++++ .../distributed_warehouse_etl_cron.sql | 1486 +++++++++ postgresql/gm_code/.gm_etl_cron.sh.swp | Bin 0 -> 24576 bytes postgresql/gm_code/gm_etl_cron.sh | 2323 +++++++++++++ postgresql/gm_code/gm_etl_cron.sql | 2306 +++++++++++++ postgresql/h | 0 .../home_page_pdp_flash.sh | 748 +++++ .../home_page_pdp_flash.sql | 735 +++++ postgresql/loyalty_point/loyalty_point_etl.sh | 137 + .../loyalty_point/loyalty_point_etl.sql | 120 + .../new_user_funnel/new_user_funnel_etl.sh | 274 ++ .../new_user_funnel/new_user_funnel_etl.sql | 257 ++ .../notification_dashboard_anchor_brand.sh | 625 ++++ .../notification_doashboard_anchor_brand.sql | 605 ++++ .../offender_dashboard/offender_dashboard.sh | 953 ++++++ .../offender_dashboard/offender_dashboard.sql | 934 ++++++ postgresql/oos_restock/oos_restock_etl.sh | 654 ++++ postgresql/oos_restock/oos_restock_etl.sql | 637 ++++ .../product_matrics/product_matrics_etl.sh | 1049 ++++++ .../product_matrics/product_matrics_etl.sql | 1033 ++++++ .../retention_code/retention_code_etl.sh | 578 ++++ .../retention_code/retention_code_etl.sql | 563 ++++ .../retention_code/retention_code_etl_new.sh | 587 ++++ .../revenue_leakage/revenue_leakage_etl.sh | 639 ++++ .../revenue_leakage/revenue_leakage_etl.sql | 621 ++++ postgresql/sold_product/sold_product.sh | 123 + postgresql/sold_product/sold_product.sql | 104 + product_metrics/product_metrics.sh | 1061 ++++++ product_metrics/product_metrics.sql | 1042 ++++++ query.log | 26 + rdash/Mongo_rdash360_dashboard.sh | 89 + rdash/mongo_rdash360.log | 10 + rdash/mongo_rdash360.sql | 85 + redshift/.gm_dashboard.sh.swp | Bin 0 -> 53248 bytes redshift/.gm_dashboard_v1.sh.swp | Bin 0 -> 77824 bytes redshift/Appflyer_etl.sh | 289 ++ redshift/Product_metrics.sh | 1051 ++++++ redshift/Product_metrics_2.sh | 1882 +++++++++++ redshift/am_dashboard.sh | 222 ++ redshift/am_recommendation.sh | 2880 +++++++++++++++++ .../anchor_brand_notification_dashboard.sh | 624 ++++ redshift/cart_abandment.sh | 426 +++ redshift/city_mapping_for_online_lead.sh | 187 ++ redshift/cogs_alert.sh | 96 + redshift/consignment_sales_report.sh | 228 ++ redshift/consignment_sales_report_v1.sh | 290 ++ redshift/demand_forcasting.sh | 1007 ++++++ redshift/distributed_warehouse_dashboard.sh | 1580 +++++++++ redshift/gamification_etl.sh | 907 ++++++ redshift/gm_dashboard.sh | 2370 ++++++++++++++ redshift/gm_dashboard_new.sh | 2481 ++++++++++++++ redshift/home_page_pdp_flash.sh | 749 +++++ redshift/inapp_appflayer_etl.sh | 269 ++ redshift/inapp_appflayer_etl2.sh | 314 ++ redshift/loyalty_point.sh | 87 + redshift/new_user_funnel_report.sh | 303 ++ redshift/offender_dashboard.sh | 962 ++++++ redshift/oos_stock_restock.sh | 650 ++++ redshift/payment_dashboard.sh | 153 + redshift/rdash_dashboard.sh | 149 + redshift/retention_dashboard.sh | 459 +++ redshift/revenue_leakage.sh | 640 ++++ redshift/sold_product.sh | 122 + redshift/sql_code/360_rdash_etl.sql | 131 + redshift/sql_code/Demand_forecasting.sql | 992 ++++++ redshift/sql_code/am_dashboard.sql | 204 ++ redshift/sql_code/am_recommendation_etl.sql | 2863 ++++++++++++++++ redshift/sql_code/appflyer_etl.sql | 273 ++ redshift/sql_code/cart_abandment_etl.sql | 409 +++ .../sql_code/consignment_sales_report.sql | 272 ++ .../distributed_warehouse_etl_cron.sql | 1564 +++++++++ .../sql_code/etl_inappevent_appsflyer.sql | 295 ++ redshift/sql_code/gamification_etl.sql | 889 +++++ redshift/sql_code/gm_etl_cron.sql | 2461 ++++++++++++++ redshift/sql_code/home_page_pdp_flash.sql | 735 +++++ redshift/sql_code/increase_cogs_alert.sql | 78 + redshift/sql_code/loyalty_point_etl.sql | 69 + redshift/sql_code/new_user_funnel_etl.sql | 285 ++ .../notification_doashboard_anchor_brand.sql | 605 ++++ redshift/sql_code/offender_dashboard.sql | 942 ++++++ redshift/sql_code/oos_restock_etl.sql | 632 ++++ redshift/sql_code/payment_dashboard.sql | 135 + redshift/sql_code/product_matrics_etl.sql | 1865 +++++++++++ redshift/sql_code/retention_code_etl.sql | 442 +++ redshift/sql_code/revenue_leakage_etl.sql | 621 ++++ redshift/sql_code/sold_product.sql | 105 + redshift/sql_code/whatsapp_campaign.sql | 497 +++ redshift/whatsapp_notification.sh | 516 +++ retention.log | 16 + retention/etlRetention_Metabase.log | 0 retention/ltv_cohorts_retention.sh | 406 +++ revenue_leakage.log | 10 + revenue_leakage/revenue_leakage.log | 10 + revenue_leakage/revenue_leakage.sql | 381 +++ revenue_leakage/revenue_leakage_code.sh | 385 +++ test.sh | 14 + .../.etlwarehouseAnalysis.sh.swp | Bin 0 -> 20480 bytes warehouseAnalysis/1 | 23 + warehouseAnalysis/OM_New_VS_Existing.sql | 32 + warehouseAnalysis/OM_Test_Launched.sql | 14 + warehouseAnalysis/etl_appsflyer.log | 16 + warehouseAnalysis/etlwarehouseAnalysis.log | 22 + warehouseAnalysis/etlwarehouseAnalysis_v1.sh | 1005 ++++++ warehouseAnalysis/etlwarehouseAnalysis_v2.sh | 1190 +++++++ warehouseAnalysis/etlwarehouseAnalysis_v3.sh | 1253 +++++++ warehouseAnalysis/etlwarehouseAnalysis_v4.sh | 1407 ++++++++ warehouseAnalysis/install_uninstall_rate.sql | 69 + warehouseAnalysis/retention.log | 14 + warehouseAnalysis/retention.sql | 470 +++ warehouseAnalysis/retention_code.sh | 474 +++ warehouseAnalysis/warehouseAnalysis.sql | 1392 ++++++++ 214 files changed, 109095 insertions(+) create mode 100644 .etlTransaction_job.sh.swp create mode 100644 .idea/.gitignore create mode 100644 .swp create mode 100644 AM_ACQ/AM_Dashboard create mode 100644 AM_ACQ/AM_Dashboard.log create mode 100644 AM_ACQ/AM_dashboard.sql create mode 100644 Am_recommendation/am_recommendation.log create mode 100644 Am_recommendation/am_recommendation.sql create mode 100644 Am_recommendation/etl_am_recommendation.sh create mode 100644 DB_funnel_new_users.log create mode 100644 Final_GM_v1.sql create mode 100644 Gm_dashboard/.etl_gmdashboard_new_v4.sh.swo create mode 100644 Gm_dashboard/2q create mode 100644 Gm_dashboard/Final_GM_v1.sql create mode 100644 Gm_dashboard/Final_GM_v2.sql create mode 100644 Gm_dashboard/OOS_code.log create mode 100644 Gm_dashboard/etlDemandForecasting.log create mode 100644 Gm_dashboard/etlTransaction_job_v1.log create mode 100644 Gm_dashboard/etlTransaction_job_v2.log create mode 100755 Gm_dashboard/etl_gmdashboard_new_v2.sh create mode 100644 Gm_dashboard/etl_gmdashboard_new_v3.sh create mode 100644 Gm_dashboard/etl_gmdashboard_new_v5.sh create mode 100644 Gm_dashboard/etl_gmdashboard_v1.sh create mode 100644 OM_New_VS_Existing.log create mode 100644 OM_New_VS_Existing.sh create mode 100644 OM_Test_Launched.log create mode 100644 OM_Test_launch.sh create mode 100644 OOS/DB_funnel_new_users.log create mode 100644 OOS/DB_funnel_new_users.sh create mode 100644 OOS/DB_funnel_new_users.sql create mode 100644 OOS/OOS_code.log create mode 100644 OOS/OOS_code.sh create mode 100644 OOS/OOS_code.sql create mode 100644 OOS/sample.log create mode 100644 OOS/sample.sh create mode 100644 OOS/sample.sql create mode 100644 OOS_code.log create mode 100644 README.md create mode 100644 a.out create mode 100644 am_recommendation.log create mode 100644 appsflyer/etl_appsflayer.log create mode 100644 appsflyer/etl_appsflyer.log create mode 100644 appsflyer/etl_appsflyer.sh create mode 100644 appsflyer/etl_appsflyer.sql create mode 100644 appsflyer/etl_inappevent_appsflyer.sh create mode 100644 appsflyer/etl_inappevent_appsflyer.sql create mode 100644 clevertap_funnel_reports.log create mode 100644 clevertap_funnel_reports/clevertap_funnel_reports.log create mode 100644 clevertap_funnel_reports/clevertap_funnel_reports.sql create mode 100644 clevertap_funnel_reports/etl_cart_conversion.sh create mode 100644 consignement/consignement_code.sql create mode 100644 consignement_code create mode 100644 consignement_code.log create mode 100755 cron.sh create mode 100644 demandForecasting/Demand_forecasting.sql create mode 100644 demandForecasting/Retention_Metabase.sql create mode 100644 demandForecasting/db_funnel_code.sql create mode 100644 demandForecasting/etlDemandForecasting.log create mode 100644 demandForecasting/etlDemandForecasting.sh create mode 100644 demandForecasting/to_run_etlDemandForecasting.sh create mode 100644 etlTransaction_job.log create mode 100644 etlTransaction_job.sh create mode 100644 etlTransaction_job_v2.log create mode 100644 etlTrasnsaction_job.log create mode 100644 etltransaction_job_final.sh create mode 100644 etlwarehouseAnalysis.log create mode 100644 home_page_pdp_flash/home_page_pdp_flash.log create mode 100644 home_page_pdp_flash/home_page_pdp_flash.sql create mode 100644 home_page_pdp_flash/home_page_pdp_flash_script.sh create mode 100644 install_uninsatll_rate.sh create mode 100644 loyalty_point/loyalty_point.sh create mode 100644 loyalty_point/loyalty_point.sql create mode 100644 mongo_rdash360.log create mode 100644 new_users/conversion_report.sh create mode 100644 new_users/conversion_report.sql create mode 100644 new_users/db_funnel_code.sh create mode 100644 new_users/dbfunnel_Metabase.log create mode 100644 new_users/reseller_business_metrics.sh create mode 100644 new_users/reseller_business_metrics_code.sql create mode 100644 new_users/reseller_business_metrics_code_Metabase.log create mode 100644 postgresql/360rdash/360_rdash_etl.sql create mode 100644 postgresql/360rdash/rdash_etl_cron.sh create mode 100644 postgresql/am_dashboard/am_dashboard.sh create mode 100644 postgresql/am_dashboard/am_dashboard.sql create mode 100644 postgresql/am_recommendation/am_recommendation_etl.sql create mode 100644 postgresql/am_recommendation/am_recommendation_etl_cron.sh create mode 100644 postgresql/am_recommendation/whatsapp_campaign.sh create mode 100644 postgresql/am_recommendation/whatsapp_campaign.sql create mode 100644 postgresql/appflyer/appflyer_etl.sh create mode 100644 postgresql/appflyer/appflyer_etl.sql create mode 100644 postgresql/appflyer/etl_inappevent_appsflyer.sql create mode 100644 postgresql/appflyer/in_app_appsflyer_etl.sh create mode 100644 postgresql/card_abandment/cart_abandment_etl.sh create mode 100644 postgresql/card_abandment/cart_abandment_etl.sql create mode 100644 postgresql/city_mapping_for_online_lead_db.sh create mode 100644 postgresql/city_mapping_for_online_lead_db.sql create mode 100644 postgresql/cogs_alert/cogs_increase_alert.sh create mode 100644 postgresql/cogs_alert/increase_cogs_alert.sql create mode 100644 postgresql/consignement/consignment_sales_report.sh create mode 100644 postgresql/consignement/consignment_sales_report.sql create mode 100644 postgresql/consignement/pwd create mode 100644 postgresql/demand_forecasting/Demand_forecasting.sql create mode 100644 postgresql/demand_forecasting/demand_forecasting_etl.sh create mode 100644 postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sh create mode 100644 postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sql create mode 100644 postgresql/gm_code/.gm_etl_cron.sh.swp create mode 100644 postgresql/gm_code/gm_etl_cron.sh create mode 100644 postgresql/gm_code/gm_etl_cron.sql create mode 100644 postgresql/h create mode 100644 postgresql/home_page_pdp_flash/home_page_pdp_flash.sh create mode 100644 postgresql/home_page_pdp_flash/home_page_pdp_flash.sql create mode 100644 postgresql/loyalty_point/loyalty_point_etl.sh create mode 100644 postgresql/loyalty_point/loyalty_point_etl.sql create mode 100644 postgresql/new_user_funnel/new_user_funnel_etl.sh create mode 100644 postgresql/new_user_funnel/new_user_funnel_etl.sql create mode 100644 postgresql/notification_dashboard_anchor_brand/notification_dashboard_anchor_brand.sh create mode 100644 postgresql/notification_dashboard_anchor_brand/notification_doashboard_anchor_brand.sql create mode 100644 postgresql/offender_dashboard/offender_dashboard.sh create mode 100644 postgresql/offender_dashboard/offender_dashboard.sql create mode 100644 postgresql/oos_restock/oos_restock_etl.sh create mode 100644 postgresql/oos_restock/oos_restock_etl.sql create mode 100644 postgresql/product_matrics/product_matrics_etl.sh create mode 100644 postgresql/product_matrics/product_matrics_etl.sql create mode 100644 postgresql/retention_code/retention_code_etl.sh create mode 100644 postgresql/retention_code/retention_code_etl.sql create mode 100644 postgresql/retention_code/retention_code_etl_new.sh create mode 100644 postgresql/revenue_leakage/revenue_leakage_etl.sh create mode 100644 postgresql/revenue_leakage/revenue_leakage_etl.sql create mode 100644 postgresql/sold_product/sold_product.sh create mode 100644 postgresql/sold_product/sold_product.sql create mode 100644 product_metrics/product_metrics.sh create mode 100644 product_metrics/product_metrics.sql create mode 100644 query.log create mode 100644 rdash/Mongo_rdash360_dashboard.sh create mode 100644 rdash/mongo_rdash360.log create mode 100644 rdash/mongo_rdash360.sql create mode 100644 redshift/.gm_dashboard.sh.swp create mode 100644 redshift/.gm_dashboard_v1.sh.swp create mode 100644 redshift/Appflyer_etl.sh create mode 100644 redshift/Product_metrics.sh create mode 100644 redshift/Product_metrics_2.sh create mode 100644 redshift/am_dashboard.sh create mode 100644 redshift/am_recommendation.sh create mode 100644 redshift/anchor_brand_notification_dashboard.sh create mode 100644 redshift/cart_abandment.sh create mode 100644 redshift/city_mapping_for_online_lead.sh create mode 100644 redshift/cogs_alert.sh create mode 100644 redshift/consignment_sales_report.sh create mode 100644 redshift/consignment_sales_report_v1.sh create mode 100644 redshift/demand_forcasting.sh create mode 100644 redshift/distributed_warehouse_dashboard.sh create mode 100644 redshift/gamification_etl.sh create mode 100644 redshift/gm_dashboard.sh create mode 100644 redshift/gm_dashboard_new.sh create mode 100644 redshift/home_page_pdp_flash.sh create mode 100644 redshift/inapp_appflayer_etl.sh create mode 100644 redshift/inapp_appflayer_etl2.sh create mode 100644 redshift/loyalty_point.sh create mode 100644 redshift/new_user_funnel_report.sh create mode 100644 redshift/offender_dashboard.sh create mode 100644 redshift/oos_stock_restock.sh create mode 100644 redshift/payment_dashboard.sh create mode 100644 redshift/rdash_dashboard.sh create mode 100644 redshift/retention_dashboard.sh create mode 100644 redshift/revenue_leakage.sh create mode 100644 redshift/sold_product.sh create mode 100644 redshift/sql_code/360_rdash_etl.sql create mode 100644 redshift/sql_code/Demand_forecasting.sql create mode 100644 redshift/sql_code/am_dashboard.sql create mode 100644 redshift/sql_code/am_recommendation_etl.sql create mode 100644 redshift/sql_code/appflyer_etl.sql create mode 100644 redshift/sql_code/cart_abandment_etl.sql create mode 100644 redshift/sql_code/consignment_sales_report.sql create mode 100644 redshift/sql_code/distributed_warehouse_etl_cron.sql create mode 100644 redshift/sql_code/etl_inappevent_appsflyer.sql create mode 100644 redshift/sql_code/gamification_etl.sql create mode 100644 redshift/sql_code/gm_etl_cron.sql create mode 100644 redshift/sql_code/home_page_pdp_flash.sql create mode 100644 redshift/sql_code/increase_cogs_alert.sql create mode 100644 redshift/sql_code/loyalty_point_etl.sql create mode 100644 redshift/sql_code/new_user_funnel_etl.sql create mode 100644 redshift/sql_code/notification_doashboard_anchor_brand.sql create mode 100644 redshift/sql_code/offender_dashboard.sql create mode 100644 redshift/sql_code/oos_restock_etl.sql create mode 100644 redshift/sql_code/payment_dashboard.sql create mode 100644 redshift/sql_code/product_matrics_etl.sql create mode 100644 redshift/sql_code/retention_code_etl.sql create mode 100644 redshift/sql_code/revenue_leakage_etl.sql create mode 100644 redshift/sql_code/sold_product.sql create mode 100644 redshift/sql_code/whatsapp_campaign.sql create mode 100644 redshift/whatsapp_notification.sh create mode 100644 retention.log create mode 100644 retention/etlRetention_Metabase.log create mode 100644 retention/ltv_cohorts_retention.sh create mode 100644 revenue_leakage.log create mode 100644 revenue_leakage/revenue_leakage.log create mode 100644 revenue_leakage/revenue_leakage.sql create mode 100644 revenue_leakage/revenue_leakage_code.sh create mode 100644 test.sh create mode 100644 warehouseAnalysis/.etlwarehouseAnalysis.sh.swp create mode 100644 warehouseAnalysis/1 create mode 100644 warehouseAnalysis/OM_New_VS_Existing.sql create mode 100644 warehouseAnalysis/OM_Test_Launched.sql create mode 100644 warehouseAnalysis/etl_appsflyer.log create mode 100644 warehouseAnalysis/etlwarehouseAnalysis.log create mode 100644 warehouseAnalysis/etlwarehouseAnalysis_v1.sh create mode 100644 warehouseAnalysis/etlwarehouseAnalysis_v2.sh create mode 100644 warehouseAnalysis/etlwarehouseAnalysis_v3.sh create mode 100644 warehouseAnalysis/etlwarehouseAnalysis_v4.sh create mode 100644 warehouseAnalysis/install_uninstall_rate.sql create mode 100644 warehouseAnalysis/retention.log create mode 100644 warehouseAnalysis/retention.sql create mode 100644 warehouseAnalysis/retention_code.sh create mode 100644 warehouseAnalysis/warehouseAnalysis.sql diff --git a/.etlTransaction_job.sh.swp b/.etlTransaction_job.sh.swp new file mode 100644 index 0000000000000000000000000000000000000000..1a0c8c0537344e1721c5424eb5f2c297a9a8b7f2 GIT binary patch literal 24576 zcmeI4eT*Ds9mj_k0igv2F%VIo9&PrP-t67p?Y(T8t?b?HwFlR`^Io7ubo1=Y?CrF> zGuxfnYmcZHDNO*0h$fnVe`unG#MdZ>wirx-7%?#hB_^7HZxBTh5E79H_4_>Yvd_%U zZLbBbFSE&~d;2`kJTJfJWuE8v`#ml7T{pRdjHLP{eBL2RAJ~&Gy?pVZGk&{Ok~F1X zZdzJ{MRoeEvHk8S*tf~Eu|aM$Gr@Dm4<2THcP#l zU0rBU!=j3Q3B(fkFO`5@KToQ3B(eJB@jy>mOw0lSOT#GVhO|&h$ZkpDFLcV(j{owBkUjy`@eVof8`=c zdKvr`tbqNX2CCpA;KSfj@S6+qEI0;^f+OHoZ~)u_7C|q#2t2=Dl70+60bW}tNj7-# z0!caqZUY(c%=wb^d+Yij4PYzi24{gk zpC?H_13v-Z05^jYNQ2A3v2!Kqeqezcz!uO2UONZv14qC;U83wRzJ1xLVP za3|OUra&580ggg(aWA+97@!|q0$zqf;wexAlVCG=H+T|yiEo2Xfqh^-cm{pL`sY}* zvVw#@h3Rq9&}^z#i}i-CXgyLW0smQ;nlF&il$bCw-SyiG(?o$UNB3yT+S4TYd9q9! zN`*F(BsunwzG?t$)v=z@9;^w@!Nex@KS30)EOVpMT zYcOxes8>_n)@nsvMGGcU_~I3vNUfT|s@3zJc$4*dE9C1{<*LzBXh*99JGKNJe9FR& zdUjR0BJrtL52vPKHzI~+KBeuqwT3~f7-~Jzd||3Ewm`-w=NBfY@rNyV zKB?5kJRO@W8UMvW~wl;K(3jYoF)yb8MJ6NRIO30Q3Ki5Fojd9Zt?aM@o(KU$QYTKCVAFI z{6e6rVa|++zqdFeB9FQrAckSbB8RZB~dB+0ju@V-HQ4Gk?;i_~Td3Tu6m zY|SM$Hnh6gu*dN`!JfgSor5Ik3acAyyxp{NiCLZ$>skAMgJk%4AI6R#U#08GDM?1v^BC6&Jt=cO^FHa^5 zr$@puVs_;!wJJr6Ry97XTJ6|vp^Gr&8gQp8)~Q`7+W6I-$0BcO)he2#8)dU6!P+rR zYg(dbVQ#UI#LL2s<_&}t0ZSsN7^g(Kz}%IvcMo9lk;SHW`RsEPE9%Qvcv=%Bz%&v@gc6W9eO*u ztE2nV8?$b&`&}7c)uMXRUkioai(dU)IcdupCMdNjay!Aw7k+}RdXv-Bg*hUwdkXTZ znoHu!S9j#?ta#0OU29-S;x)jwInK{9{&UO8WjD$eP%pbu(@-&O1G(~TRgw=~;>NR< zvZ^()EE(Fqz$(VNlQ{dXvB2()30l<@y+*6u{rx@t$s}3B#*44Vt1KL2DL&ktbudvX z4`l|m?65pSGb3^)JB+^uhK6M=J2IRZ$qo3+FPGXrWVn;q2B9qM4|)#a#l{bXy=H{3VSS1xJt;7DIu&J3hS z(X+hTRhm3}mkG!NtkB!Z?|jncIaKY*T0lA29I}!ZajarBK?q(4lVjW63eC>UEwGot!VHr0 zWD%8;uH4XD(v4p1EGrT_|6hu8_hG=!|EM(oyo9s;Q{ZuM0L%h*-hU3~_%8x>zBj>6 za5d-$mw|uaZ2v5H5Q3B(eJB@jy>mOw0lSOT#G>ek*W>8e1E$79W!J#9wWZ8WT|_RE-ile%dfV>&PUuV_zHLWwIh|rH z*h(U90Yf)~vk2nWmWc!frl#1WM)!AnT10l%OOkQ}&GA&}O-na4%YrG2HGweIkmMZx z@vnCXMz9~|>P2g`v7{UR>6j3zsye%|%+$uN!d!^$puE+}WzOl`jkW~2Q&gg9?+<1 zOJ-vwTm@IH7FJ`HVEj$^ld1yog@Ju~N8EWG|_g?oa9wpmcJCP|5@^{8mz$(5jz zb{=)i7Q85LGzJYFB+=j3pJun_`_jIWmUJ>_xb;Yqan~T@b~v|Tj{7AO|HDRt*AP!> z&a6;8fo}%l=l^fw%)ObN$8rAm{Ogb7tp5gh9Xtje1$TocVD|rQAOkK3XM#WA?EiD{ z5cm=}2(AO;Kn9nC$56(8z{*O1`%vx__$ki#Hh2N{{4wx-*zB(Y=Yoe}pFac|;BnaG zH-In0244Xw@Ct0~%OD5d1)hOz{TMg|ZUc3&2!_D>!P($B*x7#x9tHPy3S1Y!xq z5{M-bOCXj&EP+@8|F;qd?YdWWv|?v4&k?|Py9pi9!)Jh1GgILH+Ljp$6b|7;G2s?w zDOxu_e2zK}95^FV%RlAz@F>@}BN&w=+~EU(VxC+n05?G~3>uL=n$5KWfnZJ5n>8`S zIRv*3tEdQbN@Kn$j>aNXn4@mk2XBIk2`!YE)#{oEI4D}Ssq6_W8F)DJ85FY#suRSR zXyphp2{>DcPbjouhn(;fDBYm>I z{|X!i4}yc>R&W691Y5ucunru@fa}5K;8o}io(7MAuYu2kWkA7p&^bW_s*TLt&I`C2G9u#mjconkzHDEGb2R*_;$oG73dk1~Nf5ciU zoyJ<6&azw0KAp8Von`+kB;YFLpG)qWW-eJp+Jr)#z~7v^T*BkhxoiR*vE%C4 z=2eRKh@;?#H#)nVBIsGYs4Zxe#B{hk&m8W4xE{4A)%D@zp75S^AmC_UW>yNqoi_t+ z`~>+3UaOAVrklf34eI>1^|`HEHP$FJx-EiVQF#jmK$!S^5i>txIOUJcY@1&wPR~pi zXW=Y8lOXt?T8%(`n6t3sWc=@`%WCxqz3*FllF<~uzQYMV=uzMvfCw=h=PvU%A*NX6;;Oh8DQm05fd2YvV*!ZP@7SK)h(? zj9P}0Flwr>k;46zCygqc*)80T;g&D9thVfHK?7F6w&oj5oyAtPuKBlsq^PSr#JkHA zepNV~B3w&y0@$?oZY1H{z%C-W5vp{;Ec`BZZ#KDWd+=scU~D;y`zjZuc)=~te5W41 znbZ?xU^e(e)q4>I=T2*aw8rOHiqAxEO8PnQs|c-RYdw&%Wn?<)Q0lKQehI#~SS9_? zwHD+oL~X}aC-|gu<7-!2W<2zy^J4OB%Zdg1Ir)pOcr^9bkJj4K@g`E#DK2Vap7=sb z)KfURvAO17XT|v7v!Qj36J88X3NH%ZY*0E^VZ?opGjf%R#?H^Qgx5#Zt_Nj#Q!UBw z1O~Q2yl`<_gp#;5J92NuFEM6Xa|eloFVL%qs+Y^XRb0>#f2;o?yn=xU(ThWPR&?FT0*XYO`;>ks&JzT zRpB4jX76uy0nFdzh?OV&-gaV6TWVHmqvbo`=0ON<1c=LvbM*uL<6R3$^1II7FX^AN C|G_u_ literal 0 HcmV?d00001 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.swp b/.swp new file mode 100644 index 0000000000000000000000000000000000000000..d501e158e693f06a31e2bb20e9739bb0d823a5e2 GIT binary patch literal 12288 zcmeI2&2G~`5XZOND)A+_aazKmjTAf0M@xE0rK%?c6&H%c1#7)Kj<>SC+ue0Tt4h2T z620>Z+;|Zv65}=jsU;Gpib%84KV#3VXJ>xSCCa25Z(r>5QQT*^9yT9WYtJ(VvLDqR z+}yXwY8&-7qOCzRih9FX*vOkY>Y5%@m{1iQjMVkviK0SkFg8XIqEa&GS3onppuCI5g-CYfCvx)B0vO)01;Rc0@fcDypnudFsC@tzOLcU(Im)vSL;L*tobr5gEJ|@|DlzrV=rV8SpT{Z{s?-%%X(dV+ zi%JwHK}*lAabdhU9Bgwdyq_AU#+yfbI@^MicY~MZWO&+&vTkll=+1|!yV9BJ(4^g| zaF83!zuTnq+TZlVOW+En`zA_&!uE!xaq&_y!7aNm_edf%i!X!QnSI KQ7L1Fx7aT?K1Xr@ literal 0 HcmV?d00001 diff --git a/AM_ACQ/AM_Dashboard b/AM_ACQ/AM_Dashboard new file mode 100644 index 0000000..c7c0b3c --- /dev/null +++ b/AM_ACQ/AM_Dashboard @@ -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 diff --git a/AM_ACQ/AM_Dashboard.log b/AM_ACQ/AM_Dashboard.log new file mode 100644 index 0000000..265fecb --- /dev/null +++ b/AM_ACQ/AM_Dashboard.log @@ -0,0 +1,9 @@ +DROP TABLE +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +DROP TABLE diff --git a/AM_ACQ/AM_dashboard.sql b/AM_ACQ/AM_dashboard.sql new file mode 100644 index 0000000..d1eb57d --- /dev/null +++ b/AM_ACQ/AM_dashboard.sql @@ -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; + + + + diff --git a/Am_recommendation/am_recommendation.log b/Am_recommendation/am_recommendation.log new file mode 100644 index 0000000..992f3ef --- /dev/null +++ b/Am_recommendation/am_recommendation.log @@ -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 diff --git a/Am_recommendation/am_recommendation.sql b/Am_recommendation/am_recommendation.sql new file mode 100644 index 0000000..a963a46 --- /dev/null +++ b/Am_recommendation/am_recommendation.sql @@ -0,0 +1,2735 @@ + + + + +DROP TABLE public.raw_ingredient_split_name; +CREATE TABLE public.raw_ingredient_split_name AS +SELECT * , + split_part(concern,', ',1) concern1, + split_part(concern,', ',2) concern2, + split_part(concern,', ',3) concern3, + split_part(concern,', ',4) concern4, + split_part(concern,', ',5) concern5, + split_part(top_marketing_ingredients ,', ',1) ingredient1, + split_part(top_marketing_ingredients,', ',2) ingredient2, + split_part(top_marketing_ingredients,', ',3) ingredient3, + split_part(top_marketing_ingredients,', ',4) ingredient4, + split_part(top_marketing_ingredients,', ',5) ingredient5, + split_part(top_marketing_ingredients,', ',6) ingredient6, + split_part(top_marketing_ingredients,', ',7) ingredient7, + split_part(top_marketing_ingredients,', ',8) ingredient8, + split_part(top_marketing_ingredients,', ',9) ingredient9, + split_part(top_marketing_ingredients,', ',10) ingredient10, + split_part(top_marketing_ingredients,', ',11) ingredient11 +FROM + (SELECT DISTINCT group_base, + sku , + lower(concern) concern, + lower(top_marketing_ingredients) top_marketing_ingredients, + CASE + WHEN concern='' + AND top_marketing_ingredients='' THEN 1 + END flag + FROM public.ingredients_base_table_final) A +WHERE flag IS NULL; + + DROP TABLE public.raw_ingredient_mapping_table_stage; + + +CREATE TABLE public.raw_ingredient_mapping_table_stage AS +SELECT A.* , + B.group_base mapping_group_base, + B.concern mapping_concerns, + B.top_marketing_ingredients mapping_ingredients +FROM public.raw_ingredient_split_name A +LEFT JOIN + (SELECT * + FROM public.raw_ingredient_split_name) B ON 1=1; --udf_string_campare(A.ingredient1,B.top_marketing_ingredients) = A.ingredient1; + +DROP TABLE public.raw_ingredient_mapping_table_stage2; + + +CREATE TABLE public.raw_ingredient_mapping_table_stage2 AS +SELECT * , + CASE + WHEN udf_string_campare(concern1,mapping_concerns) <>'' + AND udf_string_campare(concern1,mapping_concerns) <> 'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern1, + CASE + WHEN udf_string_campare(concern2,mapping_concerns) <>'' + AND udf_string_campare(concern2,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern2, + CASE + WHEN udf_string_campare(concern3,mapping_concerns) <>'' + AND udf_string_campare(concern3,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern3, + CASE + WHEN udf_string_campare(concern4,mapping_concerns) <>'' + AND udf_string_campare(concern4,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern4, + CASE + WHEN udf_string_campare(concern5,mapping_concerns) <>'' + AND udf_string_campare(concern5,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern5, + REGEXP_COUNT(concern ,', ')+1 total_count_concern, + CASE + WHEN udf_string_campare(ingredient1,mapping_ingredients) <>'' + AND udf_string_campare(ingredient1,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient1, + CASE + WHEN udf_string_campare(ingredient2,mapping_ingredients) <>'' + AND udf_string_campare(ingredient2,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient2, + CASE + WHEN udf_string_campare(ingredient3,mapping_ingredients) <>'' + AND udf_string_campare(ingredient3,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient3, + CASE + WHEN udf_string_campare(ingredient4,mapping_ingredients) <>'' + AND udf_string_campare(ingredient4,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient4, + CASE + WHEN udf_string_campare(ingredient5,mapping_ingredients) <>'' + AND udf_string_campare(ingredient5,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient5, + CASE + WHEN udf_string_campare(ingredient6,mapping_ingredients) <>'' + AND udf_string_campare(ingredient6,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient6, + CASE + WHEN udf_string_campare(ingredient7,mapping_ingredients) <>'' + AND udf_string_campare(ingredient7,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient7, + CASE + WHEN udf_string_campare(ingredient8,mapping_ingredients) <>'' + AND udf_string_campare(ingredient8,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient8, + CASE + WHEN udf_string_campare(ingredient9,mapping_ingredients) <>'' + AND udf_string_campare(ingredient9,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient9, + CASE + WHEN udf_string_campare(ingredient10,mapping_ingredients) <>'' + AND udf_string_campare(ingredient10,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient10, + CASE + WHEN udf_string_campare(ingredient11,mapping_ingredients) <>'' + AND udf_string_campare(ingredient11,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient11, + REGEXP_COUNT(top_marketing_ingredients ,', ')+1 total_count_ingredient +FROM public.raw_ingredient_mapping_table_stage +WHERE group_base <> mapping_group_base; + + DROP TABLE public.raw_ingredient_mapping_table_stage3; + + +CREATE TABLE public.raw_ingredient_mapping_table_stage3 AS +SELECT *, + flag_concern1+flag_concern2+flag_concern3+flag_concern3+flag_concern3 total_concern_match, + flag_ingredient1+flag_ingredient2+flag_ingredient3+flag_ingredient4+flag_ingredient5+flag_ingredient6+flag_ingredient7+flag_ingredient8+flag_ingredient9+flag_ingredient10+flag_ingredient11 total_ingredient_match +FROM public.raw_ingredient_mapping_table_stage2 ; + + +DROP TABLE IF EXISTS public.raw_ingredient_mapping_table_stage4; + + +CREATE TABLE public.raw_ingredient_mapping_table_stage4 AS +SELECT *, (total_concern_match*100/total_count_concern) Percent_concern_match, (total_ingredient_match*100/total_count_ingredient) Percent_ingredient_match , + CASE WHEN concern='' AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND top_marketing_ingredients='' THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + END percent_flag +FROM public.raw_ingredient_mapping_table_stage3; + + +Drop table if exists public.raw_ingredient_mapping_table; + +Create table public.raw_ingredient_mapping_table +as +SELECT group_base, + sku, + concern, + top_marketing_ingredients, + mapping_group_base, + mapping_concerns, + mapping_ingredients, + percent_concern_match, + Percent_ingredient_match +FROM public.raw_ingredient_mapping_table_stage4 +WHERE percent_flag=1 +ORDER BY group_base , + percent_concern_match DESC , + Percent_ingredient_match DESC; + + +DROP TABLE IF EXISTS public.sku_in_stock; + + +CREATE TABLE public.sku_in_stock AS +SELECT sku , + sum(coalesce(F.stock_limit,0)) 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; + + --------------------------------------------------------------task 1 ---------------- + +drop table if exists public.recommendation_power_seller_base_alt; + +create table public.recommendation_power_seller_base_alt +as +select distinct email from public.om_acquisition_mapping_list +union select distinct "email id" from public.AM_REVENUE_LEAKAGE_LIST ; + + +INSERT INTO public.recommendation_power_seller_base_alt + values ('cindyvioleta29@yahoo.com'), +('sherryfannydesita@gmail.com'), +('masintansilalahi@yahoo.co.id'), +('rumahskincarebpp@gmail.com'), +('puturisca@gmail.com'), +('sieny.go@gmail.com'), +('oki.zhang1988@gmail.com'), +('jeremytantono@yahoo.co.id'), +('yuke.cipta@gmail.com'), +('kwonvic@hotmail.com'), +('ervinafransiska95@gmail.com'), +('indahamaliasasmita@gmail.com'), +('mirosebeauty@gmail.com'), +('fourrshoptarakan@gmail.com'), +('stuffbyms@gmail.com'), +('cindyrissa14@gmail.com'), +('lieke1212@gmail.com'), +('jelitacosmetic.yk@gmail.com'), +('metafuji@gmail.com'), +('destiyapradhani@gmail.com'), +('liepinghau@gmail.com'), +('jennifer_kambuno@yahoo.com'), +('y35beauty@gmail.com'), +('nisamulya@gmail.com'), +('issadella@yahoo.co.id'), +('hartono.henny@yahoo.co.id'), +('a2335572@gmail.com'), +('navisgusmiati24@gmail.com'), +('djiwandimelany@gmail.com'), +('colinakoh178@gmail.com'), +('fairybeaute17@gmail.com'), +('fajri.vanbio95@gmail.com'), +('zidney_yao@yahoo.com'), +('chandraalex305@gmail.com'), +('elly.elisha@gmail.com'), +('erlinapr13@gmail.com'), +('mechan_06@hotmail.com'), +('limvincents@hotmail.com'), +('tjuganteng@gmail.com'), +('sralaydrus96@yahoo.com'), +('duwik_tonx@yahoo.co.id'), +('adelshopadelia@gmail.com'), +('shierlysusanto2405@gmail.com'), +('murni.cosmetics55@gmail.com'), +('stg_purchasing_melisa@yahoo.com'), +('asri.nilasari@watsons.co.id'), +('wayanndiana@gmail.com'), +('emailtedy@gmail.com'), +('Shantifm@hotmail.com'), +('shintachristiani2911@gmail.com'), +('dotienigloo@gmail.com'), +('mirandadessy95@gmail.com'), +('pangeranpetirr@gmail.com'), +('mariamellysacandra@ymail.com'), +('evitamalaa@ymail.com'), +('lindadarwinp@gmail.com'), +('dyfabandung@gmail.com'), +('yestastore@gmail.com'), +('outletmadanibeauty@gmail.com'), +('mrs.kumara@yahoo.com'), +('cindylum23@gmail.com'), +('fairy_love90@ymail.com'), +('fajarcosmetikpekanbaru@gmail.com'), +('alapstorekosmetik@gmail.com'), +('marlinabahnur@yahoo.co.id'), +('miswarderi61@gmail.com'), +('kristian_tjia@yahoo.co.id'), +('dessi.three@gmail.com'), +('rumahkurumahta@gmail.com'), +('nabilaalifia2000@gmail.com'), +('ullyfina@gmail.com'), +('tjindaidyandy@gmail.com'), +('shintayuu@gmail.com'), +('ratihpuji001@gmail.com'), +('shynobyshould@gmail.com'), +('rismawatisapu101@gmail.com'), +('caramelmadeby@gmail.com'), +('piyamapoppy@gmail.com'), +('scarlettwhitening.manado@gmail.com'), +('makeupbliss.id@gmail.com'), +('agnyjmpardosi@gmail.com'), +('sherbeautee@gmail.com'), +('yfbeauty99@gmail.com'), +('cmstadulako36@gmail.com'), +('lizalgs97@gmail.com'), +('emailnyawabisabi@gmail.com'), +('alvhiraalamrih@gmail.com'), +('megasyaf10@gmail.com'), +('ismisarisapitri6@gmail.com'), +('realrima91@gmail.com'), +('hestynorata74@gmail.com'), +('tokopinkbox@gmail.com'), +('melly.angelica@sils.co.id'), +('baguzokee@gmail.com'), +('vandy_hood@yahoo.com'), +('stevenwerencius15@gmail.com'), +('qurniaulan123@gmail.com'), +('ivanalystia21@gmail.com'), +('claralverinas@gmail.com'), +('riaarddd@gmail.com'), +('yayz_bubble@yahoo.com'), +('elsa.rivani@rocketmail.com'), +('feyrenpusura@gmail.com'), +('gunz_r@yahoo.com'), +('asrularman077@gmail.com'), +('hellojuwitaang@gmail.com'), +('bernike.daniel@gmail.com'), +('ayuerawati1993@gmail.com'), +('sudutcantik03@gmail.com'), +('norio.agnes@gmail.com'), +('smarch2u@gmail.com'), +('nunungokta8@gmail.com'), +('siaocie.tw@gmail.com'), +('sandrachen20@icloud.com'), +('siti.komariiah@gmail.com'), +('gustine.fu999@gmail.com'), +('ennysetiono@msn.com'), +('sartikanganro@gmail.com'), +('mirandahabibie@gmail.com'), +('tokodahliapadang@gmail.com'), +('natanaelsbrn@gmail.com'), +('jelitacosmetic@galeri.com'), +('rkkosmetikviral.anj@gmail.com'), +('sancaytulak26@gmail.com'), +('kelvinhosen@gmail.com'), +('elnazulma@gmail.com'), +('jevrihuang@gmail.com'), +('araitsoke@gmail.com'), +('haju.nutrifood@gmail.com'), +('harwindaharis99@gmail.com'), +('saherasukur92@gmail.com'), +('monamelosi05@gmail.com'), +('pramistiseptarinda@gmail.com'), +('rianadewi15746@gmail.com'), +('frivaldi08@gmail.com'), +('sausankho86@gmail.com'), +('3widjayanti@gmail.com'), +('prinzlina79@gmail.com'), +('restbeauty11@gmail.com'), +('kiarriyasatul@gmail.com'), +('surkiaa56@gmail.com'), +('dellikosmetik@gmail.com'), +('dinafebriana@gmail.com'), +('risnawatiyunus@yahoo.com'), +('pudoopy@gmail.com'), +('serbunganjuk@gmail.com'), +('linda_ho29@yahoo.com'), +('cassmgadm@gmail.com'), +('jennychateriner@gmail.com'), +('thasashop.id@yahoo.com'), +('diancagoodsbywitari@gmail.com'), +('chang3mu@icloud.com'), +('hana_kho@outlook.com'), +('jwl.skinofficial@gmail.com'), +('febri.ilham68@gmail.com'), +('gurl.shopaholic@gmail.com'), +('fenitan00@gmail.com'), +('nadrachanafie@gmail.com'), +('migibeauty1@gmail.com'), +('dewisusianti888@gmail.com'), +('nazwinadya@gmail.com'), +('andinimaylasari@gmail.com'), +('syahrani.cosmetic@gmail.com'), +('primasitaa@gmail.com'), +('spy_solution77@yahoo.com'), +('geeghina@gmail.com'), +('nida.syafa5@gmail.com'), +('financialelzamora@gmail.com'), +('irkosmetikdanskincare@gmail.com'), +('sherlinhosana@gmail.com'), +('sepdiarina@yahoo.com'), +('lumbangaol.ira@gmail.com'), +('aligunawan098@gmail.com'), +('ovarash@gmail.com'), +('rbeauty.storee@gmail.com'), +('cckosmetik77@gmail.com'), +('pinkcosmetich@gmail.com'), +('dhiniemsa@yahoo.com'), +('rulyindah1911@gmail.com'), +('elvina.6789@gmail.com'), +('moeyecosmetic99@gmail.com'), +('sana.19007@mhs.unesa.ac.id'), +('ikasusanti_baru@yahoo.com'), +('indaryani82@gmail.com'), +('hsyapon@gmail.com'), +('citra.ismaya@omniretail.co'), +('thevintagestrawberry@gmail.com'), +('ramadhani.kosmetik@gmail.com'), +('linawijaya2712@yahoo.com'), +('meilings0403@gmail.com'), +('nadyawkk@gmail.com'), +('prellasurabaya@gmail.com'), +('ziyyags@gmail.com'), +('stanley_oh@ymail.com'), +('andre1177az@gmail.com'), +('s.ekaputrirosalinda@gmail.com'), +('beautyland21@icloud.com'), +('javalotuscorp@gmail.com'), +('alxsuherman@gmail.com'), +('fenny3007.kwok@gmail.com'), +('leny_kurniawati@hotmail.com'), +('soputanlady10@gmail.com'), +('lampungbeauty2019@gmail.com'), +('thebeauteebox@gmail.com'), +('tfanixling00@gmail.com'), +('andilesmono06@gmail.com'), +('briggiteleorenza@gmail.com'), +('chanytatiara@gmail.com'), +('financeklinikpiramidajaya@gmail.com'), +('exacarolina@gmail.com'), +('wildhanhafid@gmail.com'), +('himakeup08@gmail.com'), +('riska.sofani@yahoo.com'), +('uciliana35@gmail.com'), +('agusjokin08@gmail.com'), +('madozai96@gmail.com'), +('flo_ver@yahoo.com'), +('dikinarasaki@gmail.com'), +('herul.batch3@gmail.com'), +('rudy.hartono2591@gmail.com'), +('vivi.plm187@gmail.com'), +('hallo@bencuan.id'), +('rahasiamama17@gmail.com'), +('stephzhengg@gmail.com'), +('shelinabeautycare@gmail.com'), +('joelchristianpamula@gmail.com'), +('rethamardjoen11@gmail.com'), +('pomadaivon@gmail.com'), +('divaparis28@gmail.com'), +('wulan.ranpatika@gmail.com'), +('mastoreyadi@gmail.com'), +('febi.muhi@gmail.com'), +('anggriawanferi1991@gmail.com'), +('feliciacolleen80@gmail.com'), +('istie.nodasekisan@gmail.com'), +('melliyniaanugrah@gmail.com'), +('dwiaziza98@gmail.com'), +('alfrianiyelsi@gmail.com'), +('adelia.vn@gmail.com'), +('alvina.iriady@gmail.com'), +('ratnaqisya03@gmail.com'), +('jelitacosmetic.galeri@gmail.com'), +('avex307@gmail.com'), +('rika.wulandaridwan@gmail.com'), +('srihartiniseptember1966@gmail.com'), +('theresia_margareths@yahoo.com'), +('kartikahs89@gmail.com'), +('yanuararifinnur@gmail.com'), +('luthfikhtr@gmail.com'), +('mshopper.id@gmail.com'), +('fifi.liu@yahoo.com'), +('christineeva07@gmail.com'), +('claralverina23@gmail.com'), +('12101na@gmail.com'), +('joanamarvelin.wongso@yahoo.com'), +('lylysiakwee@gmail.com'), +('andyirsaldi@gmail.com'), +('greceliautami@icloud.com'), +('rhayati48@gmail.com'), +('vidya@redroseindo.com'), +('ekanuraini131@gmail.com'), +('kimhiung168@gmail.com'), +('gitaporo@gmail.com'), +('aninditateresa@gmail.com'), +('lexonbeautyofficial@gmail.com'), +('novitaroshaputri@gmail.com'), +('theara.tiara@gmail.com'), +('selvi.chephy@gmail.com'), +('vhaans007@gmail.com'), +('tokosehat130@gmail.com'), +('mirayyacosmetics@gmail.com'), +('kusnen.sota@gmail.com'), +('hokimaxi2020@gmail.com'), +('keikolux@hotmail.com'), +('andreadewi19@gmail.com'), +('harlyskin@gmail.com'), +('viroe.nando@gmail.com'), +('aurelallshop@gmail.com'), +('kosmetikiis@gmail.com'), +('lifanny86@gmail.com'), +('nitamanda28@gmail.com'), +('cantiku8899@gmail.com'), +('sultanagengs3@gmail.com'), +('nurfiitriiyah@gmail.com'), +('nana01iphone@gmail.com'), +('mulyajaaya.motor96@gmail.com'), +('titarahmawati245@gmail.com'), +('marniatihinding@gmail.com'), +('erris.mahardika@gamal.men'), +('isti.qhatsah.nur@gmail.com'), +('dewilasolo@gmail.com'), +('aninditaputri30@gmail.com'), +('aryanto.firdaus@gmail.com'), +('novitaj68@gmail.com'), +('johan_breaker27@yahoo.com'), +('elsaberliya@gmail.com'), +('dewiyuant260693@gmail.com'), +('jeffrynoah@gmail.com'), +('rikasukmawar03@gmail.com'), +('yosua.sasauw1@gmail.com'), +('fitri.miauwh@gmail.com'), +('yuli120797@gmail.com'), +('kamala.tanray@gmail.com'), +('theresiad88@gmail.com'), +('isliana29@gmail.com'), +('chietra.anggraini93@gmail.com'), +('cesario2559@gmail.com'), +('the.secret.of.beauty.id@gmail.com'), +('elsacosmetics@yahoo.com'), +('evinrahmad2@gmail.com'), +('yuhyi.albadali@gmail.com'), +('darwin.saputra.ars@gmail.com'), +('akuntingtim2903@gmail.com'), +('wahyudinrazali4@gmail.com'), +('jwuysan@gmail.com'), +('ruus.silvana@gmail.com'), +('dianmarinaa@gmail.com'), +('dp.anggreani@gmail.com'), +('patricia.ang7@gmail.com'), +('medankosmetik89@gmail.com'), +('ayuputri.ap2201@gmail.com'), +('fikrizia13@gmail.com'), +('beautiva.skincare@gmail.com'), +('gjwproject@gmail.com'), +('pramudya.a.laksono@gmail.com'), +('ssshoptarakan@gmail.com'), +('bungalintong7@gmail.com'), +('ivanakaontole@gmail.com'), +('misel.delini@yahoo.com'), +('dwipujiati1995@gmail.com'), +('vdillahilda@gmail.com'), +('ellenanatasa@gmail.com'), +('bidarishabrina@gmail.com'), +('danimmm44@gmail.com'), +('evitsrikris@gmail.com'), +('gendisbeauty92@gmail.com'), +('eriana.dian0116@gmail.com'), +('eriskhfrd26@gmail.com'), +('jasmineniscalapandya@gmail.com'), +('eryryey03@gmail.com'), +('leessleepwear@gmail.com'), +('yoseptansil6@gmail.com'), +('natdailybeauty@gmail.com'), +('sidoarjosalonsupp@gmail.com'), +('auliarahmadanti77@gmail.com'), +('gustindw@gmail.com'), +('mamihcihuyyy@gmail.com'), +('narabeautytrk@gmail.com'), +('sandikaplg18@gmail.com'), +('20240010013@lspr.edu'), +('maynetav@gmail.com'), +('intandidi88@gmail.com'), +('jeniferpurnomo99@gmail.com'), +('dewisartika192@yahoo.co.id'), +('jokosus.susanto@gmail.com'), +('iswanto.1301@gmail.com'), +('kudo.palopo14@gmail.com'), +('micellangel@nuriglobal.com'), +('theeaanggraeny@gmail.com'), +('andi.or88@gmail.com'), +('rickyoktaviawan20@gmail.com'), +('yunibalisa661@gmail.com'), +('rikiscofieldsap@gmail.com'), +('feliciabeatrixtanner0407@gmail.com'), +('mon_taca@yahoo.com'), +('ardyanawidya@gmail.com'), +('d14na_nonic@yahoo.com'), +('kadirpnm@gmail.com'), +('mita.panji@gmail.com'), +('robbchriszzz13@gmail.com'), +('anicha8490@gmail.com'), +('galuh.acc.kediri@gmail.com'), +('tokosanjayakras.81@gmail.com'), +('vivian.effendi@gmail.com'), +('lisaderia@yahoo.com'), +('anggyfebryna17@gmail.com'), +('yuanita200989@gmail.com'), +('inggycitrasari@gmail.com'), +('musdalifa300396@gmail.com'), +('widdywedariasih3@gmail.com'), +('devinoariska06@gmail.com'), +('idealistina@yahoo.co.id'), +('kelsy18111@gmail.com'), +('elizchang88@gmail.com'), +('kavbeautyofficial@gmail.com'), +('archilagiska@gmail.com'), +('ginawati442@gmail.com'), +('revii.setiadi@gmail.com'), +('nisacuss@gmail.com'), +('eviana001075@gmail.com'), +('moanazone@gmail.com'), +('idabenangraja@gmail.com'), +('babybamscase@gmail.com'), +('bellasetiady@windowslive.com'), +('regina.amalia19@gmail.com'), +('hello.vebeauty@gmail.com'), +('ferinaindrayani@gmail.com'), +('yulia.cindy81@gmail.com'), +('ennakristiana@gmail.com'), +('nur.annisa0@gmail.com'), +('marindarara13@gmail.com'), +('sonyatiaratri2@gmail.com'), +('achmadfauzi1398@gmail.com'), +('fitrijean@gmail.com'), +('zul.hendra1297@gmail.com'), +('csbymagicskin@gmail.com'), +('ridadwi873@gmail.com'), +('grosirkosmetikpekanabaru@gmail.com'), +('gladys.agatha@gmail.com'), +('rahmizarti30@gmail.com'), +('mrs.sitiazizah@gmail.com'), +('paramitaqueena@yahoo.com'), +('uwiecute22@gmail.com'), +('slife557.cs@gmail.com'), +('adindazahra655@gmail.com'), +('lavibeautyandlifestyle@gmail.com'), +('ratna.pratiwi14.rp@gmail.com'), +('yunengazraapriani@gmail.com'), +('sheefaa84@gmail.com'), +('katherinelimanu21@gmail.com'), +('tutiyuniyanti@gmail.com'), +('kartinisarimah@gmail.com'), +('jonastampubolon123@gmail.com'), +('wuisancheni77@gmail.com'), +('shella.madirza888@gmail.com'), +('yulianisetiawan836@gmail.com'), +('janiceechenn@yahoo.com'), +('ferico.ridwan@gmail.com'), +('udjayamandiripgs@gmail.com'), +('budiwijaya1962@gmail.com'), +('rumahkosmetik001@gmail.com'), +('kinanbeautystore@gmail.com'), +('yuniraemah31@yahoo.co.id'), +('barokahbeaute@gmail.com'), +('shrlymarcella@gmail.com'), +('ikrt_2507@yahoo.com'), +('dikofebyan@gmail.com'), +('restyty.riezalina@yahoo.com'), +('ratukosmetik169@gmail.com'), +('faradillahif07@gmail.com'), +('sales@dewicosmetic.com'), +('kadek.arymas@gmail.com'), +('rahmaduwijayanti@gmail.com'), +('fanamaliaauthor@gmail.com'), +('vanessapratama24@gmail.com'), +('gangnamcosmetic109@gmail.com'), +('mayanglatuconsina@gmail.com'), +('lustremaiden@gmail.com'), +('ratihalexa@gmail.com'), +('anindyalarasp@gmail.com'), +('fourayoebeauty@gmail.com'), +('selenabeauty.banjarbaru@gmail.com'), +('destyisniaty@gmail.com'), +('rmdhniannisa15@gmail.com'), +('devimutiah18@gmail.com'), +('ainunmuthiaa@gmail.com'), +('nurhikmah.rasyid@gmail.com'), +('burizma.store@gmail.com'), +('ariefariq4@gmail.com'), +('haniaekap8814@gmail.com'), +('ricomerico240@gmail.com'), +('ekanvt07@gmail.com'), +('helenlim02@gmail.com'), +('lukm.hakm@gmail.com'), +('nur_aisyah1407@icloud.com'), +('serlina.mbc@gmail.com'), +('k.inayah23@gmail.com'), +('marisrisma@yahoo.com'), +('shela.annisa99@gmail.com'), +('angguntrivany@gmail.com'), +('nntheorange@gmail.com'), +('nathaliayaulin23@gmail.com'), +('mrs.gapura@gmail.com'), +('selviathalita92@gmail.com'), +('cynthia.de06@gmail.com'), +('beautemondee@gmail.com'), +('ninikrahmawati486@gmail.com'), +('anst.slvia@gmail.com'), +('beautycarepalu@gmail.com'), +('ririwiduri2002@gmail.com'), +('ilookdaily534@gmail.com'), +('apapunada.id@gmail.com'), +('dellasyaskia6@gmail.com'), +('nadiasalmau@gmail.com'), +('tikamaharaeni19@gmail.com'), +('aansabang@gmail.com'), +('devinatalithanabila98@gmail.com'), +('ennitrisha@gmail.com'), +('marialucyanasunjaya02762@gmail.com'), +('rtnohandayani@gmail.com'), +('nsbeauty06@gmail.com'), +('idapurnamasari0129@gmail.com'), +('msreysantana@gmail.com'), +('aldillaputri5@gmail.com'), +('maderusni25@gmail.com'), +('crystalglow.myshop@gmail.com'), +('dheamuliasari@gmail.com'), +('cindyfdle@gmail.com'), +('laninabeauty.official@gmail.com'), +('nimatussaadah29@gmail.com'), +('merrylaili@gmail.com'), +('wijaya.nanda1810@gmail.com'), +('kadavikadav@gmail.com'), +('anggikurniawati40@gmail.com'), +('mamikikeyu@gmail.com'), +('marlindaabdulrazak@gmail.com'), +('alizhaaa@gmail.com'), +('queenkosmetik20@gmail.com'), +('skincarekupang21@gmail.com'), +('feny.asnawi@gmail.com'), +('shanticahyadi82@gmail.com'), +('zilvanaputri@gmail.com'), +('malikaresmanti@gmail.com'), +('yunisianturi06@gmail.com'), +('beautydarlinggg@gmail.com'), +('lilybeauty.ofc@gmail.com'), +('racyasry@gmail.com'), +('terasbeauty.official@gmail.com'), +('mybeautystory.89@gmail.com'), +('novita.chyntia@yahoo.com'), +('tiffany.kimana@gmail.com'), +('rumahdandan.rd15@gmail.com'), +('desihartanti17@gmail.com'), +('forshare.id@gmail.com'), +('elmiacosmetics@gmail.com'), +('evikartika03@gmail.com'), +('allwomeneeds1403@yahoo.com'), +('nadyahudaya@yahoo.com'), +('drchrysant@gmail.com'), +('yurimeni16@gmail.com'), +('anientya@gmail.com'), +('martina.bernadeta@gmail.com'), +('wulan.dhizhi@gmail.com'), +('andrixue@gmail.com'), +('sitiqulsum308@gmail.com'), +('galaxystoreyogyakarta@gmail.com'), +('iskifitriani@yahoo.co.id'), +('arismunandar6496@gmail.com'), +('agenlingerie.id@gmail.com'), +('indahasmoro90@gmail.com'), +('ksbeautylover@gmail.com'), +('alheabeauty.id@gmail.com'), +('putriitunggal99@gmail.com'), +('ahmadmalangah@gmail.com'), +('perfectionten@gmail.com'), +('ivonsetiono26@gmail.com'), +('agencmb@gmail.com'), +('glowivier@gmail.com'), +('pinkyscosmetics1995@gmail.com'), +('evinakuesnoooo@gmail.com'), +('chennywi0126@icloud.com'), +('ekawahyuni1603@gmail.com'), +('auliaqoriardi21@gmail.com'), +('joyakhineigsoen@gmail.com'), +('susantifajarwati@yahoo.com'), +('ramadhana.selvia2408@gmail.com'), +('andarish.shop@gmail.com'), +('cindytsania@gmail.com'), +('joctora@gmail.com'), +('hannahimanuella@gmail.com'), +('dwiwulandari028@gmail.com'), +('merrychrystin06@gmail.com'), +('fibi.kdi891@gmail.com'), +('suwitosjahari@gmail.com'), +('likestoretlogosari@gmail.com'), +('care.meiyabeauty@gmail.com'), +('ajenganggitasari1@gmail.com'), +('rizkiyanitapinem@gmail.com'), +('sashop.nkc21@gmail.com'), +('lilistiyorini@gmail.com'), +('thebellas1212@gmail.com'), +('be.bellecosmetic@gmail.com'), +('nurlisaaulia@gmail.com'), +('hantybeauty19@gmail.com'), +('mputrisanti@gmail.com'), +('hellolightbit@gmail.com'), +('nadhirauw88@gmail.com'), +('asmiatiakib@gmail.com'), +('ranintya12@gmail.com'), +('afrijalrijal671@gmail.com'), +('ifayusana1993@gmail.com'), +('makeyouup.myu@gmail.com'), +('salbinus.innung@yahoo.com'), +('novia190@gmail.com'), +('evadewim.s@gmail.com'), +('itsmiracle2020@gmail.com'), +('neveltari2703@gmail.com'), +('lieusbudiman@gmail.com'), +('ferisya10@gmail.com'), +('echa.muaniez89@gmail.com'), +('trinarachmawati81@gmail.com'), +('erwinzeey123@gmail.com'), +('sahabat_debora@yahoo.com'), +('dhee2jopankar@gmail.com'), +('yunda.ade11@gmail.com'), +('cyril_steven_alexander_glory@yahoo.com'), +('devisiska32@gmail.com'), +('aureolasa.pratama@gmail.com'), +('ika.angky123@yahoo.com'), +('halimatussakdiyah1997@gmail.com'), +('rahmatjayaj2@gmail.com'), +('jesselynangella08@yahoo.com'), +('famela.ft07@gmail.com'), +('rotinsulufinny@gmail.com'), +('staryisnianti@gmail.com'), +('yulianazayan@gmail.com'), +('fandestian@gmail.com'), +('melindariaslamusnar88@gmail.com'), +('jnwkidswear@gmail.com'), +('wiwin.adriani@gmail.com'), +('febriantisiban25@gmail.com'), +('scoviabybelova@gmail.com'), +('wenny.than@gmail.com'), +('iranipontoh443@gmail.com'), +('hello.lazetta@gmail.com'), +('moullylaroid@gmail.com'), +('laurantjia@gmail.com'), +('dbeautyshop8@gmail.com'), +('godzillaphoneacc3@gmail.com'), +('wiwinstsajaah@gmail.com'), +('rizahani39@gmail.com'), +('dwihartanti_gizi@yahoo.com'), +('fangfangsaputra@gmail.com'), +('amoise.cosmetics@gmail.com'), +('dennyroseiana@gmail.com'), +('nelly.sulistiawati@gmail.com'), +('central.kosmetik1106@gmail.com'), +('marcellino.haryono@gmail.com'), +('veralia.saragih@gmail.com'), +('cefamanusia@gmail.com'), +('arindatrisna33@gmail.com'), +('oketomatala@gmail.com'), +('vilyapangajouw19@gmail.com'), +('faracintadia@gmail.com'), +('adm.elisabethbdg@yahoo.com'), +('cutmiqyal@hotmail.com'), +('harjono.honoris@gmail.com'), +('nandorian98@gmail.com'), +('milalimbong71@gmail.comcom'), +('viliansiregar24@gmail.com'), +('eldeekosmetik@gmail.com'), +('jeane.lelengboto@yahoo.com'), +('lydia.debby@gmail.com'), +('aboutmybusiness11@gmail.com'), +('triple501ara@gmail.com'), +('masfitroh@gmail.com'), +('meisalmis@gmail.com'), +('zackfair232@gmail.com'), +('nakamia_pie@yahoo.com'), +('abeauty.jogja@gmail.com'), +('lenaaska7@gmail.com'), +('zeebacosmetics31@gmail.com'), +('chiangkencana@gmail.com'), +('syaaudriana@gmail.com'), +('nadyaffu@gmail.com'), +('fauziahanisa509@gmail.com'), +('christyciasintaungu@gmail.com'), +('pradnyani.dwi@gmail.com'), +('zarazsby@gmail.com'), +('adewahyupauji@gmail.com'), +('firmantuo@gmail.com'), +('ramadhanilayla@gmail.com'), +('novascafe4@gmail.com'), +('mayaecc@yahoo.com'), +('adeyoan721@gmail.com'), +('stephaniefransss@gmail.com'), +('hermooi.official@gmail.com'), +('ratnaanita170294@gmail.com'), +('agatri.silitonga@gmail.com'), +('dewifitri2403@gmail.com'), +('moehamadsururi14@gmail.com'), +('ariestyayys@gmail.com'), +('heavenlybeauty.adm@gmail.com'), +('ikaabsarih@gmail.com'), +('dhee_amell@yahoo.co.id'), +('devianatanuwijaya@gmail.com'), +('rumah.karara@gmail.com'), +('sreejita.deb@gmail.com'), +('tagdirs.cosmetic@gmail.com'), +('ceviwilly@gmail.com'), +('daniel.khosuma@gmail.com'), +('muhammadyanis12091991@gmail.com'), +('liechintia07@gmail.com'), +('apt.deantigano@gmail.com'), +('ian.tampo22@gmail.com'), +('kunyil505@gmail.com'), +('hongjujen69@gmail.com'), +('wimma.shop@gmail.com'), +('mysamplemart@gmail.com'), +('safitriyuli3@gmail.com'), +('novanindra23@gmail.com'), +('hartantiarline@gmail.com'), +('dianyunus7@gmail.com'), +('aniyt161aan@gmail.com'), +('aestheticbeautycare23@gmail.com'), +('marissarahmaadhania9i30@gmail.com'), +('cecebeautybali@gmail.com'), +('devani.ersa@yahoo.com'), +('windiana10@gmail.com'), +('itaufik55@gmail.com'), +('zhisca_77@yahoo.co.id'), +('jillyanesasiage@gmail.com'), +('mybestiebeauty.bdg@gmail.com'), +('purchasing@bumame-farmasi.com'), +('denisa.denis89@yahoo.com'), +('ranggatedubara@gmail.com'), +('angelialim0311@gmail.com'), +('rindaagustiana@gmail.com'), +('aleshabeautycare57@gmail.com'), +('monpri28@gmail.com'), +('move.hamsari@gmail.com'), +('beautybelle2608@gmail.com'), +('priscillalastyfera@gmail.com'), +('vildarahmawati17@gmail.com'), +('nathaniaa099@gmail.com'), +('noyura@gmail.com'), +('juhatihajibe@gmail.com'), +('winda.christina@gmail.com'), +('mruslir21@gmail.com'), +('siskagsp6@gmail.com'), +('olyviadevita02@gmail.com'), +('ecgirl2023@gmail.com'), +('songlacosmetics@gmail.com'), +('jelsymakeup@gmail.com'), +('vanywidyawati86@gmail.com'), +('ongfreddykusuma@gmail.com'), +('charisthamaysadwisahara@gmail.com'), +('incjodu@gmail.com'), +('mjbeautyblitar@gmail.com'), +('ngeng.yuan27@gmail.com'), +('mybeautymart.id@gmail.com'), +('nabillafr22@gmail.com'), +('dianajennifer118@gmail.com'), +('wetirima9988@gmail.com'), +('shely.proborini@yahoo.com'), +('pak.serba@gmail.com'), +('lili_3038@yahoo.co.id'), +('tallamersiaa@gmail.com'), +('khalisanaura156@gmail.com'), +('milaatysakinah@gmail.com'), +('ucci.arfan@gmail.com'), +('lutfiah.nurhandari@gmail.com'), +('albinadhaja94@gmail.com'), +('dindaty@gmail.com'), +('shopkievanik@gmail.com'), +('zheng.jinde7692@gmail.com'), +('silvi@getvybes.co'), +('muhammadraydevan@gmail.com'), +('alstininoviyantitoar@gmail.com'), +('riahafarul778@gmail.com'), +('nrustam26@gmail.com'), +('gayabyys.wu@gmail.com'), +('lalootust@gmail.com'), +('rina2pan@yahoo.com'), +('disariska@gmail.com'), +('ninu.fira@gmail.com'), +('yuibabaretail@gmail.com'), +('makshifa93@gmail.com'), +('nadzimalaily@gmail.com'), +('nindysyarif@gmail.com'), +('mutiarapesonanganjuk@gmail.com'), +('beautybypelangi@gmail.com'), +('mariyani3412@gmail.com'), +('anitabudi1998@gmail.com'), +('farikhanum@ymail.com'), +('riscaamalia1989@gmail.com'), +('oyha.ciez01@gmail.com'), +('afriasafitri@ymail.com'), +('gitalaksmicosmetic@gmail.com'), +('oktavianimunir@gmail.com'), +('meithaekyh@gmail.com'), +('antouwkeren@gmail.com'), +('meeranova.id@gmail.com'), +('dmuhenti@gmail.com'), +('bima.ajun@gmail.com'), +('sitipluviana@gmail.com'), +('endahpratiwi522@gmail.com'), +('dindalabibah@gmail.com'), +('pusatkosmetikpinky@gmail.com'), +('anjalisibagariang2000@gmail.com'), +('krisnairma15@gmail.com'), +('ocha.rospa@gmail.com'), +('siscabudhyani86@gmail.com'), +('jhenisarykambuno@gmail.com'), +('mp-store4@raenabeauty.com'), +('luckyyosin0913@gmail.com'), +('monica1726@yahoo.co.id'), +('deasyrimanda@gmail.com'), +('pbudianoto@gmail.com'), +('hernispratiwi@gmail.com'), +('lelymelani@yahoo.com'), +('cantiksehat.sby@gmail.com'), +('gunk.fan29@gmail.com'), +('nurhayati01022021@gmail.com'), +('wardahafi@gmail.com'), +('nonikmery256@gmail.com'), +('lismiatilenta69@gmail.com'), +('dewinurhidayah5125@gmail.com'), +('nurhikmah.hasbi@gmail.com'), +('dordiaahss@gmail.com'), +('molly.fh2@gmail.com'), +('avostoresurabaya@gmail.com'), +('alhun.lun@gmail.com'), +('thebeautycare05@gmail.com'), +('mykarol88@gmail.com'), +('angela.sania@yahoo.com'), +('divaa01@ymail.com'), +('p.ariyanti2@gmail.com'), +('rizkycosmetics@gmail.com'), +('claudiayolandpurnomo@gmail.com'), +('ayudea1820@gmail.com'), +('allshopbisnis72@gmail.com'), +('yosephinetan@yahoo.co.id'), +('ayuri.minho@yahoo.com'), +('paolahermawan@yahoo.com'), +('mirna.mirnaaa02@gmail.com'), +('kumaskincare01@gmail.com'), +('officialrabeauty@gmail.com'), +('cacabeauty1234@gmail.com'), +('nadeakayuangellika@gmail.com'), +('linacaterina79@gmail.com'), +('rianishop210@gmail.com'), +('ilhamxmaulanaaa@gmail.com'), +('aratna643@gmail.com'), +('vifi2403@gmail.com'), +('paringdyahdinda@icloud.com'), +('suantysuanty10@gmail.com'), +('nandalisa045@gmail.com'), +('tinylilproject@yahoo.com'), +('arfan.arsya2022@gmail.com'), +('dahliaqueenshop@gmail.com'), +('triantiagus@gmail.com'), +('widyastevani17@gmail.com'), +('adityo_konandar@yahoo.com'), +('zoerosyidh89@gmail.com'), +('yusmane395@gmail.com'), +('tephtephyy@gmail.com'), +('seemser.id@gmail.com'), +('tantinaduu@gmail.com'), +('yannayeshika@gmail.com'), +('juliacsecogati@gmail.com'), +('pheroneid@gmail.com'), +('pauline222bn@gmail.com'), +('dpuspitaprameswari@gmail.com'), +('irnanda.vinka@yahoo.com'), +('agamwijaya89@gmail.com'), +('miniskinjars@gmail.com'), +('faizal@raenabeauty.com'), +('hanaamelinda95@gmail.com'), +('maharanimega39@gmail.com'), +('tia.aprita@gmail.com'), +('m.milanova.sh@gmail.com'), +('belscofabrics@gmail.com'), +('pratamanaufal98@gmail.com'), +('joshanshop@gmail.com'), +('ayuameliasaja@gmail.com'), +('alyadwi00@gmail.com'), +('helloyoumeindonesia@gmail.com'), +('desyrosiyana@gmail.com'), +('isvimega@gmail.com'), +('andrianiyana815@gmail.com'), +('andiakbar.ak@gmail.com'), +('isw11business@gmail.com'), +('guo_fengsheng@yahoo.com'), +('nirmaladewi03@gmail.com'), +('novitheng90@gmail.com'), +('poppymputranto@gmail.com'), +('shellahamayu1997@gmail.com'), +('suparjoariyanti@gmail.com'), +('pipinwahyu80@gmail.com'), +('chryseladarwin.olivia@gmail.com'), +('choisebest64@gmail.com'), +('rafin.trisna@gmail.com'), +('solehac098@gmail.com'), +('ayumirah12@yahoo.com'), +('fasahdi@gmail.com'), +('vindysabeauty@gmail.con'), +('dillalutchu08@gmail.com'), +('hi.dearbella@gmail.com'), +('awili.tri@gmail.com'), +('ardiradityananda@gmail.com'), +('dayanishop474@gmail.com'), +('apriliamega08@gmail.com'), +('vindysabeauty@gmail.com'), +('gilsneedit@gmail.com'), +('meiriannaafs@gmail.com'), +('mantepkebumen66@gmail.com'), +('lisnawatirohmah@gmail.com'), +('mhelydya@gmail.com'), +('apriliandesi@yahoo.co.id'), +('memonaofficial1007@gmail.com'), +('attraversiamobeauty@gmail.com'), +('triyatmoko3116@gmail.com'), +('toxena.shop@gmail.com'), +('fitridwifebriani59@gmail.com'), +('apadotid7@gmail.com'), +('misz_irma@yahoo.com'), +('riofabian112200@gmail.com'), +('indahyuliza7@gmail.com'), +('imasmaslihah2222@gmail.com'), +('letsglowid.jbi@gmail.com'), +('mahreenbeautyskin@gmail.com'), +('aldofiras1607@gmail.com'), +('firdamelia05@gmail.com'), +('hajernihajerni@gmail.com'), +('yunusdarmawan8@gmail.com'), +('fmaharani96@gmail.com'), +('tokoyangtepat@gmail.com'), +('endangdolphin2011@gmail.com'), +('pallawalipu21@gmail.com'), +('pie_buana@yahoo.com'), +('marina.diantika@gmail.com'), +('rihandika34@gmail.com'), +('ngm6756@gmail.com'), +('djonyali59@gmail.com'), +('rismagina02@gmail.com'), +('beautybyasame@gmail.com'), +('gracerushandi@gmail.com'), +('rinianggrayniinank@gmail.com'), +('lin_qiau_hua@yahoo.com'), +('buanainskonsi@gmail.com'), +('cosmetik.giant@gmail.com'), +('reynitaballack@gmail.com'), +('shopkichii88@gmail.com'), +('fadhilahafifah48@yahoo.com'), +('vivaforeva1@gmail.com'), +('sisilia.marshella@yahoo.com'), +('irmhanurjayanti@gmail.com'), +('galuhnur81@gmail.com'), +('gees.hijab@gmail.com'), +('putrimeyanti8@gmail.com'), +('rully0152@gmail.com'), +('buntukk166@gmail.com'), +('williantocindy@gmail.com'), +('nadilla@raenabeauty.com'), +('cindyfreda23@gmail.com'), +('liiyalimz@gmail.com'), +('kikaychan90@gmail.com'), +('tiffanyyaprilia@gmail.com'), +('2017120215@student.unpar.ac.id'), +('23jamesbond11@gmail.com'), +('36yonathan@gmail.com'), +('a.azzahrasyah@gmail.com'), +('a6ustina@gmail.com'), +('abyan.shop97@gmail.com'), +('acelinbeauty@gmail.com'), +('adeevaskincaresemarang@gmail.com'), +('adeyatoko@gmail.com'), +('adimahendraganteng@gmail.com'), +('agatha@raenabeauty.com'), +('agatyalaksmi@gmail.com'), +('agungfadilah810@gmail.com'), +('ahmadluckibawazier@gmail.com'), +('ai.x1.35300@sutomo-mdn.sch.id'), +('alfiatulkhubbi@gmail.com'), +('alfonsocosmetics@gmail.com'), +('alifiarizkiatania@gmail.com'), +('aline_fathin@yahoo.com'), +('alisheretotalk@gmail.com'), +('alithapradita@gmail.com'), +('allaboutskincare312@gmail.com'), +('almardhiyyahs@gmail.com'), +('almirabintangr@gmail.com'), +('alvissyahrin619@gmail.com'), +('alycia_monicaa@yahoo.co.id'), +('amaliakusumaniez@gmail.com'), +('ameicosmetic12@gmail.com'), +('amelianucifera@gmail.com'), +('ameliaresnaw@gmail.com'), +('analiani86@gmail.com'), +('andikharianto978@gmail.com'), +('angelinkayla118@gmail.com'), +('angellim0311@gmail.com'), +('angellina.lady96@gmail.com'), +('anggararnda972@gmail.com'), +('anggrini@raenabeauty.com'), +('anggriprad@gmail.com'), +('anisaauliaabas@gmail.com'), +('anisafarah24@gmail.com'), +('annachf.tan@gmail.com'), +('antodukos@gmail.com'), +('antonisang27@gmail.com'), +('appl3fuji@gmail.com'), +('arisrahmat333@gmail.com'), +('artiwayan14@gmail.com'), +('artrisnandari@gmail.com'), +('arumsekarsari573@gmail.com'), +('asepmirwan1@gmail.com'), +('assyafarrizki@gmail.com'), +('astrsari@gmail.com'), +('atikaputrims97@gmail.com'), +('audi.tabrany@gmail.com'), +('avoskinbeautybandung@gmail.com'), +('avuastore@gmail.com'), +('ayunamadewi@gmail.com'), +('babdungdyfa@gmail.com'), +('beatricetheresia1@gmail.com'), +('beautyshoujo@gmail.com'), +('bella.beautittaid@gmail.com'), +('bellaikne77@gmail.com'), +('bembie07@gmail.com'), +('beningkosmetik59@gmail.com'), +('biutidy@gmail.com'), +('brilianifridayasti@gmail.com'), +('budiondream@gmail.com'), +('business.qhansaanakkami@gmail.com'), +('cahaya.kosmetik.id@gmail.com'), +('cahyanidwisepti20@gmail.com'), +('cantikatya2@gmail.com'), +('catherinerambang620@gmail.com'), +('caty.adja@gmail.com'), +('cderz23@yahoo.com'), +('ceceliasansan8@gmail.com'), +('cerimartgo@gmail.com'), +('chandrahalim714@gmail.com'), +('chen.wong21@gmail.com'), +('cheryl.marsha.heryanto@gmail.com'), +('chococoip@gmail.com'), +('christianyendi@gmail.com'), +('cindyaharvardiana@gmail.com'), +('cinthyaruhantopp1@gmail.com'), +('cinthyaruhantopp3@gmail.com'), +('cisilia268@gmail.com'), +('clarysocimartinez@gmail.com'), +('cosellebeauty@gmail.com'), +('cvindokosmetikasejahtera@gmail.com'), +('d.lilacshop@gmail.com'), +('danielbudisantoso@gmail.com'), +('danielsimanjuntak231@gmail.com'), +('dedywijayamail@gmail.com'), +('definitionbeautyjogja@gmail.com'), +('dektasyaputra1301@gmail.com'), +('dellalisapaly@gmail.com'), +('despielia@gmail.com'), +('despielia92@gmail.com'), +('desyoong21@gmail.com'), +('deviannisme@gmail.com'), +('dewimellinda10@gmail.com'), +('dhevio1426@gmail.com'), +('dianakuliah@gmail.com'), +('dianps.haryanto@yahoo.com'), +('dillaparamita0@gmail.com'), +('dinaapril280@gmail.com'), +('dini.dwiarti3105@gmail.com'), +('dpsbykosme@gmail.com'), +('dwiagustin363@gmail.com'), +('dyas253@gmail.com'), +('dyfacafe@gmail.com'), +('eka.fokus@gmail.com'), +('eleonora@raenabeauty.com'), +('elfayunengsih20@gmail.com'), +('elisiabriana@gmail.com'), +('ellierahardja@gmail.com'), +('elsa.primasari.eps@gmail.com'), +('elsaokta26@gmail.com'), +('endahtriwedanti28@gmail.com'), +('endang.kosmetik1@gmail.com'), +('endangmarbunbbm678@gmail.com'), +('ennykoesrini676@gmail.com'), +('ennysetiono@gmail.com'), +('enokfatimah405@gmail.com'), +('entok345@gmail.com'), +('enungnurhalimah93@gmail.com'), +('envirolisasekar@gmail.com'), +('ernazulianita00@gmail.com'), +('estrellabeautee@gmail.com'), +('etienneskincare@gmail.com'), +('etisuningsih117@gmail.com'), +('evan.zelizta98@gmail.com'), +('evelyn.vio81@gmail.com'), +('evinrahmad4@gmail.com'), +('fajar950@gmail.com'), +('fajaroldman@gmail.com'), +('farah.fbindonesia@gmail.com'), +('faridahsalma@gmail.com'), +('febrina@raenabeauty.com'), +('feliceclarissa90@gmail.com'), +('fennyardisheila@gmail.com'), +('fera.julianti93@gmail.com'), +('feriscainez@gmail.com'), +('fffgh8@gmail.com'), +('fikrizia_insani@yahoo.com'), +('firabanun@gmail.com'), +('fitria_rida@yahoo.com'), +('fitriamalda123@gmail.com'), +('fitriand0192@gmail.com'), +('flavianusferdiaf@gmail.com'), +('flawless.officialshop@gmail.com'), +('flimtysurabayaofficial@gmail.com'), +('fmieayam@gmail.com'), +('freshnglow77@gmail.com'), +('friendhik25@gmail.com'), +('gabriastore@gmail.com'), +('gajahmadawaee@gmail.com'), +('galaticos02@hotmail.com'), +('gbjmarket@gmail.com'), +('givonsal@gmail.com'), +('glammarket64@gmail.com'), +('gloryharahap@gmail.com'), +('grosirmurahsukoharjo5@gmail.com'), +('guo_fengsheng@yahoo.co.id'), +('gustina.denaiyenti21@gmail.com'), +('hanselchan55@gmail.com'), +('hantarancilandak@gmail.com'), +('hardiantinila3@gmail.com'), +('hartono.henny@yahoo.com'), +('hasanyeong8@gmail.com'), +('helbert.simanjuntak@gmail.com'), +('hello.sheetmask@gmail.com'), +('hellopakbu@gmail.com'), +('helmikamanihuruk@gmail.com'), +('herdianametalic@gmail.com'), +('hestydharmayanti933@gmail.com'), +('heymarcell@gmail.com'), +('hijriahr08@gmail.com'), +('hulahulahali@gmail.com'), +('idcosmeticsidcosmetics@gmail.com'), +('ihc.korea@gmail.com'), +('ihccianjur@gmail.com'), +('iis.sugiati83@gmail.com'), +('ijallevine@gmail.com'), +('ikromyafik147@gmail.com'), +('imahgrosir@gmail.com'), +('indahpurwanti4410@gmail.com'), +('indrianimentaruk2@yahoo.com'), +('info.athirabeauty@gmail.com'), +('inggrid.kambuno@yahoo.com'), +('isbeyondstyle@gmail.com'), +('istrinyamalik@gmail.com'), +('iwanku203@gmail.com'), +('jacovcho@gmail.com'), +('jayaananda247@gmail.com'), +('jayde67@genesvjq.com'), +('jazzpro131@gmail.com'), +('jcrshop20@gmail.com'), +('jeliem96@gmail.com'), +('jelysoviana@gmail.com'), +('jesiskaruslan@gmail.com'), +('jessicaa2208@gmail.com'), +('jscang93@gmail.com'), +('jschrynt@gmail.com'), +('juliaviliajulia@gmail.com'), +('jullyangelina85@gmail.com'), +('jumrayana@gmail.com'), +('junidiarisna@gmail.com'), +('jwinarso889@gmail.com'), +('karinaagustin613@yahoo.co.id'), +('katalogskincarekorea@gmail.com'), +('keanoliem3@gmail.com'), +('kekensetiawan@gmail.com'), +('khairunniszahaifa@yahoo.com'), +('kikiseptiana92@gmail.com'), +('klikandshop18@gmail.com'), +('kntz199@gmail.com'), +('kosmetiklancarjaya@gmail.com'), +('kuboykukuh@gmail.com'), +('kunarifin@gmail.com'), +('kurniadi.phan@gmail.com'), +('kyuhaekoinline@gmail.com'), +('kyuhyunkoinline5@gmail.com'), +('lancarjayakosmetik@gmail.com'), +('lanisiska45@gmail.com'), +('laure.devienne@gmail.com'), +('lavoilla@gmail.com'), +('leejaeha31@gmail.com'), +('lenasinaga853@yahoo.com'), +('leo_azzuri@yahoo.com'), +('libbyclaudiar@gmail.com'), +('Liec534@gmail.com'), +('liedhawei@yahoo.com'), +('lina.marlinajkt228@gmail.com'), +('linamay2088@gmail.com'), +('lincebaiku34@gomail.com'), +('lindasekararum@gmail.com'), +('lindaten93@gmail.com'), +('lio.edhen@gmail.com'), +('lionachristina110887@gmail.com'), +('lolithaameliaa.la@gmail.com'), +('lolytha.arina@yahoo.co.id'), +('loverini1508@gmail.com'), +('lricosantoso@gmail.com'), +('luckyluna.id@gmail.com'), +('luckyme2611@gmail.com'), +('lusianaivang@yahoo.com'), +('luvpiggy2312@gmail.com'), +('lyannaming@gmail.com'), +('madya.wa@gmail.com'), +('mahesabeautyandstore@gmail.com'), +('maianton@yahoo.com'), +('mailto.wabisabi@gmail.com'), +('makeupfutura@gmail.com'), +('maramaratus@gmail.com'), +('marciaaact@gmail.com'), +('maria.ariesw87@gmail.com'), +('mariaboons0@gmail.com'), +('marianaulfa11@gmail.com'), +('mariiahulfah123@gmail.com'), +('marlinadiansafitri@gmail.com'), +('maroon16store@gmail.com'), +('marshandaditha@gmail.com'), +('martza.swastikasari@gmail.com'), +('maryani4398@gmail.com'), +('maryavictoryss3@gmail.com'), +('matrasunik@gmail.com'), +('maupesanposter@gmail.com'), +('maximlaundry@yahoo.com'), +('megashinta39@gmail.com'), +('mekarkosmetik.serang@gmail.com'), +('melaplestari2@gmail.com'), +('melindasuriyam01@gmail.com'), +('melisahariyanto14@gmail.com'), +('mentaripuspa59@gmail.com'), +('mh.lifestyles.in@gmail.com'), +('mikanmicheelen@gmail.com'), +('mirzaluzzia123@gmail.com'), +('misluciana@yahoo.com'), +('mitha2904aprilia@gmail.com'), +('mitufaya@gmail.com'), +('mitzukooutfit@gmail.com'), +('mizuharaaya@gmail.com'), +('mohd.nurfitriadin@gmail.com'), +('moildy.id@gmail.com'), +('monalisaflorist@gmail.com'), +('morashopmedan@gmail.com'), +('mr.yung30@gmail.com'), +('muhammad.hidayat.101@gmail.com'), +('mutiaazzahra177@gmail.com'), +('mutiarajesica0209@gmail.com'), +('mykagendis@gmail.com'), +('nadya.desnam@gmail.com'), +('nailavira102@gmail.com'), +('nanalistya22@gmail.com'), +('nandadevie1968@gmail.com'), +('nandipintaa@gmail.com'), +('nari38700@gmail.com'), +('natasyaputri4982@gmail.com'), +('naturrcosmetic@gmail.com'), +('niasepti28@gmail.com'), +('nicoags26@gmail.com'), +('nicolebeautyindonesia@gmail.com'), +('nicoyuganugraha@gmail.com'), +('ninadelinda26@gmail.com'), +('nirmala.sari709@gmail.com'), +('nonaewa@gmail.com'), +('nophaaelfrida@gmail.com'), +('nova.punya@gmail.com'), +('novandy.yohanes@gmail.com'), +('novi0128@gmail.com'), +('novitageraldinetgt@gmail.com'), +('nscollastica@gmail.com'), +('ntyaau26@gmail.com'), +('nuciferaamelia@gmail.com'), +('nugrahapiawai@gmail.com'), +('nur.wazilah@gmail.com'), +('nurazhwa170@gmail.com'), +('nurinasp@gmail.com'), +('nurlaelaramadanti@gmail.com'), +('officialbuyorbye@gmail.com'), +('oktinningrum@gmail.com'), +('opponingrum@gmail.com'), +('oyyisocial@gmail.com'), +('pajerokita@gmail.com'), +('pandridoge@gmail.com'), +('papamarvel_suhendy@yahoo.com'), +('pardosiares@gmail.com'), +('pastiibisaa@gmail.com'), +('pipitsanti999@gmail.com'), +('popoybunny@gmail.com'), +('prakososyam0403@gmail.com'), +('pramita2701@gmail.com'), +('prioritybekasi@gmail.com'), +('properbeauty.id@gmail.com'), +('psalimatulh@gmail.com'), +('puellabeautyshop@gmail.com'), +('puterifedina@gmail.com'), +('putriparapara@gmail.com'), +('putrisildosz@gmail.com'), +('putrivhidi@gmail.com'), +('putryalfionta@yahoo.com'), +('r.alika003@gmail.com'), +('r0n_ny@yahoo.com'), +('radhiah.hasan26@gmail.com'), +('rahayuwrdhn@gmail.com'), +('ranicantikhebat@gmail.com'), +('ratnashary4@gmail.com'), +('raynoldpanjaitan100@gmail.com'), +('regsmakeup@gmail.com'), +('resditamargaretha@gmail.com'), +('reviyuliswan86@gmail.com'), +('ribkanads@gmail.com'), +('richarddarmawanlaidji@gmail.com'), +('rickylie1989@gmail.com'), +('ries091@gmail.com'), +('rikiimpiandes@gmail.com'), +('rikomm822@gmail.com'), +('rinayunikartikaa@gmail.com'), +('rindaariani36@gmail.com'), +('riniismiyanti_94@yahoo.com'), +('ririalijofa@gmail.com'), +('riskalestari014@gmail.com'), +('rkcjawa@yahoo.com'), +('robeth.saputra01@gmail.com'), +('romlahabadsyam@gmail.com'), +('ronnywongsolim@gmail.com'), +('rovihshsh@gmail.com'), +('roviwardanap@gmail.com'), +('roviwardanaputra@gmail.com'), +('rperlita11@gmail.com'), +('rtjayaselalu@gmail.com'), +('rzkamll18@gmail.com'), +('sabithaanisa03@gmail.com'), +('saniaa3095@gmail.com'), +('saniyasch@gmail.com'), +('sansbeauty01@gmail.com'), +('santisujanto@gmail.com'), +('sarah@getvybes.co'), +('sari.astuty12@gmail.com'), +('sarihakim96@gmail.com'), +('sarmutias1@gmail.com'), +('sarmutias2@gmail.com'), +('sarmutias5@gmail.com'), +('seilareisya@gmail.com'), +('selfi.yonghwa08@gmail.com'), +('sendychristians@gmail.com'), +('sepriyanamelly@gmail.com'), +('serlinaepriyanti07@gmail.com'), +('setiawan.laia31@gmail.com'), +('setiawanandrew818@gmail.com'), +('sherlysherlyvi@gmail.com'), +('sholi.hijubpanda@gmail.com'), +('shopalover123@gmail.com'), +('shopbintar@gmail.com'), +('shoujoshopcosmetic@gmail.com'), +('siauwelizabeth@gmail.com'), +('sintafauziah41@gmail.com'), +('siti.lathifatul.k@gmail.com'), +('sitilathifatulkhofi@gmail.com'), +('siuni.reno@gmail.com'), +('siwiberlianti@gmail.com'), +('skincareindoid@gmail.com'), +('skinqueenproject@gmail.com'), +('smcosmeticsub@gmail.com'), +('snana602@gmail.com'), +('snrofi27@gmail.com'), +('soniadepi21@gmail.com'), +('sonie.usman@gmail.com'), +('sreejita@raenabeauty.com'), +('srialfia.san@gmail.com'), +('srihendrawati1@gmail.com'), +('sriyuliani9379@gmail.com'), +('stacia_andani91@yahoo.com'), +('staciaandani91@gmail.com'), +('stasyifa@gmail.com'), +('stefani_eli@ymail.com'), +('stepfanny91@gmail.com'), +('storemajujaya12@gmail.com'), +('sultonudin.madani21@gmail.com'), +('sun.edihtantra@gmail.com'), +('suryakosmetikjogja@gmail.com'), +('suryaniani1960@gmail.com'), +('susantiyap68@gmail.com'), +('suwandikarina61@gmail.com'), +('suwati169@gmail.com'), +('syahlaag@gmail.com'), +('syahliyaf@gmail.com'), +('syahna2128@gmail.com'), +('syahpudin09.183@gmail.com'), +('syifasahb@gmail.com'), +('tagustatang@gmail.com'), +('takiidonlineshopping@gmail.com'), +('talinazulfah@gmail.com'), +('tanmelky@yahoo.com'), +('taraaengg@gmail.com'), +('taspremium88@gmail.com'), +('taufikismail198720@gmail.com'), +('tcahyanti@gmail.com'), +('tfnhtp@gmail.com'), +('the_fortune_eo@yahoo.com'), +('theakirana133@gmail.com'), +('thebeausell@gmail.com'), +('thebeautyneed@gmail.com'), +('theidk.zone@gmail.com'), +('theskindarling@gmail.com'), +('tihanperkasadefitro@gmail.com'), +('tiifandi@gmail.com'), +('timyls@yahoo.com'), +('tina.metro@yahoo.com'), +('tioharianto.allianz@yahoo.com'), +('tjulovely2013@gmail.com'), +('toko.viragrosir88@gmail.com'), +('toko.winars.palopo@gmail.com'), +('tokounikita2@gmail.com'), +('tommy_priatama@ymail.com'), +('topupfu.sp@gmail.com'), +('tyabe888@yahoo.com'), +('ulibasagrc123@gmail.com'), +('umarsikumbang2020@icloud.com'), +('vannychristinna@yahoo.com'), +('varenunggulkarya@gmail.com'), +('veliakatarina@gmail.com'), +('velvetpinkred@gmail.com'), +('vhex307@gmail.com'), +('vidiachen@yahoo.com'), +('vilencyviranyy@gmail.com'), +('vina.astini@yahoo.com'), +('vincenttia23@gmail.com'), +('vinlie2020@gmail.com'), +('violyamerin@gmail.com'), +('viraltoday124@gmail.com'), +('virginiaca09@gmail.com'), +('viserysyvaine@gmail.com'), +('viviyuvita19@gmail.com'), +('wahyudiirwan35@gmail.com'), +('wahyuekajaya@gmail.com'), +('wahyuyana.prasetya@gmail.com'), +('wellington@nihonmart.id'), +('wellypandin76@gmail.com'), +('widoagusputra90@gmail.com'), +('wiwiekapsari@icloud.com'), +('wulan3804@gmail.com'), +('wulandary.sllh@gmail.com'), +('xenpang26@gmail.com'), +('xuanliuli0@gmail.com'), +('yayarafin@gmail.com'), +('yenimaryatin332@gmail.com'), +('yenny.irawati@yahoo.com'), +('yessicasarano16@gmail.com'), +('yogialqarni1807@gmail.com'), +('yogisugianto21@gmail.com'), +('yong2.chan@gmail.com'), +('yooabeaute@gmail.com'), +('ysurjadi@gmail.com'), +('yt40gt@gmail.com'), +('yukireiyuka@gmail.com'), +('yuliacandle@gmail.com'), +('yuliananina19@yahoo.com'), +('yunisri.hartiwi@yahoo.com'), +('yunitadewi23@gmail.com'), +('zabeth_yulie@yahoo.co.id'), +('zahraashop18@gmail.com'), +('zahrabeauty.share@gmail.com'), +('zelmi5009@gmail.com'), +('zharasonline@gmail.com'), +('zivenakbar08@gmail.com'), +('Fanamaliauthor@gmail.com'), +('tjuganteng@gmailcom'), +('ayusisicantik@gmail.com'), +('tsetiyaningsih71@gmail.com'), +('oyyiapple@gmail.com'), +('angellim0311@yahoo.com'), +('fericoridwan@gmail.com'), +('Ikasusanti_story@yahoo.com'), +('hamkyu359@gmail.com'); + +DROP TABLE public.SKU_base_recommendation_base; + + +CREATE TABLE public.SKU_base_recommendation_base AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM public.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +WHERE transaction_date::date>dateadd(DAY,-90,'2023-01-22') + AND A.cogs IS NOT NULL + AND A.cogs>0 + and (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false' +GROUP BY 1, + 2, + 3, + 4, + 5 ; + + +DROP TABLE public.reseller_base_recommendation_base; + + +CREATE TABLE public.reseller_base_recommendation_base AS +SELECT reseller_id , + A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type , + --shipping_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM public.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +INNER JOIN public.recommendation_power_seller_base BB ON A.reseller_email = BB.email +WHERE transaction_date::date>dateadd(DAY,-180,'2023-01-22') + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND reseller_id IS NOT NULL +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE public.reseller_base_recommendation_base_stage1; + + +CREATE TABLE public.reseller_base_recommendation_base_stage1 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>=10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM public.reseller_base_recommendation_base; + +DROP TABLE IF EXISTS public.raw_scraping_data; + + +CREATE TABLE public.raw_scraping_data AS +SELECT E.raena_product_sku, + F.name SKU_name, + E.rce_product_id , + E.rce_variant_id, + A.category_name , + A.brand_name , + A.product_page_url , + rce_source_product_name, + product_variant_name, + product_variant_price, + product_variant_price_before_discount, + product_sold_total, + product_sold, + product_price_min, + product_price_min_before_discount, + product_price_max, + product_price_max_before_discount, + cast(percentage_match AS decimal(22,2)) percentage_match, + ratings +FROM public.raena_shopee_product_mapping E +LEFT JOIN + (SELECT A.id rce_product_id, + D.id rce_product_varient_id , + B.category_name , + C.brand_name , + product_page_url , + rce_source_product_name, + product_variant_name, + product_variant_price/10000 product_variant_price, + product_variant_price_before_discount/10000 product_variant_price_before_discount, + product_sold_total, + product_sold, + product_price_min/10000 product_price_min, + product_price_min_before_discount/10000 product_price_min_before_discount, + product_price_max/10000 product_price_max, + product_price_max_before_discount/10000 product_price_max_before_discount, + ratings + FROM raena_spider_management.rce_product A + LEFT JOIN raena_spider_management.rce_category B ON A.rce_category_id = B.id + LEFT JOIN raena_spider_management.rce_brand C ON A.rce_brand_id = C.id + LEFT JOIN raena_spider_management.rce_product_variant D ON A.id = D.rce_product_id) A ON E.rce_product_id = A.rce_product_id +AND cast(replace(E.rce_variant_id,'.0','') AS varchar) = cast(replace(A.rce_product_varient_id,',','') AS varchar) +LEFT JOIN raena_catalog_management.product F ON E.raena_product_sku=F.sku ; + + +DROP TABLE IF EXISTS public.raw_scraping_data_stage ; + + +CREATE TABLE public.raw_scraping_data_stage AS +SELECT A.* +FROM public.raw_scraping_data A +INNER JOIN + (SELECT raena_product_sku, + max(percentage_match) percentage_match + FROM public.raw_scraping_data + GROUP BY 1) B ON A.raena_product_sku = B.raena_product_sku +AND A.percentage_match =B.percentage_match; + + +DROP TABLE IF EXISTS public.sku_sales_factor; + + +CREATE TABLE public.sku_sales_factor AS +SELECT SKU, + cast(cast(sum(B.total_quantity*3) AS decimal(22,2))/cast(SUM(A.total_quantity) AS decimal(22,2)) AS decimal(22,2)) sale_factor +FROM public.SKU_base_recommendation_base A +LEFT JOIN + (SELECT raena_product_sku, + sum(DISTINCT product_sold) total_quantity + FROM public.raw_scraping_data_stage + GROUP BY 1) B ON A.sku = B.raena_product_sku +WHERE A.Pre_discount_gm>=10 +GROUP BY 1; + + +DROP TABLE IF EXISTS public.recommendation_on_low_sku_task1; + + +CREATE TABLE public.recommendation_on_low_sku_task1 AS +SELECT C.mobile Reseller_mobile , + C.email reseller_email, + A.sku, + A.sku_name, + A.brand_name, + A.category_name , + A.product_type , + Total_quantity, + total_payment_price, + total_cogs, + A.pre_discount_gm, + coalesce(B.sale_factor,0) sale_factor, + A.pre_discount_gm*coalesce(B.sale_factor,1) sorting +FROM public.reseller_base_recommendation_base_stage1 A +INNER JOIN raena_user_management.user C ON A.reseller_id = C.id +INNER JOIN + (SELECT sku , + pre_discount_gm + FROM public.SKU_base_recommendation_base) D ON A.sku = D.sku +AND D.pre_discount_gm >=10 +inner join public.sku_in_stock E on A.sku = E.sku and E.in_stock >=10 +LEFT JOIN public.sku_sales_factor B ON A.sku = B.sku +WHERE Gm_type = 'DDB' +ORDER BY reseller_id , + sale_factor DESC; +---------------------------------------------task 2 --------------- + + + + + +DROP TABLE public.SKU_base_recommendation_base_v2; + + +CREATE TABLE public.SKU_base_recommendation_base_v2 AS +SELECT A.sku, + ((sum((A.retail_price-seller_margin) *quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM public.gm_dashboard A +WHERE transaction_date::date>dateadd(DAY,-90,'2023-01-22') + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND tier_name ='GOLD' +GROUP BY 1; + +DROP TABLE IF EXISTS public.recommendation_base_historical_data_offender; + + +CREATE TABLE public.recommendation_base_historical_data_offender AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type + FROM public.reseller_base_recommendation_base_stage1 + WHERE gm_type ='Offender') A +LEFT JOIN public.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN public.sku_sales_factor C ON B.mapping_group_base = C.sku +LEFT JOIN public.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku and Pre_discount_gm>=10 +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM raena_catalog_management.product B + INNER JOIN raena_catalog_management.category C ON B.category_id = C.id + INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id + where (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false') E ON B.mapping_group_base= E.SKU; + + + DROP TABLE IF EXISTS public.recommendation_base_historical_data_ddb; + + +CREATE TABLE public.recommendation_base_historical_data_ddb AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + A.Pre_discount_gm reseller_pre_discount_gm, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + D.Pre_discount_gm, + in_stock +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type, + Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1 + WHERE gm_type ='DDB') A +LEFT JOIN public.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN public.sku_sales_factor C ON A.SKU = C.sku +LEFT JOIN public.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND D.Pre_discount_gm>=10 +LEFT JOIN public.sku_in_stock F ON B.mapping_group_base = F.sku +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM raena_catalog_management.product B + INNER JOIN raena_catalog_management.category C ON B.category_id = C.id + INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id + where (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false') E ON B.mapping_group_base= E.SKU; + + DROP TABLE public.recommentdation_offender_sku_concern; + + +CREATE TABLE public.recommentdation_offender_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM public.recommendation_base_historical_data_offender B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE recommended_concern_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC; + + + DROP TABLE public.recommentdation_offender_sku_ingredient; + + +CREATE TABLE public.recommentdation_offender_sku_ingredient AS +SELECT reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM public.recommendation_base_historical_data_offender B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +AND recommended_ingredient_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC ; + + + DROP TABLE IF EXISTS public.recommentdation_ddb_sku_concern ; + + +CREATE TABLE public.recommentdation_ddb_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM public.recommendation_base_historical_data_ddb B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE recommended_concern_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM public.recommendation_base_historical_data_ddb B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE in_stock>50 and reseller_pre_discount_gm>10; + +DROP TABLE IF EXISTS public.recommentdation_ddb_sku_ingredient ; + + +CREATE TABLE public.recommentdation_ddb_sku_ingredient AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM public.recommendation_base_historical_data_ddb B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE recommended_ingredient_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM public.recommendation_base_historical_data_ddb B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE in_stock>50 +and reseller_pre_discount_gm >=10; + +DROP TABLE public.recommentdation_sku_ingredient; + + +CREATE TABLE public.recommentdation_sku_ingredient AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + Recommended_pre_discount_gm, + sale_factor , + (Ingredient_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm) sorting, + cast(json_extract_path_text(BB.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + cast(sale_factor AS decimal(22,2)) sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM public.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM public.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join raena_catalog_management.product BB on AA.Recommended_sku= BB.sku + inner JOIN public.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + + +DROP TABLE public.recommentdation_sku_ingredient_purchase; + + +CREATE TABLE public.recommentdation_sku_ingredient_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(json_extract_path_text(BB.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM public.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku, + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM public.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join raena_catalog_management.product BB on AA.sku= BB.sku + inner JOIN public.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + + +DROP TABLE public.recommentdation_sku_concern; + + +CREATE TABLE public.recommentdation_sku_concern AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + Recommended_pre_discount_gm, + sale_factor , + concern_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm sorting, + cast(json_extract_path_text(BB.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM public.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM public.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join raena_catalog_management.product BB on AA.Recommended_sku= BB.sku + inner JOIN public.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + +DROP TABLE public.recommentdation_sku_concern_purchase; + + +CREATE TABLE public.recommentdation_sku_concern_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(json_extract_path_text(BB.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM public.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM public.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join raena_catalog_management.product BB on AA.sku= BB.sku + inner JOIN public.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + -----------------------------------------------Task 3 ------------------------------- +--inner join (select distinct id as order_id , json_extract_path_text(reseller_info,'province',TRUE) reseller_province +-- from raena_order_management.order +-- where created_at::date>dateadd(DAY,-91,'2023-01-22') and payment_status='Paid') AA on A.external_id = AA.order_id + + +DROP TABLE public.SKU_base_recommendation_base_t3 ; + + +CREATE TABLE public.SKU_base_recommendation_base_t3 AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + shipping_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM public.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +WHERE transaction_date::date>dateadd(DAY,-90,'2023-01-22') + AND A.cogs IS NOT NULL + AND A.cogs>0 + and (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false' +GROUP BY 1, + 2, + 3, + 4, + 5, + 6 ; + +DROP TABLE public.reseller_base_recommendation_base_t3; + + +CREATE TABLE public.reseller_base_recommendation_base_t3 AS +SELECT reseller_id , + A.sku, + case when shipping_to ='Customer' then shipping_province end as dropshipping_province, + case when shipping_to ='Reseller' then shipping_province end as reseller_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin) *quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Post_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM public.gm_dashboard A +INNER JOIN + (SELECT DISTINCT id AS order_id , + shipping_to + FROM raena_order_management.order + WHERE created_at::date>dateadd(DAY,-180,'2023-01-22') + AND payment_status='Paid') AA ON A.external_id = AA.order_id +INNER JOIN public.recommendation_power_seller_base BB ON A.reseller_email = BB.email +WHERE transaction_date::date>dateadd(DAY,-180,'2023-01-22') + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND reseller_id IS NOT NULL + AND order_placed_by ='Admin Panel' + AND shipping_to <> 'Marketplace' +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE public.reseller_base_recommendation_base_stage1_t3; + + +CREATE TABLE public.reseller_base_recommendation_base_stage1_t3 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM public.reseller_base_recommendation_base_t3; + + +DROP TABLE public.same_province_with_diff_sku_dropshipping; + + +CREATE TABLE public.same_province_with_diff_sku_dropshipping AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk +FROM + (SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + sum(total_quantity_sku)over(partition BY A.reseller_id ,A.SKU) total_quantity_sku , + B.Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN + (SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm, + in_stock + FROM public.SKU_base_recommendation_base_t3 A + INNER JOIN public.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join public.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6,9)B ON A.dropshipping_province = B.shipping_province where dropshipping_province is not null )A +WHERE A.sku <> A.recommended_sku and dropshipping_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + + + DROP TABLE public.recommendation_same_province_with_diff_sku_dropshipping; + + +CREATE TABLE public.recommendation_same_province_with_diff_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM public.same_province_with_diff_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = B.id +WHERE Pre_discount_gm >=10; + +DROP TABLE public.same_province_with_diff_sku_reseller; + + +CREATE TABLE public.same_province_with_diff_sku_reseller AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM + (SELECT A.reseller_id , + A.sku , + A.reseller_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + sum(total_quantity_sku)over(partition BY A.reseller_id ,A.SKU) total_quantity_sku , + B.Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN + (SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM public.SKU_base_recommendation_base_t3 A + INNER JOIN public.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join public.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6)B ON A.reseller_province = B.shipping_province where A.reseller_province is not null )A +WHERE A.sku <> A.recommended_sku and A.reseller_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + +DROP TABLE public.recommendation_same_province_with_diff_sku_reseller; + + +CREATE TABLE public.recommendation_same_province_with_diff_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM public.same_province_with_diff_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = B.id +WHERE Pre_discount_gm >=10; + +DROP TABLE public.same_province_with_same_sku_dropshipping; + + +CREATE TABLE public.same_province_with_same_sku_dropshipping AS +select * from +( +SELECT *, + total_quantity_sku/total_quantity sales_factor, +row_number() over(partition by reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk +FROM + (SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + sum(total_quantity_sku)over(partition BY A.reseller_id ,A.SKU) total_quantity_sku , + B.Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN + (SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM public.SKU_base_recommendation_base_t3 A + INNER JOIN public.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join public.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6)B ON A.dropshipping_province = B.shipping_province where A.dropshipping_province is not null )A +WHERE sku = recommended_sku and A.dropshipping_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + +DROP TABLE public.recommendation_same_province_with_same_sku_dropshipping; + + +CREATE TABLE public.recommendation_same_province_with_same_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM public.same_province_with_same_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = B.id +WHERE Pre_discount_gm >=10; + + DROP TABLE public.same_province_with_same_sku_reseller; + + +CREATE TABLE public.same_province_with_same_sku_reseller AS +select * from ( +SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM + (SELECT A.reseller_id , + A.sku , + A.reseller_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + sum(total_quantity_sku)over(partition BY A.reseller_id ,A.SKU) total_quantity_sku , + B.Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN + (SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM public.SKU_base_recommendation_base_t3 A + INNER JOIN public.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join public.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6)B ON A.reseller_province = B.shipping_province where A.reseller_province is not null )A +WHERE sku = recommended_sku and A.reseller_province is not null )AA + where rnk<11 +ORDER BY sku , + rnk; + + DROP TABLE public.recommendation_same_province_with_same_sku_reseller; + + +CREATE TABLE public.recommendation_same_province_with_same_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM public.same_province_with_same_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = B.id +WHERE Pre_discount_gm >=10; + + + diff --git a/Am_recommendation/etl_am_recommendation.sh b/Am_recommendation/etl_am_recommendation.sh new file mode 100644 index 0000000..bf09ee7 --- /dev/null +++ b/Am_recommendation/etl_am_recommendation.sh @@ -0,0 +1,2751 @@ + #!/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.raw_ingredient_split_name; +CREATE TABLE public.raw_ingredient_split_name AS +SELECT * , + split_part(concern,', ',1) concern1, + split_part(concern,', ',2) concern2, + split_part(concern,', ',3) concern3, + split_part(concern,', ',4) concern4, + split_part(concern,', ',5) concern5, + split_part(top_marketing_ingredients ,', ',1) ingredient1, + split_part(top_marketing_ingredients,', ',2) ingredient2, + split_part(top_marketing_ingredients,', ',3) ingredient3, + split_part(top_marketing_ingredients,', ',4) ingredient4, + split_part(top_marketing_ingredients,', ',5) ingredient5, + split_part(top_marketing_ingredients,', ',6) ingredient6, + split_part(top_marketing_ingredients,', ',7) ingredient7, + split_part(top_marketing_ingredients,', ',8) ingredient8, + split_part(top_marketing_ingredients,', ',9) ingredient9, + split_part(top_marketing_ingredients,', ',10) ingredient10, + split_part(top_marketing_ingredients,', ',11) ingredient11 +FROM + (SELECT DISTINCT group_base, + sku , + lower(concern) concern, + lower(top_marketing_ingredients) top_marketing_ingredients, + CASE + WHEN concern='' + AND top_marketing_ingredients='' THEN 1 + END flag + FROM public.ingredients_base_table_final) A +WHERE flag IS NULL; + + DROP TABLE public.raw_ingredient_mapping_table_stage; + + +CREATE TABLE public.raw_ingredient_mapping_table_stage AS +SELECT A.* , + B.group_base mapping_group_base, + B.concern mapping_concerns, + B.top_marketing_ingredients mapping_ingredients +FROM public.raw_ingredient_split_name A +LEFT JOIN + (SELECT * + FROM public.raw_ingredient_split_name) B ON 1=1; --udf_string_campare(A.ingredient1,B.top_marketing_ingredients) = A.ingredient1; + +DROP TABLE public.raw_ingredient_mapping_table_stage2; + + +CREATE TABLE public.raw_ingredient_mapping_table_stage2 AS +SELECT * , + CASE + WHEN udf_string_campare(concern1,mapping_concerns) <>'' + AND udf_string_campare(concern1,mapping_concerns) <> 'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern1, + CASE + WHEN udf_string_campare(concern2,mapping_concerns) <>'' + AND udf_string_campare(concern2,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern2, + CASE + WHEN udf_string_campare(concern3,mapping_concerns) <>'' + AND udf_string_campare(concern3,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern3, + CASE + WHEN udf_string_campare(concern4,mapping_concerns) <>'' + AND udf_string_campare(concern4,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern4, + CASE + WHEN udf_string_campare(concern5,mapping_concerns) <>'' + AND udf_string_campare(concern5,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern5, + REGEXP_COUNT(concern ,', ')+1 total_count_concern, + CASE + WHEN udf_string_campare(ingredient1,mapping_ingredients) <>'' + AND udf_string_campare(ingredient1,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient1, + CASE + WHEN udf_string_campare(ingredient2,mapping_ingredients) <>'' + AND udf_string_campare(ingredient2,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient2, + CASE + WHEN udf_string_campare(ingredient3,mapping_ingredients) <>'' + AND udf_string_campare(ingredient3,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient3, + CASE + WHEN udf_string_campare(ingredient4,mapping_ingredients) <>'' + AND udf_string_campare(ingredient4,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient4, + CASE + WHEN udf_string_campare(ingredient5,mapping_ingredients) <>'' + AND udf_string_campare(ingredient5,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient5, + CASE + WHEN udf_string_campare(ingredient6,mapping_ingredients) <>'' + AND udf_string_campare(ingredient6,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient6, + CASE + WHEN udf_string_campare(ingredient7,mapping_ingredients) <>'' + AND udf_string_campare(ingredient7,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient7, + CASE + WHEN udf_string_campare(ingredient8,mapping_ingredients) <>'' + AND udf_string_campare(ingredient8,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient8, + CASE + WHEN udf_string_campare(ingredient9,mapping_ingredients) <>'' + AND udf_string_campare(ingredient9,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient9, + CASE + WHEN udf_string_campare(ingredient10,mapping_ingredients) <>'' + AND udf_string_campare(ingredient10,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient10, + CASE + WHEN udf_string_campare(ingredient11,mapping_ingredients) <>'' + AND udf_string_campare(ingredient11,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient11, + REGEXP_COUNT(top_marketing_ingredients ,', ')+1 total_count_ingredient +FROM public.raw_ingredient_mapping_table_stage +WHERE group_base <> mapping_group_base; + + DROP TABLE public.raw_ingredient_mapping_table_stage3; + + +CREATE TABLE public.raw_ingredient_mapping_table_stage3 AS +SELECT *, + flag_concern1+flag_concern2+flag_concern3+flag_concern3+flag_concern3 total_concern_match, + flag_ingredient1+flag_ingredient2+flag_ingredient3+flag_ingredient4+flag_ingredient5+flag_ingredient6+flag_ingredient7+flag_ingredient8+flag_ingredient9+flag_ingredient10+flag_ingredient11 total_ingredient_match +FROM public.raw_ingredient_mapping_table_stage2 ; + + +DROP TABLE IF EXISTS public.raw_ingredient_mapping_table_stage4; + + +CREATE TABLE public.raw_ingredient_mapping_table_stage4 AS +SELECT *, (total_concern_match*100/total_count_concern) Percent_concern_match, (total_ingredient_match*100/total_count_ingredient) Percent_ingredient_match , + CASE WHEN concern='' AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND top_marketing_ingredients='' THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + END percent_flag +FROM public.raw_ingredient_mapping_table_stage3; + + +Drop table if exists public.raw_ingredient_mapping_table; + +Create table public.raw_ingredient_mapping_table +as +SELECT group_base, + sku, + concern, + top_marketing_ingredients, + mapping_group_base, + mapping_concerns, + mapping_ingredients, + percent_concern_match, + Percent_ingredient_match +FROM public.raw_ingredient_mapping_table_stage4 +WHERE percent_flag=1 +ORDER BY group_base , + percent_concern_match DESC , + Percent_ingredient_match DESC; + + +DROP TABLE IF EXISTS public.sku_in_stock; + + +CREATE TABLE public.sku_in_stock AS +SELECT sku , + sum(coalesce(F.stock_limit,0)) 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; + + --------------------------------------------------------------task 1 ---------------- + +drop table if exists public.recommendation_power_seller_base_alt; + +create table public.recommendation_power_seller_base_alt +as +select distinct email from public.om_acquisition_mapping_list +union select distinct \"email id\" from public.AM_REVENUE_LEAKAGE_LIST ; + + +INSERT INTO public.recommendation_power_seller_base_alt + values ('cindyvioleta29@yahoo.com'), +('sherryfannydesita@gmail.com'), +('masintansilalahi@yahoo.co.id'), +('rumahskincarebpp@gmail.com'), +('puturisca@gmail.com'), +('sieny.go@gmail.com'), +('oki.zhang1988@gmail.com'), +('jeremytantono@yahoo.co.id'), +('yuke.cipta@gmail.com'), +('kwonvic@hotmail.com'), +('ervinafransiska95@gmail.com'), +('indahamaliasasmita@gmail.com'), +('mirosebeauty@gmail.com'), +('fourrshoptarakan@gmail.com'), +('stuffbyms@gmail.com'), +('cindyrissa14@gmail.com'), +('lieke1212@gmail.com'), +('jelitacosmetic.yk@gmail.com'), +('metafuji@gmail.com'), +('destiyapradhani@gmail.com'), +('liepinghau@gmail.com'), +('jennifer_kambuno@yahoo.com'), +('y35beauty@gmail.com'), +('nisamulya@gmail.com'), +('issadella@yahoo.co.id'), +('hartono.henny@yahoo.co.id'), +('a2335572@gmail.com'), +('navisgusmiati24@gmail.com'), +('djiwandimelany@gmail.com'), +('colinakoh178@gmail.com'), +('fairybeaute17@gmail.com'), +('fajri.vanbio95@gmail.com'), +('zidney_yao@yahoo.com'), +('chandraalex305@gmail.com'), +('elly.elisha@gmail.com'), +('erlinapr13@gmail.com'), +('mechan_06@hotmail.com'), +('limvincents@hotmail.com'), +('tjuganteng@gmail.com'), +('sralaydrus96@yahoo.com'), +('duwik_tonx@yahoo.co.id'), +('adelshopadelia@gmail.com'), +('shierlysusanto2405@gmail.com'), +('murni.cosmetics55@gmail.com'), +('stg_purchasing_melisa@yahoo.com'), +('asri.nilasari@watsons.co.id'), +('wayanndiana@gmail.com'), +('emailtedy@gmail.com'), +('Shantifm@hotmail.com'), +('shintachristiani2911@gmail.com'), +('dotienigloo@gmail.com'), +('mirandadessy95@gmail.com'), +('pangeranpetirr@gmail.com'), +('mariamellysacandra@ymail.com'), +('evitamalaa@ymail.com'), +('lindadarwinp@gmail.com'), +('dyfabandung@gmail.com'), +('yestastore@gmail.com'), +('outletmadanibeauty@gmail.com'), +('mrs.kumara@yahoo.com'), +('cindylum23@gmail.com'), +('fairy_love90@ymail.com'), +('fajarcosmetikpekanbaru@gmail.com'), +('alapstorekosmetik@gmail.com'), +('marlinabahnur@yahoo.co.id'), +('miswarderi61@gmail.com'), +('kristian_tjia@yahoo.co.id'), +('dessi.three@gmail.com'), +('rumahkurumahta@gmail.com'), +('nabilaalifia2000@gmail.com'), +('ullyfina@gmail.com'), +('tjindaidyandy@gmail.com'), +('shintayuu@gmail.com'), +('ratihpuji001@gmail.com'), +('shynobyshould@gmail.com'), +('rismawatisapu101@gmail.com'), +('caramelmadeby@gmail.com'), +('piyamapoppy@gmail.com'), +('scarlettwhitening.manado@gmail.com'), +('makeupbliss.id@gmail.com'), +('agnyjmpardosi@gmail.com'), +('sherbeautee@gmail.com'), +('yfbeauty99@gmail.com'), +('cmstadulako36@gmail.com'), +('lizalgs97@gmail.com'), +('emailnyawabisabi@gmail.com'), +('alvhiraalamrih@gmail.com'), +('megasyaf10@gmail.com'), +('ismisarisapitri6@gmail.com'), +('realrima91@gmail.com'), +('hestynorata74@gmail.com'), +('tokopinkbox@gmail.com'), +('melly.angelica@sils.co.id'), +('baguzokee@gmail.com'), +('vandy_hood@yahoo.com'), +('stevenwerencius15@gmail.com'), +('qurniaulan123@gmail.com'), +('ivanalystia21@gmail.com'), +('claralverinas@gmail.com'), +('riaarddd@gmail.com'), +('yayz_bubble@yahoo.com'), +('elsa.rivani@rocketmail.com'), +('feyrenpusura@gmail.com'), +('gunz_r@yahoo.com'), +('asrularman077@gmail.com'), +('hellojuwitaang@gmail.com'), +('bernike.daniel@gmail.com'), +('ayuerawati1993@gmail.com'), +('sudutcantik03@gmail.com'), +('norio.agnes@gmail.com'), +('smarch2u@gmail.com'), +('nunungokta8@gmail.com'), +('siaocie.tw@gmail.com'), +('sandrachen20@icloud.com'), +('siti.komariiah@gmail.com'), +('gustine.fu999@gmail.com'), +('ennysetiono@msn.com'), +('sartikanganro@gmail.com'), +('mirandahabibie@gmail.com'), +('tokodahliapadang@gmail.com'), +('natanaelsbrn@gmail.com'), +('jelitacosmetic@galeri.com'), +('rkkosmetikviral.anj@gmail.com'), +('sancaytulak26@gmail.com'), +('kelvinhosen@gmail.com'), +('elnazulma@gmail.com'), +('jevrihuang@gmail.com'), +('araitsoke@gmail.com'), +('haju.nutrifood@gmail.com'), +('harwindaharis99@gmail.com'), +('saherasukur92@gmail.com'), +('monamelosi05@gmail.com'), +('pramistiseptarinda@gmail.com'), +('rianadewi15746@gmail.com'), +('frivaldi08@gmail.com'), +('sausankho86@gmail.com'), +('3widjayanti@gmail.com'), +('prinzlina79@gmail.com'), +('restbeauty11@gmail.com'), +('kiarriyasatul@gmail.com'), +('surkiaa56@gmail.com'), +('dellikosmetik@gmail.com'), +('dinafebriana@gmail.com'), +('risnawatiyunus@yahoo.com'), +('pudoopy@gmail.com'), +('serbunganjuk@gmail.com'), +('linda_ho29@yahoo.com'), +('cassmgadm@gmail.com'), +('jennychateriner@gmail.com'), +('thasashop.id@yahoo.com'), +('diancagoodsbywitari@gmail.com'), +('chang3mu@icloud.com'), +('hana_kho@outlook.com'), +('jwl.skinofficial@gmail.com'), +('febri.ilham68@gmail.com'), +('gurl.shopaholic@gmail.com'), +('fenitan00@gmail.com'), +('nadrachanafie@gmail.com'), +('migibeauty1@gmail.com'), +('dewisusianti888@gmail.com'), +('nazwinadya@gmail.com'), +('andinimaylasari@gmail.com'), +('syahrani.cosmetic@gmail.com'), +('primasitaa@gmail.com'), +('spy_solution77@yahoo.com'), +('geeghina@gmail.com'), +('nida.syafa5@gmail.com'), +('financialelzamora@gmail.com'), +('irkosmetikdanskincare@gmail.com'), +('sherlinhosana@gmail.com'), +('sepdiarina@yahoo.com'), +('lumbangaol.ira@gmail.com'), +('aligunawan098@gmail.com'), +('ovarash@gmail.com'), +('rbeauty.storee@gmail.com'), +('cckosmetik77@gmail.com'), +('pinkcosmetich@gmail.com'), +('dhiniemsa@yahoo.com'), +('rulyindah1911@gmail.com'), +('elvina.6789@gmail.com'), +('moeyecosmetic99@gmail.com'), +('sana.19007@mhs.unesa.ac.id'), +('ikasusanti_baru@yahoo.com'), +('indaryani82@gmail.com'), +('hsyapon@gmail.com'), +('citra.ismaya@omniretail.co'), +('thevintagestrawberry@gmail.com'), +('ramadhani.kosmetik@gmail.com'), +('linawijaya2712@yahoo.com'), +('meilings0403@gmail.com'), +('nadyawkk@gmail.com'), +('prellasurabaya@gmail.com'), +('ziyyags@gmail.com'), +('stanley_oh@ymail.com'), +('andre1177az@gmail.com'), +('s.ekaputrirosalinda@gmail.com'), +('beautyland21@icloud.com'), +('javalotuscorp@gmail.com'), +('alxsuherman@gmail.com'), +('fenny3007.kwok@gmail.com'), +('leny_kurniawati@hotmail.com'), +('soputanlady10@gmail.com'), +('lampungbeauty2019@gmail.com'), +('thebeauteebox@gmail.com'), +('tfanixling00@gmail.com'), +('andilesmono06@gmail.com'), +('briggiteleorenza@gmail.com'), +('chanytatiara@gmail.com'), +('financeklinikpiramidajaya@gmail.com'), +('exacarolina@gmail.com'), +('wildhanhafid@gmail.com'), +('himakeup08@gmail.com'), +('riska.sofani@yahoo.com'), +('uciliana35@gmail.com'), +('agusjokin08@gmail.com'), +('madozai96@gmail.com'), +('flo_ver@yahoo.com'), +('dikinarasaki@gmail.com'), +('herul.batch3@gmail.com'), +('rudy.hartono2591@gmail.com'), +('vivi.plm187@gmail.com'), +('hallo@bencuan.id'), +('rahasiamama17@gmail.com'), +('stephzhengg@gmail.com'), +('shelinabeautycare@gmail.com'), +('joelchristianpamula@gmail.com'), +('rethamardjoen11@gmail.com'), +('pomadaivon@gmail.com'), +('divaparis28@gmail.com'), +('wulan.ranpatika@gmail.com'), +('mastoreyadi@gmail.com'), +('febi.muhi@gmail.com'), +('anggriawanferi1991@gmail.com'), +('feliciacolleen80@gmail.com'), +('istie.nodasekisan@gmail.com'), +('melliyniaanugrah@gmail.com'), +('dwiaziza98@gmail.com'), +('alfrianiyelsi@gmail.com'), +('adelia.vn@gmail.com'), +('alvina.iriady@gmail.com'), +('ratnaqisya03@gmail.com'), +('jelitacosmetic.galeri@gmail.com'), +('avex307@gmail.com'), +('rika.wulandaridwan@gmail.com'), +('srihartiniseptember1966@gmail.com'), +('theresia_margareths@yahoo.com'), +('kartikahs89@gmail.com'), +('yanuararifinnur@gmail.com'), +('luthfikhtr@gmail.com'), +('mshopper.id@gmail.com'), +('fifi.liu@yahoo.com'), +('christineeva07@gmail.com'), +('claralverina23@gmail.com'), +('12101na@gmail.com'), +('joanamarvelin.wongso@yahoo.com'), +('lylysiakwee@gmail.com'), +('andyirsaldi@gmail.com'), +('greceliautami@icloud.com'), +('rhayati48@gmail.com'), +('vidya@redroseindo.com'), +('ekanuraini131@gmail.com'), +('kimhiung168@gmail.com'), +('gitaporo@gmail.com'), +('aninditateresa@gmail.com'), +('lexonbeautyofficial@gmail.com'), +('novitaroshaputri@gmail.com'), +('theara.tiara@gmail.com'), +('selvi.chephy@gmail.com'), +('vhaans007@gmail.com'), +('tokosehat130@gmail.com'), +('mirayyacosmetics@gmail.com'), +('kusnen.sota@gmail.com'), +('hokimaxi2020@gmail.com'), +('keikolux@hotmail.com'), +('andreadewi19@gmail.com'), +('harlyskin@gmail.com'), +('viroe.nando@gmail.com'), +('aurelallshop@gmail.com'), +('kosmetikiis@gmail.com'), +('lifanny86@gmail.com'), +('nitamanda28@gmail.com'), +('cantiku8899@gmail.com'), +('sultanagengs3@gmail.com'), +('nurfiitriiyah@gmail.com'), +('nana01iphone@gmail.com'), +('mulyajaaya.motor96@gmail.com'), +('titarahmawati245@gmail.com'), +('marniatihinding@gmail.com'), +('erris.mahardika@gamal.men'), +('isti.qhatsah.nur@gmail.com'), +('dewilasolo@gmail.com'), +('aninditaputri30@gmail.com'), +('aryanto.firdaus@gmail.com'), +('novitaj68@gmail.com'), +('johan_breaker27@yahoo.com'), +('elsaberliya@gmail.com'), +('dewiyuant260693@gmail.com'), +('jeffrynoah@gmail.com'), +('rikasukmawar03@gmail.com'), +('yosua.sasauw1@gmail.com'), +('fitri.miauwh@gmail.com'), +('yuli120797@gmail.com'), +('kamala.tanray@gmail.com'), +('theresiad88@gmail.com'), +('isliana29@gmail.com'), +('chietra.anggraini93@gmail.com'), +('cesario2559@gmail.com'), +('the.secret.of.beauty.id@gmail.com'), +('elsacosmetics@yahoo.com'), +('evinrahmad2@gmail.com'), +('yuhyi.albadali@gmail.com'), +('darwin.saputra.ars@gmail.com'), +('akuntingtim2903@gmail.com'), +('wahyudinrazali4@gmail.com'), +('jwuysan@gmail.com'), +('ruus.silvana@gmail.com'), +('dianmarinaa@gmail.com'), +('dp.anggreani@gmail.com'), +('patricia.ang7@gmail.com'), +('medankosmetik89@gmail.com'), +('ayuputri.ap2201@gmail.com'), +('fikrizia13@gmail.com'), +('beautiva.skincare@gmail.com'), +('gjwproject@gmail.com'), +('pramudya.a.laksono@gmail.com'), +('ssshoptarakan@gmail.com'), +('bungalintong7@gmail.com'), +('ivanakaontole@gmail.com'), +('misel.delini@yahoo.com'), +('dwipujiati1995@gmail.com'), +('vdillahilda@gmail.com'), +('ellenanatasa@gmail.com'), +('bidarishabrina@gmail.com'), +('danimmm44@gmail.com'), +('evitsrikris@gmail.com'), +('gendisbeauty92@gmail.com'), +('eriana.dian0116@gmail.com'), +('eriskhfrd26@gmail.com'), +('jasmineniscalapandya@gmail.com'), +('eryryey03@gmail.com'), +('leessleepwear@gmail.com'), +('yoseptansil6@gmail.com'), +('natdailybeauty@gmail.com'), +('sidoarjosalonsupp@gmail.com'), +('auliarahmadanti77@gmail.com'), +('gustindw@gmail.com'), +('mamihcihuyyy@gmail.com'), +('narabeautytrk@gmail.com'), +('sandikaplg18@gmail.com'), +('20240010013@lspr.edu'), +('maynetav@gmail.com'), +('intandidi88@gmail.com'), +('jeniferpurnomo99@gmail.com'), +('dewisartika192@yahoo.co.id'), +('jokosus.susanto@gmail.com'), +('iswanto.1301@gmail.com'), +('kudo.palopo14@gmail.com'), +('micellangel@nuriglobal.com'), +('theeaanggraeny@gmail.com'), +('andi.or88@gmail.com'), +('rickyoktaviawan20@gmail.com'), +('yunibalisa661@gmail.com'), +('rikiscofieldsap@gmail.com'), +('feliciabeatrixtanner0407@gmail.com'), +('mon_taca@yahoo.com'), +('ardyanawidya@gmail.com'), +('d14na_nonic@yahoo.com'), +('kadirpnm@gmail.com'), +('mita.panji@gmail.com'), +('robbchriszzz13@gmail.com'), +('anicha8490@gmail.com'), +('galuh.acc.kediri@gmail.com'), +('tokosanjayakras.81@gmail.com'), +('vivian.effendi@gmail.com'), +('lisaderia@yahoo.com'), +('anggyfebryna17@gmail.com'), +('yuanita200989@gmail.com'), +('inggycitrasari@gmail.com'), +('musdalifa300396@gmail.com'), +('widdywedariasih3@gmail.com'), +('devinoariska06@gmail.com'), +('idealistina@yahoo.co.id'), +('kelsy18111@gmail.com'), +('elizchang88@gmail.com'), +('kavbeautyofficial@gmail.com'), +('archilagiska@gmail.com'), +('ginawati442@gmail.com'), +('revii.setiadi@gmail.com'), +('nisacuss@gmail.com'), +('eviana001075@gmail.com'), +('moanazone@gmail.com'), +('idabenangraja@gmail.com'), +('babybamscase@gmail.com'), +('bellasetiady@windowslive.com'), +('regina.amalia19@gmail.com'), +('hello.vebeauty@gmail.com'), +('ferinaindrayani@gmail.com'), +('yulia.cindy81@gmail.com'), +('ennakristiana@gmail.com'), +('nur.annisa0@gmail.com'), +('marindarara13@gmail.com'), +('sonyatiaratri2@gmail.com'), +('achmadfauzi1398@gmail.com'), +('fitrijean@gmail.com'), +('zul.hendra1297@gmail.com'), +('csbymagicskin@gmail.com'), +('ridadwi873@gmail.com'), +('grosirkosmetikpekanabaru@gmail.com'), +('gladys.agatha@gmail.com'), +('rahmizarti30@gmail.com'), +('mrs.sitiazizah@gmail.com'), +('paramitaqueena@yahoo.com'), +('uwiecute22@gmail.com'), +('slife557.cs@gmail.com'), +('adindazahra655@gmail.com'), +('lavibeautyandlifestyle@gmail.com'), +('ratna.pratiwi14.rp@gmail.com'), +('yunengazraapriani@gmail.com'), +('sheefaa84@gmail.com'), +('katherinelimanu21@gmail.com'), +('tutiyuniyanti@gmail.com'), +('kartinisarimah@gmail.com'), +('jonastampubolon123@gmail.com'), +('wuisancheni77@gmail.com'), +('shella.madirza888@gmail.com'), +('yulianisetiawan836@gmail.com'), +('janiceechenn@yahoo.com'), +('ferico.ridwan@gmail.com'), +('udjayamandiripgs@gmail.com'), +('budiwijaya1962@gmail.com'), +('rumahkosmetik001@gmail.com'), +('kinanbeautystore@gmail.com'), +('yuniraemah31@yahoo.co.id'), +('barokahbeaute@gmail.com'), +('shrlymarcella@gmail.com'), +('ikrt_2507@yahoo.com'), +('dikofebyan@gmail.com'), +('restyty.riezalina@yahoo.com'), +('ratukosmetik169@gmail.com'), +('faradillahif07@gmail.com'), +('sales@dewicosmetic.com'), +('kadek.arymas@gmail.com'), +('rahmaduwijayanti@gmail.com'), +('fanamaliaauthor@gmail.com'), +('vanessapratama24@gmail.com'), +('gangnamcosmetic109@gmail.com'), +('mayanglatuconsina@gmail.com'), +('lustremaiden@gmail.com'), +('ratihalexa@gmail.com'), +('anindyalarasp@gmail.com'), +('fourayoebeauty@gmail.com'), +('selenabeauty.banjarbaru@gmail.com'), +('destyisniaty@gmail.com'), +('rmdhniannisa15@gmail.com'), +('devimutiah18@gmail.com'), +('ainunmuthiaa@gmail.com'), +('nurhikmah.rasyid@gmail.com'), +('burizma.store@gmail.com'), +('ariefariq4@gmail.com'), +('haniaekap8814@gmail.com'), +('ricomerico240@gmail.com'), +('ekanvt07@gmail.com'), +('helenlim02@gmail.com'), +('lukm.hakm@gmail.com'), +('nur_aisyah1407@icloud.com'), +('serlina.mbc@gmail.com'), +('k.inayah23@gmail.com'), +('marisrisma@yahoo.com'), +('shela.annisa99@gmail.com'), +('angguntrivany@gmail.com'), +('nntheorange@gmail.com'), +('nathaliayaulin23@gmail.com'), +('mrs.gapura@gmail.com'), +('selviathalita92@gmail.com'), +('cynthia.de06@gmail.com'), +('beautemondee@gmail.com'), +('ninikrahmawati486@gmail.com'), +('anst.slvia@gmail.com'), +('beautycarepalu@gmail.com'), +('ririwiduri2002@gmail.com'), +('ilookdaily534@gmail.com'), +('apapunada.id@gmail.com'), +('dellasyaskia6@gmail.com'), +('nadiasalmau@gmail.com'), +('tikamaharaeni19@gmail.com'), +('aansabang@gmail.com'), +('devinatalithanabila98@gmail.com'), +('ennitrisha@gmail.com'), +('marialucyanasunjaya02762@gmail.com'), +('rtnohandayani@gmail.com'), +('nsbeauty06@gmail.com'), +('idapurnamasari0129@gmail.com'), +('msreysantana@gmail.com'), +('aldillaputri5@gmail.com'), +('maderusni25@gmail.com'), +('crystalglow.myshop@gmail.com'), +('dheamuliasari@gmail.com'), +('cindyfdle@gmail.com'), +('laninabeauty.official@gmail.com'), +('nimatussaadah29@gmail.com'), +('merrylaili@gmail.com'), +('wijaya.nanda1810@gmail.com'), +('kadavikadav@gmail.com'), +('anggikurniawati40@gmail.com'), +('mamikikeyu@gmail.com'), +('marlindaabdulrazak@gmail.com'), +('alizhaaa@gmail.com'), +('queenkosmetik20@gmail.com'), +('skincarekupang21@gmail.com'), +('feny.asnawi@gmail.com'), +('shanticahyadi82@gmail.com'), +('zilvanaputri@gmail.com'), +('malikaresmanti@gmail.com'), +('yunisianturi06@gmail.com'), +('beautydarlinggg@gmail.com'), +('lilybeauty.ofc@gmail.com'), +('racyasry@gmail.com'), +('terasbeauty.official@gmail.com'), +('mybeautystory.89@gmail.com'), +('novita.chyntia@yahoo.com'), +('tiffany.kimana@gmail.com'), +('rumahdandan.rd15@gmail.com'), +('desihartanti17@gmail.com'), +('forshare.id@gmail.com'), +('elmiacosmetics@gmail.com'), +('evikartika03@gmail.com'), +('allwomeneeds1403@yahoo.com'), +('nadyahudaya@yahoo.com'), +('drchrysant@gmail.com'), +('yurimeni16@gmail.com'), +('anientya@gmail.com'), +('martina.bernadeta@gmail.com'), +('wulan.dhizhi@gmail.com'), +('andrixue@gmail.com'), +('sitiqulsum308@gmail.com'), +('galaxystoreyogyakarta@gmail.com'), +('iskifitriani@yahoo.co.id'), +('arismunandar6496@gmail.com'), +('agenlingerie.id@gmail.com'), +('indahasmoro90@gmail.com'), +('ksbeautylover@gmail.com'), +('alheabeauty.id@gmail.com'), +('putriitunggal99@gmail.com'), +('ahmadmalangah@gmail.com'), +('perfectionten@gmail.com'), +('ivonsetiono26@gmail.com'), +('agencmb@gmail.com'), +('glowivier@gmail.com'), +('pinkyscosmetics1995@gmail.com'), +('evinakuesnoooo@gmail.com'), +('chennywi0126@icloud.com'), +('ekawahyuni1603@gmail.com'), +('auliaqoriardi21@gmail.com'), +('joyakhineigsoen@gmail.com'), +('susantifajarwati@yahoo.com'), +('ramadhana.selvia2408@gmail.com'), +('andarish.shop@gmail.com'), +('cindytsania@gmail.com'), +('joctora@gmail.com'), +('hannahimanuella@gmail.com'), +('dwiwulandari028@gmail.com'), +('merrychrystin06@gmail.com'), +('fibi.kdi891@gmail.com'), +('suwitosjahari@gmail.com'), +('likestoretlogosari@gmail.com'), +('care.meiyabeauty@gmail.com'), +('ajenganggitasari1@gmail.com'), +('rizkiyanitapinem@gmail.com'), +('sashop.nkc21@gmail.com'), +('lilistiyorini@gmail.com'), +('thebellas1212@gmail.com'), +('be.bellecosmetic@gmail.com'), +('nurlisaaulia@gmail.com'), +('hantybeauty19@gmail.com'), +('mputrisanti@gmail.com'), +('hellolightbit@gmail.com'), +('nadhirauw88@gmail.com'), +('asmiatiakib@gmail.com'), +('ranintya12@gmail.com'), +('afrijalrijal671@gmail.com'), +('ifayusana1993@gmail.com'), +('makeyouup.myu@gmail.com'), +('salbinus.innung@yahoo.com'), +('novia190@gmail.com'), +('evadewim.s@gmail.com'), +('itsmiracle2020@gmail.com'), +('neveltari2703@gmail.com'), +('lieusbudiman@gmail.com'), +('ferisya10@gmail.com'), +('echa.muaniez89@gmail.com'), +('trinarachmawati81@gmail.com'), +('erwinzeey123@gmail.com'), +('sahabat_debora@yahoo.com'), +('dhee2jopankar@gmail.com'), +('yunda.ade11@gmail.com'), +('cyril_steven_alexander_glory@yahoo.com'), +('devisiska32@gmail.com'), +('aureolasa.pratama@gmail.com'), +('ika.angky123@yahoo.com'), +('halimatussakdiyah1997@gmail.com'), +('rahmatjayaj2@gmail.com'), +('jesselynangella08@yahoo.com'), +('famela.ft07@gmail.com'), +('rotinsulufinny@gmail.com'), +('staryisnianti@gmail.com'), +('yulianazayan@gmail.com'), +('fandestian@gmail.com'), +('melindariaslamusnar88@gmail.com'), +('jnwkidswear@gmail.com'), +('wiwin.adriani@gmail.com'), +('febriantisiban25@gmail.com'), +('scoviabybelova@gmail.com'), +('wenny.than@gmail.com'), +('iranipontoh443@gmail.com'), +('hello.lazetta@gmail.com'), +('moullylaroid@gmail.com'), +('laurantjia@gmail.com'), +('dbeautyshop8@gmail.com'), +('godzillaphoneacc3@gmail.com'), +('wiwinstsajaah@gmail.com'), +('rizahani39@gmail.com'), +('dwihartanti_gizi@yahoo.com'), +('fangfangsaputra@gmail.com'), +('amoise.cosmetics@gmail.com'), +('dennyroseiana@gmail.com'), +('nelly.sulistiawati@gmail.com'), +('central.kosmetik1106@gmail.com'), +('marcellino.haryono@gmail.com'), +('veralia.saragih@gmail.com'), +('cefamanusia@gmail.com'), +('arindatrisna33@gmail.com'), +('oketomatala@gmail.com'), +('vilyapangajouw19@gmail.com'), +('faracintadia@gmail.com'), +('adm.elisabethbdg@yahoo.com'), +('cutmiqyal@hotmail.com'), +('harjono.honoris@gmail.com'), +('nandorian98@gmail.com'), +('milalimbong71@gmail.comcom'), +('viliansiregar24@gmail.com'), +('eldeekosmetik@gmail.com'), +('jeane.lelengboto@yahoo.com'), +('lydia.debby@gmail.com'), +('aboutmybusiness11@gmail.com'), +('triple501ara@gmail.com'), +('masfitroh@gmail.com'), +('meisalmis@gmail.com'), +('zackfair232@gmail.com'), +('nakamia_pie@yahoo.com'), +('abeauty.jogja@gmail.com'), +('lenaaska7@gmail.com'), +('zeebacosmetics31@gmail.com'), +('chiangkencana@gmail.com'), +('syaaudriana@gmail.com'), +('nadyaffu@gmail.com'), +('fauziahanisa509@gmail.com'), +('christyciasintaungu@gmail.com'), +('pradnyani.dwi@gmail.com'), +('zarazsby@gmail.com'), +('adewahyupauji@gmail.com'), +('firmantuo@gmail.com'), +('ramadhanilayla@gmail.com'), +('novascafe4@gmail.com'), +('mayaecc@yahoo.com'), +('adeyoan721@gmail.com'), +('stephaniefransss@gmail.com'), +('hermooi.official@gmail.com'), +('ratnaanita170294@gmail.com'), +('agatri.silitonga@gmail.com'), +('dewifitri2403@gmail.com'), +('moehamadsururi14@gmail.com'), +('ariestyayys@gmail.com'), +('heavenlybeauty.adm@gmail.com'), +('ikaabsarih@gmail.com'), +('dhee_amell@yahoo.co.id'), +('devianatanuwijaya@gmail.com'), +('rumah.karara@gmail.com'), +('sreejita.deb@gmail.com'), +('tagdirs.cosmetic@gmail.com'), +('ceviwilly@gmail.com'), +('daniel.khosuma@gmail.com'), +('muhammadyanis12091991@gmail.com'), +('liechintia07@gmail.com'), +('apt.deantigano@gmail.com'), +('ian.tampo22@gmail.com'), +('kunyil505@gmail.com'), +('hongjujen69@gmail.com'), +('wimma.shop@gmail.com'), +('mysamplemart@gmail.com'), +('safitriyuli3@gmail.com'), +('novanindra23@gmail.com'), +('hartantiarline@gmail.com'), +('dianyunus7@gmail.com'), +('aniyt161aan@gmail.com'), +('aestheticbeautycare23@gmail.com'), +('marissarahmaadhania9i30@gmail.com'), +('cecebeautybali@gmail.com'), +('devani.ersa@yahoo.com'), +('windiana10@gmail.com'), +('itaufik55@gmail.com'), +('zhisca_77@yahoo.co.id'), +('jillyanesasiage@gmail.com'), +('mybestiebeauty.bdg@gmail.com'), +('purchasing@bumame-farmasi.com'), +('denisa.denis89@yahoo.com'), +('ranggatedubara@gmail.com'), +('angelialim0311@gmail.com'), +('rindaagustiana@gmail.com'), +('aleshabeautycare57@gmail.com'), +('monpri28@gmail.com'), +('move.hamsari@gmail.com'), +('beautybelle2608@gmail.com'), +('priscillalastyfera@gmail.com'), +('vildarahmawati17@gmail.com'), +('nathaniaa099@gmail.com'), +('noyura@gmail.com'), +('juhatihajibe@gmail.com'), +('winda.christina@gmail.com'), +('mruslir21@gmail.com'), +('siskagsp6@gmail.com'), +('olyviadevita02@gmail.com'), +('ecgirl2023@gmail.com'), +('songlacosmetics@gmail.com'), +('jelsymakeup@gmail.com'), +('vanywidyawati86@gmail.com'), +('ongfreddykusuma@gmail.com'), +('charisthamaysadwisahara@gmail.com'), +('incjodu@gmail.com'), +('mjbeautyblitar@gmail.com'), +('ngeng.yuan27@gmail.com'), +('mybeautymart.id@gmail.com'), +('nabillafr22@gmail.com'), +('dianajennifer118@gmail.com'), +('wetirima9988@gmail.com'), +('shely.proborini@yahoo.com'), +('pak.serba@gmail.com'), +('lili_3038@yahoo.co.id'), +('tallamersiaa@gmail.com'), +('khalisanaura156@gmail.com'), +('milaatysakinah@gmail.com'), +('ucci.arfan@gmail.com'), +('lutfiah.nurhandari@gmail.com'), +('albinadhaja94@gmail.com'), +('dindaty@gmail.com'), +('shopkievanik@gmail.com'), +('zheng.jinde7692@gmail.com'), +('silvi@getvybes.co'), +('muhammadraydevan@gmail.com'), +('alstininoviyantitoar@gmail.com'), +('riahafarul778@gmail.com'), +('nrustam26@gmail.com'), +('gayabyys.wu@gmail.com'), +('lalootust@gmail.com'), +('rina2pan@yahoo.com'), +('disariska@gmail.com'), +('ninu.fira@gmail.com'), +('yuibabaretail@gmail.com'), +('makshifa93@gmail.com'), +('nadzimalaily@gmail.com'), +('nindysyarif@gmail.com'), +('mutiarapesonanganjuk@gmail.com'), +('beautybypelangi@gmail.com'), +('mariyani3412@gmail.com'), +('anitabudi1998@gmail.com'), +('farikhanum@ymail.com'), +('riscaamalia1989@gmail.com'), +('oyha.ciez01@gmail.com'), +('afriasafitri@ymail.com'), +('gitalaksmicosmetic@gmail.com'), +('oktavianimunir@gmail.com'), +('meithaekyh@gmail.com'), +('antouwkeren@gmail.com'), +('meeranova.id@gmail.com'), +('dmuhenti@gmail.com'), +('bima.ajun@gmail.com'), +('sitipluviana@gmail.com'), +('endahpratiwi522@gmail.com'), +('dindalabibah@gmail.com'), +('pusatkosmetikpinky@gmail.com'), +('anjalisibagariang2000@gmail.com'), +('krisnairma15@gmail.com'), +('ocha.rospa@gmail.com'), +('siscabudhyani86@gmail.com'), +('jhenisarykambuno@gmail.com'), +('mp-store4@raenabeauty.com'), +('luckyyosin0913@gmail.com'), +('monica1726@yahoo.co.id'), +('deasyrimanda@gmail.com'), +('pbudianoto@gmail.com'), +('hernispratiwi@gmail.com'), +('lelymelani@yahoo.com'), +('cantiksehat.sby@gmail.com'), +('gunk.fan29@gmail.com'), +('nurhayati01022021@gmail.com'), +('wardahafi@gmail.com'), +('nonikmery256@gmail.com'), +('lismiatilenta69@gmail.com'), +('dewinurhidayah5125@gmail.com'), +('nurhikmah.hasbi@gmail.com'), +('dordiaahss@gmail.com'), +('molly.fh2@gmail.com'), +('avostoresurabaya@gmail.com'), +('alhun.lun@gmail.com'), +('thebeautycare05@gmail.com'), +('mykarol88@gmail.com'), +('angela.sania@yahoo.com'), +('divaa01@ymail.com'), +('p.ariyanti2@gmail.com'), +('rizkycosmetics@gmail.com'), +('claudiayolandpurnomo@gmail.com'), +('ayudea1820@gmail.com'), +('allshopbisnis72@gmail.com'), +('yosephinetan@yahoo.co.id'), +('ayuri.minho@yahoo.com'), +('paolahermawan@yahoo.com'), +('mirna.mirnaaa02@gmail.com'), +('kumaskincare01@gmail.com'), +('officialrabeauty@gmail.com'), +('cacabeauty1234@gmail.com'), +('nadeakayuangellika@gmail.com'), +('linacaterina79@gmail.com'), +('rianishop210@gmail.com'), +('ilhamxmaulanaaa@gmail.com'), +('aratna643@gmail.com'), +('vifi2403@gmail.com'), +('paringdyahdinda@icloud.com'), +('suantysuanty10@gmail.com'), +('nandalisa045@gmail.com'), +('tinylilproject@yahoo.com'), +('arfan.arsya2022@gmail.com'), +('dahliaqueenshop@gmail.com'), +('triantiagus@gmail.com'), +('widyastevani17@gmail.com'), +('adityo_konandar@yahoo.com'), +('zoerosyidh89@gmail.com'), +('yusmane395@gmail.com'), +('tephtephyy@gmail.com'), +('seemser.id@gmail.com'), +('tantinaduu@gmail.com'), +('yannayeshika@gmail.com'), +('juliacsecogati@gmail.com'), +('pheroneid@gmail.com'), +('pauline222bn@gmail.com'), +('dpuspitaprameswari@gmail.com'), +('irnanda.vinka@yahoo.com'), +('agamwijaya89@gmail.com'), +('miniskinjars@gmail.com'), +('faizal@raenabeauty.com'), +('hanaamelinda95@gmail.com'), +('maharanimega39@gmail.com'), +('tia.aprita@gmail.com'), +('m.milanova.sh@gmail.com'), +('belscofabrics@gmail.com'), +('pratamanaufal98@gmail.com'), +('joshanshop@gmail.com'), +('ayuameliasaja@gmail.com'), +('alyadwi00@gmail.com'), +('helloyoumeindonesia@gmail.com'), +('desyrosiyana@gmail.com'), +('isvimega@gmail.com'), +('andrianiyana815@gmail.com'), +('andiakbar.ak@gmail.com'), +('isw11business@gmail.com'), +('guo_fengsheng@yahoo.com'), +('nirmaladewi03@gmail.com'), +('novitheng90@gmail.com'), +('poppymputranto@gmail.com'), +('shellahamayu1997@gmail.com'), +('suparjoariyanti@gmail.com'), +('pipinwahyu80@gmail.com'), +('chryseladarwin.olivia@gmail.com'), +('choisebest64@gmail.com'), +('rafin.trisna@gmail.com'), +('solehac098@gmail.com'), +('ayumirah12@yahoo.com'), +('fasahdi@gmail.com'), +('vindysabeauty@gmail.con'), +('dillalutchu08@gmail.com'), +('hi.dearbella@gmail.com'), +('awili.tri@gmail.com'), +('ardiradityananda@gmail.com'), +('dayanishop474@gmail.com'), +('apriliamega08@gmail.com'), +('vindysabeauty@gmail.com'), +('gilsneedit@gmail.com'), +('meiriannaafs@gmail.com'), +('mantepkebumen66@gmail.com'), +('lisnawatirohmah@gmail.com'), +('mhelydya@gmail.com'), +('apriliandesi@yahoo.co.id'), +('memonaofficial1007@gmail.com'), +('attraversiamobeauty@gmail.com'), +('triyatmoko3116@gmail.com'), +('toxena.shop@gmail.com'), +('fitridwifebriani59@gmail.com'), +('apadotid7@gmail.com'), +('misz_irma@yahoo.com'), +('riofabian112200@gmail.com'), +('indahyuliza7@gmail.com'), +('imasmaslihah2222@gmail.com'), +('letsglowid.jbi@gmail.com'), +('mahreenbeautyskin@gmail.com'), +('aldofiras1607@gmail.com'), +('firdamelia05@gmail.com'), +('hajernihajerni@gmail.com'), +('yunusdarmawan8@gmail.com'), +('fmaharani96@gmail.com'), +('tokoyangtepat@gmail.com'), +('endangdolphin2011@gmail.com'), +('pallawalipu21@gmail.com'), +('pie_buana@yahoo.com'), +('marina.diantika@gmail.com'), +('rihandika34@gmail.com'), +('ngm6756@gmail.com'), +('djonyali59@gmail.com'), +('rismagina02@gmail.com'), +('beautybyasame@gmail.com'), +('gracerushandi@gmail.com'), +('rinianggrayniinank@gmail.com'), +('lin_qiau_hua@yahoo.com'), +('buanainskonsi@gmail.com'), +('cosmetik.giant@gmail.com'), +('reynitaballack@gmail.com'), +('shopkichii88@gmail.com'), +('fadhilahafifah48@yahoo.com'), +('vivaforeva1@gmail.com'), +('sisilia.marshella@yahoo.com'), +('irmhanurjayanti@gmail.com'), +('galuhnur81@gmail.com'), +('gees.hijab@gmail.com'), +('putrimeyanti8@gmail.com'), +('rully0152@gmail.com'), +('buntukk166@gmail.com'), +('williantocindy@gmail.com'), +('nadilla@raenabeauty.com'), +('cindyfreda23@gmail.com'), +('liiyalimz@gmail.com'), +('kikaychan90@gmail.com'), +('tiffanyyaprilia@gmail.com'), +('2017120215@student.unpar.ac.id'), +('23jamesbond11@gmail.com'), +('36yonathan@gmail.com'), +('a.azzahrasyah@gmail.com'), +('a6ustina@gmail.com'), +('abyan.shop97@gmail.com'), +('acelinbeauty@gmail.com'), +('adeevaskincaresemarang@gmail.com'), +('adeyatoko@gmail.com'), +('adimahendraganteng@gmail.com'), +('agatha@raenabeauty.com'), +('agatyalaksmi@gmail.com'), +('agungfadilah810@gmail.com'), +('ahmadluckibawazier@gmail.com'), +('ai.x1.35300@sutomo-mdn.sch.id'), +('alfiatulkhubbi@gmail.com'), +('alfonsocosmetics@gmail.com'), +('alifiarizkiatania@gmail.com'), +('aline_fathin@yahoo.com'), +('alisheretotalk@gmail.com'), +('alithapradita@gmail.com'), +('allaboutskincare312@gmail.com'), +('almardhiyyahs@gmail.com'), +('almirabintangr@gmail.com'), +('alvissyahrin619@gmail.com'), +('alycia_monicaa@yahoo.co.id'), +('amaliakusumaniez@gmail.com'), +('ameicosmetic12@gmail.com'), +('amelianucifera@gmail.com'), +('ameliaresnaw@gmail.com'), +('analiani86@gmail.com'), +('andikharianto978@gmail.com'), +('angelinkayla118@gmail.com'), +('angellim0311@gmail.com'), +('angellina.lady96@gmail.com'), +('anggararnda972@gmail.com'), +('anggrini@raenabeauty.com'), +('anggriprad@gmail.com'), +('anisaauliaabas@gmail.com'), +('anisafarah24@gmail.com'), +('annachf.tan@gmail.com'), +('antodukos@gmail.com'), +('antonisang27@gmail.com'), +('appl3fuji@gmail.com'), +('arisrahmat333@gmail.com'), +('artiwayan14@gmail.com'), +('artrisnandari@gmail.com'), +('arumsekarsari573@gmail.com'), +('asepmirwan1@gmail.com'), +('assyafarrizki@gmail.com'), +('astrsari@gmail.com'), +('atikaputrims97@gmail.com'), +('audi.tabrany@gmail.com'), +('avoskinbeautybandung@gmail.com'), +('avuastore@gmail.com'), +('ayunamadewi@gmail.com'), +('babdungdyfa@gmail.com'), +('beatricetheresia1@gmail.com'), +('beautyshoujo@gmail.com'), +('bella.beautittaid@gmail.com'), +('bellaikne77@gmail.com'), +('bembie07@gmail.com'), +('beningkosmetik59@gmail.com'), +('biutidy@gmail.com'), +('brilianifridayasti@gmail.com'), +('budiondream@gmail.com'), +('business.qhansaanakkami@gmail.com'), +('cahaya.kosmetik.id@gmail.com'), +('cahyanidwisepti20@gmail.com'), +('cantikatya2@gmail.com'), +('catherinerambang620@gmail.com'), +('caty.adja@gmail.com'), +('cderz23@yahoo.com'), +('ceceliasansan8@gmail.com'), +('cerimartgo@gmail.com'), +('chandrahalim714@gmail.com'), +('chen.wong21@gmail.com'), +('cheryl.marsha.heryanto@gmail.com'), +('chococoip@gmail.com'), +('christianyendi@gmail.com'), +('cindyaharvardiana@gmail.com'), +('cinthyaruhantopp1@gmail.com'), +('cinthyaruhantopp3@gmail.com'), +('cisilia268@gmail.com'), +('clarysocimartinez@gmail.com'), +('cosellebeauty@gmail.com'), +('cvindokosmetikasejahtera@gmail.com'), +('d.lilacshop@gmail.com'), +('danielbudisantoso@gmail.com'), +('danielsimanjuntak231@gmail.com'), +('dedywijayamail@gmail.com'), +('definitionbeautyjogja@gmail.com'), +('dektasyaputra1301@gmail.com'), +('dellalisapaly@gmail.com'), +('despielia@gmail.com'), +('despielia92@gmail.com'), +('desyoong21@gmail.com'), +('deviannisme@gmail.com'), +('dewimellinda10@gmail.com'), +('dhevio1426@gmail.com'), +('dianakuliah@gmail.com'), +('dianps.haryanto@yahoo.com'), +('dillaparamita0@gmail.com'), +('dinaapril280@gmail.com'), +('dini.dwiarti3105@gmail.com'), +('dpsbykosme@gmail.com'), +('dwiagustin363@gmail.com'), +('dyas253@gmail.com'), +('dyfacafe@gmail.com'), +('eka.fokus@gmail.com'), +('eleonora@raenabeauty.com'), +('elfayunengsih20@gmail.com'), +('elisiabriana@gmail.com'), +('ellierahardja@gmail.com'), +('elsa.primasari.eps@gmail.com'), +('elsaokta26@gmail.com'), +('endahtriwedanti28@gmail.com'), +('endang.kosmetik1@gmail.com'), +('endangmarbunbbm678@gmail.com'), +('ennykoesrini676@gmail.com'), +('ennysetiono@gmail.com'), +('enokfatimah405@gmail.com'), +('entok345@gmail.com'), +('enungnurhalimah93@gmail.com'), +('envirolisasekar@gmail.com'), +('ernazulianita00@gmail.com'), +('estrellabeautee@gmail.com'), +('etienneskincare@gmail.com'), +('etisuningsih117@gmail.com'), +('evan.zelizta98@gmail.com'), +('evelyn.vio81@gmail.com'), +('evinrahmad4@gmail.com'), +('fajar950@gmail.com'), +('fajaroldman@gmail.com'), +('farah.fbindonesia@gmail.com'), +('faridahsalma@gmail.com'), +('febrina@raenabeauty.com'), +('feliceclarissa90@gmail.com'), +('fennyardisheila@gmail.com'), +('fera.julianti93@gmail.com'), +('feriscainez@gmail.com'), +('fffgh8@gmail.com'), +('fikrizia_insani@yahoo.com'), +('firabanun@gmail.com'), +('fitria_rida@yahoo.com'), +('fitriamalda123@gmail.com'), +('fitriand0192@gmail.com'), +('flavianusferdiaf@gmail.com'), +('flawless.officialshop@gmail.com'), +('flimtysurabayaofficial@gmail.com'), +('fmieayam@gmail.com'), +('freshnglow77@gmail.com'), +('friendhik25@gmail.com'), +('gabriastore@gmail.com'), +('gajahmadawaee@gmail.com'), +('galaticos02@hotmail.com'), +('gbjmarket@gmail.com'), +('givonsal@gmail.com'), +('glammarket64@gmail.com'), +('gloryharahap@gmail.com'), +('grosirmurahsukoharjo5@gmail.com'), +('guo_fengsheng@yahoo.co.id'), +('gustina.denaiyenti21@gmail.com'), +('hanselchan55@gmail.com'), +('hantarancilandak@gmail.com'), +('hardiantinila3@gmail.com'), +('hartono.henny@yahoo.com'), +('hasanyeong8@gmail.com'), +('helbert.simanjuntak@gmail.com'), +('hello.sheetmask@gmail.com'), +('hellopakbu@gmail.com'), +('helmikamanihuruk@gmail.com'), +('herdianametalic@gmail.com'), +('hestydharmayanti933@gmail.com'), +('heymarcell@gmail.com'), +('hijriahr08@gmail.com'), +('hulahulahali@gmail.com'), +('idcosmeticsidcosmetics@gmail.com'), +('ihc.korea@gmail.com'), +('ihccianjur@gmail.com'), +('iis.sugiati83@gmail.com'), +('ijallevine@gmail.com'), +('ikromyafik147@gmail.com'), +('imahgrosir@gmail.com'), +('indahpurwanti4410@gmail.com'), +('indrianimentaruk2@yahoo.com'), +('info.athirabeauty@gmail.com'), +('inggrid.kambuno@yahoo.com'), +('isbeyondstyle@gmail.com'), +('istrinyamalik@gmail.com'), +('iwanku203@gmail.com'), +('jacovcho@gmail.com'), +('jayaananda247@gmail.com'), +('jayde67@genesvjq.com'), +('jazzpro131@gmail.com'), +('jcrshop20@gmail.com'), +('jeliem96@gmail.com'), +('jelysoviana@gmail.com'), +('jesiskaruslan@gmail.com'), +('jessicaa2208@gmail.com'), +('jscang93@gmail.com'), +('jschrynt@gmail.com'), +('juliaviliajulia@gmail.com'), +('jullyangelina85@gmail.com'), +('jumrayana@gmail.com'), +('junidiarisna@gmail.com'), +('jwinarso889@gmail.com'), +('karinaagustin613@yahoo.co.id'), +('katalogskincarekorea@gmail.com'), +('keanoliem3@gmail.com'), +('kekensetiawan@gmail.com'), +('khairunniszahaifa@yahoo.com'), +('kikiseptiana92@gmail.com'), +('klikandshop18@gmail.com'), +('kntz199@gmail.com'), +('kosmetiklancarjaya@gmail.com'), +('kuboykukuh@gmail.com'), +('kunarifin@gmail.com'), +('kurniadi.phan@gmail.com'), +('kyuhaekoinline@gmail.com'), +('kyuhyunkoinline5@gmail.com'), +('lancarjayakosmetik@gmail.com'), +('lanisiska45@gmail.com'), +('laure.devienne@gmail.com'), +('lavoilla@gmail.com'), +('leejaeha31@gmail.com'), +('lenasinaga853@yahoo.com'), +('leo_azzuri@yahoo.com'), +('libbyclaudiar@gmail.com'), +('Liec534@gmail.com'), +('liedhawei@yahoo.com'), +('lina.marlinajkt228@gmail.com'), +('linamay2088@gmail.com'), +('lincebaiku34@gomail.com'), +('lindasekararum@gmail.com'), +('lindaten93@gmail.com'), +('lio.edhen@gmail.com'), +('lionachristina110887@gmail.com'), +('lolithaameliaa.la@gmail.com'), +('lolytha.arina@yahoo.co.id'), +('loverini1508@gmail.com'), +('lricosantoso@gmail.com'), +('luckyluna.id@gmail.com'), +('luckyme2611@gmail.com'), +('lusianaivang@yahoo.com'), +('luvpiggy2312@gmail.com'), +('lyannaming@gmail.com'), +('madya.wa@gmail.com'), +('mahesabeautyandstore@gmail.com'), +('maianton@yahoo.com'), +('mailto.wabisabi@gmail.com'), +('makeupfutura@gmail.com'), +('maramaratus@gmail.com'), +('marciaaact@gmail.com'), +('maria.ariesw87@gmail.com'), +('mariaboons0@gmail.com'), +('marianaulfa11@gmail.com'), +('mariiahulfah123@gmail.com'), +('marlinadiansafitri@gmail.com'), +('maroon16store@gmail.com'), +('marshandaditha@gmail.com'), +('martza.swastikasari@gmail.com'), +('maryani4398@gmail.com'), +('maryavictoryss3@gmail.com'), +('matrasunik@gmail.com'), +('maupesanposter@gmail.com'), +('maximlaundry@yahoo.com'), +('megashinta39@gmail.com'), +('mekarkosmetik.serang@gmail.com'), +('melaplestari2@gmail.com'), +('melindasuriyam01@gmail.com'), +('melisahariyanto14@gmail.com'), +('mentaripuspa59@gmail.com'), +('mh.lifestyles.in@gmail.com'), +('mikanmicheelen@gmail.com'), +('mirzaluzzia123@gmail.com'), +('misluciana@yahoo.com'), +('mitha2904aprilia@gmail.com'), +('mitufaya@gmail.com'), +('mitzukooutfit@gmail.com'), +('mizuharaaya@gmail.com'), +('mohd.nurfitriadin@gmail.com'), +('moildy.id@gmail.com'), +('monalisaflorist@gmail.com'), +('morashopmedan@gmail.com'), +('mr.yung30@gmail.com'), +('muhammad.hidayat.101@gmail.com'), +('mutiaazzahra177@gmail.com'), +('mutiarajesica0209@gmail.com'), +('mykagendis@gmail.com'), +('nadya.desnam@gmail.com'), +('nailavira102@gmail.com'), +('nanalistya22@gmail.com'), +('nandadevie1968@gmail.com'), +('nandipintaa@gmail.com'), +('nari38700@gmail.com'), +('natasyaputri4982@gmail.com'), +('naturrcosmetic@gmail.com'), +('niasepti28@gmail.com'), +('nicoags26@gmail.com'), +('nicolebeautyindonesia@gmail.com'), +('nicoyuganugraha@gmail.com'), +('ninadelinda26@gmail.com'), +('nirmala.sari709@gmail.com'), +('nonaewa@gmail.com'), +('nophaaelfrida@gmail.com'), +('nova.punya@gmail.com'), +('novandy.yohanes@gmail.com'), +('novi0128@gmail.com'), +('novitageraldinetgt@gmail.com'), +('nscollastica@gmail.com'), +('ntyaau26@gmail.com'), +('nuciferaamelia@gmail.com'), +('nugrahapiawai@gmail.com'), +('nur.wazilah@gmail.com'), +('nurazhwa170@gmail.com'), +('nurinasp@gmail.com'), +('nurlaelaramadanti@gmail.com'), +('officialbuyorbye@gmail.com'), +('oktinningrum@gmail.com'), +('opponingrum@gmail.com'), +('oyyisocial@gmail.com'), +('pajerokita@gmail.com'), +('pandridoge@gmail.com'), +('papamarvel_suhendy@yahoo.com'), +('pardosiares@gmail.com'), +('pastiibisaa@gmail.com'), +('pipitsanti999@gmail.com'), +('popoybunny@gmail.com'), +('prakososyam0403@gmail.com'), +('pramita2701@gmail.com'), +('prioritybekasi@gmail.com'), +('properbeauty.id@gmail.com'), +('psalimatulh@gmail.com'), +('puellabeautyshop@gmail.com'), +('puterifedina@gmail.com'), +('putriparapara@gmail.com'), +('putrisildosz@gmail.com'), +('putrivhidi@gmail.com'), +('putryalfionta@yahoo.com'), +('r.alika003@gmail.com'), +('r0n_ny@yahoo.com'), +('radhiah.hasan26@gmail.com'), +('rahayuwrdhn@gmail.com'), +('ranicantikhebat@gmail.com'), +('ratnashary4@gmail.com'), +('raynoldpanjaitan100@gmail.com'), +('regsmakeup@gmail.com'), +('resditamargaretha@gmail.com'), +('reviyuliswan86@gmail.com'), +('ribkanads@gmail.com'), +('richarddarmawanlaidji@gmail.com'), +('rickylie1989@gmail.com'), +('ries091@gmail.com'), +('rikiimpiandes@gmail.com'), +('rikomm822@gmail.com'), +('rinayunikartikaa@gmail.com'), +('rindaariani36@gmail.com'), +('riniismiyanti_94@yahoo.com'), +('ririalijofa@gmail.com'), +('riskalestari014@gmail.com'), +('rkcjawa@yahoo.com'), +('robeth.saputra01@gmail.com'), +('romlahabadsyam@gmail.com'), +('ronnywongsolim@gmail.com'), +('rovihshsh@gmail.com'), +('roviwardanap@gmail.com'), +('roviwardanaputra@gmail.com'), +('rperlita11@gmail.com'), +('rtjayaselalu@gmail.com'), +('rzkamll18@gmail.com'), +('sabithaanisa03@gmail.com'), +('saniaa3095@gmail.com'), +('saniyasch@gmail.com'), +('sansbeauty01@gmail.com'), +('santisujanto@gmail.com'), +('sarah@getvybes.co'), +('sari.astuty12@gmail.com'), +('sarihakim96@gmail.com'), +('sarmutias1@gmail.com'), +('sarmutias2@gmail.com'), +('sarmutias5@gmail.com'), +('seilareisya@gmail.com'), +('selfi.yonghwa08@gmail.com'), +('sendychristians@gmail.com'), +('sepriyanamelly@gmail.com'), +('serlinaepriyanti07@gmail.com'), +('setiawan.laia31@gmail.com'), +('setiawanandrew818@gmail.com'), +('sherlysherlyvi@gmail.com'), +('sholi.hijubpanda@gmail.com'), +('shopalover123@gmail.com'), +('shopbintar@gmail.com'), +('shoujoshopcosmetic@gmail.com'), +('siauwelizabeth@gmail.com'), +('sintafauziah41@gmail.com'), +('siti.lathifatul.k@gmail.com'), +('sitilathifatulkhofi@gmail.com'), +('siuni.reno@gmail.com'), +('siwiberlianti@gmail.com'), +('skincareindoid@gmail.com'), +('skinqueenproject@gmail.com'), +('smcosmeticsub@gmail.com'), +('snana602@gmail.com'), +('snrofi27@gmail.com'), +('soniadepi21@gmail.com'), +('sonie.usman@gmail.com'), +('sreejita@raenabeauty.com'), +('srialfia.san@gmail.com'), +('srihendrawati1@gmail.com'), +('sriyuliani9379@gmail.com'), +('stacia_andani91@yahoo.com'), +('staciaandani91@gmail.com'), +('stasyifa@gmail.com'), +('stefani_eli@ymail.com'), +('stepfanny91@gmail.com'), +('storemajujaya12@gmail.com'), +('sultonudin.madani21@gmail.com'), +('sun.edihtantra@gmail.com'), +('suryakosmetikjogja@gmail.com'), +('suryaniani1960@gmail.com'), +('susantiyap68@gmail.com'), +('suwandikarina61@gmail.com'), +('suwati169@gmail.com'), +('syahlaag@gmail.com'), +('syahliyaf@gmail.com'), +('syahna2128@gmail.com'), +('syahpudin09.183@gmail.com'), +('syifasahb@gmail.com'), +('tagustatang@gmail.com'), +('takiidonlineshopping@gmail.com'), +('talinazulfah@gmail.com'), +('tanmelky@yahoo.com'), +('taraaengg@gmail.com'), +('taspremium88@gmail.com'), +('taufikismail198720@gmail.com'), +('tcahyanti@gmail.com'), +('tfnhtp@gmail.com'), +('the_fortune_eo@yahoo.com'), +('theakirana133@gmail.com'), +('thebeausell@gmail.com'), +('thebeautyneed@gmail.com'), +('theidk.zone@gmail.com'), +('theskindarling@gmail.com'), +('tihanperkasadefitro@gmail.com'), +('tiifandi@gmail.com'), +('timyls@yahoo.com'), +('tina.metro@yahoo.com'), +('tioharianto.allianz@yahoo.com'), +('tjulovely2013@gmail.com'), +('toko.viragrosir88@gmail.com'), +('toko.winars.palopo@gmail.com'), +('tokounikita2@gmail.com'), +('tommy_priatama@ymail.com'), +('topupfu.sp@gmail.com'), +('tyabe888@yahoo.com'), +('ulibasagrc123@gmail.com'), +('umarsikumbang2020@icloud.com'), +('vannychristinna@yahoo.com'), +('varenunggulkarya@gmail.com'), +('veliakatarina@gmail.com'), +('velvetpinkred@gmail.com'), +('vhex307@gmail.com'), +('vidiachen@yahoo.com'), +('vilencyviranyy@gmail.com'), +('vina.astini@yahoo.com'), +('vincenttia23@gmail.com'), +('vinlie2020@gmail.com'), +('violyamerin@gmail.com'), +('viraltoday124@gmail.com'), +('virginiaca09@gmail.com'), +('viserysyvaine@gmail.com'), +('viviyuvita19@gmail.com'), +('wahyudiirwan35@gmail.com'), +('wahyuekajaya@gmail.com'), +('wahyuyana.prasetya@gmail.com'), +('wellington@nihonmart.id'), +('wellypandin76@gmail.com'), +('widoagusputra90@gmail.com'), +('wiwiekapsari@icloud.com'), +('wulan3804@gmail.com'), +('wulandary.sllh@gmail.com'), +('xenpang26@gmail.com'), +('xuanliuli0@gmail.com'), +('yayarafin@gmail.com'), +('yenimaryatin332@gmail.com'), +('yenny.irawati@yahoo.com'), +('yessicasarano16@gmail.com'), +('yogialqarni1807@gmail.com'), +('yogisugianto21@gmail.com'), +('yong2.chan@gmail.com'), +('yooabeaute@gmail.com'), +('ysurjadi@gmail.com'), +('yt40gt@gmail.com'), +('yukireiyuka@gmail.com'), +('yuliacandle@gmail.com'), +('yuliananina19@yahoo.com'), +('yunisri.hartiwi@yahoo.com'), +('yunitadewi23@gmail.com'), +('zabeth_yulie@yahoo.co.id'), +('zahraashop18@gmail.com'), +('zahrabeauty.share@gmail.com'), +('zelmi5009@gmail.com'), +('zharasonline@gmail.com'), +('zivenakbar08@gmail.com'), +('Fanamaliauthor@gmail.com'), +('tjuganteng@gmailcom'), +('ayusisicantik@gmail.com'), +('tsetiyaningsih71@gmail.com'), +('oyyiapple@gmail.com'), +('angellim0311@yahoo.com'), +('fericoridwan@gmail.com'), +('Ikasusanti_story@yahoo.com'), +('hamkyu359@gmail.com'); + +DROP TABLE public.SKU_base_recommendation_base; + + +CREATE TABLE public.SKU_base_recommendation_base AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM public.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +WHERE transaction_date::date>dateadd(DAY,-90,'$reportDate') + AND A.cogs IS NOT NULL + AND A.cogs>0 + and (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false' +GROUP BY 1, + 2, + 3, + 4, + 5 ; + + +DROP TABLE public.reseller_base_recommendation_base; + + +CREATE TABLE public.reseller_base_recommendation_base AS +SELECT reseller_id , + A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type , + --shipping_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM public.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +INNER JOIN public.recommendation_power_seller_base BB ON A.reseller_email = BB.email +WHERE transaction_date::date>dateadd(DAY,-180,'$reportDate') + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND reseller_id IS NOT NULL +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE public.reseller_base_recommendation_base_stage1; + + +CREATE TABLE public.reseller_base_recommendation_base_stage1 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>=10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM public.reseller_base_recommendation_base; + +DROP TABLE IF EXISTS public.raw_scraping_data; + + +CREATE TABLE public.raw_scraping_data AS +SELECT E.raena_product_sku, + F.name SKU_name, + E.rce_product_id , + E.rce_variant_id, + A.category_name , + A.brand_name , + A.product_page_url , + rce_source_product_name, + product_variant_name, + product_variant_price, + product_variant_price_before_discount, + product_sold_total, + product_sold, + product_price_min, + product_price_min_before_discount, + product_price_max, + product_price_max_before_discount, + cast(percentage_match AS decimal(22,2)) percentage_match, + ratings +FROM public.raena_shopee_product_mapping E +LEFT JOIN + (SELECT A.id rce_product_id, + D.id rce_product_varient_id , + B.category_name , + C.brand_name , + product_page_url , + rce_source_product_name, + product_variant_name, + product_variant_price/10000 product_variant_price, + product_variant_price_before_discount/10000 product_variant_price_before_discount, + product_sold_total, + product_sold, + product_price_min/10000 product_price_min, + product_price_min_before_discount/10000 product_price_min_before_discount, + product_price_max/10000 product_price_max, + product_price_max_before_discount/10000 product_price_max_before_discount, + ratings + FROM raena_spider_management.rce_product A + LEFT JOIN raena_spider_management.rce_category B ON A.rce_category_id = B.id + LEFT JOIN raena_spider_management.rce_brand C ON A.rce_brand_id = C.id + LEFT JOIN raena_spider_management.rce_product_variant D ON A.id = D.rce_product_id) A ON E.rce_product_id = A.rce_product_id +AND cast(replace(E.rce_variant_id,'.0','') AS varchar) = cast(replace(A.rce_product_varient_id,',','') AS varchar) +LEFT JOIN raena_catalog_management.product F ON E.raena_product_sku=F.sku ; + + +DROP TABLE IF EXISTS public.raw_scraping_data_stage ; + + +CREATE TABLE public.raw_scraping_data_stage AS +SELECT A.* +FROM public.raw_scraping_data A +INNER JOIN + (SELECT raena_product_sku, + max(percentage_match) percentage_match + FROM public.raw_scraping_data + GROUP BY 1) B ON A.raena_product_sku = B.raena_product_sku +AND A.percentage_match =B.percentage_match; + + +DROP TABLE IF EXISTS public.sku_sales_factor; + + +CREATE TABLE public.sku_sales_factor AS +SELECT SKU, + cast(cast(sum(B.total_quantity*3) AS decimal(22,2))/cast(SUM(A.total_quantity) AS decimal(22,2)) AS decimal(22,2)) sale_factor +FROM public.SKU_base_recommendation_base A +LEFT JOIN + (SELECT raena_product_sku, + sum(DISTINCT product_sold) total_quantity + FROM public.raw_scraping_data_stage + GROUP BY 1) B ON A.sku = B.raena_product_sku +WHERE A.Pre_discount_gm>=10 +GROUP BY 1; + + +DROP TABLE IF EXISTS public.recommendation_on_low_sku_task1; + + +CREATE TABLE public.recommendation_on_low_sku_task1 AS +SELECT C.mobile Reseller_mobile , + C.email reseller_email, + A.sku, + A.sku_name, + A.brand_name, + A.category_name , + A.product_type , + Total_quantity, + total_payment_price, + total_cogs, + A.pre_discount_gm, + coalesce(B.sale_factor,0) sale_factor, + A.pre_discount_gm*coalesce(B.sale_factor,1) sorting +FROM public.reseller_base_recommendation_base_stage1 A +INNER JOIN raena_user_management.user C ON A.reseller_id = C.id +INNER JOIN + (SELECT sku , + pre_discount_gm + FROM public.SKU_base_recommendation_base) D ON A.sku = D.sku +AND D.pre_discount_gm >=10 +inner join public.sku_in_stock E on A.sku = E.sku and E.in_stock >=10 +LEFT JOIN public.sku_sales_factor B ON A.sku = B.sku +WHERE Gm_type = 'DDB' +ORDER BY reseller_id , + sale_factor DESC; +---------------------------------------------task 2 --------------- + + + + + +DROP TABLE public.SKU_base_recommendation_base_v2; + + +CREATE TABLE public.SKU_base_recommendation_base_v2 AS +SELECT A.sku, + ((sum((A.retail_price-seller_margin) *quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM public.gm_dashboard A +WHERE transaction_date::date>dateadd(DAY,-90,'$reportDate') + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND tier_name ='GOLD' +GROUP BY 1; + +DROP TABLE IF EXISTS public.recommendation_base_historical_data_offender; + + +CREATE TABLE public.recommendation_base_historical_data_offender AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type + FROM public.reseller_base_recommendation_base_stage1 + WHERE gm_type ='Offender') A +LEFT JOIN public.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN public.sku_sales_factor C ON B.mapping_group_base = C.sku +LEFT JOIN public.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku and Pre_discount_gm>=10 +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM raena_catalog_management.product B + INNER JOIN raena_catalog_management.category C ON B.category_id = C.id + INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id + where (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false') E ON B.mapping_group_base= E.SKU; + + + DROP TABLE IF EXISTS public.recommendation_base_historical_data_ddb; + + +CREATE TABLE public.recommendation_base_historical_data_ddb AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + A.Pre_discount_gm reseller_pre_discount_gm, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + D.Pre_discount_gm, + in_stock +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type, + Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1 + WHERE gm_type ='DDB') A +LEFT JOIN public.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN public.sku_sales_factor C ON A.SKU = C.sku +LEFT JOIN public.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND D.Pre_discount_gm>=10 +LEFT JOIN public.sku_in_stock F ON B.mapping_group_base = F.sku +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM raena_catalog_management.product B + INNER JOIN raena_catalog_management.category C ON B.category_id = C.id + INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id + where (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false') E ON B.mapping_group_base= E.SKU; + + DROP TABLE public.recommentdation_offender_sku_concern; + + +CREATE TABLE public.recommentdation_offender_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM public.recommendation_base_historical_data_offender B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE recommended_concern_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC; + + + DROP TABLE public.recommentdation_offender_sku_ingredient; + + +CREATE TABLE public.recommentdation_offender_sku_ingredient AS +SELECT reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM public.recommendation_base_historical_data_offender B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +AND recommended_ingredient_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC ; + + + DROP TABLE IF EXISTS public.recommentdation_ddb_sku_concern ; + + +CREATE TABLE public.recommentdation_ddb_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM public.recommendation_base_historical_data_ddb B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE recommended_concern_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM public.recommendation_base_historical_data_ddb B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE in_stock>50 and reseller_pre_discount_gm>10; + +DROP TABLE IF EXISTS public.recommentdation_ddb_sku_ingredient ; + + +CREATE TABLE public.recommentdation_ddb_sku_ingredient AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM public.recommendation_base_historical_data_ddb B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE recommended_ingredient_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM public.recommendation_base_historical_data_ddb B +INNER JOIN raena_user_management.user C ON B.reseller_id = C.id +WHERE in_stock>50 +and reseller_pre_discount_gm >=10; + +DROP TABLE public.recommentdation_sku_ingredient; + + +CREATE TABLE public.recommentdation_sku_ingredient AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + Recommended_pre_discount_gm, + sale_factor , + (Ingredient_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm) sorting, + cast(json_extract_path_text(BB.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + cast(sale_factor AS decimal(22,2)) sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM public.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM public.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join raena_catalog_management.product BB on AA.Recommended_sku= BB.sku + inner JOIN public.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + + +DROP TABLE public.recommentdation_sku_ingredient_purchase; + + +CREATE TABLE public.recommentdation_sku_ingredient_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(json_extract_path_text(BB.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM public.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku, + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM public.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join raena_catalog_management.product BB on AA.sku= BB.sku + inner JOIN public.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + + +DROP TABLE public.recommentdation_sku_concern; + + +CREATE TABLE public.recommentdation_sku_concern AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + Recommended_pre_discount_gm, + sale_factor , + concern_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm sorting, + cast(json_extract_path_text(BB.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM public.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM public.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join raena_catalog_management.product BB on AA.Recommended_sku= BB.sku + inner JOIN public.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + +DROP TABLE public.recommentdation_sku_concern_purchase; + + +CREATE TABLE public.recommentdation_sku_concern_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(json_extract_path_text(BB.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM public.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM public.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join raena_catalog_management.product BB on AA.sku= BB.sku + inner JOIN public.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + -----------------------------------------------Task 3 ------------------------------- +--inner join (select distinct id as order_id , json_extract_path_text(reseller_info,'province',TRUE) reseller_province +-- from raena_order_management.order +-- where created_at::date>dateadd(DAY,-91,'$reportDate') and payment_status='Paid') AA on A.external_id = AA.order_id + + +DROP TABLE public.SKU_base_recommendation_base_t3 ; + + +CREATE TABLE public.SKU_base_recommendation_base_t3 AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + shipping_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM public.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +WHERE transaction_date::date>dateadd(DAY,-90,'$reportDate') + AND A.cogs IS NOT NULL + AND A.cogs>0 + and (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false' +GROUP BY 1, + 2, + 3, + 4, + 5, + 6 ; + +DROP TABLE public.reseller_base_recommendation_base_t3; + + +CREATE TABLE public.reseller_base_recommendation_base_t3 AS +SELECT reseller_id , + A.sku, + case when shipping_to ='Customer' then shipping_province end as dropshipping_province, + case when shipping_to ='Reseller' then shipping_province end as reseller_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin) *quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Post_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM public.gm_dashboard A +INNER JOIN + (SELECT DISTINCT id AS order_id , + shipping_to + FROM raena_order_management.order + WHERE created_at::date>dateadd(DAY,-180,'$reportDate') + AND payment_status='Paid') AA ON A.external_id = AA.order_id +INNER JOIN public.recommendation_power_seller_base BB ON A.reseller_email = BB.email +WHERE transaction_date::date>dateadd(DAY,-180,'$reportDate') + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND reseller_id IS NOT NULL + AND order_placed_by ='Admin Panel' + AND shipping_to <> 'Marketplace' +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE public.reseller_base_recommendation_base_stage1_t3; + + +CREATE TABLE public.reseller_base_recommendation_base_stage1_t3 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM public.reseller_base_recommendation_base_t3; + + +DROP TABLE public.same_province_with_diff_sku_dropshipping; + + +CREATE TABLE public.same_province_with_diff_sku_dropshipping AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk +FROM + (SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + sum(total_quantity_sku)over(partition BY A.reseller_id ,A.SKU) total_quantity_sku , + B.Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN + (SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm, + in_stock + FROM public.SKU_base_recommendation_base_t3 A + INNER JOIN public.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join public.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6,9)B ON A.dropshipping_province = B.shipping_province where dropshipping_province is not null )A +WHERE A.sku <> A.recommended_sku and dropshipping_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + + + DROP TABLE public.recommendation_same_province_with_diff_sku_dropshipping; + + +CREATE TABLE public.recommendation_same_province_with_diff_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM public.same_province_with_diff_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = B.id +WHERE Pre_discount_gm >=10; + +DROP TABLE public.same_province_with_diff_sku_reseller; + + +CREATE TABLE public.same_province_with_diff_sku_reseller AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM + (SELECT A.reseller_id , + A.sku , + A.reseller_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + sum(total_quantity_sku)over(partition BY A.reseller_id ,A.SKU) total_quantity_sku , + B.Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN + (SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM public.SKU_base_recommendation_base_t3 A + INNER JOIN public.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join public.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6)B ON A.reseller_province = B.shipping_province where A.reseller_province is not null )A +WHERE A.sku <> A.recommended_sku and A.reseller_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + +DROP TABLE public.recommendation_same_province_with_diff_sku_reseller; + + +CREATE TABLE public.recommendation_same_province_with_diff_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM public.same_province_with_diff_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = B.id +WHERE Pre_discount_gm >=10; + +DROP TABLE public.same_province_with_same_sku_dropshipping; + + +CREATE TABLE public.same_province_with_same_sku_dropshipping AS +select * from +( +SELECT *, + total_quantity_sku/total_quantity sales_factor, +row_number() over(partition by reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk +FROM + (SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + sum(total_quantity_sku)over(partition BY A.reseller_id ,A.SKU) total_quantity_sku , + B.Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN + (SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM public.SKU_base_recommendation_base_t3 A + INNER JOIN public.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join public.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6)B ON A.dropshipping_province = B.shipping_province where A.dropshipping_province is not null )A +WHERE sku = recommended_sku and A.dropshipping_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + +DROP TABLE public.recommendation_same_province_with_same_sku_dropshipping; + + +CREATE TABLE public.recommendation_same_province_with_same_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM public.same_province_with_same_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = B.id +WHERE Pre_discount_gm >=10; + + DROP TABLE public.same_province_with_same_sku_reseller; + + +CREATE TABLE public.same_province_with_same_sku_reseller AS +select * from ( +SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM + (SELECT A.reseller_id , + A.sku , + A.reseller_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + sum(total_quantity_sku)over(partition BY A.reseller_id ,A.SKU) total_quantity_sku , + B.Pre_discount_gm + FROM public.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN + (SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM public.SKU_base_recommendation_base_t3 A + INNER JOIN public.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join public.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6)B ON A.reseller_province = B.shipping_province where A.reseller_province is not null )A +WHERE sku = recommended_sku and A.reseller_province is not null )AA + where rnk<11 +ORDER BY sku , + rnk; + + DROP TABLE public.recommendation_same_province_with_same_sku_reseller; + + +CREATE TABLE public.recommendation_same_province_with_same_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM public.same_province_with_same_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = B.id +WHERE Pre_discount_gm >=10; + + +" > /home/ec2-user/cronjob/Am_recommendation/am_recommendation.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_recommendation/am_recommendation.sql > am_recommendation.log + diff --git a/DB_funnel_new_users.log b/DB_funnel_new_users.log new file mode 100644 index 0000000..7ccc129 --- /dev/null +++ b/DB_funnel_new_users.log @@ -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 diff --git a/Final_GM_v1.sql b/Final_GM_v1.sql new file mode 100644 index 0000000..ac62538 --- /dev/null +++ b/Final_GM_v1.sql @@ -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; + + diff --git a/Gm_dashboard/.etl_gmdashboard_new_v4.sh.swo b/Gm_dashboard/.etl_gmdashboard_new_v4.sh.swo new file mode 100644 index 0000000000000000000000000000000000000000..7ac2275393c2f7b3b6c9fb8dcd451ee430e76c70 GIT binary patch literal 12288 zcmeI%yH3L}6o6qDu_6$>K!MrLr4{NMAO^;=MowZiMI?^m1U7g@9*CQxCJdE2bd2;{ zvX720NzS)LPQ#~%$N5Ud!nccl*tKdSgvnyrIn#`DhvK%DQ7G>gawVgs(zSH9YYU@Y zDZJQw22_x}EW{Qv*;`H%14='$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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_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, + price_type +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, + actual_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.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, + actual_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, + price_type +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) + 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-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + case when product_type_class in ('Flash Bundle','Flash') then 'Flash' + when coupon_applied_on ='Cart' and price_type ='NEGOTIATED_PRICE' + then 'Negotiated' + when coupon_applied_on = 'Shipping Fee' then 'Shipping' + when coupon_applied_on is not null then 'Brand' + end discount_type +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 A.external_id = B.external_id + AND A.parent_sku = B.parent_sku then (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/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 + 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; + +--Alter table public.base_netsuite_final +--add columns discount_type varchar,promo_amount decimal(22,2),actual_wholesale_price decimal(22,2); + + +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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0)-coalesce((case when final_discount >0 then 0 else actual_wholesale_price*quantity - effective_wholesale_price*quantity end ),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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,0 diff, + discount_type, + case when final_discount >0 then 0 else actual_wholesale_price*quantity - effective_wholesale_price*quantity end promo_amount, + actual_wholesale_price +FROM public.order_level_data A +LEFT JOIN public.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + +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' AS 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, + CASE + WHEN B.discount_type = 'Flash' + AND promo_amount IS NOT NULL THEN 'Flash' + WHEN B.discount_type = 'Negotiated' + AND promo_amount IS NOT NULL THEN 'Negotiated' + WHEN promo_amount IS NOT NULL THEN 'Brand' + END discount_promo_type , + promo_amount, + actual_wholesale_price +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, + 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', + '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 , + Customer_type, + BB.gm_target, + CASE + WHEN B.discount_type = 'Flash' + AND promo_amount IS NOT NULL THEN 'Flash' + WHEN B.discount_type = 'Negotiated' + AND promo_amount IS NOT NULL THEN 'Negotiated' + WHEN promo_amount IS NOT NULL THEN 'Brand' + END , + promo_amount, + actual_wholesale_price; + + + + +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, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price +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, + discount_promo_type, + promo_amount, + actual_wholesale_price +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 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price +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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + 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, + discount_promo_type; + +--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; + +DROP TABLE IF EXISTS public.GM_GROWTH_TAB3; +CREATE TABLE public.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) + +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) + +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) + +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) +ORDER BY 3; + + +" > /home/ec2-user/cronjob/Gm_dashboard/Final_GM_v2.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_v2.sql > etlTransaction_job_v2.log + + + + + + diff --git a/Gm_dashboard/Final_GM_v1.sql b/Gm_dashboard/Final_GM_v1.sql new file mode 100644 index 0000000..e231c03 --- /dev/null +++ b/Gm_dashboard/Final_GM_v1.sql @@ -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; + + diff --git a/Gm_dashboard/Final_GM_v2.sql b/Gm_dashboard/Final_GM_v2.sql new file mode 100644 index 0000000..e6320ae --- /dev/null +++ b/Gm_dashboard/Final_GM_v2.sql @@ -0,0 +1,1937 @@ + + +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 DISTINCT external_id, + transaction_date+interval'7 hours' 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, + is_campaign, + order_placed_by +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, + is_campaign, + order_placed_by + FROM raena_order_management.order A + WHERE payment_status='Paid' + AND cast(A.created_at+interval '7 hours' AS date) >='2023-09-24' ) A +LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code; + + +SELECT max(created_at) +FROM raena_order_management.order ; + + +DROP TABLE IF EXISTS public.base_netsuite_stage1_V1; + + +CREATE TABLE public.base_netsuite_stage1_V1 AS +SELECT DISTINCT transaction_date 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_wholesale_price, + CASE + WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount + ELSE B.coupon_discount + END discount_price, + CASE + WHEN discount_amount > 0 + AND A.coupon_code IS NULL + AND A.order_placed_by = 'admin' + AND coupon_applied_on IS NULL THEN 'Cart' + ELSE coupon_applied_on + END 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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type, + a.is_campaign, + B.cogs_discount_type +FROM public.order_level_data A +LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id and status <> 'Cancelled' +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, + actual_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+interval'7 hours' AS date)>='2023-09-24') F ON B.id = F.sales_sub_order_id +ORDER BY 1, + 2; + + +DROP TABLE IF EXISTS public.base_netsuite_stage2_v1_base ; + + +CREATE TABLE public.base_netsuite_stage2_v1_base 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, + actual_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, + price_type, + is_campaign, + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND A.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + cogs_discount_type +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 +LEFT JOIN + (SELECT distinct B.order_id , + A.sku , + A.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) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON A.sku = D.sku +AND A.transaction_date::date BETWEEN D.created_at AND last_date; + + +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 quantity, + parent_quantity, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE retail_price + END retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_retail_price + END parent_retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE effective_wholesale_price + END effective_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_wholesale_price + END parent_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE actual_wholesale_price + END actual_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_price + END discount_price, + coupon_applied_on, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_amount + END discount_amount, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE dynamic_price + END dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_dynamic_price + END parent_dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_price + END payment_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_amount + END payment_amount, + product_class, + parent_product_class, + product_type_class, + sales_sub_order_id, + loyalty_discount, + price_type, + is_campaign, + CASE + WHEN A.sku = D.sku THEN 0 + ELSE cogs + END cogs, + cogs_flag, + cogs_discount_type +FROM public.base_netsuite_stage2_v1_base A +LEFT JOIN + (SELECT DISTINCT sku + FROM raena_catalog_management.product + WHERE lower(name) LIKE '%not for sale%' + OR lower(name) LIKE '%free item%' + OR lower(name) LIKE '%free gift%') D ON A.sku = D.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 + AND retail_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 discount_price + + WHEN coupon_applied_on ='Cart' + AND discount_price=0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id) + END AS Final_discount , + retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + CASE + WHEN product_type_class IN ('Flash Bundle', + 'Flash') THEN 'Flash' + WHEN coupon_applied_on ='Cart' + AND price_type ='NEGOTIATED_PRICE' THEN 'Negotiated' + WHEN coupon_applied_on = 'Shipping Fee' THEN 'Shipping' + WHEN coupon_applied_on IS NOT NULL THEN 'Brand' + END discount_type +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 A.external_id = B.external_id + AND A.parent_sku = B.parent_sku THEN (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/ttl_wholesale_price) + END AS additional_discount, + CASE + WHEN final_discount<>0 THEN final_discount-seller_margin + END AS 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 + 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 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) -coalesce((CASE WHEN final_discount >0 THEN 0 ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity END),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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, + 0 diff, + CASE + WHEN price_type IS NOT NULL THEN price_type + ELSE discount_type + END discount_type , + CASE + WHEN final_discount >0 THEN 0 + ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity + END promo_amount, + actual_wholesale_price, + effective_wholesale_price, + A.is_campaign, + '' sales_sub_order_id, + cogs_discount_type, + parent_sku +FROM public.order_level_data A +LEFT JOIN public.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + + +with sales_sub_order_table as( +select order_id , parent_sku ,quantity, payment_amount,payment_amount/quantity payment_amount_quantity +from raena_order_management.sales_sub_order sso +where order_id in ('OD1668674111980647REG','OD1669347541350078REG','OD1668588329391772REG') +) +update public.base_netsuite_final +set discounted_price =payment_amount_quantity +from public.base_netsuite_final G +Inner join sales_sub_order_table B on G.external_id = B.order_id +and G.product_sku = B.parent_sku ; + + +SELECT max(transaction_date) Netsuite_base_date +FROM public.base_netsuite_final; + + + +DROP TABLE IF EXISTS public.gm_dimensions_stage1; + +CREATE TABLE public.gm_dimensions_stage1 AS +SELECT distinct 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, + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + is_campaign +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 +LEFT JOIN raena_order_management.channel D ON A.channel_id = D.id +WHERE payment_status ='Paid' + AND cast(A.created_at+interval'7 hours' AS date) >='2023-09-24'; + +drop table if exists sku_gm_mapping; + +create table sku_gm_mapping +as +select A.sku ,A.name sku_name,B.name brand_name,C.name as category_name,product_type + 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 ; + + +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 AS transaction_date, + B.order_discount_amount , + B.shipping_cost , + A.shipping_province, + B.coupon_code , + A.discount_type , + B.coupon_applied_on , + brand_name, + category_name , + product_type , + product_sku sku, + 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 , + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type , + Customer_type, + BB.gm_target, + B.discount_type discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + coalesce(cogs_type,CASE WHEN B.product_sku = D.sku + AND B.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + A.is_campaign, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END AS DSF_Flag, + cogs_discount_type, + order_type, + parent_sku +FROM public.gm_dimensions_stage1 A +INNER JOIN public.base_netsuite_final B ON A.external_id =B.external_id +LEFT JOIN + (SELECT distinct B.order_id , + A.sku , + A.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) C ON B.product_sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON B.product_sku = D.sku +AND B.transaction_date::date BETWEEN D.created_at AND last_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 B.product_sku = BB.sku +AND A.reseller_tier_name =BB.tierName +left join sku_gm_mapping AAA on B.product_sku = AAA.sku +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid') ; + +SELECT max(transaction_date) gm_dashboard_date +FROM public.GM_dashboard; + +DROP TABLE IF EXISTS public.GM_dashboard_v1; + +CREATE TABLE public.GM_dashboard_v1 AS +SELECT * +FROM public.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + +DELETE +FROM public.GM_dashboard +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + +INSERT INTO public.GM_dashboard +SELECT external_id, + transaction_date, + order_discount_amount, + shipping_cost, + shipping_province, + coupon_code, + discount_type, + coupon_applied_on, + brand_name, + category_name, + product_type, + sku, + sku_name, + reseller_name, + reseller_email, + reseller_mobile, + tier_name, + reseller_id, + quantity, + 0 retail_price, + 0 seller_margin, + 0 , + 0 , + 0, + 0, + item_type, + order_placed_by, + 0 cogs, + order_recipient, + brand_type, + customer_type, + gm_target, + discount_promo_type, + 0 promo_amount, + 0, + 0, + cogs_flag, + is_campaign, + dsf_flag, + cogs_discount_type, + order_type, + parent_sku +FROM public.GM_dashboard_v1 +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + + + +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, + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when E.name is not null then 'Channel Order' + else 'Reseller Order' + end order_type, + 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,'province',TRUE) reseller_province, + 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 +LEFT JOIN raena_order_management.channel E ON A.channel_id = E.id +WHERE cast(A.created_at+interval'7 hours' AS date) >='2023-09-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 DISTINCT 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 , + a.is_campaign , + C.Country, + B.quantity , + B.retail_price, + seller_margin, + discounted_price, + loyalty_discount, + effective_coupon_discount , + additional_discount, + item_type, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END DSF_flag, + 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type, + A.order_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 +WHERE A.external_id NOT IN + ( SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid'); + +DROP TABLE IF EXISTS public.business_report_v1; + + +CREATE TABLE public.business_report_v1 AS +SELECT * +FROM public.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + +DELETE +FROM public.business_report USING + (SELECT DISTINCT sku, + sku_name, + external_id + FROM public.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B +WHERE public.business_report.external_id = B.external_id + AND public.business_report.sku=B.sku + AND public.business_report.external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + +INSERT INTO public.business_report +SELECT external_id, + created_date, + order_discount_amount, + shipping_cost, + coupon_code, + brand_name, + product_type, + category_name, + sku_name, + sku, + reseller_id, + customer_id, + cart_id, + flash_sale_id, + shipping_to, + payment_status, + status, + stock_type, + order_placed_by, + order_warehouse, + channel_id, + medium, + marketplace, + provider, + 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, + country, + quantity, + 0 retail_price, + 0 seller_margin, + 0 discount_price, + 0 loyalty_discount, + 0 effective_coupon_discount, + 0 additional_discount, + item_type, + discount_promo_type, + 0 promo_amount, + 0 actual_wholesale_price, + 0 effective_wholesale_price, + reseller_province, + dsf_flag, + brand_type, + order_type +FROM + (SELECT public.business_report_v1.* + FROM public.business_report_v1 + INNER JOIN + (SELECT DISTINCT sku, + sku_name, + external_id + FROM public.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B ON public.business_report_v1.external_id = B.external_id + AND public.business_report_v1.sku=B.sku) A +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + +SELECT max(created_date) business_date +FROM public.business_report; + +DELETE +FROM public.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + + +INSERT INTO OM_GM_DB_Product_category +SELECT external_id, + transaction_date, + "Time", + order_discount_amount, + shipping_cost, + coupon_code , + discount_type , + coupon_applied_on , + brand_name, + category_name , + product_type , + sku, + sku_name , + reseller_name , + reseller_email , + base.reseller_mobile , + tier_name , + reseller_id , + quantity , + retail_price , + seller_margin , + discounted_price , + additional_discount, + item_type , + order_placed_by , + cogs, + shipping_province , + Flag , + wholesale_price, + order_recipient, + brand_type, + Customer_type, + gm_target, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + CASE + WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + is_campaign, + dsf_flag, + cogs_flag, + cogs_discount_type, + order_type, + parent_sku +FROM + (SELECT A.external_id , + cast(transaction_date AS date) 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 , + A.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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + is_campaign, + A.dsf_flag, + A.cogs_flag, + A.cogs_discount_type, + A.order_type, + parent_sku + 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))/(CASE WHEN retail_price<>0 THEN (retail_price-seller_margin)*quantity END) 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 cast(transaction_date AS date) >='2023-09-24' + AND A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid')) base +LEFT JOIN user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(transaction_date) om_gm_db_product_date +FROM public.OM_GM_DB_Product_category; + +update public.gm_dashboard + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from public.gm_dashboard where sku like '______X'); + + update public.om_gm_db_product_category + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from public.om_gm_db_product_category where sku like '______X'); + +DELETE +FROM public.GM_GROWTH_TAB1 +WHERE created_date >='2023-09-24'; + +INSERT INTO public.GM_GROWTH_TAB1 +SELECT tier, + 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, + base.reseller_mobile, + Number_of_Orders , + RSP , + wholesale_price , + Payment_Price , + Effective_Coupon_Discount, + Total_Loyalty_point, + additional_discount, + seller_margin, + AOV, + quantity, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + CASE + WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + external_id, + order_type +FROM + (SELECT tier, + 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, + reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + external_id, + order_type + FROM + (SELECT DISTINCT coalesce(a.tier,'NA') AS tier, + 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, + reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + 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) >= '2023-09-24' + AND channel_id IS NOT NULL) b ON a.external_id=b.id + WHERE payment_status='Paid' + AND created_date >= '2023-09-24') + GROUP BY tier, + 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, + reseller_mobile, + external_id, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type) base +LEFT JOIN user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(created_date) gm_growth_date +FROM public.GM_GROWTH_TAB1; + + +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; + + +DROP TABLE IF EXISTS public.GM_GROWTH_TAB3; +CREATE TABLE public.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +ORDER BY 3; + +DROP TABLE shipping_fee_base_table; + +CREATE TABLE shipping_fee_base_table AS +SELECT A.order_id , + sum(CASE WHEN A.sales_sub_order_shipment_id = C.reference_id THEN ((C.shipping_amount)/(C.order_weight))*(C.final_weight) ELSE B.shipping_amount END) shipping_amount +FROM + (SELECT DISTINCT order_id , + sales_sub_order_shipment_id + FROM raena_order_management.sales_sub_order) A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.sales_sub_order_shipment_id = B.id +LEFT JOIN public.OM_Logistic_final_shipping C ON A.sales_sub_order_shipment_id = C.reference_id +GROUP BY A.order_id; + +DROP TABLE public.shipping_fee_sku_base_stage1 ; + +CREATE TABLE public.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + +DROP TABLE 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 public.shipping_fee_sku_base_stage2_v1; + +CREATE TABLE public.shipping_fee_sku_base_stage2_v1 AS +SELECT order_id , sales_sub_order_shipment_id, + CASE + WHEN (sum(B.weight*Quantity))<1.3 THEN 1 + WHEN (sum(B.weight*Quantity))>=1.3 + AND (ABS(sum(B.weight*Quantity)) - FLOOR(ABS(sum(B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.weight*Quantity))+1 + ELSE FLOOR(sum(B.weight*Quantity)) + END AS sku_weight +FROM public.shipping_fee_sku_base_stage1 A +LEFT JOIN public.weight_sku_table B ON A.sku = B.sku +group by 1,2; + +DROP TABLE public.shipping_fee_sku_base_stage2; + +CREATE TABLE public.shipping_fee_sku_base_stage2 AS +SELECT A.*, + B.weight*sku_weight/sum(B.weight) over(partition by A.order_id , A.sales_sub_order_shipment_id ) Final_weight +FROM public.shipping_fee_sku_base_stage1 A +left join public.shipping_fee_sku_base_stage2_v1 C on A.order_id = C.order_id and A.sales_sub_order_shipment_id = C.sales_sub_order_shipment_id +LEFT join public.weight_sku_table B ON A.sku = B.sku ; + + + +DROP TABLE public.shipping_fee_sku_base_stage3; + +CREATE TABLE public.shipping_fee_sku_base_stage3 AS +SELECT A.*, + (Final_weight*coalesce(Calculated_shipping_fee,shipping_amount))/sum(Final_weight) over(partition BY A.sales_sub_order_shipment_id) new_shipment_amount +FROM public.shipping_fee_sku_base_stage2 A +left join public.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.sales_sub_order_shipment_id = B.reference_id ; + + +DROP TABLE sku_level_shipping_fee_final; + +CREATE TABLE public.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM public.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + +DROP TABLE public.shipping_fee_sku_base_old_stage1; + +CREATE TABLE public.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM gm_dashboard A +LEFT JOIN public.weight_sku_table B ON A.sku = B.sku; + +DROP TABLE public.shipping_fee_sku_base_old_stage2; + +CREATE TABLE public.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM public.shipping_fee_sku_base_old_stage1 ; + +DROP TABLE public.sku_level_shipping_fee_old_final; + +CREATE TABLE public.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM public.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3; + +DROP TABLE IF EXISTS public.payment_order_stage; + +CREATE TABLE public.payment_order_stage 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 public.payment_order; + +CREATE TABLE public.payment_order AS +SELECT A.id , + A.created_at, + A.order_id +FROM public.payment_order_stage A +INNER JOIN + (SELECT order_id , + max(created_at) max_created_at + FROM public.payment_order_stage + GROUP BY order_id) B ON A.created_at= B.max_created_at +AND A.order_id = B.order_id; + +select count(1) from public.payment_order; + +drop table if exists om_app_admin_dump_table; + +CREATE TABLE om_app_admin_dump_table AS +SELECT raena_order_management.order.id AS order_id, + dateadd(hour,7,raena_order_management.order.created_at) AS order_date, + dateadd(hour,7,raena_order_management.order.created_at) AS id_order_date, + status AS order_status, + Payment_status, + D.name AS channel_name, + cast(is_bank_transfer AS varchar) AS bank_transfer, + cast(is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + payment_amount, + shipping_amount AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + raena_order_management.order.stock_type AS product_stock_type, + sku AS product_sku, + oi.name AS product_name, + product_code, + date_key, + product_type, + batch, + country, + quantity, + wholesale_price , + retail_price, + total_price, + Total_earnings, + raena_order_management.order.is_slash_price_enabled , + Brand_name, + category_name, + product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + 0 total_cogs, + 'NA' bundle_sku, + 0 sku_level_shipping_fee +FROM raena_order_management.order +INNER JOIN raena_order_management.order_item oi ON raena_order_management.order .id=oi.order_id +left join public.payment_order C on raena_order_management.order.id=C.order_id +left join raena_order_management.channel D on raena_order_management.order.channel_id = D.id +WHERE raena_order_management.order.created_at <'2021-10-28' +UNION all +SELECT A.id AS order_id, + (A.created_at+interval '7 hours') AS order_date, + (A.created_at+interval '7 hours') AS id_order_date, + A.status AS order_status, + A.Payment_status, + D.name AS channel_name, + cast(A.is_bank_transfer AS varchar) AS bank_transfer, + cast(A.is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + GB.payment_amount, + coalesce(GB.new_shipping_amount_final,shipping_amount) AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + A.Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + A.shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN A.order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + A.stock_type AS product_stock_type, + coalesce(GB.sku,oi.sku) AS product_sku, + coalesce(gb.sku_name,oi.name) AS product_name, + product_code, + date_key, + coalesce(GB.product_type,oi.product_type), + batch, + country, + coalesce(GB.quantity,oi.quantity), + coalesce(GB.actual_wholesale_price,oi.wholesale_price) , + coalesce(GB.retail_price,oi.retail_price), + ((GB.retail_price*GB.quantity)-(GB.seller_margin*GB.quantity)), + Total_earnings, + A.is_slash_price_enabled , + coalesce(gb.brand_name,oi.Brand_name), + coalesce(gb.category_name,oi.category_name), + oi.product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + (GB.cogs) as total_cogs, + case when oi.parent_sku is null then 'NA' else oi.parent_sku end bundle_sku, + new_shipping_amount sku_level_shipping_amount +FROM raena_order_management.order A +inner join (select AD.* , + sum(new_shipping_amount) over(partition by AD.external_id) new_shipping_amount_final , + sum(discounted_price*quantity) over(partition by AD.external_id) payment_amount + from (SELECT A.*, + discounted_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_discounted_price, + actual_wholesale_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_actual_wholesale_price , + retail_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_retail_price, + seller_margin/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_seller_margin , + cogs/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_cogs, + (case when (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)/count(1) over(partition BY A.external_id,A.sku,A.item_type) new_shipping_amount + FROM public.gm_dashboard A + left join public.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 public.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) AD + )GB + ON A.id = GB.external_id AND GB.sku not like 'BAZ%' + left JOIN (select A.*,B.sku parent_sku + from raena_order_management.order_item A + left join raena_order_management.order_item B on A.parent_item_id = B.id ) oi + ON GB.external_id=oi.order_id + AND OI.SKU not like 'BAZ%' + AND GB.sku=oi.sku + and GB.parent_sku= oi.parent_sku +left join public.payment_order C on A.id=C.order_id +left join raena_order_management.channel D on A.channel_id = D.id +WHERE cast(A.created_at+interval '7 hours' as date) >='2021-10-28' ; + + + +DROP TABLE IF EXISTS public.remove_duplicate_from_app_admin; + + +CREATE TABLE public.remove_duplicate_from_app_admin AS +SELECT A.external_id, + wholesale_price_gm, + wholesale_price_app_admin, + wholesale_price_gm-wholesale_price_app_admin diff +FROM + (SELECT external_id , + sum(discounted_price*quantity) P1, + sum((retail_price*quantity)-(seller_margin*quantity)) wholesale_price_gm + FROM public.gm_dashboard gd + GROUP BY external_id) A +FULL OUTER JOIN + (SELECT order_id , + payment_amount p2, + sum(total_price) wholesale_price_app_admin + FROM public.om_app_admin_dump_table oaadt + GROUP BY order_id, + payment_amount) B ON A.external_id= B.order_id +WHERE wholesale_price_gm-wholesale_price_app_admin<>0; + + +DROP TABLE IF EXISTS public.dubplicate_record_app_admin; + + +CREATE TABLE public.dubplicate_record_app_admin AS +SELECT DISTINCT * +FROM public.om_app_admin_dump_table oaadt +WHERE order_id IN + (SELECT DISTINCT external_id + FROM public.remove_duplicate_from_app_admin); + + +DELETE +FROM public.om_app_admin_dump_table +WHERE order_id IN + (SELECT DISTINCT external_id + FROM public.remove_duplicate_from_app_admin); + +INSERT INTO public.om_app_admin_dump_table +SELECT * +FROM public.dubplicate_record_app_admin; + + + + diff --git a/Gm_dashboard/OOS_code.log b/Gm_dashboard/OOS_code.log new file mode 100644 index 0000000..843c287 --- /dev/null +++ b/Gm_dashboard/OOS_code.log @@ -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 diff --git a/Gm_dashboard/etlDemandForecasting.log b/Gm_dashboard/etlDemandForecasting.log new file mode 100644 index 0000000..725196e --- /dev/null +++ b/Gm_dashboard/etlDemandForecasting.log @@ -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 diff --git a/Gm_dashboard/etlTransaction_job_v1.log b/Gm_dashboard/etlTransaction_job_v1.log new file mode 100644 index 0000000..9898ba9 --- /dev/null +++ b/Gm_dashboard/etlTransaction_job_v1.log @@ -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 diff --git a/Gm_dashboard/etlTransaction_job_v2.log b/Gm_dashboard/etlTransaction_job_v2.log new file mode 100644 index 0000000..07d288f --- /dev/null +++ b/Gm_dashboard/etlTransaction_job_v2.log @@ -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 diff --git a/Gm_dashboard/etl_gmdashboard_new_v2.sh b/Gm_dashboard/etl_gmdashboard_new_v2.sh new file mode 100755 index 0000000..b521bdb --- /dev/null +++ b/Gm_dashboard/etl_gmdashboard_new_v2.sh @@ -0,0 +1,1183 @@ + #!/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 distinct 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type +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, + actual_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.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, + actual_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, + price_type +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 discount_price --retail_price*quantity*discount_amount/sum(retail_price*quantity)over(partition BY A.external_id) + 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-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + case when product_type_class in ('Flash Bundle','Flash') then 'Flash' + when coupon_applied_on ='Cart' and price_type ='NEGOTIATED_PRICE' + then 'Negotiated' + when coupon_applied_on = 'Shipping Fee' then 'Shipping' + when coupon_applied_on is not null then 'Brand' + end discount_type +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 A.external_id = B.external_id + AND A.parent_sku = B.parent_sku then (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/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 + 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; + +--Alter table public.base_netsuite_final +--add columns discount_type varchar,promo_amount decimal(22,2),actual_wholesale_price decimal(22,2); + + +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+interval'7 Hours' 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0)-coalesce((case when final_discount >0 then 0 else actual_wholesale_price*quantity - effective_wholesale_price*quantity end ),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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,0 diff, + discount_type, + case when final_discount >0 then 0 else actual_wholesale_price*quantity - effective_wholesale_price*quantity end promo_amount, + actual_wholesale_price, + effective_wholesale_price +FROM public.order_level_data A +LEFT JOIN public.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + +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 AS 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, + CASE + WHEN B.discount_type = 'Flash' + AND promo_amount IS NOT NULL THEN 'Flash' + WHEN B.discount_type = 'Negotiated' + AND promo_amount IS NOT NULL THEN 'Negotiated' + WHEN promo_amount IS NOT NULL THEN 'Brand' + END discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price +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 , + max(cogs_non_promo) cogs_non_promo, + max(cogs_promo) cogs_promo, + 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, + 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', + '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 , + Customer_type, + BB.gm_target, + CASE + WHEN B.discount_type = 'Flash' + AND promo_amount IS NOT NULL THEN 'Flash' + WHEN B.discount_type = 'Negotiated' + AND promo_amount IS NOT NULL THEN 'Negotiated' + WHEN promo_amount IS NOT NULL THEN 'Brand' + END , + promo_amount, + actual_wholesale_price, + effective_wholesale_price; + + + + +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,'province',TRUE) reseller_province, + 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, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province +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 cast(transaction_date as date) >='$reportDate'); + + +INSERT INTO OM_GM_DB_Product_category +SELECT A.external_id , +cast(transaction_date as date) 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price +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 cast(transaction_date as 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province +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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + 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, + discount_promo_type, + reseller_province; + +--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; + +DROP TABLE IF EXISTS public.GM_GROWTH_TAB3; +CREATE TABLE public.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) + +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) + +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) + +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 7,8,9,10,11,12,13,14 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator + from public.OM_GM_DB_Product_category a + inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id + group by 2,3,4,5,6,7,8,9,10,11,12,13,14 +) +ORDER BY 3; + +drop table shipping_fee_base_table; + +create table shipping_fee_base_table as +select A.order_id , + sum(case when A.sales_sub_order_shipment_id = C.reference_id then ((C.shipping_amount)/(C.order_weight))*(C.final_weight) else B.shipping_amount end ) shipping_amount +from (select distinct order_id ,sales_sub_order_shipment_id from raena_order_management.sales_sub_order) A +left join raena_order_management.sales_sub_order_shipment B on A.sales_sub_order_shipment_id = B.id +left join public.OM_Logistic_final_shipping C on A.sales_sub_order_shipment_id = C.reference_id +group by A.order_id; + +DROP TABLE public.shipping_fee_sku_base_stage1 ; + + +CREATE TABLE public.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + +DROP TABLE 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 public.shipping_fee_sku_base_stage2; + + +CREATE TABLE public.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 public.shipping_fee_sku_base_stage1 A +LEFT JOIN public.weight_sku_table B ON A.sku = B.sku ; + + +DROP TABLE public.shipping_fee_sku_base_stage3; + + +CREATE TABLE public.shipping_fee_sku_base_stage3 AS +SELECT *, + (sku_weight*shipping_amount)/sum(sku_weight) over(partition BY sales_sub_order_shipment_id) new_shipment_amount +FROM public.shipping_fee_sku_base_stage2; + + +DROP TABLE sku_level_shipping_fee_final; + + +CREATE TABLE public.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM public.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + + +DROP TABLE public.shipping_fee_sku_base_old_stage1; + + +CREATE TABLE public.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM gm_dashboard A +LEFT JOIN public.weight_sku_table B ON A.sku = B.sku; + + + +DROP TABLE public.shipping_fee_sku_base_old_stage2; + + +CREATE TABLE public.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM public.shipping_fee_sku_base_old_stage1 ; + + +DROP TABLE public.sku_level_shipping_fee_old_final; + +CREATE TABLE public.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM public.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3; + + +" > /home/ec2-user/cronjob/Gm_dashboard/Final_GM_v2.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_v2.sql > etlTransaction_job_v2.log + + + + + + diff --git a/Gm_dashboard/etl_gmdashboard_new_v3.sh b/Gm_dashboard/etl_gmdashboard_new_v3.sh new file mode 100644 index 0000000..28e206a --- /dev/null +++ b/Gm_dashboard/etl_gmdashboard_new_v3.sh @@ -0,0 +1,1911 @@ + #!/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.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 distinct external_id, + transaction_date+interval'7 hours' 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, + is_campaign, + order_placed_by +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, + is_campaign, + order_placed_by + FROM raena_order_management.order A + WHERE payment_status='Paid' + AND cast(A.created_at+interval '7 hours' AS date) >='$reportDate' + ) A +LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code; + + +select max(created_at) from raena_order_management.order ; + +DROP TABLE IF EXISTS public.base_netsuite_stage1_V1; + + +CREATE TABLE public.base_netsuite_stage1_V1 AS +SELECT DISTINCT transaction_date 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_wholesale_price, + CASE + WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount + ELSE B.coupon_discount + END discount_price, + case when discount_amount > 0 and A.coupon_code IS NULL and A.order_placed_by = 'admin' and coupon_applied_on is null then 'Cart' else coupon_applied_on end 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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type, + a.is_campaign +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, + actual_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+interval'7 hours' AS date)>='$reportDate') F ON B.id = F.sales_sub_order_id +ORDER BY 1,2; + + +DROP TABLE IF EXISTS public.base_netsuite_stage2_v1_base ; + +CREATE TABLE public.base_netsuite_stage2_v1_base 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, + actual_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, + price_type, + is_campaign, + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND A.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag +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 +LEFT JOIN + (SELECT B.order_id , + A.sku , + A.created_at created_at , + A.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) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (SELECT sku, + cogs, + promo, + created_at::date created_at, + coalesce(lead(created_at::date-interval'1 day' , 1) over (partition BY sku + ORDER BY sku,created_at),CURRENT_DATE+interval'1 day')::date last_date + FROM raena_catalog_management.cogs_audit ca + ORDER BY 1 , + 4) D ON A.sku = D.sku +AND A.transaction_date::date BETWEEN D.created_at AND last_date; + + 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 quantity, + parent_quantity, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else retail_price end retail_price, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else parent_retail_price end parent_retail_price, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else effective_wholesale_price end effective_wholesale_price, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else parent_wholesale_price end parent_wholesale_price, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else actual_wholesale_price end actual_wholesale_price, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else discount_price end discount_price, + coupon_applied_on, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else discount_amount end discount_amount, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else dynamic_price end dynamic_price, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else parent_dynamic_price end parent_dynamic_price, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else payment_price end payment_price, + case when cogs = 0 or cogs is null or A.sku = D.sku then 0 else payment_amount end payment_amount, + product_class, + parent_product_class, + product_type_class, + sales_sub_order_id, + loyalty_discount, + price_type, + is_campaign, + case when A.sku = D.sku then 0 else cogs end cogs, + cogs_flag +from public.base_netsuite_stage2_v1_base A +left join (select distinct sku + from raena_catalog_management.product + where lower(name) like '%not for sale%' + or lower(name) like '%free item%' + or lower(name) like '%free gift%') D + on A.sku = D.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 discount_price --retail_price*quantity*discount_amount/sum(retail_price*quantity)over(partition BY A.external_id) + -- 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 , + ---case when price_type ='NEGOTIATED_PRICE' then + --- 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 discount_price + --- 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 + --- else + 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-coalesce(actual_wholesale_price)*quantity end) over(partition by A.external_id )) + /sum(CASE WHEN discount_price>0 and retail_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 discount_price ---(discount_amount-sum(retail_price*quantity-coalesce(actual_wholesale_price)*quantity) over(partition by A.external_id )) + WHEN coupon_applied_on ='Cart' + AND discount_price=0 THEN retail_price*quantity*(discount_amount) + ---sum(retail_price*quantity-coalesce(actual_wholesale_price)*quantity) over(partition by A.external_id )) + /sum(case when retail_price<>0 then retail_price*quantity end )over(partition BY A.external_id) + ---end + end as Final_discount , + retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + case when product_type_class in ('Flash Bundle','Flash') then 'Flash' + when coupon_applied_on ='Cart' and price_type ='NEGOTIATED_PRICE' + then 'Negotiated' + when coupon_applied_on = 'Shipping Fee' then 'Shipping' + when coupon_applied_on is not null then 'Brand' + end discount_type +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 A.external_id = B.external_id + AND A.parent_sku = B.parent_sku then (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/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 + ---case when price_type ='NEGOTIATED_PRICE' then final_discount-seller_margin else final_discount end effective_coupon_discount +case when final_discount<>0 then final_discount-seller_margin end as 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 + 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; + +--Alter table public.base_netsuite_final +--add columns cogs_flag varchar,promo_amount decimal(22,2),actual_wholesale_price decimal(22,2); + + +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 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) + -coalesce((case when final_discount >0 then 0 else actual_wholesale_price*quantity - effective_wholesale_price*quantity end ),0) + -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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,0 diff, + case when price_type is not null then price_type else discount_type end discount_type , + case when final_discount >0 then 0 else actual_wholesale_price*quantity - effective_wholesale_price*quantity end promo_amount, + actual_wholesale_price, + effective_wholesale_price, + A.is_campaign, + '' sales_sub_order_id +FROM public.order_level_data A +LEFT JOIN public.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + +Select max(transaction_date) Netsuite_base_date from public.base_netsuite_final; + +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, + is_campaign +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 cast(A.created_at+interval'7 hours' 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 AS 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 , + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) 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, + --CASE + -- WHEN B.discount_type = 'Flash' + -- AND promo_amount IS NOT NULL THEN 'Flash' + -- WHEN B.discount_type = 'Negotiated' + -- AND promo_amount IS NOT NULL THEN 'Negotiated' + -- WHEN promo_amount IS NOT NULL THEN 'Brand' + -- END + B.discount_type discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND B.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + A.is_campaign, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END AS DSF_Flag +FROM public.gm_dimensions_stage1 A +INNER JOIN (select distinct * from public.base_netsuite_final) B ON A.external_id =B.external_id +AND A.sku=B.product_sku +LEFT JOIN + (SELECT B.order_id , + A.sku , + A.created_at created_at , + A.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) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (SELECT sku, + cogs, + promo, + (created_at)::date created_at, + coalesce(lead(created_at::date-interval'1 day' , 1) over (partition BY sku + ORDER BY sku,created_at),CURRENT_DATE+interval'1 day')::date last_date + FROM raena_catalog_management.cogs_audit ca + ORDER BY 1 , + 4) D ON A.sku = D.sku +AND B.transaction_date::date BETWEEN D.created_at AND last_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 +WHERE A.external_id NOT IN + ( SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid') ; + + +Select max(transaction_date) gm_dashboard_date from public.GM_dashboard; + +DROP TABLE IF EXISTS public.GM_dashboard_v1; + + +CREATE TABLE public.GM_dashboard_v1 AS +SELECT * +FROM public.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + + +DELETE +FROM public.GM_dashboard +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + + +INSERT INTO public.GM_dashboard +SELECT external_id, + transaction_date, + order_discount_amount, + shipping_cost, + shipping_province, + coupon_code, + discount_type, + coupon_applied_on, + brand_name, + category_name, + product_type, + sku, + sku_name, + reseller_name, + reseller_email, + reseller_mobile, + tier_name, + reseller_id, + quantity, + 0 retail_price, + 0 seller_margin, + 0 , + 0 , + 0, + 0, + item_type, + order_placed_by, + 0 cogs, + order_recipient, + brand_type, + customer_type, + gm_target, + discount_promo_type, + 0 promo_amount, + 0, + 0, + cogs_flag, + is_campaign, + dsf_flag +FROM public.GM_dashboard_v1 +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + + + + +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,'province',TRUE) reseller_province, + 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+interval'7 hours' 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 distinct 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 , + a.is_campaign , + C.Country, + B.quantity , + B.retail_price, + seller_margin, + discounted_price, + loyalty_discount, + effective_coupon_discount , + additional_discount, + item_type, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + case when A.external_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + 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 +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 +where A.external_id not in ( + select distinct id from raena_order_management.order where status in ('Cancelled') and payment_status= 'Paid'); + + + + +DROP TABLE IF EXISTS public.business_report_v1; + + +CREATE TABLE public.business_report_v1 AS +SELECT * +FROM public.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + + +DELETE +FROM public.business_report USING + (SELECT DISTINCT sku, + sku_name, + external_id + FROM public.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B +WHERE public.business_report.external_id = B.external_id + AND public.business_report.sku=B.sku + AND public.business_report.external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + + +INSERT INTO public.business_report +SELECT external_id, + created_date, + order_discount_amount, + shipping_cost, + coupon_code, + brand_name, + product_type, + category_name, + sku_name, + sku, + reseller_id, + customer_id, + cart_id, + flash_sale_id, + shipping_to, + payment_status, + status, + stock_type, + order_placed_by, + order_warehouse, + channel_id, + medium, + marketplace, + provider, + 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, + country, + quantity, + 0 retail_price, + 0 seller_margin, + 0 discount_price, + 0 loyalty_discount, + 0 effective_coupon_discount, + 0 additional_discount, + item_type, + discount_promo_type, + 0 promo_amount, + 0 actual_wholesale_price, + 0 effective_wholesale_price, + reseller_province, + dsf_flag, + brand_type +FROM + (SELECT public.business_report_v1.* + FROM public.business_report_v1 + INNER JOIN + (SELECT DISTINCT sku, + sku_name, + external_id + FROM public.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B ON public.business_report_v1.external_id = B.external_id + AND public.business_report_v1.sku=B.sku) A +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + + +Select max(created_date) business_date from public.business_report; + +drop table if exists om_user_ist_install_table; + + + +create table om_user_ist_install_table +as +select email,app_install_date +from ( +select distinct email,app_install_date,row_number() over (partition by email order by app_install_date) as R +from ( +select distinct email,app_install_date +from om_clevertap_install_jan_may +where email notnull and email!='' +union +select distinct email,cast(created_at as date) as created_at +from raena_user_management.user u +) +) where R=1; + +select 1 ; + + + DELETE +FROM public.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + + + +INSERT INTO OM_GM_DB_Product_category +SELECT external_id, + transaction_date, + \"Time\", + order_discount_amount, + shipping_cost, + coupon_code , + discount_type , + coupon_applied_on , + brand_name, + category_name , + product_type , + sku, + sku_name , + reseller_name , + reseller_email , + base.reseller_mobile , + tier_name , + reseller_id , + quantity , + retail_price , + seller_margin , + discounted_price , + additional_discount, + item_type , + order_placed_by , + cogs, + shipping_province , + Flag , + wholesale_price, + order_recipient, + brand_type, + Customer_type, + gm_target, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + CASE WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' end AS New_existing_flag, + is_campaign, + dsf_flag, + cogs_flag +FROM + (SELECT A.external_id , + cast(transaction_date AS date) 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + is_campaign, + A.dsf_flag, + A.cogs_flag + 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))/(case when retail_price<>0 then (retail_price-seller_margin)*quantity end) 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 cast(transaction_date as date) >='$reportDate' + and A.external_id not in ( + select distinct id from raena_order_management.order where status in ('Cancelled') and payment_status= 'Paid')) base +LEFT JOIN user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +Select max(transaction_date) om_gm_db_product_date from public.OM_GM_DB_Product_category; + + +--Business Dashboard +DELETE +FROM public.GM_GROWTH_TAB1 +WHERE created_date >='$reportDate'; + +/* +INSERT INTO public.GM_GROWTH_TAB1 +select base.*, case WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na'end as New_existing_flag +from ( +SELECT tier, + 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,reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type + from (SELECT DISTINCT coalesce(a.tier,'NA') as tier, + 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,reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + 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 payment_status='Paid' + AND created_date >= '$reportDate' + UNION SELECT DISTINCT coalesce(a.tier,'NA') as tier, + 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,reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + 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 payment_status='Paid' + AND created_date >= '$reportDate') + GROUP BY tier,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,reseller_mobile, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type +) base +left join user_type_table flag on replace(base.reseller_mobile,'+','')=flag.reseller_mobile; +*/ + + +INSERT INTO public.GM_GROWTH_TAB1 +select tier, + 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,base.reseller_mobile, + Number_of_Orders , + RSP , + wholesale_price , + Payment_Price , + Effective_Coupon_Discount, + Total_Loyalty_point, + additional_discount, + seller_margin, + AOV, + quantity, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + case WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na'end as New_existing_flag, + external_id +from ( +SELECT tier, + 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,reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + external_id + from ( + SELECT DISTINCT coalesce(a.tier,'NA') as tier, + 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,reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + 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 payment_status='Paid' + AND created_date >= '$reportDate') + GROUP BY tier,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,reseller_mobile, + external_id, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type +) base +left join user_type_table flag on replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +Select max(created_date) gm_growth_date from public.GM_GROWTH_TAB1; + +--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; + + +DROP TABLE IF EXISTS public.GM_GROWTH_TAB3; +CREATE TABLE public.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 +) +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 +) +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 +) +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 +) +ORDER BY 3; + +drop table shipping_fee_base_table; + +create table shipping_fee_base_table as +select A.order_id , + sum(case when A.sales_sub_order_shipment_id = C.reference_id then ((C.shipping_amount)/(C.order_weight))*(C.final_weight) else B.shipping_amount end ) shipping_amount +from (select distinct order_id ,sales_sub_order_shipment_id from raena_order_management.sales_sub_order) A +left join raena_order_management.sales_sub_order_shipment B on A.sales_sub_order_shipment_id = B.id +left join public.OM_Logistic_final_shipping C on A.sales_sub_order_shipment_id = C.reference_id +group by A.order_id; + +DROP TABLE public.shipping_fee_sku_base_stage1 ; + + +CREATE TABLE public.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + +DROP TABLE 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 public.shipping_fee_sku_base_stage2; + + +CREATE TABLE public.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 public.shipping_fee_sku_base_stage1 A +LEFT JOIN public.weight_sku_table B ON A.sku = B.sku ; + + +DROP TABLE public.shipping_fee_sku_base_stage3; + + +CREATE TABLE public.shipping_fee_sku_base_stage3 AS +SELECT *, + (sku_weight*shipping_amount)/sum(sku_weight) over(partition BY sales_sub_order_shipment_id) new_shipment_amount +FROM public.shipping_fee_sku_base_stage2; + + +DROP TABLE sku_level_shipping_fee_final; + + +CREATE TABLE public.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM public.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + +DROP TABLE public.shipping_fee_sku_base_old_stage1; + + +CREATE TABLE public.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM gm_dashboard A +LEFT JOIN public.weight_sku_table B ON A.sku = B.sku; + + + +DROP TABLE public.shipping_fee_sku_base_old_stage2; + + +CREATE TABLE public.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM public.shipping_fee_sku_base_old_stage1 ; + + +DROP TABLE public.sku_level_shipping_fee_old_final; + +CREATE TABLE public.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM public.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3; + + + +DROP TABLE IF EXISTS public.payment_order_stage; + + +CREATE TABLE public.payment_order_stage 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 public.payment_order; + + +CREATE TABLE public.payment_order AS +SELECT A.id , + A.created_at, + A.order_id +FROM public.payment_order_stage A +INNER JOIN + (SELECT order_id , + max(created_at) max_created_at + FROM public.payment_order_stage + GROUP BY order_id) B ON A.created_at= B.max_created_at +AND A.order_id = B.order_id; + +select count(1) from public.payment_order; + +drop table if exists om_app_admin_dump_table; + +CREATE TABLE om_app_admin_dump_table AS +SELECT raena_order_management.order.id AS order_id, + dateadd(hour,7,raena_order_management.order.created_at) AS order_date, + dateadd(hour,7,raena_order_management.order.created_at) AS id_order_date, + status AS order_status, + Payment_status, + order_store AS sales_channel, + cast(is_bank_transfer AS varchar) AS bank_transfer, + cast(is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + payment_amount, + shipping_amount AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN order_placed_by ='Raena' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + raena_order_management.order.stock_type AS product_stock_type, + sku AS product_sku, + oi.name AS product_name, + product_code, + date_key, + product_type, + batch, + country, + quantity, + wholesale_price , + retail_price, + total_price, + Total_earnings, + raena_order_management.order.is_slash_price_enabled , + Brand_name, + category_name, + product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id +FROM raena_order_management.order +INNER JOIN raena_order_management.order_item oi ON raena_order_management.order .id=oi.order_id +left join public.payment_order C on raena_order_management.order.id=C.order_id +WHERE raena_order_management.order.created_at <'2021-10-28' +UNION +SELECT A.id AS order_id, + (A.created_at+interval '7 hours') AS order_date, + (A.created_at+interval '7 hours') AS id_order_date, + A.status AS order_status, + A.Payment_status, + A.order_store AS sales_channel, + cast(A.is_bank_transfer AS varchar) AS bank_transfer, + cast(A.is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + sum(GB.new_discounted_price*gb.quantity) over(partition by A.id) payment_amount, + shipping_amount AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + A.Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + A.shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN A.order_placed_by ='Raena' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + A.stock_type AS product_stock_type, + coalesce(GB.sku,oi.sku) AS product_sku, + coalesce(gb.sku_name,oi.name) AS product_name, + product_code, + date_key, + coalesce(GB.product_type,oi.product_type), + batch, + country, + coalesce(GB.quantity,oi.quantity), + coalesce(GB.new_actual_wholesale_price,oi.wholesale_price) , + coalesce(GB.new_retail_price,oi.retail_price), + coalesce(((GB.new_retail_price*GB.quantity)-(GB.new_seller_margin*GB.quantity)),total_price), + Total_earnings, + A.is_slash_price_enabled , + coalesce(gb.brand_name,oi.Brand_name), + coalesce(gb.category_name,oi.category_name), + oi.product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id +FROM raena_order_management.order A +left JOIN raena_order_management.order_item oi ON A.id=oi.order_id AND OI.SKU not like 'BAZ%' +left JOIN (select *,discounted_price/count(1) over(partition by external_id,sku,item_type) new_discounted_price, + actual_wholesale_price/count(1) over(partition by external_id,sku,item_type) new_actual_wholesale_price , + retail_price/count(1) over(partition by external_id,sku,item_type) new_retail_price, + seller_margin/count(1) over(partition by external_id,sku,item_type) new_seller_margin + from public.gm_dashboard) GB ON oi.order_id = GB.external_id AND oi.sku = GB.sku and GB.sku not like 'BAZ%' +left join public.payment_order C on A.id=C.order_id +WHERE cast(A.created_at+interval '7 hours' as date) >='2021-10-28' ; +" > /home/ec2-user/cronjob/Gm_dashboard/Final_GM_v2.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_v2.sql + diff --git a/Gm_dashboard/etl_gmdashboard_new_v5.sh b/Gm_dashboard/etl_gmdashboard_new_v5.sh new file mode 100644 index 0000000..4a1a375 --- /dev/null +++ b/Gm_dashboard/etl_gmdashboard_new_v5.sh @@ -0,0 +1,1957 @@ + #!/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.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 DISTINCT external_id, + transaction_date+interval'7 hours' 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, + is_campaign, + order_placed_by +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, + is_campaign, + order_placed_by + FROM raena_order_management.order A + WHERE payment_status='Paid' + AND cast(A.created_at+interval '7 hours' AS date) >='$reportDate' ) A +LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code; + + +SELECT max(created_at) +FROM raena_order_management.order ; + + +DROP TABLE IF EXISTS public.base_netsuite_stage1_V1; + + +CREATE TABLE public.base_netsuite_stage1_V1 AS +SELECT DISTINCT transaction_date 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_wholesale_price, + CASE + WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount + ELSE B.coupon_discount + END discount_price, + CASE + WHEN discount_amount > 0 + AND A.coupon_code IS NULL + AND A.order_placed_by = 'admin' + AND coupon_applied_on IS NULL THEN 'Cart' + ELSE coupon_applied_on + END 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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type, + a.is_campaign, + B.cogs_discount_type +FROM public.order_level_data A +LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id and status <> 'Cancelled' +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, + actual_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+interval'7 hours' AS date)>='$reportDate') F ON B.id = F.sales_sub_order_id +ORDER BY 1, + 2; + + +DROP TABLE IF EXISTS public.base_netsuite_stage2_v1_base ; + + +CREATE TABLE public.base_netsuite_stage2_v1_base 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, + actual_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, + price_type, + is_campaign, + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND A.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + cogs_discount_type +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 +LEFT JOIN + (SELECT distinct B.order_id , + A.sku , + A.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) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON A.sku = D.sku +AND A.transaction_date::date BETWEEN D.created_at AND last_date; + + +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 quantity, + parent_quantity, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE retail_price + END retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_retail_price + END parent_retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE effective_wholesale_price + END effective_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_wholesale_price + END parent_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE actual_wholesale_price + END actual_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_price + END discount_price, + coupon_applied_on, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_amount + END discount_amount, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE dynamic_price + END dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_dynamic_price + END parent_dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_price + END payment_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_amount + END payment_amount, + product_class, + parent_product_class, + product_type_class, + sales_sub_order_id, + loyalty_discount, + price_type, + is_campaign, + CASE + WHEN A.sku = D.sku THEN 0 + ELSE cogs + END cogs, + cogs_flag, + cogs_discount_type +FROM public.base_netsuite_stage2_v1_base A +LEFT JOIN + (SELECT DISTINCT sku + FROM raena_catalog_management.product + WHERE lower(name) LIKE '%not for sale%' + OR lower(name) LIKE '%free item%' + OR lower(name) LIKE '%free gift%') D ON A.sku = D.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 + AND retail_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 discount_price + + WHEN coupon_applied_on ='Cart' + AND discount_price=0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id) + END AS Final_discount , + retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + CASE + WHEN product_type_class IN ('Flash Bundle', + 'Flash') THEN 'Flash' + WHEN coupon_applied_on ='Cart' + AND price_type ='NEGOTIATED_PRICE' THEN 'Negotiated' + WHEN coupon_applied_on = 'Shipping Fee' THEN 'Shipping' + WHEN coupon_applied_on IS NOT NULL THEN 'Brand' + END discount_type +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 A.external_id = B.external_id + AND A.parent_sku = B.parent_sku THEN (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/ttl_wholesale_price) + END AS additional_discount, + CASE + WHEN final_discount<>0 THEN final_discount-seller_margin + END AS 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 + 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 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) -coalesce((CASE WHEN final_discount >0 THEN 0 ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity END),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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, + 0 diff, + CASE + WHEN price_type IS NOT NULL THEN price_type + ELSE discount_type + END discount_type , + CASE + WHEN final_discount >0 THEN 0 + ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity + END promo_amount, + actual_wholesale_price, + effective_wholesale_price, + A.is_campaign, + '' sales_sub_order_id, + cogs_discount_type, + parent_sku +FROM public.order_level_data A +LEFT JOIN public.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + + +with sales_sub_order_table as( +select order_id , parent_sku ,quantity, payment_amount,payment_amount/quantity payment_amount_quantity +from raena_order_management.sales_sub_order sso +where order_id in ('OD1668674111980647REG','OD1669347541350078REG','OD1668588329391772REG') +) +update public.base_netsuite_final +set discounted_price =payment_amount_quantity +from public.base_netsuite_final G +Inner join sales_sub_order_table B on G.external_id = B.order_id +and G.product_sku = B.parent_sku ; + + +SELECT max(transaction_date) Netsuite_base_date +FROM public.base_netsuite_final; + + + +DROP TABLE IF EXISTS public.gm_dimensions_stage1; + +CREATE TABLE public.gm_dimensions_stage1 AS +SELECT distinct 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, + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + is_campaign +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 +LEFT JOIN raena_order_management.channel D ON A.channel_id = D.id +WHERE payment_status ='Paid' + AND cast(A.created_at+interval'7 hours' AS date) >='$reportDate'; + +drop table if exists sku_gm_mapping; + +create table sku_gm_mapping +as +select A.sku ,A.name sku_name,B.name brand_name,C.name as category_name,product_type + 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 ; + + +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 AS transaction_date, + B.order_discount_amount , + B.shipping_cost , + A.shipping_province, + B.coupon_code , + A.discount_type , + B.coupon_applied_on , + brand_name, + category_name , + product_type , + product_sku sku, + 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 , + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type , + Customer_type, + BB.gm_target, + B.discount_type discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + coalesce(cogs_type,CASE WHEN B.product_sku = D.sku + AND B.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + A.is_campaign, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END AS DSF_Flag, + cogs_discount_type, + order_type, + parent_sku +FROM public.gm_dimensions_stage1 A +INNER JOIN public.base_netsuite_final B ON A.external_id =B.external_id +LEFT JOIN + (SELECT distinct B.order_id , + A.sku , + A.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) C ON B.product_sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON B.product_sku = D.sku +AND B.transaction_date::date BETWEEN D.created_at AND last_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 B.product_sku = BB.sku +AND A.reseller_tier_name =BB.tierName +left join sku_gm_mapping AAA on B.product_sku = AAA.sku +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid') ; + +SELECT max(transaction_date) gm_dashboard_date +FROM public.GM_dashboard; + +DROP TABLE IF EXISTS public.GM_dashboard_v1; + +CREATE TABLE public.GM_dashboard_v1 AS +SELECT * +FROM public.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + +DELETE +FROM public.GM_dashboard +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + +INSERT INTO public.GM_dashboard +SELECT external_id, + transaction_date, + order_discount_amount, + shipping_cost, + shipping_province, + coupon_code, + discount_type, + coupon_applied_on, + brand_name, + category_name, + product_type, + sku, + sku_name, + reseller_name, + reseller_email, + reseller_mobile, + tier_name, + reseller_id, + quantity, + 0 retail_price, + 0 seller_margin, + 0 , + 0 , + 0, + 0, + item_type, + order_placed_by, + 0 cogs, + order_recipient, + brand_type, + customer_type, + gm_target, + discount_promo_type, + 0 promo_amount, + 0, + 0, + cogs_flag, + is_campaign, + dsf_flag, + cogs_discount_type, + order_type, + parent_sku +FROM public.GM_dashboard_v1 +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + + + +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, + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when E.name is not null then 'Channel Order' + else 'Reseller Order' + end order_type, + 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,'province',TRUE) reseller_province, + 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 +LEFT JOIN raena_order_management.channel E ON A.channel_id = E.id +WHERE cast(A.created_at+interval'7 hours' 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 DISTINCT 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 , + a.is_campaign , + C.Country, + B.quantity , + B.retail_price, + seller_margin, + discounted_price, + loyalty_discount, + effective_coupon_discount , + additional_discount, + item_type, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END DSF_flag, + 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type, + A.order_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 +WHERE A.external_id NOT IN + ( SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid'); + +DROP TABLE IF EXISTS public.business_report_v1; + + +CREATE TABLE public.business_report_v1 AS +SELECT * +FROM public.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + +DELETE +FROM public.business_report USING + (SELECT DISTINCT sku, + sku_name, + external_id + FROM public.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B +WHERE public.business_report.external_id = B.external_id + AND public.business_report.sku=B.sku + AND public.business_report.external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + +INSERT INTO public.business_report +SELECT external_id, + created_date, + order_discount_amount, + shipping_cost, + coupon_code, + brand_name, + product_type, + category_name, + sku_name, + sku, + reseller_id, + customer_id, + cart_id, + flash_sale_id, + shipping_to, + payment_status, + status, + stock_type, + order_placed_by, + order_warehouse, + channel_id, + medium, + marketplace, + provider, + 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, + country, + quantity, + 0 retail_price, + 0 seller_margin, + 0 discount_price, + 0 loyalty_discount, + 0 effective_coupon_discount, + 0 additional_discount, + item_type, + discount_promo_type, + 0 promo_amount, + 0 actual_wholesale_price, + 0 effective_wholesale_price, + reseller_province, + dsf_flag, + brand_type, + order_type +FROM + (SELECT public.business_report_v1.* + FROM public.business_report_v1 + INNER JOIN + (SELECT DISTINCT sku, + sku_name, + external_id + FROM public.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B ON public.business_report_v1.external_id = B.external_id + AND public.business_report_v1.sku=B.sku) A +WHERE external_id IN + (SELECT DISTINCT external_id + FROM public.business_dimensions_stage1); + +SELECT max(created_date) business_date +FROM public.business_report; + +DELETE +FROM public.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT external_id + FROM public.gm_dimensions_stage1); + + +INSERT INTO OM_GM_DB_Product_category +SELECT external_id, + transaction_date, + \"Time\", + order_discount_amount, + shipping_cost, + coupon_code , + discount_type , + coupon_applied_on , + brand_name, + category_name , + product_type , + sku, + sku_name , + reseller_name , + reseller_email , + base.reseller_mobile , + tier_name , + reseller_id , + quantity , + retail_price , + seller_margin , + discounted_price , + additional_discount, + item_type , + order_placed_by , + cogs, + shipping_province , + Flag , + wholesale_price, + order_recipient, + brand_type, + Customer_type, + gm_target, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + CASE + WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + is_campaign, + dsf_flag, + cogs_flag, + cogs_discount_type, + order_type, + parent_sku +FROM + (SELECT A.external_id , + cast(transaction_date AS date) 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 , + A.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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + is_campaign, + A.dsf_flag, + A.cogs_flag, + A.cogs_discount_type, + A.order_type, + parent_sku + 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))/(CASE WHEN retail_price<>0 THEN (retail_price-seller_margin)*quantity END) 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 cast(transaction_date AS date) >='$reportDate' + AND A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid')) base +LEFT JOIN user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(transaction_date) om_gm_db_product_date +FROM public.OM_GM_DB_Product_category; + +update public.gm_dashboard + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from public.gm_dashboard where sku like '______X'); + + update public.om_gm_db_product_category + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from public.om_gm_db_product_category where sku like '______X'); + +DELETE +FROM public.GM_GROWTH_TAB1 +WHERE created_date >='$reportDate'; + +INSERT INTO public.GM_GROWTH_TAB1 +SELECT tier, + 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, + base.reseller_mobile, + Number_of_Orders , + RSP , + wholesale_price , + Payment_Price , + Effective_Coupon_Discount, + Total_Loyalty_point, + additional_discount, + seller_margin, + AOV, + quantity, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + CASE + WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + external_id, + order_type +FROM + (SELECT tier, + 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, + reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + external_id, + order_type + FROM + (SELECT DISTINCT coalesce(a.tier,'NA') AS tier, + 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, + reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + 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 payment_status='Paid' + AND created_date >= '$reportDate') + GROUP BY tier, + 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, + reseller_mobile, + external_id, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type) base +LEFT JOIN user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(created_date) gm_growth_date +FROM public.GM_GROWTH_TAB1; + + +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; + + +DROP TABLE IF EXISTS public.GM_GROWTH_TAB3; +CREATE TABLE public.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from public.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management."order") o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +ORDER BY 3; + +DROP TABLE shipping_fee_base_table; + +CREATE TABLE shipping_fee_base_table AS +SELECT A.order_id , + sum(CASE WHEN A.sales_sub_order_shipment_id = C.reference_id THEN ((C.shipping_amount)/(C.order_weight))*(C.final_weight) ELSE B.shipping_amount END) shipping_amount +FROM + (SELECT DISTINCT order_id , + sales_sub_order_shipment_id + FROM raena_order_management.sales_sub_order) A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.sales_sub_order_shipment_id = B.id +LEFT JOIN public.OM_Logistic_final_shipping C ON A.sales_sub_order_shipment_id = C.reference_id +GROUP BY A.order_id; + +DROP TABLE public.shipping_fee_sku_base_stage1 ; + +CREATE TABLE public.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + +DROP TABLE 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 public.shipping_fee_sku_base_stage2_v1; + +CREATE TABLE public.shipping_fee_sku_base_stage2_v1 AS +SELECT order_id , sales_sub_order_shipment_id, + CASE + WHEN (sum(B.weight*Quantity))<1.3 THEN 1 + WHEN (sum(B.weight*Quantity))>=1.3 + AND (ABS(sum(B.weight*Quantity)) - FLOOR(ABS(sum(B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.weight*Quantity))+1 + ELSE FLOOR(sum(B.weight*Quantity)) + END AS sku_weight +FROM public.shipping_fee_sku_base_stage1 A +LEFT JOIN public.weight_sku_table B ON A.sku = B.sku +group by 1,2; + +DROP TABLE public.shipping_fee_sku_base_stage2; + +CREATE TABLE public.shipping_fee_sku_base_stage2 AS +SELECT A.*, + B.weight*sku_weight/sum(B.weight) over(partition by A.order_id , A.sales_sub_order_shipment_id ) Final_weight +FROM public.shipping_fee_sku_base_stage1 A +left join public.shipping_fee_sku_base_stage2_v1 C on A.order_id = C.order_id and A.sales_sub_order_shipment_id = C.sales_sub_order_shipment_id +LEFT join public.weight_sku_table B ON A.sku = B.sku ; + + + +DROP TABLE public.shipping_fee_sku_base_stage3; + +CREATE TABLE public.shipping_fee_sku_base_stage3 AS +SELECT A.*, + (Final_weight*coalesce(Calculated_shipping_fee,shipping_amount))/sum(Final_weight) over(partition BY A.sales_sub_order_shipment_id) new_shipment_amount +FROM public.shipping_fee_sku_base_stage2 A +left join public.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.sales_sub_order_shipment_id = B.reference_id ; + + +DROP TABLE sku_level_shipping_fee_final; + +CREATE TABLE public.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM public.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + +DROP TABLE public.shipping_fee_sku_base_old_stage1; + +CREATE TABLE public.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM gm_dashboard A +LEFT JOIN public.weight_sku_table B ON A.sku = B.sku; + +DROP TABLE public.shipping_fee_sku_base_old_stage2; + +CREATE TABLE public.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM public.shipping_fee_sku_base_old_stage1 ; + +DROP TABLE public.sku_level_shipping_fee_old_final; + +CREATE TABLE public.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM public.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3; + +DROP TABLE IF EXISTS public.payment_order_stage; + +CREATE TABLE public.payment_order_stage 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 public.payment_order; + +CREATE TABLE public.payment_order AS +SELECT A.id , + A.created_at, + A.order_id +FROM public.payment_order_stage A +INNER JOIN + (SELECT order_id , + max(created_at) max_created_at + FROM public.payment_order_stage + GROUP BY order_id) B ON A.created_at= B.max_created_at +AND A.order_id = B.order_id; + +select count(1) from public.payment_order; + +drop table if exists om_app_admin_dump_table; + +CREATE TABLE om_app_admin_dump_table AS +SELECT raena_order_management.order.id AS order_id, + dateadd(hour,7,raena_order_management.order.created_at) AS order_date, + dateadd(hour,7,raena_order_management.order.created_at) AS id_order_date, + status AS order_status, + Payment_status, + D.name AS channel_name, + cast(is_bank_transfer AS varchar) AS bank_transfer, + cast(is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + payment_amount, + shipping_amount AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + raena_order_management.order.stock_type AS product_stock_type, + sku AS product_sku, + oi.name AS product_name, + product_code, + date_key, + product_type, + batch, + country, + quantity, + wholesale_price , + retail_price, + total_price, + Total_earnings, + raena_order_management.order.is_slash_price_enabled , + Brand_name, + category_name, + product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + 0 total_cogs, + 'NA' bundle_sku, + 0 sku_level_shipping_fee +FROM raena_order_management.order +INNER JOIN raena_order_management.order_item oi ON raena_order_management.order .id=oi.order_id +left join public.payment_order C on raena_order_management.order.id=C.order_id +left join raena_order_management.channel D on raena_order_management.order.channel_id = D.id +WHERE raena_order_management.order.created_at <'2021-10-28' +UNION all +SELECT A.id AS order_id, + (A.created_at+interval '7 hours') AS order_date, + (A.created_at+interval '7 hours') AS id_order_date, + A.status AS order_status, + A.Payment_status, + D.name AS channel_name, + cast(A.is_bank_transfer AS varchar) AS bank_transfer, + cast(A.is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + GB.payment_amount, + coalesce(GB.new_shipping_amount_final,shipping_amount) AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + A.Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + A.shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN A.order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + A.stock_type AS product_stock_type, + coalesce(GB.sku,oi.sku) AS product_sku, + coalesce(gb.sku_name,oi.name) AS product_name, + product_code, + date_key, + coalesce(GB.product_type,oi.product_type), + batch, + country, + coalesce(GB.quantity,oi.quantity), + coalesce(GB.actual_wholesale_price,oi.wholesale_price) , + coalesce(GB.retail_price,oi.retail_price), + ((GB.retail_price*GB.quantity)-(GB.seller_margin*GB.quantity)), + Total_earnings, + A.is_slash_price_enabled , + coalesce(gb.brand_name,oi.Brand_name), + coalesce(gb.category_name,oi.category_name), + oi.product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + (GB.cogs) as total_cogs, + case when oi.parent_sku is null then 'NA' else oi.parent_sku end bundle_sku, + new_shipping_amount sku_level_shipping_amount +FROM raena_order_management.order A +inner join (select AD.* , + sum(new_shipping_amount) over(partition by AD.external_id) new_shipping_amount_final , + sum(discounted_price*quantity) over(partition by AD.external_id) payment_amount + from (SELECT A.*, + discounted_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_discounted_price, + actual_wholesale_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_actual_wholesale_price , + retail_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_retail_price, + seller_margin/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_seller_margin , + cogs/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_cogs, + (case when (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)/count(1) over(partition BY A.external_id,A.sku,A.item_type) new_shipping_amount + FROM public.gm_dashboard A + left join public.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 public.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) AD + )GB + ON A.id = GB.external_id AND GB.sku not like 'BAZ%' + left JOIN (select A.*,B.sku parent_sku + from raena_order_management.order_item A + left join raena_order_management.order_item B on A.parent_item_id = B.id ) oi + ON GB.external_id=oi.order_id + AND OI.SKU not like 'BAZ%' + AND GB.sku=oi.sku + and GB.parent_sku= oi.parent_sku +left join public.payment_order C on A.id=C.order_id +left join raena_order_management.channel D on A.channel_id = D.id +WHERE cast(A.created_at+interval '7 hours' as date) >='2021-10-28' ; + + + +DROP TABLE IF EXISTS public.remove_duplicate_from_app_admin; + + +CREATE TABLE public.remove_duplicate_from_app_admin AS +SELECT A.external_id, + wholesale_price_gm, + wholesale_price_app_admin, + wholesale_price_gm-wholesale_price_app_admin diff +FROM + (SELECT external_id , + sum(discounted_price*quantity) P1, + sum((retail_price*quantity)-(seller_margin*quantity)) wholesale_price_gm + FROM public.gm_dashboard gd + GROUP BY external_id) A +FULL OUTER JOIN + (SELECT order_id , + payment_amount p2, + sum(total_price) wholesale_price_app_admin + FROM public.om_app_admin_dump_table oaadt + GROUP BY order_id, + payment_amount) B ON A.external_id= B.order_id +WHERE wholesale_price_gm-wholesale_price_app_admin<>0; + + +DROP TABLE IF EXISTS public.dubplicate_record_app_admin; + + +CREATE TABLE public.dubplicate_record_app_admin AS +SELECT DISTINCT * +FROM public.om_app_admin_dump_table oaadt +WHERE order_id IN + (SELECT DISTINCT external_id + FROM public.remove_duplicate_from_app_admin); + + +DELETE +FROM public.om_app_admin_dump_table +WHERE order_id IN + (SELECT DISTINCT external_id + FROM public.remove_duplicate_from_app_admin); + +INSERT INTO public.om_app_admin_dump_table +SELECT * +FROM public.dubplicate_record_app_admin; + + + +" > /home/ec2-user/cronjob/Gm_dashboard/Final_GM_v2.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_v2.sql + + + diff --git a/Gm_dashboard/etl_gmdashboard_v1.sh b/Gm_dashboard/etl_gmdashboard_v1.sh new file mode 100644 index 0000000..2c3e4b5 --- /dev/null +++ b/Gm_dashboard/etl_gmdashboard_v1.sh @@ -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 + diff --git a/OM_New_VS_Existing.log b/OM_New_VS_Existing.log new file mode 100644 index 0000000..e69de29 diff --git a/OM_New_VS_Existing.sh b/OM_New_VS_Existing.sh new file mode 100644 index 0000000..be27308 --- /dev/null +++ b/OM_New_VS_Existing.sh @@ -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 diff --git a/OM_Test_Launched.log b/OM_Test_Launched.log new file mode 100644 index 0000000..9bc8f62 --- /dev/null +++ b/OM_Test_Launched.log @@ -0,0 +1,5 @@ +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE diff --git a/OM_Test_launch.sh b/OM_Test_launch.sh new file mode 100644 index 0000000..e4fdc2b --- /dev/null +++ b/OM_Test_launch.sh @@ -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 diff --git a/OOS/DB_funnel_new_users.log b/OOS/DB_funnel_new_users.log new file mode 100644 index 0000000..eab02e7 --- /dev/null +++ b/OOS/DB_funnel_new_users.log @@ -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 diff --git a/OOS/DB_funnel_new_users.sh b/OOS/DB_funnel_new_users.sh new file mode 100644 index 0000000..9e6f851 --- /dev/null +++ b/OOS/DB_funnel_new_users.sh @@ -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 diff --git a/OOS/DB_funnel_new_users.sql b/OOS/DB_funnel_new_users.sql new file mode 100644 index 0000000..9f80777 --- /dev/null +++ b/OOS/DB_funnel_new_users.sql @@ -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; + + + diff --git a/OOS/OOS_code.log b/OOS/OOS_code.log new file mode 100644 index 0000000..843c287 --- /dev/null +++ b/OOS/OOS_code.log @@ -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 diff --git a/OOS/OOS_code.sh b/OOS/OOS_code.sh new file mode 100644 index 0000000..98bb549 --- /dev/null +++ b/OOS/OOS_code.sh @@ -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 diff --git a/OOS/OOS_code.sql b/OOS/OOS_code.sql new file mode 100644 index 0000000..e046ee9 --- /dev/null +++ b/OOS/OOS_code.sql @@ -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; + + + + + + diff --git a/OOS/sample.log b/OOS/sample.log new file mode 100644 index 0000000..c0722a9 --- /dev/null +++ b/OOS/sample.log @@ -0,0 +1,2 @@ +DROP TABLE +SELECT diff --git a/OOS/sample.sh b/OOS/sample.sh new file mode 100644 index 0000000..353f97c --- /dev/null +++ b/OOS/sample.sh @@ -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 diff --git a/OOS/sample.sql b/OOS/sample.sql new file mode 100644 index 0000000..b557529 --- /dev/null +++ b/OOS/sample.sql @@ -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!=''; + + + diff --git a/OOS_code.log b/OOS_code.log new file mode 100644 index 0000000..287cf1f --- /dev/null +++ b/OOS_code.log @@ -0,0 +1 @@ +DROP TABLE diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/a.out b/a.out new file mode 100644 index 0000000..e69de29 diff --git a/am_recommendation.log b/am_recommendation.log new file mode 100644 index 0000000..992f3ef --- /dev/null +++ b/am_recommendation.log @@ -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 diff --git a/appsflyer/etl_appsflayer.log b/appsflyer/etl_appsflayer.log new file mode 100644 index 0000000..e69de29 diff --git a/appsflyer/etl_appsflyer.log b/appsflyer/etl_appsflyer.log new file mode 100644 index 0000000..0a6d71c --- /dev/null +++ b/appsflyer/etl_appsflyer.log @@ -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 diff --git a/appsflyer/etl_appsflyer.sh b/appsflyer/etl_appsflyer.sh new file mode 100644 index 0000000..9b5e13f --- /dev/null +++ b/appsflyer/etl_appsflyer.sh @@ -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 + diff --git a/appsflyer/etl_appsflyer.sql b/appsflyer/etl_appsflyer.sql new file mode 100644 index 0000000..19189c7 --- /dev/null +++ b/appsflyer/etl_appsflyer.sql @@ -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; + + diff --git a/appsflyer/etl_inappevent_appsflyer.sh b/appsflyer/etl_inappevent_appsflyer.sh new file mode 100644 index 0000000..bc2f09e --- /dev/null +++ b/appsflyer/etl_inappevent_appsflyer.sh @@ -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 diff --git a/appsflyer/etl_inappevent_appsflyer.sql b/appsflyer/etl_inappevent_appsflyer.sql new file mode 100644 index 0000000..da4d699 --- /dev/null +++ b/appsflyer/etl_inappevent_appsflyer.sql @@ -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; + + diff --git a/clevertap_funnel_reports.log b/clevertap_funnel_reports.log new file mode 100644 index 0000000..0e5099f --- /dev/null +++ b/clevertap_funnel_reports.log @@ -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 diff --git a/clevertap_funnel_reports/clevertap_funnel_reports.log b/clevertap_funnel_reports/clevertap_funnel_reports.log new file mode 100644 index 0000000..22a70fd --- /dev/null +++ b/clevertap_funnel_reports/clevertap_funnel_reports.log @@ -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 diff --git a/clevertap_funnel_reports/clevertap_funnel_reports.sql b/clevertap_funnel_reports/clevertap_funnel_reports.sql new file mode 100644 index 0000000..6845aea --- /dev/null +++ b/clevertap_funnel_reports/clevertap_funnel_reports.sql @@ -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; + + diff --git a/clevertap_funnel_reports/etl_cart_conversion.sh b/clevertap_funnel_reports/etl_cart_conversion.sh new file mode 100644 index 0000000..4ebfefa --- /dev/null +++ b/clevertap_funnel_reports/etl_cart_conversion.sh @@ -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 + diff --git a/consignement/consignement_code.sql b/consignement/consignement_code.sql new file mode 100644 index 0000000..d714e3f --- /dev/null +++ b/consignement/consignement_code.sql @@ -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) + + diff --git a/consignement_code b/consignement_code new file mode 100644 index 0000000..2e14d5c --- /dev/null +++ b/consignement_code @@ -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 + diff --git a/consignement_code.log b/consignement_code.log new file mode 100644 index 0000000..e69de29 diff --git a/cron.sh b/cron.sh new file mode 100755 index 0000000..7b8b269 --- /dev/null +++ b/cron.sh @@ -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 diff --git a/demandForecasting/Demand_forecasting.sql b/demandForecasting/Demand_forecasting.sql new file mode 100644 index 0000000..4e0a0ce --- /dev/null +++ b/demandForecasting/Demand_forecasting.sql @@ -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) + END6 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 + + + diff --git a/demandForecasting/db_funnel_code.sql b/demandForecasting/db_funnel_code.sql new file mode 100644 index 0000000..a64aa4f --- /dev/null +++ b/demandForecasting/db_funnel_code.sql @@ -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 + + diff --git a/demandForecasting/etlDemandForecasting.log b/demandForecasting/etlDemandForecasting.log new file mode 100644 index 0000000..e69de29 diff --git a/demandForecasting/etlDemandForecasting.sh b/demandForecasting/etlDemandForecasting.sh new file mode 100644 index 0000000..e40d5ae --- /dev/null +++ b/demandForecasting/etlDemandForecasting.sh @@ -0,0 +1,1013 @@ +#!/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"0 day" "+%Y-%m-%d") +echo 'reportDate'=$reportDate + +echo " +--------------------------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,'$reportDate')) B ON external_id =id +WHERE discounted_price <>0 and cast(transaction_date AS date) between dateadd(d,-120,'$reportDate') and '$reportDate'; + + -------------------------------------------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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' +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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') 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,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') 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,'$reportDate') AND '$reportDate' + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END) B ON (CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END)=B.txn_period +WHERE cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND '$reportDate' +GROUP BY sku, + CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') 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) >'$reportDate' 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 /home/ec2-user/cronjob/redshift/sql_code/Demand_forecasting.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/Demand_forecasting.sql + + + diff --git a/demandForecasting/to_run_etlDemandForecasting.sh b/demandForecasting/to_run_etlDemandForecasting.sh new file mode 100644 index 0000000..390fa2b --- /dev/null +++ b/demandForecasting/to_run_etlDemandForecasting.sh @@ -0,0 +1,1008 @@ +#!/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='2022-06-01' +echo 'reportDate'=$reportDate + +echo " +--------------------------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, + 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)) pre_discount_gm, + 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)) 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,'$reportDate')) B ON external_id =id +WHERE discounted_price <>0 and cast(transaction_date AS date) between dateadd(d,-120,'$reportDate') and '$reportDate'; + + -------------------------------------------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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' +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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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, + 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,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') 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,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') 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,'$reportDate') AND '$reportDate' + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END) B ON (CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END)=B.txn_period +WHERE cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND '$reportDate' +GROUP BY sku, + CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END, + Total_revenue; + +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 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) >'$reportDate' 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 /home/ec2-user/cronjob/demandForecasting/Demand_forecasting.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/demandForecasting/Demand_forecasting.sql > etlDemandForecasting.log + + + diff --git a/etlTransaction_job.log b/etlTransaction_job.log new file mode 100644 index 0000000..89b38b2 --- /dev/null +++ b/etlTransaction_job.log @@ -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 diff --git a/etlTransaction_job.sh b/etlTransaction_job.sh new file mode 100644 index 0000000..be520b8 --- /dev/null +++ b/etlTransaction_job.sh @@ -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, + diff --git a/etlTransaction_job_v2.log b/etlTransaction_job_v2.log new file mode 100644 index 0000000..d6da419 --- /dev/null +++ b/etlTransaction_job_v2.log @@ -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 diff --git a/etlTrasnsaction_job.log b/etlTrasnsaction_job.log new file mode 100644 index 0000000..f906f85 --- /dev/null +++ b/etlTrasnsaction_job.log @@ -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 diff --git a/etltransaction_job_final.sh b/etltransaction_job_final.sh new file mode 100644 index 0000000..7337f16 --- /dev/null +++ b/etltransaction_job_final.sh @@ -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 diff --git a/etlwarehouseAnalysis.log b/etlwarehouseAnalysis.log new file mode 100644 index 0000000..9c83159 --- /dev/null +++ b/etlwarehouseAnalysis.log @@ -0,0 +1,10 @@ +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT diff --git a/home_page_pdp_flash/home_page_pdp_flash.log b/home_page_pdp_flash/home_page_pdp_flash.log new file mode 100644 index 0000000..01a9ab1 --- /dev/null +++ b/home_page_pdp_flash/home_page_pdp_flash.log @@ -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 diff --git a/home_page_pdp_flash/home_page_pdp_flash.sql b/home_page_pdp_flash/home_page_pdp_flash.sql new file mode 100644 index 0000000..9440d83 --- /dev/null +++ b/home_page_pdp_flash/home_page_pdp_flash.sql @@ -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; + + + diff --git a/home_page_pdp_flash/home_page_pdp_flash_script.sh b/home_page_pdp_flash/home_page_pdp_flash_script.sh new file mode 100644 index 0000000..4193a99 --- /dev/null +++ b/home_page_pdp_flash/home_page_pdp_flash_script.sh @@ -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 diff --git a/install_uninsatll_rate.sh b/install_uninsatll_rate.sh new file mode 100644 index 0000000..4eb1072 --- /dev/null +++ b/install_uninsatll_rate.sh @@ -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 diff --git a/loyalty_point/loyalty_point.sh b/loyalty_point/loyalty_point.sh new file mode 100644 index 0000000..a6508c0 --- /dev/null +++ b/loyalty_point/loyalty_point.sh @@ -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 + + diff --git a/loyalty_point/loyalty_point.sql b/loyalty_point/loyalty_point.sql new file mode 100644 index 0000000..55cc43d --- /dev/null +++ b/loyalty_point/loyalty_point.sql @@ -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; + + diff --git a/mongo_rdash360.log b/mongo_rdash360.log new file mode 100644 index 0000000..6a6e66b --- /dev/null +++ b/mongo_rdash360.log @@ -0,0 +1,6 @@ +DROP TABLE +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE diff --git a/new_users/conversion_report.sh b/new_users/conversion_report.sh new file mode 100644 index 0000000..18a0ca4 --- /dev/null +++ b/new_users/conversion_report.sh @@ -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 + diff --git a/new_users/conversion_report.sql b/new_users/conversion_report.sql new file mode 100644 index 0000000..e4170f4 --- /dev/null +++ b/new_users/conversion_report.sql @@ -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 + + diff --git a/new_users/db_funnel_code.sh b/new_users/db_funnel_code.sh new file mode 100644 index 0000000..fbfd12f --- /dev/null +++ b/new_users/db_funnel_code.sh @@ -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 + + diff --git a/new_users/dbfunnel_Metabase.log b/new_users/dbfunnel_Metabase.log new file mode 100644 index 0000000..e69de29 diff --git a/new_users/reseller_business_metrics.sh b/new_users/reseller_business_metrics.sh new file mode 100644 index 0000000..d665e6b --- /dev/null +++ b/new_users/reseller_business_metrics.sh @@ -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 + diff --git a/new_users/reseller_business_metrics_code.sql b/new_users/reseller_business_metrics_code.sql new file mode 100644 index 0000000..c7106c3 --- /dev/null +++ b/new_users/reseller_business_metrics_code.sql @@ -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 + + diff --git a/new_users/reseller_business_metrics_code_Metabase.log b/new_users/reseller_business_metrics_code_Metabase.log new file mode 100644 index 0000000..e69de29 diff --git a/postgresql/360rdash/360_rdash_etl.sql b/postgresql/360rdash/360_rdash_etl.sql new file mode 100644 index 0000000..faf30d5 --- /dev/null +++ b/postgresql/360rdash/360_rdash_etl.sql @@ -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; + + + + diff --git a/postgresql/360rdash/rdash_etl_cron.sh b/postgresql/360rdash/rdash_etl_cron.sh new file mode 100644 index 0000000..c3d18c8 --- /dev/null +++ b/postgresql/360rdash/rdash_etl_cron.sh @@ -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 diff --git a/postgresql/am_dashboard/am_dashboard.sh b/postgresql/am_dashboard/am_dashboard.sh new file mode 100644 index 0000000..45b1d6e --- /dev/null +++ b/postgresql/am_dashboard/am_dashboard.sh @@ -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 diff --git a/postgresql/am_dashboard/am_dashboard.sql b/postgresql/am_dashboard/am_dashboard.sql new file mode 100644 index 0000000..73bd432 --- /dev/null +++ b/postgresql/am_dashboard/am_dashboard.sql @@ -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; + + + diff --git a/postgresql/am_recommendation/am_recommendation_etl.sql b/postgresql/am_recommendation/am_recommendation_etl.sql new file mode 100644 index 0000000..5fc34cf --- /dev/null +++ b/postgresql/am_recommendation/am_recommendation_etl.sql @@ -0,0 +1,2860 @@ + + + + +DROP TABLE raena_analytics.raw_ingredient_split_name; +CREATE TABLE raena_analytics.raw_ingredient_split_name AS +SELECT * , + split_part(concern,', ',1) concern1, + split_part(concern,', ',2) concern2, + split_part(concern,', ',3) concern3, + split_part(concern,', ',4) concern4, + split_part(concern,', ',5) concern5, + split_part(top_marketing_ingredients ,', ',1) ingredient1, + split_part(top_marketing_ingredients,', ',2) ingredient2, + split_part(top_marketing_ingredients,', ',3) ingredient3, + split_part(top_marketing_ingredients,', ',4) ingredient4, + split_part(top_marketing_ingredients,', ',5) ingredient5, + split_part(top_marketing_ingredients,', ',6) ingredient6, + split_part(top_marketing_ingredients,', ',7) ingredient7, + split_part(top_marketing_ingredients,', ',8) ingredient8, + split_part(top_marketing_ingredients,', ',9) ingredient9, + split_part(top_marketing_ingredients,', ',10) ingredient10, + split_part(top_marketing_ingredients,', ',11) ingredient11 +FROM + (SELECT DISTINCT group_base, + sku , + lower(concern) concern, + lower(top_marketing_ingredients) top_marketing_ingredients, + CASE + WHEN concern='' + AND top_marketing_ingredients='' THEN 1 + END flag + FROM raena_analytics.ingredients_base_table_final) A +WHERE flag IS NULL; + + DROP TABLE raena_analytics.raw_ingredient_mapping_table_stage; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage AS +SELECT A.* , + B.group_base mapping_group_base, + B.concern mapping_concerns, + B.top_marketing_ingredients mapping_ingredients +FROM raena_analytics.raw_ingredient_split_name A +LEFT JOIN + (SELECT * + FROM raena_analytics.raw_ingredient_split_name) B ON 1=1; + +DROP TABLE if exists raena_analytics.raw_ingredient_mapping_table_stage2; + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage2 AS +SELECT * , + CASE + WHEN raena_analytics.udf_string_campare(concern1,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern1,mapping_concerns) <> 'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern1, + CASE + WHEN raena_analytics.udf_string_campare(concern2,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern2,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern2, + CASE + WHEN raena_analytics.udf_string_campare(concern3,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern3,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern3, + CASE + WHEN raena_analytics.udf_string_campare(concern4,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern4,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern4, + CASE + WHEN raena_analytics.udf_string_campare(concern5,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern5,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern5, + ARRAY_LENGTH(REGEXP_SPLIT_TO_ARRAY(concern,','),1)total_count_concern, + CASE + WHEN raena_analytics.udf_string_campare(ingredient1,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient1,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient1, + CASE + WHEN raena_analytics.udf_string_campare(ingredient2,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient2,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient2, + CASE + WHEN raena_analytics.udf_string_campare(ingredient3,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient3,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient3, + CASE + WHEN raena_analytics.udf_string_campare(ingredient4,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient4,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient4, + CASE + WHEN raena_analytics.udf_string_campare(ingredient5,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient5,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient5, + CASE + WHEN raena_analytics.udf_string_campare(ingredient6,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient6,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient6, + CASE + WHEN raena_analytics.udf_string_campare(ingredient7,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient7,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient7, + CASE + WHEN raena_analytics.udf_string_campare(ingredient8,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient8,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient8, + CASE + WHEN raena_analytics.udf_string_campare(ingredient9,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient9,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient9, + CASE + WHEN raena_analytics.udf_string_campare(ingredient10,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient10,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient10, + CASE + WHEN raena_analytics.udf_string_campare(ingredient11,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient11,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient11, + ARRAY_LENGTH(REGEXP_SPLIT_TO_ARRAY(top_marketing_ingredients,','),1) total_count_ingredient +FROM raena_analytics.raw_ingredient_mapping_table_stage +WHERE group_base <> mapping_group_base; + + DROP TABLE if exists raena_analytics.raw_ingredient_mapping_table_stage3; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage3 AS +SELECT *, + flag_concern1+flag_concern2+flag_concern3+flag_concern3+flag_concern3 total_concern_match, + flag_ingredient1+flag_ingredient2+flag_ingredient3+flag_ingredient4+flag_ingredient5+flag_ingredient6+flag_ingredient7+flag_ingredient8+flag_ingredient9+flag_ingredient10+flag_ingredient11 total_ingredient_match +FROM raena_analytics.raw_ingredient_mapping_table_stage2 ; + + +DROP TABLE IF EXISTS raena_analytics.raw_ingredient_mapping_table_stage4; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage4 AS +SELECT *, (total_concern_match*100/total_count_concern) Percent_concern_match, (total_ingredient_match*100/total_count_ingredient) Percent_ingredient_match , + CASE WHEN concern='' AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND top_marketing_ingredients='' THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + END percent_flag +FROM raena_analytics.raw_ingredient_mapping_table_stage3; + + +Drop table if exists raena_analytics.raw_ingredient_mapping_table; + +Create table raena_analytics.raw_ingredient_mapping_table +as +SELECT group_base, + sku, + concern, + top_marketing_ingredients, + mapping_group_base, + mapping_concerns, + mapping_ingredients, + percent_concern_match, + Percent_ingredient_match +FROM raena_analytics.raw_ingredient_mapping_table_stage4 +WHERE percent_flag=1 +ORDER BY group_base , + percent_concern_match DESC , + Percent_ingredient_match DESC; + + +DROP TABLE IF EXISTS raena_analytics.sku_in_stock; + + +CREATE TABLE raena_analytics.sku_in_stock AS +SELECT sku , + sum(coalesce(F.stock_limit,0)) 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; + + --------------------------------------------------------------task 1 ---------------- + +drop table if exists raena_analytics.recommendation_power_seller_base_alt; + +create table raena_analytics.recommendation_power_seller_base_alt +as +select distinct email from raena_analytics.om_acquisition_mapping_list +union select distinct "email id" from raena_analytics.AM_REVENUE_LEAKAGE_LIST ; + + +INSERT INTO raena_analytics.recommendation_power_seller_base_alt + values ('cindyvioleta29@yahoo.com'), +('sherryfannydesita@gmail.com'), +('masintansilalahi@yahoo.co.id'), +('rumahskincarebpp@gmail.com'), +('puturisca@gmail.com'), +('sieny.go@gmail.com'), +('oki.zhang1988@gmail.com'), +('jeremytantono@yahoo.co.id'), +('yuke.cipta@gmail.com'), +('kwonvic@hotmail.com'), +('ervinafransiska95@gmail.com'), +('indahamaliasasmita@gmail.com'), +('mirosebeauty@gmail.com'), +('fourrshoptarakan@gmail.com'), +('stuffbyms@gmail.com'), +('cindyrissa14@gmail.com'), +('lieke1212@gmail.com'), +('jelitacosmetic.yk@gmail.com'), +('metafuji@gmail.com'), +('destiyapradhani@gmail.com'), +('liepinghau@gmail.com'), +('jennifer_kambuno@yahoo.com'), +('y35beauty@gmail.com'), +('nisamulya@gmail.com'), +('issadella@yahoo.co.id'), +('hartono.henny@yahoo.co.id'), +('a2335572@gmail.com'), +('navisgusmiati24@gmail.com'), +('djiwandimelany@gmail.com'), +('colinakoh178@gmail.com'), +('fairybeaute17@gmail.com'), +('fajri.vanbio95@gmail.com'), +('zidney_yao@yahoo.com'), +('chandraalex305@gmail.com'), +('elly.elisha@gmail.com'), +('erlinapr13@gmail.com'), +('mechan_06@hotmail.com'), +('limvincents@hotmail.com'), +('tjuganteng@gmail.com'), +('sralaydrus96@yahoo.com'), +('duwik_tonx@yahoo.co.id'), +('adelshopadelia@gmail.com'), +('shierlysusanto2405@gmail.com'), +('murni.cosmetics55@gmail.com'), +('stg_purchasing_melisa@yahoo.com'), +('asri.nilasari@watsons.co.id'), +('wayanndiana@gmail.com'), +('emailtedy@gmail.com'), +('Shantifm@hotmail.com'), +('shintachristiani2911@gmail.com'), +('dotienigloo@gmail.com'), +('mirandadessy95@gmail.com'), +('pangeranpetirr@gmail.com'), +('mariamellysacandra@ymail.com'), +('evitamalaa@ymail.com'), +('lindadarwinp@gmail.com'), +('dyfabandung@gmail.com'), +('yestastore@gmail.com'), +('outletmadanibeauty@gmail.com'), +('mrs.kumara@yahoo.com'), +('cindylum23@gmail.com'), +('fairy_love90@ymail.com'), +('fajarcosmetikpekanbaru@gmail.com'), +('alapstorekosmetik@gmail.com'), +('marlinabahnur@yahoo.co.id'), +('miswarderi61@gmail.com'), +('kristian_tjia@yahoo.co.id'), +('dessi.three@gmail.com'), +('rumahkurumahta@gmail.com'), +('nabilaalifia2000@gmail.com'), +('ullyfina@gmail.com'), +('tjindaidyandy@gmail.com'), +('shintayuu@gmail.com'), +('ratihpuji001@gmail.com'), +('shynobyshould@gmail.com'), +('rismawatisapu101@gmail.com'), +('caramelmadeby@gmail.com'), +('piyamapoppy@gmail.com'), +('scarlettwhitening.manado@gmail.com'), +('makeupbliss.id@gmail.com'), +('agnyjmpardosi@gmail.com'), +('sherbeautee@gmail.com'), +('yfbeauty99@gmail.com'), +('cmstadulako36@gmail.com'), +('lizalgs97@gmail.com'), +('emailnyawabisabi@gmail.com'), +('alvhiraalamrih@gmail.com'), +('megasyaf10@gmail.com'), +('ismisarisapitri6@gmail.com'), +('realrima91@gmail.com'), +('hestynorata74@gmail.com'), +('tokopinkbox@gmail.com'), +('melly.angelica@sils.co.id'), +('baguzokee@gmail.com'), +('vandy_hood@yahoo.com'), +('stevenwerencius15@gmail.com'), +('qurniaulan123@gmail.com'), +('ivanalystia21@gmail.com'), +('claralverinas@gmail.com'), +('riaarddd@gmail.com'), +('yayz_bubble@yahoo.com'), +('elsa.rivani@rocketmail.com'), +('feyrenpusura@gmail.com'), +('gunz_r@yahoo.com'), +('asrularman077@gmail.com'), +('hellojuwitaang@gmail.com'), +('bernike.daniel@gmail.com'), +('ayuerawati1993@gmail.com'), +('sudutcantik03@gmail.com'), +('norio.agnes@gmail.com'), +('smarch2u@gmail.com'), +('nunungokta8@gmail.com'), +('siaocie.tw@gmail.com'), +('sandrachen20@icloud.com'), +('siti.komariiah@gmail.com'), +('gustine.fu999@gmail.com'), +('ennysetiono@msn.com'), +('sartikanganro@gmail.com'), +('mirandahabibie@gmail.com'), +('tokodahliapadang@gmail.com'), +('natanaelsbrn@gmail.com'), +('jelitacosmetic@galeri.com'), +('rkkosmetikviral.anj@gmail.com'), +('sancaytulak26@gmail.com'), +('kelvinhosen@gmail.com'), +('elnazulma@gmail.com'), +('jevrihuang@gmail.com'), +('araitsoke@gmail.com'), +('haju.nutrifood@gmail.com'), +('harwindaharis99@gmail.com'), +('saherasukur92@gmail.com'), +('monamelosi05@gmail.com'), +('pramistiseptarinda@gmail.com'), +('rianadewi15746@gmail.com'), +('frivaldi08@gmail.com'), +('sausankho86@gmail.com'), +('3widjayanti@gmail.com'), +('prinzlina79@gmail.com'), +('restbeauty11@gmail.com'), +('kiarriyasatul@gmail.com'), +('surkiaa56@gmail.com'), +('dellikosmetik@gmail.com'), +('dinafebriana@gmail.com'), +('risnawatiyunus@yahoo.com'), +('pudoopy@gmail.com'), +('serbunganjuk@gmail.com'), +('linda_ho29@yahoo.com'), +('cassmgadm@gmail.com'), +('jennychateriner@gmail.com'), +('thasashop.id@yahoo.com'), +('diancagoodsbywitari@gmail.com'), +('chang3mu@icloud.com'), +('hana_kho@outlook.com'), +('jwl.skinofficial@gmail.com'), +('febri.ilham68@gmail.com'), +('gurl.shopaholic@gmail.com'), +('fenitan00@gmail.com'), +('nadrachanafie@gmail.com'), +('migibeauty1@gmail.com'), +('dewisusianti888@gmail.com'), +('nazwinadya@gmail.com'), +('andinimaylasari@gmail.com'), +('syahrani.cosmetic@gmail.com'), +('primasitaa@gmail.com'), +('spy_solution77@yahoo.com'), +('geeghina@gmail.com'), +('nida.syafa5@gmail.com'), +('financialelzamora@gmail.com'), +('irkosmetikdanskincare@gmail.com'), +('sherlinhosana@gmail.com'), +('sepdiarina@yahoo.com'), +('lumbangaol.ira@gmail.com'), +('aligunawan098@gmail.com'), +('ovarash@gmail.com'), +('rbeauty.storee@gmail.com'), +('cckosmetik77@gmail.com'), +('pinkcosmetich@gmail.com'), +('dhiniemsa@yahoo.com'), +('rulyindah1911@gmail.com'), +('elvina.6789@gmail.com'), +('moeyecosmetic99@gmail.com'), +('sana.19007@mhs.unesa.ac.id'), +('ikasusanti_baru@yahoo.com'), +('indaryani82@gmail.com'), +('hsyapon@gmail.com'), +('citra.ismaya@omniretail.co'), +('thevintagestrawberry@gmail.com'), +('ramadhani.kosmetik@gmail.com'), +('linawijaya2712@yahoo.com'), +('meilings0403@gmail.com'), +('nadyawkk@gmail.com'), +('prellasurabaya@gmail.com'), +('ziyyags@gmail.com'), +('stanley_oh@ymail.com'), +('andre1177az@gmail.com'), +('s.ekaputrirosalinda@gmail.com'), +('beautyland21@icloud.com'), +('javalotuscorp@gmail.com'), +('alxsuherman@gmail.com'), +('fenny3007.kwok@gmail.com'), +('leny_kurniawati@hotmail.com'), +('soputanlady10@gmail.com'), +('lampungbeauty2019@gmail.com'), +('thebeauteebox@gmail.com'), +('tfanixling00@gmail.com'), +('andilesmono06@gmail.com'), +('briggiteleorenza@gmail.com'), +('chanytatiara@gmail.com'), +('financeklinikpiramidajaya@gmail.com'), +('exacarolina@gmail.com'), +('wildhanhafid@gmail.com'), +('himakeup08@gmail.com'), +('riska.sofani@yahoo.com'), +('uciliana35@gmail.com'), +('agusjokin08@gmail.com'), +('madozai96@gmail.com'), +('flo_ver@yahoo.com'), +('dikinarasaki@gmail.com'), +('herul.batch3@gmail.com'), +('rudy.hartono2591@gmail.com'), +('vivi.plm187@gmail.com'), +('hallo@bencuan.id'), +('rahasiamama17@gmail.com'), +('stephzhengg@gmail.com'), +('shelinabeautycare@gmail.com'), +('joelchristianpamula@gmail.com'), +('rethamardjoen11@gmail.com'), +('pomadaivon@gmail.com'), +('divaparis28@gmail.com'), +('wulan.ranpatika@gmail.com'), +('mastoreyadi@gmail.com'), +('febi.muhi@gmail.com'), +('anggriawanferi1991@gmail.com'), +('feliciacolleen80@gmail.com'), +('istie.nodasekisan@gmail.com'), +('melliyniaanugrah@gmail.com'), +('dwiaziza98@gmail.com'), +('alfrianiyelsi@gmail.com'), +('adelia.vn@gmail.com'), +('alvina.iriady@gmail.com'), +('ratnaqisya03@gmail.com'), +('jelitacosmetic.galeri@gmail.com'), +('avex307@gmail.com'), +('rika.wulandaridwan@gmail.com'), +('srihartiniseptember1966@gmail.com'), +('theresia_margareths@yahoo.com'), +('kartikahs89@gmail.com'), +('yanuararifinnur@gmail.com'), +('luthfikhtr@gmail.com'), +('mshopper.id@gmail.com'), +('fifi.liu@yahoo.com'), +('christineeva07@gmail.com'), +('claralverina23@gmail.com'), +('12101na@gmail.com'), +('joanamarvelin.wongso@yahoo.com'), +('lylysiakwee@gmail.com'), +('andyirsaldi@gmail.com'), +('greceliautami@icloud.com'), +('rhayati48@gmail.com'), +('vidya@redroseindo.com'), +('ekanuraini131@gmail.com'), +('kimhiung168@gmail.com'), +('gitaporo@gmail.com'), +('aninditateresa@gmail.com'), +('lexonbeautyofficial@gmail.com'), +('novitaroshaputri@gmail.com'), +('theara.tiara@gmail.com'), +('selvi.chephy@gmail.com'), +('vhaans007@gmail.com'), +('tokosehat130@gmail.com'), +('mirayyacosmetics@gmail.com'), +('kusnen.sota@gmail.com'), +('hokimaxi2020@gmail.com'), +('keikolux@hotmail.com'), +('andreadewi19@gmail.com'), +('harlyskin@gmail.com'), +('viroe.nando@gmail.com'), +('aurelallshop@gmail.com'), +('kosmetikiis@gmail.com'), +('lifanny86@gmail.com'), +('nitamanda28@gmail.com'), +('cantiku8899@gmail.com'), +('sultanagengs3@gmail.com'), +('nurfiitriiyah@gmail.com'), +('nana01iphone@gmail.com'), +('mulyajaaya.motor96@gmail.com'), +('titarahmawati245@gmail.com'), +('marniatihinding@gmail.com'), +('erris.mahardika@gamal.men'), +('isti.qhatsah.nur@gmail.com'), +('dewilasolo@gmail.com'), +('aninditaputri30@gmail.com'), +('aryanto.firdaus@gmail.com'), +('novitaj68@gmail.com'), +('johan_breaker27@yahoo.com'), +('elsaberliya@gmail.com'), +('dewiyuant260693@gmail.com'), +('jeffrynoah@gmail.com'), +('rikasukmawar03@gmail.com'), +('yosua.sasauw1@gmail.com'), +('fitri.miauwh@gmail.com'), +('yuli120797@gmail.com'), +('kamala.tanray@gmail.com'), +('theresiad88@gmail.com'), +('isliana29@gmail.com'), +('chietra.anggraini93@gmail.com'), +('cesario2559@gmail.com'), +('the.secret.of.beauty.id@gmail.com'), +('elsacosmetics@yahoo.com'), +('evinrahmad2@gmail.com'), +('yuhyi.albadali@gmail.com'), +('darwin.saputra.ars@gmail.com'), +('akuntingtim2903@gmail.com'), +('wahyudinrazali4@gmail.com'), +('jwuysan@gmail.com'), +('ruus.silvana@gmail.com'), +('dianmarinaa@gmail.com'), +('dp.anggreani@gmail.com'), +('patricia.ang7@gmail.com'), +('medankosmetik89@gmail.com'), +('ayuputri.ap2201@gmail.com'), +('fikrizia13@gmail.com'), +('beautiva.skincare@gmail.com'), +('gjwproject@gmail.com'), +('pramudya.a.laksono@gmail.com'), +('ssshoptarakan@gmail.com'), +('bungalintong7@gmail.com'), +('ivanakaontole@gmail.com'), +('misel.delini@yahoo.com'), +('dwipujiati1995@gmail.com'), +('vdillahilda@gmail.com'), +('ellenanatasa@gmail.com'), +('bidarishabrina@gmail.com'), +('danimmm44@gmail.com'), +('evitsrikris@gmail.com'), +('gendisbeauty92@gmail.com'), +('eriana.dian0116@gmail.com'), +('eriskhfrd26@gmail.com'), +('jasmineniscalapandya@gmail.com'), +('eryryey03@gmail.com'), +('leessleepwear@gmail.com'), +('yoseptansil6@gmail.com'), +('natdailybeauty@gmail.com'), +('sidoarjosalonsupp@gmail.com'), +('auliarahmadanti77@gmail.com'), +('gustindw@gmail.com'), +('mamihcihuyyy@gmail.com'), +('narabeautytrk@gmail.com'), +('sandikaplg18@gmail.com'), +('20240010013@lspr.edu'), +('maynetav@gmail.com'), +('intandidi88@gmail.com'), +('jeniferpurnomo99@gmail.com'), +('dewisartika192@yahoo.co.id'), +('jokosus.susanto@gmail.com'), +('iswanto.1301@gmail.com'), +('kudo.palopo14@gmail.com'), +('micellangel@nuriglobal.com'), +('theeaanggraeny@gmail.com'), +('andi.or88@gmail.com'), +('rickyoktaviawan20@gmail.com'), +('yunibalisa661@gmail.com'), +('rikiscofieldsap@gmail.com'), +('feliciabeatrixtanner0407@gmail.com'), +('mon_taca@yahoo.com'), +('ardyanawidya@gmail.com'), +('d14na_nonic@yahoo.com'), +('kadirpnm@gmail.com'), +('mita.panji@gmail.com'), +('robbchriszzz13@gmail.com'), +('anicha8490@gmail.com'), +('galuh.acc.kediri@gmail.com'), +('tokosanjayakras.81@gmail.com'), +('vivian.effendi@gmail.com'), +('lisaderia@yahoo.com'), +('anggyfebryna17@gmail.com'), +('yuanita200989@gmail.com'), +('inggycitrasari@gmail.com'), +('musdalifa300396@gmail.com'), +('widdywedariasih3@gmail.com'), +('devinoariska06@gmail.com'), +('idealistina@yahoo.co.id'), +('kelsy18111@gmail.com'), +('elizchang88@gmail.com'), +('kavbeautyofficial@gmail.com'), +('archilagiska@gmail.com'), +('ginawati442@gmail.com'), +('revii.setiadi@gmail.com'), +('nisacuss@gmail.com'), +('eviana001075@gmail.com'), +('moanazone@gmail.com'), +('idabenangraja@gmail.com'), +('babybamscase@gmail.com'), +('bellasetiady@windowslive.com'), +('regina.amalia19@gmail.com'), +('hello.vebeauty@gmail.com'), +('ferinaindrayani@gmail.com'), +('yulia.cindy81@gmail.com'), +('ennakristiana@gmail.com'), +('nur.annisa0@gmail.com'), +('marindarara13@gmail.com'), +('sonyatiaratri2@gmail.com'), +('achmadfauzi1398@gmail.com'), +('fitrijean@gmail.com'), +('zul.hendra1297@gmail.com'), +('csbymagicskin@gmail.com'), +('ridadwi873@gmail.com'), +('grosirkosmetikpekanabaru@gmail.com'), +('gladys.agatha@gmail.com'), +('rahmizarti30@gmail.com'), +('mrs.sitiazizah@gmail.com'), +('paramitaqueena@yahoo.com'), +('uwiecute22@gmail.com'), +('slife557.cs@gmail.com'), +('adindazahra655@gmail.com'), +('lavibeautyandlifestyle@gmail.com'), +('ratna.pratiwi14.rp@gmail.com'), +('yunengazraapriani@gmail.com'), +('sheefaa84@gmail.com'), +('katherinelimanu21@gmail.com'), +('tutiyuniyanti@gmail.com'), +('kartinisarimah@gmail.com'), +('jonastampubolon123@gmail.com'), +('wuisancheni77@gmail.com'), +('shella.madirza888@gmail.com'), +('yulianisetiawan836@gmail.com'), +('janiceechenn@yahoo.com'), +('ferico.ridwan@gmail.com'), +('udjayamandiripgs@gmail.com'), +('budiwijaya1962@gmail.com'), +('rumahkosmetik001@gmail.com'), +('kinanbeautystore@gmail.com'), +('yuniraemah31@yahoo.co.id'), +('barokahbeaute@gmail.com'), +('shrlymarcella@gmail.com'), +('ikrt_2507@yahoo.com'), +('dikofebyan@gmail.com'), +('restyty.riezalina@yahoo.com'), +('ratukosmetik169@gmail.com'), +('faradillahif07@gmail.com'), +('sales@dewicosmetic.com'), +('kadek.arymas@gmail.com'), +('rahmaduwijayanti@gmail.com'), +('fanamaliaauthor@gmail.com'), +('vanessapratama24@gmail.com'), +('gangnamcosmetic109@gmail.com'), +('mayanglatuconsina@gmail.com'), +('lustremaiden@gmail.com'), +('ratihalexa@gmail.com'), +('anindyalarasp@gmail.com'), +('fourayoebeauty@gmail.com'), +('selenabeauty.banjarbaru@gmail.com'), +('destyisniaty@gmail.com'), +('rmdhniannisa15@gmail.com'), +('devimutiah18@gmail.com'), +('ainunmuthiaa@gmail.com'), +('nurhikmah.rasyid@gmail.com'), +('burizma.store@gmail.com'), +('ariefariq4@gmail.com'), +('haniaekap8814@gmail.com'), +('ricomerico240@gmail.com'), +('ekanvt07@gmail.com'), +('helenlim02@gmail.com'), +('lukm.hakm@gmail.com'), +('nur_aisyah1407@icloud.com'), +('serlina.mbc@gmail.com'), +('k.inayah23@gmail.com'), +('marisrisma@yahoo.com'), +('shela.annisa99@gmail.com'), +('angguntrivany@gmail.com'), +('nntheorange@gmail.com'), +('nathaliayaulin23@gmail.com'), +('mrs.gapura@gmail.com'), +('selviathalita92@gmail.com'), +('cynthia.de06@gmail.com'), +('beautemondee@gmail.com'), +('ninikrahmawati486@gmail.com'), +('anst.slvia@gmail.com'), +('beautycarepalu@gmail.com'), +('ririwiduri2002@gmail.com'), +('ilookdaily534@gmail.com'), +('apapunada.id@gmail.com'), +('dellasyaskia6@gmail.com'), +('nadiasalmau@gmail.com'), +('tikamaharaeni19@gmail.com'), +('aansabang@gmail.com'), +('devinatalithanabila98@gmail.com'), +('ennitrisha@gmail.com'), +('marialucyanasunjaya02762@gmail.com'), +('rtnohandayani@gmail.com'), +('nsbeauty06@gmail.com'), +('idapurnamasari0129@gmail.com'), +('msreysantana@gmail.com'), +('aldillaputri5@gmail.com'), +('maderusni25@gmail.com'), +('crystalglow.myshop@gmail.com'), +('dheamuliasari@gmail.com'), +('cindyfdle@gmail.com'), +('laninabeauty.official@gmail.com'), +('nimatussaadah29@gmail.com'), +('merrylaili@gmail.com'), +('wijaya.nanda1810@gmail.com'), +('kadavikadav@gmail.com'), +('anggikurniawati40@gmail.com'), +('mamikikeyu@gmail.com'), +('marlindaabdulrazak@gmail.com'), +('alizhaaa@gmail.com'), +('queenkosmetik20@gmail.com'), +('skincarekupang21@gmail.com'), +('feny.asnawi@gmail.com'), +('shanticahyadi82@gmail.com'), +('zilvanaputri@gmail.com'), +('malikaresmanti@gmail.com'), +('yunisianturi06@gmail.com'), +('beautydarlinggg@gmail.com'), +('lilybeauty.ofc@gmail.com'), +('racyasry@gmail.com'), +('terasbeauty.official@gmail.com'), +('mybeautystory.89@gmail.com'), +('novita.chyntia@yahoo.com'), +('tiffany.kimana@gmail.com'), +('rumahdandan.rd15@gmail.com'), +('desihartanti17@gmail.com'), +('forshare.id@gmail.com'), +('elmiacosmetics@gmail.com'), +('evikartika03@gmail.com'), +('allwomeneeds1403@yahoo.com'), +('nadyahudaya@yahoo.com'), +('drchrysant@gmail.com'), +('yurimeni16@gmail.com'), +('anientya@gmail.com'), +('martina.bernadeta@gmail.com'), +('wulan.dhizhi@gmail.com'), +('andrixue@gmail.com'), +('sitiqulsum308@gmail.com'), +('galaxystoreyogyakarta@gmail.com'), +('iskifitriani@yahoo.co.id'), +('arismunandar6496@gmail.com'), +('agenlingerie.id@gmail.com'), +('indahasmoro90@gmail.com'), +('ksbeautylover@gmail.com'), +('alheabeauty.id@gmail.com'), +('putriitunggal99@gmail.com'), +('ahmadmalangah@gmail.com'), +('perfectionten@gmail.com'), +('ivonsetiono26@gmail.com'), +('agencmb@gmail.com'), +('glowivier@gmail.com'), +('pinkyscosmetics1995@gmail.com'), +('evinakuesnoooo@gmail.com'), +('chennywi0126@icloud.com'), +('ekawahyuni1603@gmail.com'), +('auliaqoriardi21@gmail.com'), +('joyakhineigsoen@gmail.com'), +('susantifajarwati@yahoo.com'), +('ramadhana.selvia2408@gmail.com'), +('andarish.shop@gmail.com'), +('cindytsania@gmail.com'), +('joctora@gmail.com'), +('hannahimanuella@gmail.com'), +('dwiwulandari028@gmail.com'), +('merrychrystin06@gmail.com'), +('fibi.kdi891@gmail.com'), +('suwitosjahari@gmail.com'), +('likestoretlogosari@gmail.com'), +('care.meiyabeauty@gmail.com'), +('ajenganggitasari1@gmail.com'), +('rizkiyanitapinem@gmail.com'), +('sashop.nkc21@gmail.com'), +('lilistiyorini@gmail.com'), +('thebellas1212@gmail.com'), +('be.bellecosmetic@gmail.com'), +('nurlisaaulia@gmail.com'), +('hantybeauty19@gmail.com'), +('mputrisanti@gmail.com'), +('hellolightbit@gmail.com'), +('nadhirauw88@gmail.com'), +('asmiatiakib@gmail.com'), +('ranintya12@gmail.com'), +('afrijalrijal671@gmail.com'), +('ifayusana1993@gmail.com'), +('makeyouup.myu@gmail.com'), +('salbinus.innung@yahoo.com'), +('novia190@gmail.com'), +('evadewim.s@gmail.com'), +('itsmiracle2020@gmail.com'), +('neveltari2703@gmail.com'), +('lieusbudiman@gmail.com'), +('ferisya10@gmail.com'), +('echa.muaniez89@gmail.com'), +('trinarachmawati81@gmail.com'), +('erwinzeey123@gmail.com'), +('sahabat_debora@yahoo.com'), +('dhee2jopankar@gmail.com'), +('yunda.ade11@gmail.com'), +('cyril_steven_alexander_glory@yahoo.com'), +('devisiska32@gmail.com'), +('aureolasa.pratama@gmail.com'), +('ika.angky123@yahoo.com'), +('halimatussakdiyah1997@gmail.com'), +('rahmatjayaj2@gmail.com'), +('jesselynangella08@yahoo.com'), +('famela.ft07@gmail.com'), +('rotinsulufinny@gmail.com'), +('staryisnianti@gmail.com'), +('yulianazayan@gmail.com'), +('fandestian@gmail.com'), +('melindariaslamusnar88@gmail.com'), +('jnwkidswear@gmail.com'), +('wiwin.adriani@gmail.com'), +('febriantisiban25@gmail.com'), +('scoviabybelova@gmail.com'), +('wenny.than@gmail.com'), +('iranipontoh443@gmail.com'), +('hello.lazetta@gmail.com'), +('moullylaroid@gmail.com'), +('laurantjia@gmail.com'), +('dbeautyshop8@gmail.com'), +('godzillaphoneacc3@gmail.com'), +('wiwinstsajaah@gmail.com'), +('rizahani39@gmail.com'), +('dwihartanti_gizi@yahoo.com'), +('fangfangsaputra@gmail.com'), +('amoise.cosmetics@gmail.com'), +('dennyroseiana@gmail.com'), +('nelly.sulistiawati@gmail.com'), +('central.kosmetik1106@gmail.com'), +('marcellino.haryono@gmail.com'), +('veralia.saragih@gmail.com'), +('cefamanusia@gmail.com'), +('arindatrisna33@gmail.com'), +('oketomatala@gmail.com'), +('vilyapangajouw19@gmail.com'), +('faracintadia@gmail.com'), +('adm.elisabethbdg@yahoo.com'), +('cutmiqyal@hotmail.com'), +('harjono.honoris@gmail.com'), +('nandorian98@gmail.com'), +('milalimbong71@gmail.comcom'), +('viliansiregar24@gmail.com'), +('eldeekosmetik@gmail.com'), +('jeane.lelengboto@yahoo.com'), +('lydia.debby@gmail.com'), +('aboutmybusiness11@gmail.com'), +('triple501ara@gmail.com'), +('masfitroh@gmail.com'), +('meisalmis@gmail.com'), +('zackfair232@gmail.com'), +('nakamia_pie@yahoo.com'), +('abeauty.jogja@gmail.com'), +('lenaaska7@gmail.com'), +('zeebacosmetics31@gmail.com'), +('chiangkencana@gmail.com'), +('syaaudriana@gmail.com'), +('nadyaffu@gmail.com'), +('fauziahanisa509@gmail.com'), +('christyciasintaungu@gmail.com'), +('pradnyani.dwi@gmail.com'), +('zarazsby@gmail.com'), +('adewahyupauji@gmail.com'), +('firmantuo@gmail.com'), +('ramadhanilayla@gmail.com'), +('novascafe4@gmail.com'), +('mayaecc@yahoo.com'), +('adeyoan721@gmail.com'), +('stephaniefransss@gmail.com'), +('hermooi.official@gmail.com'), +('ratnaanita170294@gmail.com'), +('agatri.silitonga@gmail.com'), +('dewifitri2403@gmail.com'), +('moehamadsururi14@gmail.com'), +('ariestyayys@gmail.com'), +('heavenlybeauty.adm@gmail.com'), +('ikaabsarih@gmail.com'), +('dhee_amell@yahoo.co.id'), +('devianatanuwijaya@gmail.com'), +('rumah.karara@gmail.com'), +('sreejita.deb@gmail.com'), +('tagdirs.cosmetic@gmail.com'), +('ceviwilly@gmail.com'), +('daniel.khosuma@gmail.com'), +('muhammadyanis12091991@gmail.com'), +('liechintia07@gmail.com'), +('apt.deantigano@gmail.com'), +('ian.tampo22@gmail.com'), +('kunyil505@gmail.com'), +('hongjujen69@gmail.com'), +('wimma.shop@gmail.com'), +('mysamplemart@gmail.com'), +('safitriyuli3@gmail.com'), +('novanindra23@gmail.com'), +('hartantiarline@gmail.com'), +('dianyunus7@gmail.com'), +('aniyt161aan@gmail.com'), +('aestheticbeautycare23@gmail.com'), +('marissarahmaadhania9i30@gmail.com'), +('cecebeautybali@gmail.com'), +('devani.ersa@yahoo.com'), +('windiana10@gmail.com'), +('itaufik55@gmail.com'), +('zhisca_77@yahoo.co.id'), +('jillyanesasiage@gmail.com'), +('mybestiebeauty.bdg@gmail.com'), +('purchasing@bumame-farmasi.com'), +('denisa.denis89@yahoo.com'), +('ranggatedubara@gmail.com'), +('angelialim0311@gmail.com'), +('rindaagustiana@gmail.com'), +('aleshabeautycare57@gmail.com'), +('monpri28@gmail.com'), +('move.hamsari@gmail.com'), +('beautybelle2608@gmail.com'), +('priscillalastyfera@gmail.com'), +('vildarahmawati17@gmail.com'), +('nathaniaa099@gmail.com'), +('noyura@gmail.com'), +('juhatihajibe@gmail.com'), +('winda.christina@gmail.com'), +('mruslir21@gmail.com'), +('siskagsp6@gmail.com'), +('olyviadevita02@gmail.com'), +('ecgirl2023@gmail.com'), +('songlacosmetics@gmail.com'), +('jelsymakeup@gmail.com'), +('vanywidyawati86@gmail.com'), +('ongfreddykusuma@gmail.com'), +('charisthamaysadwisahara@gmail.com'), +('incjodu@gmail.com'), +('mjbeautyblitar@gmail.com'), +('ngeng.yuan27@gmail.com'), +('mybeautymart.id@gmail.com'), +('nabillafr22@gmail.com'), +('dianajennifer118@gmail.com'), +('wetirima9988@gmail.com'), +('shely.proborini@yahoo.com'), +('pak.serba@gmail.com'), +('lili_3038@yahoo.co.id'), +('tallamersiaa@gmail.com'), +('khalisanaura156@gmail.com'), +('milaatysakinah@gmail.com'), +('ucci.arfan@gmail.com'), +('lutfiah.nurhandari@gmail.com'), +('albinadhaja94@gmail.com'), +('dindaty@gmail.com'), +('shopkievanik@gmail.com'), +('zheng.jinde7692@gmail.com'), +('silvi@getvybes.co'), +('muhammadraydevan@gmail.com'), +('alstininoviyantitoar@gmail.com'), +('riahafarul778@gmail.com'), +('nrustam26@gmail.com'), +('gayabyys.wu@gmail.com'), +('lalootust@gmail.com'), +('rina2pan@yahoo.com'), +('disariska@gmail.com'), +('ninu.fira@gmail.com'), +('yuibabaretail@gmail.com'), +('makshifa93@gmail.com'), +('nadzimalaily@gmail.com'), +('nindysyarif@gmail.com'), +('mutiarapesonanganjuk@gmail.com'), +('beautybypelangi@gmail.com'), +('mariyani3412@gmail.com'), +('anitabudi1998@gmail.com'), +('farikhanum@ymail.com'), +('riscaamalia1989@gmail.com'), +('oyha.ciez01@gmail.com'), +('afriasafitri@ymail.com'), +('gitalaksmicosmetic@gmail.com'), +('oktavianimunir@gmail.com'), +('meithaekyh@gmail.com'), +('antouwkeren@gmail.com'), +('meeranova.id@gmail.com'), +('dmuhenti@gmail.com'), +('bima.ajun@gmail.com'), +('sitipluviana@gmail.com'), +('endahpratiwi522@gmail.com'), +('dindalabibah@gmail.com'), +('pusatkosmetikpinky@gmail.com'), +('anjalisibagariang2000@gmail.com'), +('krisnairma15@gmail.com'), +('ocha.rospa@gmail.com'), +('siscabudhyani86@gmail.com'), +('jhenisarykambuno@gmail.com'), +('mp-store4@raenabeauty.com'), +('luckyyosin0913@gmail.com'), +('monica1726@yahoo.co.id'), +('deasyrimanda@gmail.com'), +('pbudianoto@gmail.com'), +('hernispratiwi@gmail.com'), +('lelymelani@yahoo.com'), +('cantiksehat.sby@gmail.com'), +('gunk.fan29@gmail.com'), +('nurhayati01022021@gmail.com'), +('wardahafi@gmail.com'), +('nonikmery256@gmail.com'), +('lismiatilenta69@gmail.com'), +('dewinurhidayah5125@gmail.com'), +('nurhikmah.hasbi@gmail.com'), +('dordiaahss@gmail.com'), +('molly.fh2@gmail.com'), +('avostoresurabaya@gmail.com'), +('alhun.lun@gmail.com'), +('thebeautycare05@gmail.com'), +('mykarol88@gmail.com'), +('angela.sania@yahoo.com'), +('divaa01@ymail.com'), +('p.ariyanti2@gmail.com'), +('rizkycosmetics@gmail.com'), +('claudiayolandpurnomo@gmail.com'), +('ayudea1820@gmail.com'), +('allshopbisnis72@gmail.com'), +('yosephinetan@yahoo.co.id'), +('ayuri.minho@yahoo.com'), +('paolahermawan@yahoo.com'), +('mirna.mirnaaa02@gmail.com'), +('kumaskincare01@gmail.com'), +('officialrabeauty@gmail.com'), +('cacabeauty1234@gmail.com'), +('nadeakayuangellika@gmail.com'), +('linacaterina79@gmail.com'), +('rianishop210@gmail.com'), +('ilhamxmaulanaaa@gmail.com'), +('aratna643@gmail.com'), +('vifi2403@gmail.com'), +('paringdyahdinda@icloud.com'), +('suantysuanty10@gmail.com'), +('nandalisa045@gmail.com'), +('tinylilproject@yahoo.com'), +('arfan.arsya2022@gmail.com'), +('dahliaqueenshop@gmail.com'), +('triantiagus@gmail.com'), +('widyastevani17@gmail.com'), +('adityo_konandar@yahoo.com'), +('zoerosyidh89@gmail.com'), +('yusmane395@gmail.com'), +('tephtephyy@gmail.com'), +('seemser.id@gmail.com'), +('tantinaduu@gmail.com'), +('yannayeshika@gmail.com'), +('juliacsecogati@gmail.com'), +('pheroneid@gmail.com'), +('pauline222bn@gmail.com'), +('dpuspitaprameswari@gmail.com'), +('irnanda.vinka@yahoo.com'), +('agamwijaya89@gmail.com'), +('miniskinjars@gmail.com'), +('faizal@raenabeauty.com'), +('hanaamelinda95@gmail.com'), +('maharanimega39@gmail.com'), +('tia.aprita@gmail.com'), +('m.milanova.sh@gmail.com'), +('belscofabrics@gmail.com'), +('pratamanaufal98@gmail.com'), +('joshanshop@gmail.com'), +('ayuameliasaja@gmail.com'), +('alyadwi00@gmail.com'), +('helloyoumeindonesia@gmail.com'), +('desyrosiyana@gmail.com'), +('isvimega@gmail.com'), +('andrianiyana815@gmail.com'), +('andiakbar.ak@gmail.com'), +('isw11business@gmail.com'), +('guo_fengsheng@yahoo.com'), +('nirmaladewi03@gmail.com'), +('novitheng90@gmail.com'), +('poppymputranto@gmail.com'), +('shellahamayu1997@gmail.com'), +('suparjoariyanti@gmail.com'), +('pipinwahyu80@gmail.com'), +('chryseladarwin.olivia@gmail.com'), +('choisebest64@gmail.com'), +('rafin.trisna@gmail.com'), +('solehac098@gmail.com'), +('ayumirah12@yahoo.com'), +('fasahdi@gmail.com'), +('vindysabeauty@gmail.con'), +('dillalutchu08@gmail.com'), +('hi.dearbella@gmail.com'), +('awili.tri@gmail.com'), +('ardiradityananda@gmail.com'), +('dayanishop474@gmail.com'), +('apriliamega08@gmail.com'), +('vindysabeauty@gmail.com'), +('gilsneedit@gmail.com'), +('meiriannaafs@gmail.com'), +('mantepkebumen66@gmail.com'), +('lisnawatirohmah@gmail.com'), +('mhelydya@gmail.com'), +('apriliandesi@yahoo.co.id'), +('memonaofficial1007@gmail.com'), +('attraversiamobeauty@gmail.com'), +('triyatmoko3116@gmail.com'), +('toxena.shop@gmail.com'), +('fitridwifebriani59@gmail.com'), +('apadotid7@gmail.com'), +('misz_irma@yahoo.com'), +('riofabian112200@gmail.com'), +('indahyuliza7@gmail.com'), +('imasmaslihah2222@gmail.com'), +('letsglowid.jbi@gmail.com'), +('mahreenbeautyskin@gmail.com'), +('aldofiras1607@gmail.com'), +('firdamelia05@gmail.com'), +('hajernihajerni@gmail.com'), +('yunusdarmawan8@gmail.com'), +('fmaharani96@gmail.com'), +('tokoyangtepat@gmail.com'), +('endangdolphin2011@gmail.com'), +('pallawalipu21@gmail.com'), +('pie_buana@yahoo.com'), +('marina.diantika@gmail.com'), +('rihandika34@gmail.com'), +('ngm6756@gmail.com'), +('djonyali59@gmail.com'), +('rismagina02@gmail.com'), +('beautybyasame@gmail.com'), +('gracerushandi@gmail.com'), +('rinianggrayniinank@gmail.com'), +('lin_qiau_hua@yahoo.com'), +('buanainskonsi@gmail.com'), +('cosmetik.giant@gmail.com'), +('reynitaballack@gmail.com'), +('shopkichii88@gmail.com'), +('fadhilahafifah48@yahoo.com'), +('vivaforeva1@gmail.com'), +('sisilia.marshella@yahoo.com'), +('irmhanurjayanti@gmail.com'), +('galuhnur81@gmail.com'), +('gees.hijab@gmail.com'), +('putrimeyanti8@gmail.com'), +('rully0152@gmail.com'), +('buntukk166@gmail.com'), +('williantocindy@gmail.com'), +('nadilla@raenabeauty.com'), +('cindyfreda23@gmail.com'), +('liiyalimz@gmail.com'), +('kikaychan90@gmail.com'), +('tiffanyyaprilia@gmail.com'), +('2017120215@student.unpar.ac.id'), +('23jamesbond11@gmail.com'), +('36yonathan@gmail.com'), +('a.azzahrasyah@gmail.com'), +('a6ustina@gmail.com'), +('abyan.shop97@gmail.com'), +('acelinbeauty@gmail.com'), +('adeevaskincaresemarang@gmail.com'), +('adeyatoko@gmail.com'), +('adimahendraganteng@gmail.com'), +('agatha@raenabeauty.com'), +('agatyalaksmi@gmail.com'), +('agungfadilah810@gmail.com'), +('ahmadluckibawazier@gmail.com'), +('ai.x1.35300@sutomo-mdn.sch.id'), +('alfiatulkhubbi@gmail.com'), +('alfonsocosmetics@gmail.com'), +('alifiarizkiatania@gmail.com'), +('aline_fathin@yahoo.com'), +('alisheretotalk@gmail.com'), +('alithapradita@gmail.com'), +('allaboutskincare312@gmail.com'), +('almardhiyyahs@gmail.com'), +('almirabintangr@gmail.com'), +('alvissyahrin619@gmail.com'), +('alycia_monicaa@yahoo.co.id'), +('amaliakusumaniez@gmail.com'), +('ameicosmetic12@gmail.com'), +('amelianucifera@gmail.com'), +('ameliaresnaw@gmail.com'), +('analiani86@gmail.com'), +('andikharianto978@gmail.com'), +('angelinkayla118@gmail.com'), +('angellim0311@gmail.com'), +('angellina.lady96@gmail.com'), +('anggararnda972@gmail.com'), +('anggrini@raenabeauty.com'), +('anggriprad@gmail.com'), +('anisaauliaabas@gmail.com'), +('anisafarah24@gmail.com'), +('annachf.tan@gmail.com'), +('antodukos@gmail.com'), +('antonisang27@gmail.com'), +('appl3fuji@gmail.com'), +('arisrahmat333@gmail.com'), +('artiwayan14@gmail.com'), +('artrisnandari@gmail.com'), +('arumsekarsari573@gmail.com'), +('asepmirwan1@gmail.com'), +('assyafarrizki@gmail.com'), +('astrsari@gmail.com'), +('atikaputrims97@gmail.com'), +('audi.tabrany@gmail.com'), +('avoskinbeautybandung@gmail.com'), +('avuastore@gmail.com'), +('ayunamadewi@gmail.com'), +('babdungdyfa@gmail.com'), +('beatricetheresia1@gmail.com'), +('beautyshoujo@gmail.com'), +('bella.beautittaid@gmail.com'), +('bellaikne77@gmail.com'), +('bembie07@gmail.com'), +('beningkosmetik59@gmail.com'), +('biutidy@gmail.com'), +('brilianifridayasti@gmail.com'), +('budiondream@gmail.com'), +('business.qhansaanakkami@gmail.com'), +('cahaya.kosmetik.id@gmail.com'), +('cahyanidwisepti20@gmail.com'), +('cantikatya2@gmail.com'), +('catherinerambang620@gmail.com'), +('caty.adja@gmail.com'), +('cderz23@yahoo.com'), +('ceceliasansan8@gmail.com'), +('cerimartgo@gmail.com'), +('chandrahalim714@gmail.com'), +('chen.wong21@gmail.com'), +('cheryl.marsha.heryanto@gmail.com'), +('chococoip@gmail.com'), +('christianyendi@gmail.com'), +('cindyaharvardiana@gmail.com'), +('cinthyaruhantopp1@gmail.com'), +('cinthyaruhantopp3@gmail.com'), +('cisilia268@gmail.com'), +('clarysocimartinez@gmail.com'), +('cosellebeauty@gmail.com'), +('cvindokosmetikasejahtera@gmail.com'), +('d.lilacshop@gmail.com'), +('danielbudisantoso@gmail.com'), +('danielsimanjuntak231@gmail.com'), +('dedywijayamail@gmail.com'), +('definitionbeautyjogja@gmail.com'), +('dektasyaputra1301@gmail.com'), +('dellalisapaly@gmail.com'), +('despielia@gmail.com'), +('despielia92@gmail.com'), +('desyoong21@gmail.com'), +('deviannisme@gmail.com'), +('dewimellinda10@gmail.com'), +('dhevio1426@gmail.com'), +('dianakuliah@gmail.com'), +('dianps.haryanto@yahoo.com'), +('dillaparamita0@gmail.com'), +('dinaapril280@gmail.com'), +('dini.dwiarti3105@gmail.com'), +('dpsbykosme@gmail.com'), +('dwiagustin363@gmail.com'), +('dyas253@gmail.com'), +('dyfacafe@gmail.com'), +('eka.fokus@gmail.com'), +('eleonora@raenabeauty.com'), +('elfayunengsih20@gmail.com'), +('elisiabriana@gmail.com'), +('ellierahardja@gmail.com'), +('elsa.primasari.eps@gmail.com'), +('elsaokta26@gmail.com'), +('endahtriwedanti28@gmail.com'), +('endang.kosmetik1@gmail.com'), +('endangmarbunbbm678@gmail.com'), +('ennykoesrini676@gmail.com'), +('ennysetiono@gmail.com'), +('enokfatimah405@gmail.com'), +('entok345@gmail.com'), +('enungnurhalimah93@gmail.com'), +('envirolisasekar@gmail.com'), +('ernazulianita00@gmail.com'), +('estrellabeautee@gmail.com'), +('etienneskincare@gmail.com'), +('etisuningsih117@gmail.com'), +('evan.zelizta98@gmail.com'), +('evelyn.vio81@gmail.com'), +('evinrahmad4@gmail.com'), +('fajar950@gmail.com'), +('fajaroldman@gmail.com'), +('farah.fbindonesia@gmail.com'), +('faridahsalma@gmail.com'), +('febrina@raenabeauty.com'), +('feliceclarissa90@gmail.com'), +('fennyardisheila@gmail.com'), +('fera.julianti93@gmail.com'), +('feriscainez@gmail.com'), +('fffgh8@gmail.com'), +('fikrizia_insani@yahoo.com'), +('firabanun@gmail.com'), +('fitria_rida@yahoo.com'), +('fitriamalda123@gmail.com'), +('fitriand0192@gmail.com'), +('flavianusferdiaf@gmail.com'), +('flawless.officialshop@gmail.com'), +('flimtysurabayaofficial@gmail.com'), +('fmieayam@gmail.com'), +('freshnglow77@gmail.com'), +('friendhik25@gmail.com'), +('gabriastore@gmail.com'), +('gajahmadawaee@gmail.com'), +('galaticos02@hotmail.com'), +('gbjmarket@gmail.com'), +('givonsal@gmail.com'), +('glammarket64@gmail.com'), +('gloryharahap@gmail.com'), +('grosirmurahsukoharjo5@gmail.com'), +('guo_fengsheng@yahoo.co.id'), +('gustina.denaiyenti21@gmail.com'), +('hanselchan55@gmail.com'), +('hantarancilandak@gmail.com'), +('hardiantinila3@gmail.com'), +('hartono.henny@yahoo.com'), +('hasanyeong8@gmail.com'), +('helbert.simanjuntak@gmail.com'), +('hello.sheetmask@gmail.com'), +('hellopakbu@gmail.com'), +('helmikamanihuruk@gmail.com'), +('herdianametalic@gmail.com'), +('hestydharmayanti933@gmail.com'), +('heymarcell@gmail.com'), +('hijriahr08@gmail.com'), +('hulahulahali@gmail.com'), +('idcosmeticsidcosmetics@gmail.com'), +('ihc.korea@gmail.com'), +('ihccianjur@gmail.com'), +('iis.sugiati83@gmail.com'), +('ijallevine@gmail.com'), +('ikromyafik147@gmail.com'), +('imahgrosir@gmail.com'), +('indahpurwanti4410@gmail.com'), +('indrianimentaruk2@yahoo.com'), +('info.athirabeauty@gmail.com'), +('inggrid.kambuno@yahoo.com'), +('isbeyondstyle@gmail.com'), +('istrinyamalik@gmail.com'), +('iwanku203@gmail.com'), +('jacovcho@gmail.com'), +('jayaananda247@gmail.com'), +('jayde67@genesvjq.com'), +('jazzpro131@gmail.com'), +('jcrshop20@gmail.com'), +('jeliem96@gmail.com'), +('jelysoviana@gmail.com'), +('jesiskaruslan@gmail.com'), +('jessicaa2208@gmail.com'), +('jscang93@gmail.com'), +('jschrynt@gmail.com'), +('juliaviliajulia@gmail.com'), +('jullyangelina85@gmail.com'), +('jumrayana@gmail.com'), +('junidiarisna@gmail.com'), +('jwinarso889@gmail.com'), +('karinaagustin613@yahoo.co.id'), +('katalogskincarekorea@gmail.com'), +('keanoliem3@gmail.com'), +('kekensetiawan@gmail.com'), +('khairunniszahaifa@yahoo.com'), +('kikiseptiana92@gmail.com'), +('klikandshop18@gmail.com'), +('kntz199@gmail.com'), +('kosmetiklancarjaya@gmail.com'), +('kuboykukuh@gmail.com'), +('kunarifin@gmail.com'), +('kurniadi.phan@gmail.com'), +('kyuhaekoinline@gmail.com'), +('kyuhyunkoinline5@gmail.com'), +('lancarjayakosmetik@gmail.com'), +('lanisiska45@gmail.com'), +('laure.devienne@gmail.com'), +('lavoilla@gmail.com'), +('leejaeha31@gmail.com'), +('lenasinaga853@yahoo.com'), +('leo_azzuri@yahoo.com'), +('libbyclaudiar@gmail.com'), +('Liec534@gmail.com'), +('liedhawei@yahoo.com'), +('lina.marlinajkt228@gmail.com'), +('linamay2088@gmail.com'), +('lincebaiku34@gomail.com'), +('lindasekararum@gmail.com'), +('lindaten93@gmail.com'), +('lio.edhen@gmail.com'), +('lionachristina110887@gmail.com'), +('lolithaameliaa.la@gmail.com'), +('lolytha.arina@yahoo.co.id'), +('loverini1508@gmail.com'), +('lricosantoso@gmail.com'), +('luckyluna.id@gmail.com'), +('luckyme2611@gmail.com'), +('lusianaivang@yahoo.com'), +('luvpiggy2312@gmail.com'), +('lyannaming@gmail.com'), +('madya.wa@gmail.com'), +('mahesabeautyandstore@gmail.com'), +('maianton@yahoo.com'), +('mailto.wabisabi@gmail.com'), +('makeupfutura@gmail.com'), +('maramaratus@gmail.com'), +('marciaaact@gmail.com'), +('maria.ariesw87@gmail.com'), +('mariaboons0@gmail.com'), +('marianaulfa11@gmail.com'), +('mariiahulfah123@gmail.com'), +('marlinadiansafitri@gmail.com'), +('maroon16store@gmail.com'), +('marshandaditha@gmail.com'), +('martza.swastikasari@gmail.com'), +('maryani4398@gmail.com'), +('maryavictoryss3@gmail.com'), +('matrasunik@gmail.com'), +('maupesanposter@gmail.com'), +('maximlaundry@yahoo.com'), +('megashinta39@gmail.com'), +('mekarkosmetik.serang@gmail.com'), +('melaplestari2@gmail.com'), +('melindasuriyam01@gmail.com'), +('melisahariyanto14@gmail.com'), +('mentaripuspa59@gmail.com'), +('mh.lifestyles.in@gmail.com'), +('mikanmicheelen@gmail.com'), +('mirzaluzzia123@gmail.com'), +('misluciana@yahoo.com'), +('mitha2904aprilia@gmail.com'), +('mitufaya@gmail.com'), +('mitzukooutfit@gmail.com'), +('mizuharaaya@gmail.com'), +('mohd.nurfitriadin@gmail.com'), +('moildy.id@gmail.com'), +('monalisaflorist@gmail.com'), +('morashopmedan@gmail.com'), +('mr.yung30@gmail.com'), +('muhammad.hidayat.101@gmail.com'), +('mutiaazzahra177@gmail.com'), +('mutiarajesica0209@gmail.com'), +('mykagendis@gmail.com'), +('nadya.desnam@gmail.com'), +('nailavira102@gmail.com'), +('nanalistya22@gmail.com'), +('nandadevie1968@gmail.com'), +('nandipintaa@gmail.com'), +('nari38700@gmail.com'), +('natasyaputri4982@gmail.com'), +('naturrcosmetic@gmail.com'), +('niasepti28@gmail.com'), +('nicoags26@gmail.com'), +('nicolebeautyindonesia@gmail.com'), +('nicoyuganugraha@gmail.com'), +('ninadelinda26@gmail.com'), +('nirmala.sari709@gmail.com'), +('nonaewa@gmail.com'), +('nophaaelfrida@gmail.com'), +('nova.punya@gmail.com'), +('novandy.yohanes@gmail.com'), +('novi0128@gmail.com'), +('novitageraldinetgt@gmail.com'), +('nscollastica@gmail.com'), +('ntyaau26@gmail.com'), +('nuciferaamelia@gmail.com'), +('nugrahapiawai@gmail.com'), +('nur.wazilah@gmail.com'), +('nurazhwa170@gmail.com'), +('nurinasp@gmail.com'), +('nurlaelaramadanti@gmail.com'), +('officialbuyorbye@gmail.com'), +('oktinningrum@gmail.com'), +('opponingrum@gmail.com'), +('oyyisocial@gmail.com'), +('pajerokita@gmail.com'), +('pandridoge@gmail.com'), +('papamarvel_suhendy@yahoo.com'), +('pardosiares@gmail.com'), +('pastiibisaa@gmail.com'), +('pipitsanti999@gmail.com'), +('popoybunny@gmail.com'), +('prakososyam0403@gmail.com'), +('pramita2701@gmail.com'), +('prioritybekasi@gmail.com'), +('properbeauty.id@gmail.com'), +('psalimatulh@gmail.com'), +('puellabeautyshop@gmail.com'), +('puterifedina@gmail.com'), +('putriparapara@gmail.com'), +('putrisildosz@gmail.com'), +('putrivhidi@gmail.com'), +('putryalfionta@yahoo.com'), +('r.alika003@gmail.com'), +('r0n_ny@yahoo.com'), +('radhiah.hasan26@gmail.com'), +('rahayuwrdhn@gmail.com'), +('ranicantikhebat@gmail.com'), +('ratnashary4@gmail.com'), +('raynoldpanjaitan100@gmail.com'), +('regsmakeup@gmail.com'), +('resditamargaretha@gmail.com'), +('reviyuliswan86@gmail.com'), +('ribkanads@gmail.com'), +('richarddarmawanlaidji@gmail.com'), +('rickylie1989@gmail.com'), +('ries091@gmail.com'), +('rikiimpiandes@gmail.com'), +('rikomm822@gmail.com'), +('rinayunikartikaa@gmail.com'), +('rindaariani36@gmail.com'), +('riniismiyanti_94@yahoo.com'), +('ririalijofa@gmail.com'), +('riskalestari014@gmail.com'), +('rkcjawa@yahoo.com'), +('robeth.saputra01@gmail.com'), +('romlahabadsyam@gmail.com'), +('ronnywongsolim@gmail.com'), +('rovihshsh@gmail.com'), +('roviwardanap@gmail.com'), +('roviwardanaputra@gmail.com'), +('rperlita11@gmail.com'), +('rtjayaselalu@gmail.com'), +('rzkamll18@gmail.com'), +('sabithaanisa03@gmail.com'), +('saniaa3095@gmail.com'), +('saniyasch@gmail.com'), +('sansbeauty01@gmail.com'), +('santisujanto@gmail.com'), +('sarah@getvybes.co'), +('sari.astuty12@gmail.com'), +('sarihakim96@gmail.com'), +('sarmutias1@gmail.com'), +('sarmutias2@gmail.com'), +('sarmutias5@gmail.com'), +('seilareisya@gmail.com'), +('selfi.yonghwa08@gmail.com'), +('sendychristians@gmail.com'), +('sepriyanamelly@gmail.com'), +('serlinaepriyanti07@gmail.com'), +('setiawan.laia31@gmail.com'), +('setiawanandrew818@gmail.com'), +('sherlysherlyvi@gmail.com'), +('sholi.hijubpanda@gmail.com'), +('shopalover123@gmail.com'), +('shopbintar@gmail.com'), +('shoujoshopcosmetic@gmail.com'), +('siauwelizabeth@gmail.com'), +('sintafauziah41@gmail.com'), +('siti.lathifatul.k@gmail.com'), +('sitilathifatulkhofi@gmail.com'), +('siuni.reno@gmail.com'), +('siwiberlianti@gmail.com'), +('skincareindoid@gmail.com'), +('skinqueenproject@gmail.com'), +('smcosmeticsub@gmail.com'), +('snana602@gmail.com'), +('snrofi27@gmail.com'), +('soniadepi21@gmail.com'), +('sonie.usman@gmail.com'), +('sreejita@raenabeauty.com'), +('srialfia.san@gmail.com'), +('srihendrawati1@gmail.com'), +('sriyuliani9379@gmail.com'), +('stacia_andani91@yahoo.com'), +('staciaandani91@gmail.com'), +('stasyifa@gmail.com'), +('stefani_eli@ymail.com'), +('stepfanny91@gmail.com'), +('storemajujaya12@gmail.com'), +('sultonudin.madani21@gmail.com'), +('sun.edihtantra@gmail.com'), +('suryakosmetikjogja@gmail.com'), +('suryaniani1960@gmail.com'), +('susantiyap68@gmail.com'), +('suwandikarina61@gmail.com'), +('suwati169@gmail.com'), +('syahlaag@gmail.com'), +('syahliyaf@gmail.com'), +('syahna2128@gmail.com'), +('syahpudin09.183@gmail.com'), +('syifasahb@gmail.com'), +('tagustatang@gmail.com'), +('takiidonlineshopping@gmail.com'), +('talinazulfah@gmail.com'), +('tanmelky@yahoo.com'), +('taraaengg@gmail.com'), +('taspremium88@gmail.com'), +('taufikismail198720@gmail.com'), +('tcahyanti@gmail.com'), +('tfnhtp@gmail.com'), +('the_fortune_eo@yahoo.com'), +('theakirana133@gmail.com'), +('thebeausell@gmail.com'), +('thebeautyneed@gmail.com'), +('theidk.zone@gmail.com'), +('theskindarling@gmail.com'), +('tihanperkasadefitro@gmail.com'), +('tiifandi@gmail.com'), +('timyls@yahoo.com'), +('tina.metro@yahoo.com'), +('tioharianto.allianz@yahoo.com'), +('tjulovely2013@gmail.com'), +('toko.viragrosir88@gmail.com'), +('toko.winars.palopo@gmail.com'), +('tokounikita2@gmail.com'), +('tommy_priatama@ymail.com'), +('topupfu.sp@gmail.com'), +('tyabe888@yahoo.com'), +('ulibasagrc123@gmail.com'), +('umarsikumbang2020@icloud.com'), +('vannychristinna@yahoo.com'), +('varenunggulkarya@gmail.com'), +('veliakatarina@gmail.com'), +('velvetpinkred@gmail.com'), +('vhex307@gmail.com'), +('vidiachen@yahoo.com'), +('vilencyviranyy@gmail.com'), +('vina.astini@yahoo.com'), +('vincenttia23@gmail.com'), +('vinlie2020@gmail.com'), +('violyamerin@gmail.com'), +('viraltoday124@gmail.com'), +('virginiaca09@gmail.com'), +('viserysyvaine@gmail.com'), +('viviyuvita19@gmail.com'), +('wahyudiirwan35@gmail.com'), +('wahyuekajaya@gmail.com'), +('wahyuyana.prasetya@gmail.com'), +('wellington@nihonmart.id'), +('wellypandin76@gmail.com'), +('widoagusputra90@gmail.com'), +('wiwiekapsari@icloud.com'), +('wulan3804@gmail.com'), +('wulandary.sllh@gmail.com'), +('xenpang26@gmail.com'), +('xuanliuli0@gmail.com'), +('yayarafin@gmail.com'), +('yenimaryatin332@gmail.com'), +('yenny.irawati@yahoo.com'), +('yessicasarano16@gmail.com'), +('yogialqarni1807@gmail.com'), +('yogisugianto21@gmail.com'), +('yong2.chan@gmail.com'), +('yooabeaute@gmail.com'), +('ysurjadi@gmail.com'), +('yt40gt@gmail.com'), +('yukireiyuka@gmail.com'), +('yuliacandle@gmail.com'), +('yuliananina19@yahoo.com'), +('yunisri.hartiwi@yahoo.com'), +('yunitadewi23@gmail.com'), +('zabeth_yulie@yahoo.co.id'), +('zahraashop18@gmail.com'), +('zahrabeauty.share@gmail.com'), +('zelmi5009@gmail.com'), +('zharasonline@gmail.com'), +('zivenakbar08@gmail.com'), +('Fanamaliauthor@gmail.com'), +('tjuganteng@gmailcom'), +('ayusisicantik@gmail.com'), +('tsetiyaningsih71@gmail.com'), +('oyyiapple@gmail.com'), +('angellim0311@yahoo.com'), +('fericoridwan@gmail.com'), +('Ikasusanti_story@yahoo.com'), +('hamkyu359@gmail.com'); + + +DROP TABLE raena_analytics.SKU_base_recommendation_base; + +CREATE TABLE raena_analytics.SKU_base_recommendation_base AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum(total_payment_price) total_payment_price, + sum(total_cogs)total_cogs, + ((sum(retail_price)-sum(total_cogs))/sum(retail_price))*100 Pre_discount_gm +FROM + (SELECT sku, + product_type, + sum(quantity)quantity, + sum((retail_price-seller_margin)*quantity) total_payment_price, + sum(cogs*quantity)total_cogs, + sum((retail_price-seller_margin)*quantity)retail_price + FROM raena_analytics.gm_dashboard + WHERE transaction_date::date>cast('2023-11-21' AS date)-interval'90 days' + AND cogs IS NOT NULL + AND cogs>0 + GROUP BY sku, + product_type)A +INNER JOIN + (SELECT sku , + category_id, + brand_id, + name + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B ON A.sku = B.sku +INNER JOIN + (SELECT id , + name + FROM raena_catalog_management.category) C ON B.category_id = C.id +INNER JOIN + (SELECT id , + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id +GROUP BY 1, + 2, + 3, + 4, + 5 ; + + +DROP TABLE raena_analytics.reseller_base_recommendation_base; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base AS +SELECT reseller_id , + A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum(total_payment_price) total_payment_price, + sum(total_cogs)total_cogs, + ((sum(retail_price)-sum(total_cogs))/sum(retail_price))*100 Pre_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM (SELECT sku,reseller_id , +reseller_email, + product_type, + sum(quantity)quantity, + sum((retail_price-seller_margin)*quantity) total_payment_price, + sum(cogs*quantity)total_cogs, + sum((retail_price-seller_margin)*quantity)retail_price + FROM raena_analytics.gm_dashboard + WHERE transaction_date::date>cast('2023-11-21' AS date)-interval'90 days' + AND cogs IS NOT NULL + AND cogs>0 + AND reseller_id IS NOT NULL + GROUP BY sku, + product_type,reseller_id,reseller_email ) A +INNER JOIN (SELECT sku , + category_id, + brand_id, + name + FROM raena_catalog_management.product) B ON A.sku = B.sku +INNER JOIN (SELECT id , + name + FROM raena_catalog_management.category) C ON B.category_id = C.id +INNER JOIN (SELECT id , + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id +INNER JOIN raena_analytics.recommendation_power_seller_base BB ON A.reseller_email = BB.email +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_stage1; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_stage1 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>=10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM raena_analytics.reseller_base_recommendation_base; + +DROP TABLE IF EXISTS raena_analytics.raw_scraping_data; + + + +CREATE TABLE raena_analytics.raw_scraping_data AS +SELECT E.raena_product_sku, + F.name SKU_name, + E.rce_product_id , + E.rce_variant_id, + A.category_name , + A.brand_name , + A.product_page_url , + rce_source_product_name, + product_variant_name, + product_variant_price, + product_variant_price_before_discount, + product_sold_total, + product_sold, + product_price_min, + product_price_min_before_discount, + product_price_max, + product_price_max_before_discount, + cast(percentage_match AS decimal(22,2)) percentage_match, + ratings +FROM raena_analytics.raena_shopee_product_mapping E +LEFT JOIN + (SELECT A.id rce_product_id, + D.id rce_product_varient_id , + B.category_name , + C.brand_name , + product_page_url , + rce_source_product_name, + product_variant_name, + product_variant_price/10000 product_variant_price, + product_variant_price_before_discount/10000 product_variant_price_before_discount, + product_sold_total, + product_sold, + product_price_min/10000 product_price_min, + product_price_min_before_discount/10000 product_price_min_before_discount, + product_price_max/10000 product_price_max, + product_price_max_before_discount/10000 product_price_max_before_discount, + ratings + FROM raena_spider_management.rce_product A + LEFT JOIN raena_spider_management.rce_category B ON A.rce_category_id = B.id + LEFT JOIN raena_spider_management.rce_brand C ON A.rce_brand_id = cast(C.id as varchar) + LEFT JOIN raena_spider_management.rce_product_variant D ON A.id = D.rce_product_id) A ON E.rce_product_id = cast(A.rce_product_id as varchar) +AND cast(replace(E.rce_variant_id,'.0','') AS varchar) = cast(replace(cast(A.rce_product_varient_id as varchar),',','') AS varchar) +LEFT JOIN raena_catalog_management.product F ON E.raena_product_sku=F.sku ; + + +DROP TABLE IF EXISTS raena_analytics.raw_scraping_data_stage ; + + +CREATE TABLE raena_analytics.raw_scraping_data_stage AS +SELECT A.* +FROM raena_analytics.raw_scraping_data A +INNER JOIN + (SELECT raena_product_sku, + max(percentage_match) percentage_match + FROM raena_analytics.raw_scraping_data + GROUP BY 1) B ON A.raena_product_sku = B.raena_product_sku +AND A.percentage_match =B.percentage_match; + + +DROP TABLE IF EXISTS raena_analytics.sku_sales_factor; + + +CREATE TABLE raena_analytics.sku_sales_factor AS +SELECT SKU, + cast(cast(sum(B.total_quantity*3) AS decimal(22,2))/cast(SUM(A.total_quantity) AS decimal(22,2)) AS decimal(22,2)) sale_factor +FROM raena_analytics.SKU_base_recommendation_base A +LEFT JOIN + (SELECT raena_product_sku, + sum(DISTINCT product_sold) total_quantity + FROM raena_analytics.raw_scraping_data_stage + GROUP BY 1) B ON A.sku = B.raena_product_sku +WHERE A.Pre_discount_gm>=10 +GROUP BY 1; + + +DROP TABLE IF EXISTS raena_analytics.recommendation_on_low_sku_task1; + + +CREATE TABLE raena_analytics.recommendation_on_low_sku_task1 AS +SELECT C.mobile Reseller_mobile , + C.email reseller_email, + A.sku, + A.sku_name, + A.brand_name, + A.category_name , + A.product_type , + Total_quantity, + total_payment_price, + total_cogs, + A.pre_discount_gm, + coalesce(B.sale_factor,0) sale_factor, + A.pre_discount_gm*coalesce(B.sale_factor,1) sorting +FROM + (SELECT * + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE Gm_type = 'DDB') A +INNER JOIN + (SELECT mobile, + email, + id + FROM raena_user_management.user) C ON A.reseller_id = cast(C.id AS varchar) +INNER JOIN + (SELECT sku , + pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base) D ON A.sku = D.sku +AND D.pre_discount_gm >=10 +INNER JOIN raena_analytics.sku_in_stock E ON A.sku = E.sku +AND E.in_stock >=10 +LEFT JOIN raena_analytics.sku_sales_factor B ON A.sku = B.sku; +---------------------------------------------task 2 --------------- + + + + + +DROP TABLE raena_analytics.SKU_base_recommendation_base_v2; + + +CREATE TABLE raena_analytics.SKU_base_recommendation_base_v2 AS +SELECT A.sku, + ((sum((A.retail_price-seller_margin) *quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM raena_analytics.gm_dashboard A +WHERE transaction_date::date>cast('2023-11-21' as date)-interval'90 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND tier_name ='GOLD' +GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.recommendation_base_historical_data_offender; + + +CREATE TABLE raena_analytics.recommendation_base_historical_data_offender AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE gm_type ='Offender') A +LEFT JOIN raena_analytics.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN raena_analytics.sku_sales_factor C ON B.mapping_group_base = C.sku +LEFT JOIN raena_analytics.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND Pre_discount_gm>=10 +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM + (SELECT sku, + name, + product_type, + category_id, + brand_id + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.category) C ON B.category_id = C.id + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id ) E ON B.mapping_group_base= E.SKU; + + + DROP TABLE IF EXISTS raena_analytics.recommendation_base_historical_data_ddb; + + +CREATE TABLE raena_analytics.recommendation_base_historical_data_ddb AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + A.Pre_discount_gm reseller_pre_discount_gm, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + D.Pre_discount_gm, + in_stock +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type, + Pre_discount_gm + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE gm_type ='DDB') A +LEFT JOIN raena_analytics.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN raena_analytics.sku_sales_factor C ON A.SKU = C.sku +LEFT JOIN raena_analytics.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND D.Pre_discount_gm>=10 +LEFT JOIN raena_analytics.sku_in_stock F ON B.mapping_group_base = F.sku +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM + (SELECT sku, + name, + product_type, + category_id, + brand_id + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.category) C ON B.category_id = C.id + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id ) E ON B.mapping_group_base= E.SKU; + + DROP TABLE raena_analytics.recommentdation_offender_sku_concern; + + +CREATE TABLE raena_analytics.recommentdation_offender_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_offender B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_concern_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC; + + + DROP TABLE raena_analytics.recommentdation_offender_sku_ingredient; + + +CREATE TABLE raena_analytics.recommentdation_offender_sku_ingredient AS +SELECT reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_offender B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +AND recommended_ingredient_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC ; + + + DROP TABLE IF EXISTS raena_analytics.recommentdation_ddb_sku_concern ; + + +CREATE TABLE raena_analytics.recommentdation_ddb_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_concern_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE in_stock>50 and reseller_pre_discount_gm>10; + +DROP TABLE IF EXISTS raena_analytics.recommentdation_ddb_sku_ingredient ; + + +CREATE TABLE raena_analytics.recommentdation_ddb_sku_ingredient AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_ingredient_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE in_stock>50 +and reseller_pre_discount_gm >=10; + +DROP TABLE raena_analytics.recommentdation_sku_ingredient; + + +CREATE TABLE raena_analytics.recommentdation_sku_ingredient AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + Recommended_pre_discount_gm, + sale_factor , + (Ingredient_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm) sorting, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + cast(sale_factor AS decimal(22,2)) sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, tier_price::json->> 'bf645e97-8a48-4977-8367-e987489760f9' tier_price from raena_catalog_management.product) BB on AA.Recommended_sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + + +DROP TABLE raena_analytics.recommentdation_sku_ingredient_purchase; + + +CREATE TABLE raena_analytics.recommentdation_sku_ingredient_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(tier_price AS decimal(22,2))gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku, + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, tier_price::json->> 'bf645e97-8a48-4977-8367-e987489760f9' tier_price from raena_catalog_management.product) BB on AA.sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + + +DROP TABLE raena_analytics.recommentdation_sku_concern; + + +CREATE TABLE raena_analytics.recommentdation_sku_concern AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + Recommended_pre_discount_gm, + sale_factor , + concern_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm sorting, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, tier_price::json->> 'bf645e97-8a48-4977-8367-e987489760f9' tier_price from raena_catalog_management.product) BB on AA.Recommended_sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + +DROP TABLE raena_analytics.recommentdation_sku_concern_purchase; + + +CREATE TABLE raena_analytics.recommentdation_sku_concern_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, tier_price::json->> 'bf645e97-8a48-4977-8367-e987489760f9' tier_price from raena_catalog_management.product) BB on AA.sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + -----------------------------------------------Task 3 -------------------------------(completed) +--inner join (select distinct id as order_id , json_extract_path_text(reseller_info,'province',TRUE) reseller_province +-- from raena_order_management.order +-- where created_at::date>dateadd(DAY,-91,'2023-11-21') and payment_status='Paid') AA on A.external_id = AA.order_id + + +DROP TABLE raena_analytics.SKU_base_recommendation_base_t3 ; + + +CREATE TABLE raena_analytics.SKU_base_recommendation_base_t3 AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + shipping_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM raena_analytics.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +WHERE transaction_date::date>cast('2023-11-21' as date)-interval'90 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + and (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false' +GROUP BY 1, + 2, + 3, + 4, + 5, + 6 ; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_t3; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_t3 AS +SELECT reseller_id , + A.sku, + case when shipping_to ='Customer' then shipping_province end as dropshipping_province, + case when shipping_to ='Reseller' then shipping_province end as reseller_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin) *quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Post_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM raena_analytics.gm_dashboard A +INNER JOIN + (SELECT DISTINCT id AS order_id , + shipping_to + FROM raena_order_management.order + WHERE created_at::date>cast('2023-11-21' as date)-interval'180 days' + AND payment_status='Paid') AA ON A.external_id = AA.order_id +INNER JOIN raena_analytics.recommendation_power_seller_base BB ON A.reseller_email = BB.email +WHERE transaction_date::date>cast('2023-11-21' as date)-interval'180 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND reseller_id IS NOT NULL + AND order_placed_by ='Admin Panel' + AND shipping_to <> 'Marketplace' +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_stage1_t3; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_stage1_t3 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM raena_analytics.reseller_base_recommendation_base_t3; + + +drop table if exists raena_analytics.reseller_base_recommendation_base_stage1_t3_v1; + +create table raena_analytics.reseller_base_recommendation_base_stage1_t3_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm, + in_stock + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6,9; + + drop table if exists raena_analytics.reseller_base_recommendation_base_stage1_t3_v2; + +create table raena_analytics.reseller_base_recommendation_base_stage1_t3_v2 +as + SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku total_quantity_sku , + B.Pre_discount_gm + FROM (select distinct reseller_id , + sku, + dropshipping_province + from raena_analytics.reseller_base_recommendation_base_stage1_t3 + where dropshipping_province is not null ) A + LEFT JOIN raena_analytics.reseller_base_recommendation_base_stage1_t3_v1 B + ON A.dropshipping_province = B.shipping_province ; + + + +DROP TABLE raena_analytics.same_province_with_diff_sku_dropshipping; + + + +CREATE TABLE raena_analytics.same_province_with_diff_sku_dropshipping AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk +from raena_analytics.reseller_base_recommendation_base_stage1_t3_v2 A + WHERE A.sku <> A.recommended_sku and dropshipping_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + + --------------- + DROP TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM raena_analytics.same_province_with_diff_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + +drop table if exists raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1; + +create table raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +DROP TABLE IF EXISTS raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2 AS +SELECT A.reseller_id , + A.sku , + A.reseller_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm +FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A +LEFT JOIN raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1 B ON A.reseller_province = B.shipping_province +WHERE A.reseller_province IS NOT NULL; + + +DROP TABLE raena_analytics.same_province_with_diff_sku_reseller; + + +CREATE TABLE raena_analytics.same_province_with_diff_sku_reseller AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2 A +WHERE A.sku <> A.recommended_sku and A.reseller_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + +---- + + +DROP TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM raena_analytics.same_province_with_diff_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + +drop table if exists raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1; + +create table raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE IF EXISTS raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2 AS +SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm +FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A +LEFT JOIN raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1 B ON A.dropshipping_province = B.shipping_province +WHERE A.dropshipping_province IS NOT NULL; + +DROP TABLE raena_analytics.same_province_with_same_sku_dropshipping; + + +CREATE TABLE raena_analytics.same_province_with_same_sku_dropshipping AS +SELECT * +FROM + (SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition BY reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk + FROM raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2 A + WHERE sku = recommended_sku + AND A.dropshipping_province IS NOT NULL) AA +WHERE rnk<11 +ORDER BY sku , + rnk; + +DROP TABLE raena_analytics.recommendation_same_province_with_same_sku_dropshipping; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_same_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM raena_analytics.same_province_with_same_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + + +drop table if exists raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1; + +create table raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +drop table if exists raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2; + +create table raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2 +as +SELECT A.reseller_id , + A.sku , + A.reseller_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm + FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1 B + ON A.reseller_province = B.shipping_province where A.reseller_province is not null ; + + + DROP TABLE raena_analytics.same_province_with_same_sku_reseller; + + +CREATE TABLE raena_analytics.same_province_with_same_sku_reseller AS +select * from ( +SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2 A +WHERE sku = recommended_sku and A.reseller_province is not null )AA + where rnk<11 +ORDER BY sku , + rnk; + + DROP TABLE raena_analytics.recommendation_same_province_with_same_sku_reseller; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_same_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM raena_analytics.same_province_with_same_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + + + diff --git a/postgresql/am_recommendation/am_recommendation_etl_cron.sh b/postgresql/am_recommendation/am_recommendation_etl_cron.sh new file mode 100644 index 0000000..8f12b17 --- /dev/null +++ b/postgresql/am_recommendation/am_recommendation_etl_cron.sh @@ -0,0 +1,2876 @@ + #!/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 raena_analytics.raw_ingredient_split_name; +CREATE TABLE raena_analytics.raw_ingredient_split_name AS +SELECT * , + split_part(concern,', ',1) concern1, + split_part(concern,', ',2) concern2, + split_part(concern,', ',3) concern3, + split_part(concern,', ',4) concern4, + split_part(concern,', ',5) concern5, + split_part(top_marketing_ingredients ,', ',1) ingredient1, + split_part(top_marketing_ingredients,', ',2) ingredient2, + split_part(top_marketing_ingredients,', ',3) ingredient3, + split_part(top_marketing_ingredients,', ',4) ingredient4, + split_part(top_marketing_ingredients,', ',5) ingredient5, + split_part(top_marketing_ingredients,', ',6) ingredient6, + split_part(top_marketing_ingredients,', ',7) ingredient7, + split_part(top_marketing_ingredients,', ',8) ingredient8, + split_part(top_marketing_ingredients,', ',9) ingredient9, + split_part(top_marketing_ingredients,', ',10) ingredient10, + split_part(top_marketing_ingredients,', ',11) ingredient11 +FROM + (SELECT DISTINCT group_base, + sku , + lower(concern) concern, + lower(top_marketing_ingredients) top_marketing_ingredients, + CASE + WHEN concern='' + AND top_marketing_ingredients='' THEN 1 + END flag + FROM raena_analytics.ingredients_base_table_final) A +WHERE flag IS NULL; + + DROP TABLE raena_analytics.raw_ingredient_mapping_table_stage; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage AS +SELECT A.* , + B.group_base mapping_group_base, + B.concern mapping_concerns, + B.top_marketing_ingredients mapping_ingredients +FROM raena_analytics.raw_ingredient_split_name A +LEFT JOIN + (SELECT * + FROM raena_analytics.raw_ingredient_split_name) B ON 1=1; + +DROP TABLE if exists raena_analytics.raw_ingredient_mapping_table_stage2; + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage2 AS +SELECT * , + CASE + WHEN raena_analytics.udf_string_campare(concern1,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern1,mapping_concerns) <> 'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern1, + CASE + WHEN raena_analytics.udf_string_campare(concern2,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern2,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern2, + CASE + WHEN raena_analytics.udf_string_campare(concern3,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern3,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern3, + CASE + WHEN raena_analytics.udf_string_campare(concern4,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern4,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern4, + CASE + WHEN raena_analytics.udf_string_campare(concern5,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern5,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern5, + ARRAY_LENGTH(REGEXP_SPLIT_TO_ARRAY(concern,','),1)total_count_concern, + CASE + WHEN raena_analytics.udf_string_campare(ingredient1,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient1,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient1, + CASE + WHEN raena_analytics.udf_string_campare(ingredient2,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient2,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient2, + CASE + WHEN raena_analytics.udf_string_campare(ingredient3,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient3,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient3, + CASE + WHEN raena_analytics.udf_string_campare(ingredient4,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient4,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient4, + CASE + WHEN raena_analytics.udf_string_campare(ingredient5,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient5,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient5, + CASE + WHEN raena_analytics.udf_string_campare(ingredient6,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient6,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient6, + CASE + WHEN raena_analytics.udf_string_campare(ingredient7,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient7,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient7, + CASE + WHEN raena_analytics.udf_string_campare(ingredient8,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient8,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient8, + CASE + WHEN raena_analytics.udf_string_campare(ingredient9,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient9,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient9, + CASE + WHEN raena_analytics.udf_string_campare(ingredient10,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient10,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient10, + CASE + WHEN raena_analytics.udf_string_campare(ingredient11,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient11,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient11, + ARRAY_LENGTH(REGEXP_SPLIT_TO_ARRAY(top_marketing_ingredients,','),1) total_count_ingredient +FROM raena_analytics.raw_ingredient_mapping_table_stage +WHERE group_base <> mapping_group_base; + + DROP TABLE if exists raena_analytics.raw_ingredient_mapping_table_stage3; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage3 AS +SELECT *, + flag_concern1+flag_concern2+flag_concern3+flag_concern3+flag_concern3 total_concern_match, + flag_ingredient1+flag_ingredient2+flag_ingredient3+flag_ingredient4+flag_ingredient5+flag_ingredient6+flag_ingredient7+flag_ingredient8+flag_ingredient9+flag_ingredient10+flag_ingredient11 total_ingredient_match +FROM raena_analytics.raw_ingredient_mapping_table_stage2 ; + + +DROP TABLE IF EXISTS raena_analytics.raw_ingredient_mapping_table_stage4; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage4 AS +SELECT *, (total_concern_match*100/total_count_concern) Percent_concern_match, (total_ingredient_match*100/total_count_ingredient) Percent_ingredient_match , + CASE WHEN concern='' AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND top_marketing_ingredients='' THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + END percent_flag +FROM raena_analytics.raw_ingredient_mapping_table_stage3; + + +Drop table if exists raena_analytics.raw_ingredient_mapping_table; + +Create table raena_analytics.raw_ingredient_mapping_table +as +SELECT group_base, + sku, + concern, + top_marketing_ingredients, + mapping_group_base, + mapping_concerns, + mapping_ingredients, + percent_concern_match, + Percent_ingredient_match +FROM raena_analytics.raw_ingredient_mapping_table_stage4 +WHERE percent_flag=1 +ORDER BY group_base , + percent_concern_match DESC , + Percent_ingredient_match DESC; + + +DROP TABLE IF EXISTS raena_analytics.sku_in_stock; + + +CREATE TABLE raena_analytics.sku_in_stock AS +SELECT sku , + sum(coalesce(F.stock_limit,0)) 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; + + --------------------------------------------------------------task 1 ---------------- + +drop table if exists raena_analytics.recommendation_power_seller_base_alt; + +create table raena_analytics.recommendation_power_seller_base_alt +as +select distinct email from raena_analytics.om_acquisition_mapping_list +union select distinct \"email id\" from raena_analytics.AM_REVENUE_LEAKAGE_LIST ; + + +INSERT INTO raena_analytics.recommendation_power_seller_base_alt + values ('cindyvioleta29@yahoo.com'), +('sherryfannydesita@gmail.com'), +('masintansilalahi@yahoo.co.id'), +('rumahskincarebpp@gmail.com'), +('puturisca@gmail.com'), +('sieny.go@gmail.com'), +('oki.zhang1988@gmail.com'), +('jeremytantono@yahoo.co.id'), +('yuke.cipta@gmail.com'), +('kwonvic@hotmail.com'), +('ervinafransiska95@gmail.com'), +('indahamaliasasmita@gmail.com'), +('mirosebeauty@gmail.com'), +('fourrshoptarakan@gmail.com'), +('stuffbyms@gmail.com'), +('cindyrissa14@gmail.com'), +('lieke1212@gmail.com'), +('jelitacosmetic.yk@gmail.com'), +('metafuji@gmail.com'), +('destiyapradhani@gmail.com'), +('liepinghau@gmail.com'), +('jennifer_kambuno@yahoo.com'), +('y35beauty@gmail.com'), +('nisamulya@gmail.com'), +('issadella@yahoo.co.id'), +('hartono.henny@yahoo.co.id'), +('a2335572@gmail.com'), +('navisgusmiati24@gmail.com'), +('djiwandimelany@gmail.com'), +('colinakoh178@gmail.com'), +('fairybeaute17@gmail.com'), +('fajri.vanbio95@gmail.com'), +('zidney_yao@yahoo.com'), +('chandraalex305@gmail.com'), +('elly.elisha@gmail.com'), +('erlinapr13@gmail.com'), +('mechan_06@hotmail.com'), +('limvincents@hotmail.com'), +('tjuganteng@gmail.com'), +('sralaydrus96@yahoo.com'), +('duwik_tonx@yahoo.co.id'), +('adelshopadelia@gmail.com'), +('shierlysusanto2405@gmail.com'), +('murni.cosmetics55@gmail.com'), +('stg_purchasing_melisa@yahoo.com'), +('asri.nilasari@watsons.co.id'), +('wayanndiana@gmail.com'), +('emailtedy@gmail.com'), +('Shantifm@hotmail.com'), +('shintachristiani2911@gmail.com'), +('dotienigloo@gmail.com'), +('mirandadessy95@gmail.com'), +('pangeranpetirr@gmail.com'), +('mariamellysacandra@ymail.com'), +('evitamalaa@ymail.com'), +('lindadarwinp@gmail.com'), +('dyfabandung@gmail.com'), +('yestastore@gmail.com'), +('outletmadanibeauty@gmail.com'), +('mrs.kumara@yahoo.com'), +('cindylum23@gmail.com'), +('fairy_love90@ymail.com'), +('fajarcosmetikpekanbaru@gmail.com'), +('alapstorekosmetik@gmail.com'), +('marlinabahnur@yahoo.co.id'), +('miswarderi61@gmail.com'), +('kristian_tjia@yahoo.co.id'), +('dessi.three@gmail.com'), +('rumahkurumahta@gmail.com'), +('nabilaalifia2000@gmail.com'), +('ullyfina@gmail.com'), +('tjindaidyandy@gmail.com'), +('shintayuu@gmail.com'), +('ratihpuji001@gmail.com'), +('shynobyshould@gmail.com'), +('rismawatisapu101@gmail.com'), +('caramelmadeby@gmail.com'), +('piyamapoppy@gmail.com'), +('scarlettwhitening.manado@gmail.com'), +('makeupbliss.id@gmail.com'), +('agnyjmpardosi@gmail.com'), +('sherbeautee@gmail.com'), +('yfbeauty99@gmail.com'), +('cmstadulako36@gmail.com'), +('lizalgs97@gmail.com'), +('emailnyawabisabi@gmail.com'), +('alvhiraalamrih@gmail.com'), +('megasyaf10@gmail.com'), +('ismisarisapitri6@gmail.com'), +('realrima91@gmail.com'), +('hestynorata74@gmail.com'), +('tokopinkbox@gmail.com'), +('melly.angelica@sils.co.id'), +('baguzokee@gmail.com'), +('vandy_hood@yahoo.com'), +('stevenwerencius15@gmail.com'), +('qurniaulan123@gmail.com'), +('ivanalystia21@gmail.com'), +('claralverinas@gmail.com'), +('riaarddd@gmail.com'), +('yayz_bubble@yahoo.com'), +('elsa.rivani@rocketmail.com'), +('feyrenpusura@gmail.com'), +('gunz_r@yahoo.com'), +('asrularman077@gmail.com'), +('hellojuwitaang@gmail.com'), +('bernike.daniel@gmail.com'), +('ayuerawati1993@gmail.com'), +('sudutcantik03@gmail.com'), +('norio.agnes@gmail.com'), +('smarch2u@gmail.com'), +('nunungokta8@gmail.com'), +('siaocie.tw@gmail.com'), +('sandrachen20@icloud.com'), +('siti.komariiah@gmail.com'), +('gustine.fu999@gmail.com'), +('ennysetiono@msn.com'), +('sartikanganro@gmail.com'), +('mirandahabibie@gmail.com'), +('tokodahliapadang@gmail.com'), +('natanaelsbrn@gmail.com'), +('jelitacosmetic@galeri.com'), +('rkkosmetikviral.anj@gmail.com'), +('sancaytulak26@gmail.com'), +('kelvinhosen@gmail.com'), +('elnazulma@gmail.com'), +('jevrihuang@gmail.com'), +('araitsoke@gmail.com'), +('haju.nutrifood@gmail.com'), +('harwindaharis99@gmail.com'), +('saherasukur92@gmail.com'), +('monamelosi05@gmail.com'), +('pramistiseptarinda@gmail.com'), +('rianadewi15746@gmail.com'), +('frivaldi08@gmail.com'), +('sausankho86@gmail.com'), +('3widjayanti@gmail.com'), +('prinzlina79@gmail.com'), +('restbeauty11@gmail.com'), +('kiarriyasatul@gmail.com'), +('surkiaa56@gmail.com'), +('dellikosmetik@gmail.com'), +('dinafebriana@gmail.com'), +('risnawatiyunus@yahoo.com'), +('pudoopy@gmail.com'), +('serbunganjuk@gmail.com'), +('linda_ho29@yahoo.com'), +('cassmgadm@gmail.com'), +('jennychateriner@gmail.com'), +('thasashop.id@yahoo.com'), +('diancagoodsbywitari@gmail.com'), +('chang3mu@icloud.com'), +('hana_kho@outlook.com'), +('jwl.skinofficial@gmail.com'), +('febri.ilham68@gmail.com'), +('gurl.shopaholic@gmail.com'), +('fenitan00@gmail.com'), +('nadrachanafie@gmail.com'), +('migibeauty1@gmail.com'), +('dewisusianti888@gmail.com'), +('nazwinadya@gmail.com'), +('andinimaylasari@gmail.com'), +('syahrani.cosmetic@gmail.com'), +('primasitaa@gmail.com'), +('spy_solution77@yahoo.com'), +('geeghina@gmail.com'), +('nida.syafa5@gmail.com'), +('financialelzamora@gmail.com'), +('irkosmetikdanskincare@gmail.com'), +('sherlinhosana@gmail.com'), +('sepdiarina@yahoo.com'), +('lumbangaol.ira@gmail.com'), +('aligunawan098@gmail.com'), +('ovarash@gmail.com'), +('rbeauty.storee@gmail.com'), +('cckosmetik77@gmail.com'), +('pinkcosmetich@gmail.com'), +('dhiniemsa@yahoo.com'), +('rulyindah1911@gmail.com'), +('elvina.6789@gmail.com'), +('moeyecosmetic99@gmail.com'), +('sana.19007@mhs.unesa.ac.id'), +('ikasusanti_baru@yahoo.com'), +('indaryani82@gmail.com'), +('hsyapon@gmail.com'), +('citra.ismaya@omniretail.co'), +('thevintagestrawberry@gmail.com'), +('ramadhani.kosmetik@gmail.com'), +('linawijaya2712@yahoo.com'), +('meilings0403@gmail.com'), +('nadyawkk@gmail.com'), +('prellasurabaya@gmail.com'), +('ziyyags@gmail.com'), +('stanley_oh@ymail.com'), +('andre1177az@gmail.com'), +('s.ekaputrirosalinda@gmail.com'), +('beautyland21@icloud.com'), +('javalotuscorp@gmail.com'), +('alxsuherman@gmail.com'), +('fenny3007.kwok@gmail.com'), +('leny_kurniawati@hotmail.com'), +('soputanlady10@gmail.com'), +('lampungbeauty2019@gmail.com'), +('thebeauteebox@gmail.com'), +('tfanixling00@gmail.com'), +('andilesmono06@gmail.com'), +('briggiteleorenza@gmail.com'), +('chanytatiara@gmail.com'), +('financeklinikpiramidajaya@gmail.com'), +('exacarolina@gmail.com'), +('wildhanhafid@gmail.com'), +('himakeup08@gmail.com'), +('riska.sofani@yahoo.com'), +('uciliana35@gmail.com'), +('agusjokin08@gmail.com'), +('madozai96@gmail.com'), +('flo_ver@yahoo.com'), +('dikinarasaki@gmail.com'), +('herul.batch3@gmail.com'), +('rudy.hartono2591@gmail.com'), +('vivi.plm187@gmail.com'), +('hallo@bencuan.id'), +('rahasiamama17@gmail.com'), +('stephzhengg@gmail.com'), +('shelinabeautycare@gmail.com'), +('joelchristianpamula@gmail.com'), +('rethamardjoen11@gmail.com'), +('pomadaivon@gmail.com'), +('divaparis28@gmail.com'), +('wulan.ranpatika@gmail.com'), +('mastoreyadi@gmail.com'), +('febi.muhi@gmail.com'), +('anggriawanferi1991@gmail.com'), +('feliciacolleen80@gmail.com'), +('istie.nodasekisan@gmail.com'), +('melliyniaanugrah@gmail.com'), +('dwiaziza98@gmail.com'), +('alfrianiyelsi@gmail.com'), +('adelia.vn@gmail.com'), +('alvina.iriady@gmail.com'), +('ratnaqisya03@gmail.com'), +('jelitacosmetic.galeri@gmail.com'), +('avex307@gmail.com'), +('rika.wulandaridwan@gmail.com'), +('srihartiniseptember1966@gmail.com'), +('theresia_margareths@yahoo.com'), +('kartikahs89@gmail.com'), +('yanuararifinnur@gmail.com'), +('luthfikhtr@gmail.com'), +('mshopper.id@gmail.com'), +('fifi.liu@yahoo.com'), +('christineeva07@gmail.com'), +('claralverina23@gmail.com'), +('12101na@gmail.com'), +('joanamarvelin.wongso@yahoo.com'), +('lylysiakwee@gmail.com'), +('andyirsaldi@gmail.com'), +('greceliautami@icloud.com'), +('rhayati48@gmail.com'), +('vidya@redroseindo.com'), +('ekanuraini131@gmail.com'), +('kimhiung168@gmail.com'), +('gitaporo@gmail.com'), +('aninditateresa@gmail.com'), +('lexonbeautyofficial@gmail.com'), +('novitaroshaputri@gmail.com'), +('theara.tiara@gmail.com'), +('selvi.chephy@gmail.com'), +('vhaans007@gmail.com'), +('tokosehat130@gmail.com'), +('mirayyacosmetics@gmail.com'), +('kusnen.sota@gmail.com'), +('hokimaxi2020@gmail.com'), +('keikolux@hotmail.com'), +('andreadewi19@gmail.com'), +('harlyskin@gmail.com'), +('viroe.nando@gmail.com'), +('aurelallshop@gmail.com'), +('kosmetikiis@gmail.com'), +('lifanny86@gmail.com'), +('nitamanda28@gmail.com'), +('cantiku8899@gmail.com'), +('sultanagengs3@gmail.com'), +('nurfiitriiyah@gmail.com'), +('nana01iphone@gmail.com'), +('mulyajaaya.motor96@gmail.com'), +('titarahmawati245@gmail.com'), +('marniatihinding@gmail.com'), +('erris.mahardika@gamal.men'), +('isti.qhatsah.nur@gmail.com'), +('dewilasolo@gmail.com'), +('aninditaputri30@gmail.com'), +('aryanto.firdaus@gmail.com'), +('novitaj68@gmail.com'), +('johan_breaker27@yahoo.com'), +('elsaberliya@gmail.com'), +('dewiyuant260693@gmail.com'), +('jeffrynoah@gmail.com'), +('rikasukmawar03@gmail.com'), +('yosua.sasauw1@gmail.com'), +('fitri.miauwh@gmail.com'), +('yuli120797@gmail.com'), +('kamala.tanray@gmail.com'), +('theresiad88@gmail.com'), +('isliana29@gmail.com'), +('chietra.anggraini93@gmail.com'), +('cesario2559@gmail.com'), +('the.secret.of.beauty.id@gmail.com'), +('elsacosmetics@yahoo.com'), +('evinrahmad2@gmail.com'), +('yuhyi.albadali@gmail.com'), +('darwin.saputra.ars@gmail.com'), +('akuntingtim2903@gmail.com'), +('wahyudinrazali4@gmail.com'), +('jwuysan@gmail.com'), +('ruus.silvana@gmail.com'), +('dianmarinaa@gmail.com'), +('dp.anggreani@gmail.com'), +('patricia.ang7@gmail.com'), +('medankosmetik89@gmail.com'), +('ayuputri.ap2201@gmail.com'), +('fikrizia13@gmail.com'), +('beautiva.skincare@gmail.com'), +('gjwproject@gmail.com'), +('pramudya.a.laksono@gmail.com'), +('ssshoptarakan@gmail.com'), +('bungalintong7@gmail.com'), +('ivanakaontole@gmail.com'), +('misel.delini@yahoo.com'), +('dwipujiati1995@gmail.com'), +('vdillahilda@gmail.com'), +('ellenanatasa@gmail.com'), +('bidarishabrina@gmail.com'), +('danimmm44@gmail.com'), +('evitsrikris@gmail.com'), +('gendisbeauty92@gmail.com'), +('eriana.dian0116@gmail.com'), +('eriskhfrd26@gmail.com'), +('jasmineniscalapandya@gmail.com'), +('eryryey03@gmail.com'), +('leessleepwear@gmail.com'), +('yoseptansil6@gmail.com'), +('natdailybeauty@gmail.com'), +('sidoarjosalonsupp@gmail.com'), +('auliarahmadanti77@gmail.com'), +('gustindw@gmail.com'), +('mamihcihuyyy@gmail.com'), +('narabeautytrk@gmail.com'), +('sandikaplg18@gmail.com'), +('20240010013@lspr.edu'), +('maynetav@gmail.com'), +('intandidi88@gmail.com'), +('jeniferpurnomo99@gmail.com'), +('dewisartika192@yahoo.co.id'), +('jokosus.susanto@gmail.com'), +('iswanto.1301@gmail.com'), +('kudo.palopo14@gmail.com'), +('micellangel@nuriglobal.com'), +('theeaanggraeny@gmail.com'), +('andi.or88@gmail.com'), +('rickyoktaviawan20@gmail.com'), +('yunibalisa661@gmail.com'), +('rikiscofieldsap@gmail.com'), +('feliciabeatrixtanner0407@gmail.com'), +('mon_taca@yahoo.com'), +('ardyanawidya@gmail.com'), +('d14na_nonic@yahoo.com'), +('kadirpnm@gmail.com'), +('mita.panji@gmail.com'), +('robbchriszzz13@gmail.com'), +('anicha8490@gmail.com'), +('galuh.acc.kediri@gmail.com'), +('tokosanjayakras.81@gmail.com'), +('vivian.effendi@gmail.com'), +('lisaderia@yahoo.com'), +('anggyfebryna17@gmail.com'), +('yuanita200989@gmail.com'), +('inggycitrasari@gmail.com'), +('musdalifa300396@gmail.com'), +('widdywedariasih3@gmail.com'), +('devinoariska06@gmail.com'), +('idealistina@yahoo.co.id'), +('kelsy18111@gmail.com'), +('elizchang88@gmail.com'), +('kavbeautyofficial@gmail.com'), +('archilagiska@gmail.com'), +('ginawati442@gmail.com'), +('revii.setiadi@gmail.com'), +('nisacuss@gmail.com'), +('eviana001075@gmail.com'), +('moanazone@gmail.com'), +('idabenangraja@gmail.com'), +('babybamscase@gmail.com'), +('bellasetiady@windowslive.com'), +('regina.amalia19@gmail.com'), +('hello.vebeauty@gmail.com'), +('ferinaindrayani@gmail.com'), +('yulia.cindy81@gmail.com'), +('ennakristiana@gmail.com'), +('nur.annisa0@gmail.com'), +('marindarara13@gmail.com'), +('sonyatiaratri2@gmail.com'), +('achmadfauzi1398@gmail.com'), +('fitrijean@gmail.com'), +('zul.hendra1297@gmail.com'), +('csbymagicskin@gmail.com'), +('ridadwi873@gmail.com'), +('grosirkosmetikpekanabaru@gmail.com'), +('gladys.agatha@gmail.com'), +('rahmizarti30@gmail.com'), +('mrs.sitiazizah@gmail.com'), +('paramitaqueena@yahoo.com'), +('uwiecute22@gmail.com'), +('slife557.cs@gmail.com'), +('adindazahra655@gmail.com'), +('lavibeautyandlifestyle@gmail.com'), +('ratna.pratiwi14.rp@gmail.com'), +('yunengazraapriani@gmail.com'), +('sheefaa84@gmail.com'), +('katherinelimanu21@gmail.com'), +('tutiyuniyanti@gmail.com'), +('kartinisarimah@gmail.com'), +('jonastampubolon123@gmail.com'), +('wuisancheni77@gmail.com'), +('shella.madirza888@gmail.com'), +('yulianisetiawan836@gmail.com'), +('janiceechenn@yahoo.com'), +('ferico.ridwan@gmail.com'), +('udjayamandiripgs@gmail.com'), +('budiwijaya1962@gmail.com'), +('rumahkosmetik001@gmail.com'), +('kinanbeautystore@gmail.com'), +('yuniraemah31@yahoo.co.id'), +('barokahbeaute@gmail.com'), +('shrlymarcella@gmail.com'), +('ikrt_2507@yahoo.com'), +('dikofebyan@gmail.com'), +('restyty.riezalina@yahoo.com'), +('ratukosmetik169@gmail.com'), +('faradillahif07@gmail.com'), +('sales@dewicosmetic.com'), +('kadek.arymas@gmail.com'), +('rahmaduwijayanti@gmail.com'), +('fanamaliaauthor@gmail.com'), +('vanessapratama24@gmail.com'), +('gangnamcosmetic109@gmail.com'), +('mayanglatuconsina@gmail.com'), +('lustremaiden@gmail.com'), +('ratihalexa@gmail.com'), +('anindyalarasp@gmail.com'), +('fourayoebeauty@gmail.com'), +('selenabeauty.banjarbaru@gmail.com'), +('destyisniaty@gmail.com'), +('rmdhniannisa15@gmail.com'), +('devimutiah18@gmail.com'), +('ainunmuthiaa@gmail.com'), +('nurhikmah.rasyid@gmail.com'), +('burizma.store@gmail.com'), +('ariefariq4@gmail.com'), +('haniaekap8814@gmail.com'), +('ricomerico240@gmail.com'), +('ekanvt07@gmail.com'), +('helenlim02@gmail.com'), +('lukm.hakm@gmail.com'), +('nur_aisyah1407@icloud.com'), +('serlina.mbc@gmail.com'), +('k.inayah23@gmail.com'), +('marisrisma@yahoo.com'), +('shela.annisa99@gmail.com'), +('angguntrivany@gmail.com'), +('nntheorange@gmail.com'), +('nathaliayaulin23@gmail.com'), +('mrs.gapura@gmail.com'), +('selviathalita92@gmail.com'), +('cynthia.de06@gmail.com'), +('beautemondee@gmail.com'), +('ninikrahmawati486@gmail.com'), +('anst.slvia@gmail.com'), +('beautycarepalu@gmail.com'), +('ririwiduri2002@gmail.com'), +('ilookdaily534@gmail.com'), +('apapunada.id@gmail.com'), +('dellasyaskia6@gmail.com'), +('nadiasalmau@gmail.com'), +('tikamaharaeni19@gmail.com'), +('aansabang@gmail.com'), +('devinatalithanabila98@gmail.com'), +('ennitrisha@gmail.com'), +('marialucyanasunjaya02762@gmail.com'), +('rtnohandayani@gmail.com'), +('nsbeauty06@gmail.com'), +('idapurnamasari0129@gmail.com'), +('msreysantana@gmail.com'), +('aldillaputri5@gmail.com'), +('maderusni25@gmail.com'), +('crystalglow.myshop@gmail.com'), +('dheamuliasari@gmail.com'), +('cindyfdle@gmail.com'), +('laninabeauty.official@gmail.com'), +('nimatussaadah29@gmail.com'), +('merrylaili@gmail.com'), +('wijaya.nanda1810@gmail.com'), +('kadavikadav@gmail.com'), +('anggikurniawati40@gmail.com'), +('mamikikeyu@gmail.com'), +('marlindaabdulrazak@gmail.com'), +('alizhaaa@gmail.com'), +('queenkosmetik20@gmail.com'), +('skincarekupang21@gmail.com'), +('feny.asnawi@gmail.com'), +('shanticahyadi82@gmail.com'), +('zilvanaputri@gmail.com'), +('malikaresmanti@gmail.com'), +('yunisianturi06@gmail.com'), +('beautydarlinggg@gmail.com'), +('lilybeauty.ofc@gmail.com'), +('racyasry@gmail.com'), +('terasbeauty.official@gmail.com'), +('mybeautystory.89@gmail.com'), +('novita.chyntia@yahoo.com'), +('tiffany.kimana@gmail.com'), +('rumahdandan.rd15@gmail.com'), +('desihartanti17@gmail.com'), +('forshare.id@gmail.com'), +('elmiacosmetics@gmail.com'), +('evikartika03@gmail.com'), +('allwomeneeds1403@yahoo.com'), +('nadyahudaya@yahoo.com'), +('drchrysant@gmail.com'), +('yurimeni16@gmail.com'), +('anientya@gmail.com'), +('martina.bernadeta@gmail.com'), +('wulan.dhizhi@gmail.com'), +('andrixue@gmail.com'), +('sitiqulsum308@gmail.com'), +('galaxystoreyogyakarta@gmail.com'), +('iskifitriani@yahoo.co.id'), +('arismunandar6496@gmail.com'), +('agenlingerie.id@gmail.com'), +('indahasmoro90@gmail.com'), +('ksbeautylover@gmail.com'), +('alheabeauty.id@gmail.com'), +('putriitunggal99@gmail.com'), +('ahmadmalangah@gmail.com'), +('perfectionten@gmail.com'), +('ivonsetiono26@gmail.com'), +('agencmb@gmail.com'), +('glowivier@gmail.com'), +('pinkyscosmetics1995@gmail.com'), +('evinakuesnoooo@gmail.com'), +('chennywi0126@icloud.com'), +('ekawahyuni1603@gmail.com'), +('auliaqoriardi21@gmail.com'), +('joyakhineigsoen@gmail.com'), +('susantifajarwati@yahoo.com'), +('ramadhana.selvia2408@gmail.com'), +('andarish.shop@gmail.com'), +('cindytsania@gmail.com'), +('joctora@gmail.com'), +('hannahimanuella@gmail.com'), +('dwiwulandari028@gmail.com'), +('merrychrystin06@gmail.com'), +('fibi.kdi891@gmail.com'), +('suwitosjahari@gmail.com'), +('likestoretlogosari@gmail.com'), +('care.meiyabeauty@gmail.com'), +('ajenganggitasari1@gmail.com'), +('rizkiyanitapinem@gmail.com'), +('sashop.nkc21@gmail.com'), +('lilistiyorini@gmail.com'), +('thebellas1212@gmail.com'), +('be.bellecosmetic@gmail.com'), +('nurlisaaulia@gmail.com'), +('hantybeauty19@gmail.com'), +('mputrisanti@gmail.com'), +('hellolightbit@gmail.com'), +('nadhirauw88@gmail.com'), +('asmiatiakib@gmail.com'), +('ranintya12@gmail.com'), +('afrijalrijal671@gmail.com'), +('ifayusana1993@gmail.com'), +('makeyouup.myu@gmail.com'), +('salbinus.innung@yahoo.com'), +('novia190@gmail.com'), +('evadewim.s@gmail.com'), +('itsmiracle2020@gmail.com'), +('neveltari2703@gmail.com'), +('lieusbudiman@gmail.com'), +('ferisya10@gmail.com'), +('echa.muaniez89@gmail.com'), +('trinarachmawati81@gmail.com'), +('erwinzeey123@gmail.com'), +('sahabat_debora@yahoo.com'), +('dhee2jopankar@gmail.com'), +('yunda.ade11@gmail.com'), +('cyril_steven_alexander_glory@yahoo.com'), +('devisiska32@gmail.com'), +('aureolasa.pratama@gmail.com'), +('ika.angky123@yahoo.com'), +('halimatussakdiyah1997@gmail.com'), +('rahmatjayaj2@gmail.com'), +('jesselynangella08@yahoo.com'), +('famela.ft07@gmail.com'), +('rotinsulufinny@gmail.com'), +('staryisnianti@gmail.com'), +('yulianazayan@gmail.com'), +('fandestian@gmail.com'), +('melindariaslamusnar88@gmail.com'), +('jnwkidswear@gmail.com'), +('wiwin.adriani@gmail.com'), +('febriantisiban25@gmail.com'), +('scoviabybelova@gmail.com'), +('wenny.than@gmail.com'), +('iranipontoh443@gmail.com'), +('hello.lazetta@gmail.com'), +('moullylaroid@gmail.com'), +('laurantjia@gmail.com'), +('dbeautyshop8@gmail.com'), +('godzillaphoneacc3@gmail.com'), +('wiwinstsajaah@gmail.com'), +('rizahani39@gmail.com'), +('dwihartanti_gizi@yahoo.com'), +('fangfangsaputra@gmail.com'), +('amoise.cosmetics@gmail.com'), +('dennyroseiana@gmail.com'), +('nelly.sulistiawati@gmail.com'), +('central.kosmetik1106@gmail.com'), +('marcellino.haryono@gmail.com'), +('veralia.saragih@gmail.com'), +('cefamanusia@gmail.com'), +('arindatrisna33@gmail.com'), +('oketomatala@gmail.com'), +('vilyapangajouw19@gmail.com'), +('faracintadia@gmail.com'), +('adm.elisabethbdg@yahoo.com'), +('cutmiqyal@hotmail.com'), +('harjono.honoris@gmail.com'), +('nandorian98@gmail.com'), +('milalimbong71@gmail.comcom'), +('viliansiregar24@gmail.com'), +('eldeekosmetik@gmail.com'), +('jeane.lelengboto@yahoo.com'), +('lydia.debby@gmail.com'), +('aboutmybusiness11@gmail.com'), +('triple501ara@gmail.com'), +('masfitroh@gmail.com'), +('meisalmis@gmail.com'), +('zackfair232@gmail.com'), +('nakamia_pie@yahoo.com'), +('abeauty.jogja@gmail.com'), +('lenaaska7@gmail.com'), +('zeebacosmetics31@gmail.com'), +('chiangkencana@gmail.com'), +('syaaudriana@gmail.com'), +('nadyaffu@gmail.com'), +('fauziahanisa509@gmail.com'), +('christyciasintaungu@gmail.com'), +('pradnyani.dwi@gmail.com'), +('zarazsby@gmail.com'), +('adewahyupauji@gmail.com'), +('firmantuo@gmail.com'), +('ramadhanilayla@gmail.com'), +('novascafe4@gmail.com'), +('mayaecc@yahoo.com'), +('adeyoan721@gmail.com'), +('stephaniefransss@gmail.com'), +('hermooi.official@gmail.com'), +('ratnaanita170294@gmail.com'), +('agatri.silitonga@gmail.com'), +('dewifitri2403@gmail.com'), +('moehamadsururi14@gmail.com'), +('ariestyayys@gmail.com'), +('heavenlybeauty.adm@gmail.com'), +('ikaabsarih@gmail.com'), +('dhee_amell@yahoo.co.id'), +('devianatanuwijaya@gmail.com'), +('rumah.karara@gmail.com'), +('sreejita.deb@gmail.com'), +('tagdirs.cosmetic@gmail.com'), +('ceviwilly@gmail.com'), +('daniel.khosuma@gmail.com'), +('muhammadyanis12091991@gmail.com'), +('liechintia07@gmail.com'), +('apt.deantigano@gmail.com'), +('ian.tampo22@gmail.com'), +('kunyil505@gmail.com'), +('hongjujen69@gmail.com'), +('wimma.shop@gmail.com'), +('mysamplemart@gmail.com'), +('safitriyuli3@gmail.com'), +('novanindra23@gmail.com'), +('hartantiarline@gmail.com'), +('dianyunus7@gmail.com'), +('aniyt161aan@gmail.com'), +('aestheticbeautycare23@gmail.com'), +('marissarahmaadhania9i30@gmail.com'), +('cecebeautybali@gmail.com'), +('devani.ersa@yahoo.com'), +('windiana10@gmail.com'), +('itaufik55@gmail.com'), +('zhisca_77@yahoo.co.id'), +('jillyanesasiage@gmail.com'), +('mybestiebeauty.bdg@gmail.com'), +('purchasing@bumame-farmasi.com'), +('denisa.denis89@yahoo.com'), +('ranggatedubara@gmail.com'), +('angelialim0311@gmail.com'), +('rindaagustiana@gmail.com'), +('aleshabeautycare57@gmail.com'), +('monpri28@gmail.com'), +('move.hamsari@gmail.com'), +('beautybelle2608@gmail.com'), +('priscillalastyfera@gmail.com'), +('vildarahmawati17@gmail.com'), +('nathaniaa099@gmail.com'), +('noyura@gmail.com'), +('juhatihajibe@gmail.com'), +('winda.christina@gmail.com'), +('mruslir21@gmail.com'), +('siskagsp6@gmail.com'), +('olyviadevita02@gmail.com'), +('ecgirl2023@gmail.com'), +('songlacosmetics@gmail.com'), +('jelsymakeup@gmail.com'), +('vanywidyawati86@gmail.com'), +('ongfreddykusuma@gmail.com'), +('charisthamaysadwisahara@gmail.com'), +('incjodu@gmail.com'), +('mjbeautyblitar@gmail.com'), +('ngeng.yuan27@gmail.com'), +('mybeautymart.id@gmail.com'), +('nabillafr22@gmail.com'), +('dianajennifer118@gmail.com'), +('wetirima9988@gmail.com'), +('shely.proborini@yahoo.com'), +('pak.serba@gmail.com'), +('lili_3038@yahoo.co.id'), +('tallamersiaa@gmail.com'), +('khalisanaura156@gmail.com'), +('milaatysakinah@gmail.com'), +('ucci.arfan@gmail.com'), +('lutfiah.nurhandari@gmail.com'), +('albinadhaja94@gmail.com'), +('dindaty@gmail.com'), +('shopkievanik@gmail.com'), +('zheng.jinde7692@gmail.com'), +('silvi@getvybes.co'), +('muhammadraydevan@gmail.com'), +('alstininoviyantitoar@gmail.com'), +('riahafarul778@gmail.com'), +('nrustam26@gmail.com'), +('gayabyys.wu@gmail.com'), +('lalootust@gmail.com'), +('rina2pan@yahoo.com'), +('disariska@gmail.com'), +('ninu.fira@gmail.com'), +('yuibabaretail@gmail.com'), +('makshifa93@gmail.com'), +('nadzimalaily@gmail.com'), +('nindysyarif@gmail.com'), +('mutiarapesonanganjuk@gmail.com'), +('beautybypelangi@gmail.com'), +('mariyani3412@gmail.com'), +('anitabudi1998@gmail.com'), +('farikhanum@ymail.com'), +('riscaamalia1989@gmail.com'), +('oyha.ciez01@gmail.com'), +('afriasafitri@ymail.com'), +('gitalaksmicosmetic@gmail.com'), +('oktavianimunir@gmail.com'), +('meithaekyh@gmail.com'), +('antouwkeren@gmail.com'), +('meeranova.id@gmail.com'), +('dmuhenti@gmail.com'), +('bima.ajun@gmail.com'), +('sitipluviana@gmail.com'), +('endahpratiwi522@gmail.com'), +('dindalabibah@gmail.com'), +('pusatkosmetikpinky@gmail.com'), +('anjalisibagariang2000@gmail.com'), +('krisnairma15@gmail.com'), +('ocha.rospa@gmail.com'), +('siscabudhyani86@gmail.com'), +('jhenisarykambuno@gmail.com'), +('mp-store4@raenabeauty.com'), +('luckyyosin0913@gmail.com'), +('monica1726@yahoo.co.id'), +('deasyrimanda@gmail.com'), +('pbudianoto@gmail.com'), +('hernispratiwi@gmail.com'), +('lelymelani@yahoo.com'), +('cantiksehat.sby@gmail.com'), +('gunk.fan29@gmail.com'), +('nurhayati01022021@gmail.com'), +('wardahafi@gmail.com'), +('nonikmery256@gmail.com'), +('lismiatilenta69@gmail.com'), +('dewinurhidayah5125@gmail.com'), +('nurhikmah.hasbi@gmail.com'), +('dordiaahss@gmail.com'), +('molly.fh2@gmail.com'), +('avostoresurabaya@gmail.com'), +('alhun.lun@gmail.com'), +('thebeautycare05@gmail.com'), +('mykarol88@gmail.com'), +('angela.sania@yahoo.com'), +('divaa01@ymail.com'), +('p.ariyanti2@gmail.com'), +('rizkycosmetics@gmail.com'), +('claudiayolandpurnomo@gmail.com'), +('ayudea1820@gmail.com'), +('allshopbisnis72@gmail.com'), +('yosephinetan@yahoo.co.id'), +('ayuri.minho@yahoo.com'), +('paolahermawan@yahoo.com'), +('mirna.mirnaaa02@gmail.com'), +('kumaskincare01@gmail.com'), +('officialrabeauty@gmail.com'), +('cacabeauty1234@gmail.com'), +('nadeakayuangellika@gmail.com'), +('linacaterina79@gmail.com'), +('rianishop210@gmail.com'), +('ilhamxmaulanaaa@gmail.com'), +('aratna643@gmail.com'), +('vifi2403@gmail.com'), +('paringdyahdinda@icloud.com'), +('suantysuanty10@gmail.com'), +('nandalisa045@gmail.com'), +('tinylilproject@yahoo.com'), +('arfan.arsya2022@gmail.com'), +('dahliaqueenshop@gmail.com'), +('triantiagus@gmail.com'), +('widyastevani17@gmail.com'), +('adityo_konandar@yahoo.com'), +('zoerosyidh89@gmail.com'), +('yusmane395@gmail.com'), +('tephtephyy@gmail.com'), +('seemser.id@gmail.com'), +('tantinaduu@gmail.com'), +('yannayeshika@gmail.com'), +('juliacsecogati@gmail.com'), +('pheroneid@gmail.com'), +('pauline222bn@gmail.com'), +('dpuspitaprameswari@gmail.com'), +('irnanda.vinka@yahoo.com'), +('agamwijaya89@gmail.com'), +('miniskinjars@gmail.com'), +('faizal@raenabeauty.com'), +('hanaamelinda95@gmail.com'), +('maharanimega39@gmail.com'), +('tia.aprita@gmail.com'), +('m.milanova.sh@gmail.com'), +('belscofabrics@gmail.com'), +('pratamanaufal98@gmail.com'), +('joshanshop@gmail.com'), +('ayuameliasaja@gmail.com'), +('alyadwi00@gmail.com'), +('helloyoumeindonesia@gmail.com'), +('desyrosiyana@gmail.com'), +('isvimega@gmail.com'), +('andrianiyana815@gmail.com'), +('andiakbar.ak@gmail.com'), +('isw11business@gmail.com'), +('guo_fengsheng@yahoo.com'), +('nirmaladewi03@gmail.com'), +('novitheng90@gmail.com'), +('poppymputranto@gmail.com'), +('shellahamayu1997@gmail.com'), +('suparjoariyanti@gmail.com'), +('pipinwahyu80@gmail.com'), +('chryseladarwin.olivia@gmail.com'), +('choisebest64@gmail.com'), +('rafin.trisna@gmail.com'), +('solehac098@gmail.com'), +('ayumirah12@yahoo.com'), +('fasahdi@gmail.com'), +('vindysabeauty@gmail.con'), +('dillalutchu08@gmail.com'), +('hi.dearbella@gmail.com'), +('awili.tri@gmail.com'), +('ardiradityananda@gmail.com'), +('dayanishop474@gmail.com'), +('apriliamega08@gmail.com'), +('vindysabeauty@gmail.com'), +('gilsneedit@gmail.com'), +('meiriannaafs@gmail.com'), +('mantepkebumen66@gmail.com'), +('lisnawatirohmah@gmail.com'), +('mhelydya@gmail.com'), +('apriliandesi@yahoo.co.id'), +('memonaofficial1007@gmail.com'), +('attraversiamobeauty@gmail.com'), +('triyatmoko3116@gmail.com'), +('toxena.shop@gmail.com'), +('fitridwifebriani59@gmail.com'), +('apadotid7@gmail.com'), +('misz_irma@yahoo.com'), +('riofabian112200@gmail.com'), +('indahyuliza7@gmail.com'), +('imasmaslihah2222@gmail.com'), +('letsglowid.jbi@gmail.com'), +('mahreenbeautyskin@gmail.com'), +('aldofiras1607@gmail.com'), +('firdamelia05@gmail.com'), +('hajernihajerni@gmail.com'), +('yunusdarmawan8@gmail.com'), +('fmaharani96@gmail.com'), +('tokoyangtepat@gmail.com'), +('endangdolphin2011@gmail.com'), +('pallawalipu21@gmail.com'), +('pie_buana@yahoo.com'), +('marina.diantika@gmail.com'), +('rihandika34@gmail.com'), +('ngm6756@gmail.com'), +('djonyali59@gmail.com'), +('rismagina02@gmail.com'), +('beautybyasame@gmail.com'), +('gracerushandi@gmail.com'), +('rinianggrayniinank@gmail.com'), +('lin_qiau_hua@yahoo.com'), +('buanainskonsi@gmail.com'), +('cosmetik.giant@gmail.com'), +('reynitaballack@gmail.com'), +('shopkichii88@gmail.com'), +('fadhilahafifah48@yahoo.com'), +('vivaforeva1@gmail.com'), +('sisilia.marshella@yahoo.com'), +('irmhanurjayanti@gmail.com'), +('galuhnur81@gmail.com'), +('gees.hijab@gmail.com'), +('putrimeyanti8@gmail.com'), +('rully0152@gmail.com'), +('buntukk166@gmail.com'), +('williantocindy@gmail.com'), +('nadilla@raenabeauty.com'), +('cindyfreda23@gmail.com'), +('liiyalimz@gmail.com'), +('kikaychan90@gmail.com'), +('tiffanyyaprilia@gmail.com'), +('2017120215@student.unpar.ac.id'), +('23jamesbond11@gmail.com'), +('36yonathan@gmail.com'), +('a.azzahrasyah@gmail.com'), +('a6ustina@gmail.com'), +('abyan.shop97@gmail.com'), +('acelinbeauty@gmail.com'), +('adeevaskincaresemarang@gmail.com'), +('adeyatoko@gmail.com'), +('adimahendraganteng@gmail.com'), +('agatha@raenabeauty.com'), +('agatyalaksmi@gmail.com'), +('agungfadilah810@gmail.com'), +('ahmadluckibawazier@gmail.com'), +('ai.x1.35300@sutomo-mdn.sch.id'), +('alfiatulkhubbi@gmail.com'), +('alfonsocosmetics@gmail.com'), +('alifiarizkiatania@gmail.com'), +('aline_fathin@yahoo.com'), +('alisheretotalk@gmail.com'), +('alithapradita@gmail.com'), +('allaboutskincare312@gmail.com'), +('almardhiyyahs@gmail.com'), +('almirabintangr@gmail.com'), +('alvissyahrin619@gmail.com'), +('alycia_monicaa@yahoo.co.id'), +('amaliakusumaniez@gmail.com'), +('ameicosmetic12@gmail.com'), +('amelianucifera@gmail.com'), +('ameliaresnaw@gmail.com'), +('analiani86@gmail.com'), +('andikharianto978@gmail.com'), +('angelinkayla118@gmail.com'), +('angellim0311@gmail.com'), +('angellina.lady96@gmail.com'), +('anggararnda972@gmail.com'), +('anggrini@raenabeauty.com'), +('anggriprad@gmail.com'), +('anisaauliaabas@gmail.com'), +('anisafarah24@gmail.com'), +('annachf.tan@gmail.com'), +('antodukos@gmail.com'), +('antonisang27@gmail.com'), +('appl3fuji@gmail.com'), +('arisrahmat333@gmail.com'), +('artiwayan14@gmail.com'), +('artrisnandari@gmail.com'), +('arumsekarsari573@gmail.com'), +('asepmirwan1@gmail.com'), +('assyafarrizki@gmail.com'), +('astrsari@gmail.com'), +('atikaputrims97@gmail.com'), +('audi.tabrany@gmail.com'), +('avoskinbeautybandung@gmail.com'), +('avuastore@gmail.com'), +('ayunamadewi@gmail.com'), +('babdungdyfa@gmail.com'), +('beatricetheresia1@gmail.com'), +('beautyshoujo@gmail.com'), +('bella.beautittaid@gmail.com'), +('bellaikne77@gmail.com'), +('bembie07@gmail.com'), +('beningkosmetik59@gmail.com'), +('biutidy@gmail.com'), +('brilianifridayasti@gmail.com'), +('budiondream@gmail.com'), +('business.qhansaanakkami@gmail.com'), +('cahaya.kosmetik.id@gmail.com'), +('cahyanidwisepti20@gmail.com'), +('cantikatya2@gmail.com'), +('catherinerambang620@gmail.com'), +('caty.adja@gmail.com'), +('cderz23@yahoo.com'), +('ceceliasansan8@gmail.com'), +('cerimartgo@gmail.com'), +('chandrahalim714@gmail.com'), +('chen.wong21@gmail.com'), +('cheryl.marsha.heryanto@gmail.com'), +('chococoip@gmail.com'), +('christianyendi@gmail.com'), +('cindyaharvardiana@gmail.com'), +('cinthyaruhantopp1@gmail.com'), +('cinthyaruhantopp3@gmail.com'), +('cisilia268@gmail.com'), +('clarysocimartinez@gmail.com'), +('cosellebeauty@gmail.com'), +('cvindokosmetikasejahtera@gmail.com'), +('d.lilacshop@gmail.com'), +('danielbudisantoso@gmail.com'), +('danielsimanjuntak231@gmail.com'), +('dedywijayamail@gmail.com'), +('definitionbeautyjogja@gmail.com'), +('dektasyaputra1301@gmail.com'), +('dellalisapaly@gmail.com'), +('despielia@gmail.com'), +('despielia92@gmail.com'), +('desyoong21@gmail.com'), +('deviannisme@gmail.com'), +('dewimellinda10@gmail.com'), +('dhevio1426@gmail.com'), +('dianakuliah@gmail.com'), +('dianps.haryanto@yahoo.com'), +('dillaparamita0@gmail.com'), +('dinaapril280@gmail.com'), +('dini.dwiarti3105@gmail.com'), +('dpsbykosme@gmail.com'), +('dwiagustin363@gmail.com'), +('dyas253@gmail.com'), +('dyfacafe@gmail.com'), +('eka.fokus@gmail.com'), +('eleonora@raenabeauty.com'), +('elfayunengsih20@gmail.com'), +('elisiabriana@gmail.com'), +('ellierahardja@gmail.com'), +('elsa.primasari.eps@gmail.com'), +('elsaokta26@gmail.com'), +('endahtriwedanti28@gmail.com'), +('endang.kosmetik1@gmail.com'), +('endangmarbunbbm678@gmail.com'), +('ennykoesrini676@gmail.com'), +('ennysetiono@gmail.com'), +('enokfatimah405@gmail.com'), +('entok345@gmail.com'), +('enungnurhalimah93@gmail.com'), +('envirolisasekar@gmail.com'), +('ernazulianita00@gmail.com'), +('estrellabeautee@gmail.com'), +('etienneskincare@gmail.com'), +('etisuningsih117@gmail.com'), +('evan.zelizta98@gmail.com'), +('evelyn.vio81@gmail.com'), +('evinrahmad4@gmail.com'), +('fajar950@gmail.com'), +('fajaroldman@gmail.com'), +('farah.fbindonesia@gmail.com'), +('faridahsalma@gmail.com'), +('febrina@raenabeauty.com'), +('feliceclarissa90@gmail.com'), +('fennyardisheila@gmail.com'), +('fera.julianti93@gmail.com'), +('feriscainez@gmail.com'), +('fffgh8@gmail.com'), +('fikrizia_insani@yahoo.com'), +('firabanun@gmail.com'), +('fitria_rida@yahoo.com'), +('fitriamalda123@gmail.com'), +('fitriand0192@gmail.com'), +('flavianusferdiaf@gmail.com'), +('flawless.officialshop@gmail.com'), +('flimtysurabayaofficial@gmail.com'), +('fmieayam@gmail.com'), +('freshnglow77@gmail.com'), +('friendhik25@gmail.com'), +('gabriastore@gmail.com'), +('gajahmadawaee@gmail.com'), +('galaticos02@hotmail.com'), +('gbjmarket@gmail.com'), +('givonsal@gmail.com'), +('glammarket64@gmail.com'), +('gloryharahap@gmail.com'), +('grosirmurahsukoharjo5@gmail.com'), +('guo_fengsheng@yahoo.co.id'), +('gustina.denaiyenti21@gmail.com'), +('hanselchan55@gmail.com'), +('hantarancilandak@gmail.com'), +('hardiantinila3@gmail.com'), +('hartono.henny@yahoo.com'), +('hasanyeong8@gmail.com'), +('helbert.simanjuntak@gmail.com'), +('hello.sheetmask@gmail.com'), +('hellopakbu@gmail.com'), +('helmikamanihuruk@gmail.com'), +('herdianametalic@gmail.com'), +('hestydharmayanti933@gmail.com'), +('heymarcell@gmail.com'), +('hijriahr08@gmail.com'), +('hulahulahali@gmail.com'), +('idcosmeticsidcosmetics@gmail.com'), +('ihc.korea@gmail.com'), +('ihccianjur@gmail.com'), +('iis.sugiati83@gmail.com'), +('ijallevine@gmail.com'), +('ikromyafik147@gmail.com'), +('imahgrosir@gmail.com'), +('indahpurwanti4410@gmail.com'), +('indrianimentaruk2@yahoo.com'), +('info.athirabeauty@gmail.com'), +('inggrid.kambuno@yahoo.com'), +('isbeyondstyle@gmail.com'), +('istrinyamalik@gmail.com'), +('iwanku203@gmail.com'), +('jacovcho@gmail.com'), +('jayaananda247@gmail.com'), +('jayde67@genesvjq.com'), +('jazzpro131@gmail.com'), +('jcrshop20@gmail.com'), +('jeliem96@gmail.com'), +('jelysoviana@gmail.com'), +('jesiskaruslan@gmail.com'), +('jessicaa2208@gmail.com'), +('jscang93@gmail.com'), +('jschrynt@gmail.com'), +('juliaviliajulia@gmail.com'), +('jullyangelina85@gmail.com'), +('jumrayana@gmail.com'), +('junidiarisna@gmail.com'), +('jwinarso889@gmail.com'), +('karinaagustin613@yahoo.co.id'), +('katalogskincarekorea@gmail.com'), +('keanoliem3@gmail.com'), +('kekensetiawan@gmail.com'), +('khairunniszahaifa@yahoo.com'), +('kikiseptiana92@gmail.com'), +('klikandshop18@gmail.com'), +('kntz199@gmail.com'), +('kosmetiklancarjaya@gmail.com'), +('kuboykukuh@gmail.com'), +('kunarifin@gmail.com'), +('kurniadi.phan@gmail.com'), +('kyuhaekoinline@gmail.com'), +('kyuhyunkoinline5@gmail.com'), +('lancarjayakosmetik@gmail.com'), +('lanisiska45@gmail.com'), +('laure.devienne@gmail.com'), +('lavoilla@gmail.com'), +('leejaeha31@gmail.com'), +('lenasinaga853@yahoo.com'), +('leo_azzuri@yahoo.com'), +('libbyclaudiar@gmail.com'), +('Liec534@gmail.com'), +('liedhawei@yahoo.com'), +('lina.marlinajkt228@gmail.com'), +('linamay2088@gmail.com'), +('lincebaiku34@gomail.com'), +('lindasekararum@gmail.com'), +('lindaten93@gmail.com'), +('lio.edhen@gmail.com'), +('lionachristina110887@gmail.com'), +('lolithaameliaa.la@gmail.com'), +('lolytha.arina@yahoo.co.id'), +('loverini1508@gmail.com'), +('lricosantoso@gmail.com'), +('luckyluna.id@gmail.com'), +('luckyme2611@gmail.com'), +('lusianaivang@yahoo.com'), +('luvpiggy2312@gmail.com'), +('lyannaming@gmail.com'), +('madya.wa@gmail.com'), +('mahesabeautyandstore@gmail.com'), +('maianton@yahoo.com'), +('mailto.wabisabi@gmail.com'), +('makeupfutura@gmail.com'), +('maramaratus@gmail.com'), +('marciaaact@gmail.com'), +('maria.ariesw87@gmail.com'), +('mariaboons0@gmail.com'), +('marianaulfa11@gmail.com'), +('mariiahulfah123@gmail.com'), +('marlinadiansafitri@gmail.com'), +('maroon16store@gmail.com'), +('marshandaditha@gmail.com'), +('martza.swastikasari@gmail.com'), +('maryani4398@gmail.com'), +('maryavictoryss3@gmail.com'), +('matrasunik@gmail.com'), +('maupesanposter@gmail.com'), +('maximlaundry@yahoo.com'), +('megashinta39@gmail.com'), +('mekarkosmetik.serang@gmail.com'), +('melaplestari2@gmail.com'), +('melindasuriyam01@gmail.com'), +('melisahariyanto14@gmail.com'), +('mentaripuspa59@gmail.com'), +('mh.lifestyles.in@gmail.com'), +('mikanmicheelen@gmail.com'), +('mirzaluzzia123@gmail.com'), +('misluciana@yahoo.com'), +('mitha2904aprilia@gmail.com'), +('mitufaya@gmail.com'), +('mitzukooutfit@gmail.com'), +('mizuharaaya@gmail.com'), +('mohd.nurfitriadin@gmail.com'), +('moildy.id@gmail.com'), +('monalisaflorist@gmail.com'), +('morashopmedan@gmail.com'), +('mr.yung30@gmail.com'), +('muhammad.hidayat.101@gmail.com'), +('mutiaazzahra177@gmail.com'), +('mutiarajesica0209@gmail.com'), +('mykagendis@gmail.com'), +('nadya.desnam@gmail.com'), +('nailavira102@gmail.com'), +('nanalistya22@gmail.com'), +('nandadevie1968@gmail.com'), +('nandipintaa@gmail.com'), +('nari38700@gmail.com'), +('natasyaputri4982@gmail.com'), +('naturrcosmetic@gmail.com'), +('niasepti28@gmail.com'), +('nicoags26@gmail.com'), +('nicolebeautyindonesia@gmail.com'), +('nicoyuganugraha@gmail.com'), +('ninadelinda26@gmail.com'), +('nirmala.sari709@gmail.com'), +('nonaewa@gmail.com'), +('nophaaelfrida@gmail.com'), +('nova.punya@gmail.com'), +('novandy.yohanes@gmail.com'), +('novi0128@gmail.com'), +('novitageraldinetgt@gmail.com'), +('nscollastica@gmail.com'), +('ntyaau26@gmail.com'), +('nuciferaamelia@gmail.com'), +('nugrahapiawai@gmail.com'), +('nur.wazilah@gmail.com'), +('nurazhwa170@gmail.com'), +('nurinasp@gmail.com'), +('nurlaelaramadanti@gmail.com'), +('officialbuyorbye@gmail.com'), +('oktinningrum@gmail.com'), +('opponingrum@gmail.com'), +('oyyisocial@gmail.com'), +('pajerokita@gmail.com'), +('pandridoge@gmail.com'), +('papamarvel_suhendy@yahoo.com'), +('pardosiares@gmail.com'), +('pastiibisaa@gmail.com'), +('pipitsanti999@gmail.com'), +('popoybunny@gmail.com'), +('prakososyam0403@gmail.com'), +('pramita2701@gmail.com'), +('prioritybekasi@gmail.com'), +('properbeauty.id@gmail.com'), +('psalimatulh@gmail.com'), +('puellabeautyshop@gmail.com'), +('puterifedina@gmail.com'), +('putriparapara@gmail.com'), +('putrisildosz@gmail.com'), +('putrivhidi@gmail.com'), +('putryalfionta@yahoo.com'), +('r.alika003@gmail.com'), +('r0n_ny@yahoo.com'), +('radhiah.hasan26@gmail.com'), +('rahayuwrdhn@gmail.com'), +('ranicantikhebat@gmail.com'), +('ratnashary4@gmail.com'), +('raynoldpanjaitan100@gmail.com'), +('regsmakeup@gmail.com'), +('resditamargaretha@gmail.com'), +('reviyuliswan86@gmail.com'), +('ribkanads@gmail.com'), +('richarddarmawanlaidji@gmail.com'), +('rickylie1989@gmail.com'), +('ries091@gmail.com'), +('rikiimpiandes@gmail.com'), +('rikomm822@gmail.com'), +('rinayunikartikaa@gmail.com'), +('rindaariani36@gmail.com'), +('riniismiyanti_94@yahoo.com'), +('ririalijofa@gmail.com'), +('riskalestari014@gmail.com'), +('rkcjawa@yahoo.com'), +('robeth.saputra01@gmail.com'), +('romlahabadsyam@gmail.com'), +('ronnywongsolim@gmail.com'), +('rovihshsh@gmail.com'), +('roviwardanap@gmail.com'), +('roviwardanaputra@gmail.com'), +('rperlita11@gmail.com'), +('rtjayaselalu@gmail.com'), +('rzkamll18@gmail.com'), +('sabithaanisa03@gmail.com'), +('saniaa3095@gmail.com'), +('saniyasch@gmail.com'), +('sansbeauty01@gmail.com'), +('santisujanto@gmail.com'), +('sarah@getvybes.co'), +('sari.astuty12@gmail.com'), +('sarihakim96@gmail.com'), +('sarmutias1@gmail.com'), +('sarmutias2@gmail.com'), +('sarmutias5@gmail.com'), +('seilareisya@gmail.com'), +('selfi.yonghwa08@gmail.com'), +('sendychristians@gmail.com'), +('sepriyanamelly@gmail.com'), +('serlinaepriyanti07@gmail.com'), +('setiawan.laia31@gmail.com'), +('setiawanandrew818@gmail.com'), +('sherlysherlyvi@gmail.com'), +('sholi.hijubpanda@gmail.com'), +('shopalover123@gmail.com'), +('shopbintar@gmail.com'), +('shoujoshopcosmetic@gmail.com'), +('siauwelizabeth@gmail.com'), +('sintafauziah41@gmail.com'), +('siti.lathifatul.k@gmail.com'), +('sitilathifatulkhofi@gmail.com'), +('siuni.reno@gmail.com'), +('siwiberlianti@gmail.com'), +('skincareindoid@gmail.com'), +('skinqueenproject@gmail.com'), +('smcosmeticsub@gmail.com'), +('snana602@gmail.com'), +('snrofi27@gmail.com'), +('soniadepi21@gmail.com'), +('sonie.usman@gmail.com'), +('sreejita@raenabeauty.com'), +('srialfia.san@gmail.com'), +('srihendrawati1@gmail.com'), +('sriyuliani9379@gmail.com'), +('stacia_andani91@yahoo.com'), +('staciaandani91@gmail.com'), +('stasyifa@gmail.com'), +('stefani_eli@ymail.com'), +('stepfanny91@gmail.com'), +('storemajujaya12@gmail.com'), +('sultonudin.madani21@gmail.com'), +('sun.edihtantra@gmail.com'), +('suryakosmetikjogja@gmail.com'), +('suryaniani1960@gmail.com'), +('susantiyap68@gmail.com'), +('suwandikarina61@gmail.com'), +('suwati169@gmail.com'), +('syahlaag@gmail.com'), +('syahliyaf@gmail.com'), +('syahna2128@gmail.com'), +('syahpudin09.183@gmail.com'), +('syifasahb@gmail.com'), +('tagustatang@gmail.com'), +('takiidonlineshopping@gmail.com'), +('talinazulfah@gmail.com'), +('tanmelky@yahoo.com'), +('taraaengg@gmail.com'), +('taspremium88@gmail.com'), +('taufikismail198720@gmail.com'), +('tcahyanti@gmail.com'), +('tfnhtp@gmail.com'), +('the_fortune_eo@yahoo.com'), +('theakirana133@gmail.com'), +('thebeausell@gmail.com'), +('thebeautyneed@gmail.com'), +('theidk.zone@gmail.com'), +('theskindarling@gmail.com'), +('tihanperkasadefitro@gmail.com'), +('tiifandi@gmail.com'), +('timyls@yahoo.com'), +('tina.metro@yahoo.com'), +('tioharianto.allianz@yahoo.com'), +('tjulovely2013@gmail.com'), +('toko.viragrosir88@gmail.com'), +('toko.winars.palopo@gmail.com'), +('tokounikita2@gmail.com'), +('tommy_priatama@ymail.com'), +('topupfu.sp@gmail.com'), +('tyabe888@yahoo.com'), +('ulibasagrc123@gmail.com'), +('umarsikumbang2020@icloud.com'), +('vannychristinna@yahoo.com'), +('varenunggulkarya@gmail.com'), +('veliakatarina@gmail.com'), +('velvetpinkred@gmail.com'), +('vhex307@gmail.com'), +('vidiachen@yahoo.com'), +('vilencyviranyy@gmail.com'), +('vina.astini@yahoo.com'), +('vincenttia23@gmail.com'), +('vinlie2020@gmail.com'), +('violyamerin@gmail.com'), +('viraltoday124@gmail.com'), +('virginiaca09@gmail.com'), +('viserysyvaine@gmail.com'), +('viviyuvita19@gmail.com'), +('wahyudiirwan35@gmail.com'), +('wahyuekajaya@gmail.com'), +('wahyuyana.prasetya@gmail.com'), +('wellington@nihonmart.id'), +('wellypandin76@gmail.com'), +('widoagusputra90@gmail.com'), +('wiwiekapsari@icloud.com'), +('wulan3804@gmail.com'), +('wulandary.sllh@gmail.com'), +('xenpang26@gmail.com'), +('xuanliuli0@gmail.com'), +('yayarafin@gmail.com'), +('yenimaryatin332@gmail.com'), +('yenny.irawati@yahoo.com'), +('yessicasarano16@gmail.com'), +('yogialqarni1807@gmail.com'), +('yogisugianto21@gmail.com'), +('yong2.chan@gmail.com'), +('yooabeaute@gmail.com'), +('ysurjadi@gmail.com'), +('yt40gt@gmail.com'), +('yukireiyuka@gmail.com'), +('yuliacandle@gmail.com'), +('yuliananina19@yahoo.com'), +('yunisri.hartiwi@yahoo.com'), +('yunitadewi23@gmail.com'), +('zabeth_yulie@yahoo.co.id'), +('zahraashop18@gmail.com'), +('zahrabeauty.share@gmail.com'), +('zelmi5009@gmail.com'), +('zharasonline@gmail.com'), +('zivenakbar08@gmail.com'), +('Fanamaliauthor@gmail.com'), +('tjuganteng@gmailcom'), +('ayusisicantik@gmail.com'), +('tsetiyaningsih71@gmail.com'), +('oyyiapple@gmail.com'), +('angellim0311@yahoo.com'), +('fericoridwan@gmail.com'), +('Ikasusanti_story@yahoo.com'), +('hamkyu359@gmail.com'); + + +DROP TABLE raena_analytics.SKU_base_recommendation_base; + +CREATE TABLE raena_analytics.SKU_base_recommendation_base AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum(total_payment_price) total_payment_price, + sum(total_cogs)total_cogs, + ((sum(retail_price)-sum(total_cogs))/sum(retail_price))*100 Pre_discount_gm +FROM + (SELECT sku, + product_type, + sum(quantity)quantity, + sum((retail_price-seller_margin)*quantity) total_payment_price, + sum(cogs*quantity)total_cogs, + sum((retail_price-seller_margin)*quantity)retail_price + FROM raena_analytics.gm_dashboard + WHERE transaction_date::date>cast('$reportDate' AS date)-interval'90 days' + AND cogs IS NOT NULL + AND cogs>0 + GROUP BY sku, + product_type)A +INNER JOIN + (SELECT sku , + category_id, + brand_id, + name + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B ON A.sku = B.sku +INNER JOIN + (SELECT id , + name + FROM raena_catalog_management.category) C ON B.category_id = C.id +INNER JOIN + (SELECT id , + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id +GROUP BY 1, + 2, + 3, + 4, + 5 ; + + +DROP TABLE raena_analytics.reseller_base_recommendation_base; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base AS +SELECT reseller_id , + A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum(total_payment_price) total_payment_price, + sum(total_cogs)total_cogs, + ((sum(retail_price)-sum(total_cogs))/sum(retail_price))*100 Pre_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM (SELECT sku,reseller_id , +reseller_email, + product_type, + sum(quantity)quantity, + sum((retail_price-seller_margin)*quantity) total_payment_price, + sum(cogs*quantity)total_cogs, + sum((retail_price-seller_margin)*quantity)retail_price + FROM raena_analytics.gm_dashboard + WHERE transaction_date::date>cast('$reportDate' AS date)-interval'90 days' + AND cogs IS NOT NULL + AND cogs>0 + AND reseller_id IS NOT NULL + GROUP BY sku, + product_type,reseller_id,reseller_email ) A +INNER JOIN (SELECT sku , + category_id, + brand_id, + name + FROM raena_catalog_management.product) B ON A.sku = B.sku +INNER JOIN (SELECT id , + name + FROM raena_catalog_management.category) C ON B.category_id = C.id +INNER JOIN (SELECT id , + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id +INNER JOIN raena_analytics.recommendation_power_seller_base BB ON A.reseller_email = BB.email +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_stage1; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_stage1 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>=10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM raena_analytics.reseller_base_recommendation_base; + +DROP TABLE IF EXISTS raena_analytics.raw_scraping_data; + + + +CREATE TABLE raena_analytics.raw_scraping_data AS +SELECT E.raena_product_sku, + F.name SKU_name, + E.rce_product_id , + E.rce_variant_id, + A.category_name , + A.brand_name , + A.product_page_url , + rce_source_product_name, + product_variant_name, + product_variant_price, + product_variant_price_before_discount, + product_sold_total, + product_sold, + product_price_min, + product_price_min_before_discount, + product_price_max, + product_price_max_before_discount, + cast(percentage_match AS decimal(22,2)) percentage_match, + ratings +FROM raena_analytics.raena_shopee_product_mapping E +LEFT JOIN + (SELECT A.id rce_product_id, + D.id rce_product_varient_id , + B.category_name , + C.brand_name , + product_page_url , + rce_source_product_name, + product_variant_name, + product_variant_price/10000 product_variant_price, + product_variant_price_before_discount/10000 product_variant_price_before_discount, + product_sold_total, + product_sold, + product_price_min/10000 product_price_min, + product_price_min_before_discount/10000 product_price_min_before_discount, + product_price_max/10000 product_price_max, + product_price_max_before_discount/10000 product_price_max_before_discount, + ratings + FROM raena_spider_management.rce_product A + LEFT JOIN raena_spider_management.rce_category B ON A.rce_category_id = B.id + LEFT JOIN raena_spider_management.rce_brand C ON A.rce_brand_id = cast(C.id as varchar) + LEFT JOIN raena_spider_management.rce_product_variant D ON A.id = D.rce_product_id) A ON E.rce_product_id = cast(A.rce_product_id as varchar) +AND cast(replace(E.rce_variant_id,'.0','') AS varchar) = cast(replace(cast(A.rce_product_varient_id as varchar),',','') AS varchar) +LEFT JOIN raena_catalog_management.product F ON E.raena_product_sku=F.sku ; + + +DROP TABLE IF EXISTS raena_analytics.raw_scraping_data_stage ; + + +CREATE TABLE raena_analytics.raw_scraping_data_stage AS +SELECT A.* +FROM raena_analytics.raw_scraping_data A +INNER JOIN + (SELECT raena_product_sku, + max(percentage_match) percentage_match + FROM raena_analytics.raw_scraping_data + GROUP BY 1) B ON A.raena_product_sku = B.raena_product_sku +AND A.percentage_match =B.percentage_match; + + +DROP TABLE IF EXISTS raena_analytics.sku_sales_factor; + + +CREATE TABLE raena_analytics.sku_sales_factor AS +SELECT SKU, + cast(cast(sum(B.total_quantity*3) AS decimal(22,2))/cast(SUM(A.total_quantity) AS decimal(22,2)) AS decimal(22,2)) sale_factor +FROM raena_analytics.SKU_base_recommendation_base A +LEFT JOIN + (SELECT raena_product_sku, + sum(DISTINCT product_sold) total_quantity + FROM raena_analytics.raw_scraping_data_stage + GROUP BY 1) B ON A.sku = B.raena_product_sku +WHERE A.Pre_discount_gm>=10 +GROUP BY 1; + + +DROP TABLE IF EXISTS raena_analytics.recommendation_on_low_sku_task1; + + +CREATE TABLE raena_analytics.recommendation_on_low_sku_task1 AS +SELECT C.mobile Reseller_mobile , + C.email reseller_email, + A.sku, + A.sku_name, + A.brand_name, + A.category_name , + A.product_type , + Total_quantity, + total_payment_price, + total_cogs, + A.pre_discount_gm, + coalesce(B.sale_factor,0) sale_factor, + A.pre_discount_gm*coalesce(B.sale_factor,1) sorting +FROM + (SELECT * + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE Gm_type = 'DDB') A +INNER JOIN + (SELECT mobile, + email, + id + FROM raena_user_management.user) C ON A.reseller_id = cast(C.id AS varchar) +INNER JOIN + (SELECT sku , + pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base) D ON A.sku = D.sku +AND D.pre_discount_gm >=10 +INNER JOIN raena_analytics.sku_in_stock E ON A.sku = E.sku +AND E.in_stock >=10 +LEFT JOIN raena_analytics.sku_sales_factor B ON A.sku = B.sku; +---------------------------------------------task 2 --------------- + + + + + +DROP TABLE raena_analytics.SKU_base_recommendation_base_v2; + + +CREATE TABLE raena_analytics.SKU_base_recommendation_base_v2 AS +SELECT A.sku, + ((sum((A.retail_price-seller_margin) *quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM raena_analytics.gm_dashboard A +WHERE transaction_date::date>cast('$reportDate' as date)-interval'90 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND tier_name ='GOLD' +GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.recommendation_base_historical_data_offender; + + +CREATE TABLE raena_analytics.recommendation_base_historical_data_offender AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE gm_type ='Offender') A +LEFT JOIN raena_analytics.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN raena_analytics.sku_sales_factor C ON B.mapping_group_base = C.sku +LEFT JOIN raena_analytics.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND Pre_discount_gm>=10 +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM + (SELECT sku, + name, + product_type, + category_id, + brand_id + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.category) C ON B.category_id = C.id + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id ) E ON B.mapping_group_base= E.SKU; + + + DROP TABLE IF EXISTS raena_analytics.recommendation_base_historical_data_ddb; + + +CREATE TABLE raena_analytics.recommendation_base_historical_data_ddb AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + A.Pre_discount_gm reseller_pre_discount_gm, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + D.Pre_discount_gm, + in_stock +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type, + Pre_discount_gm + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE gm_type ='DDB') A +LEFT JOIN raena_analytics.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN raena_analytics.sku_sales_factor C ON A.SKU = C.sku +LEFT JOIN raena_analytics.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND D.Pre_discount_gm>=10 +LEFT JOIN raena_analytics.sku_in_stock F ON B.mapping_group_base = F.sku +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM + (SELECT sku, + name, + product_type, + category_id, + brand_id + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.category) C ON B.category_id = C.id + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id ) E ON B.mapping_group_base= E.SKU; + + DROP TABLE raena_analytics.recommentdation_offender_sku_concern; + + +CREATE TABLE raena_analytics.recommentdation_offender_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_offender B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_concern_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC; + + + DROP TABLE raena_analytics.recommentdation_offender_sku_ingredient; + + +CREATE TABLE raena_analytics.recommentdation_offender_sku_ingredient AS +SELECT reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_offender B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +AND recommended_ingredient_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC ; + + + DROP TABLE IF EXISTS raena_analytics.recommentdation_ddb_sku_concern ; + + +CREATE TABLE raena_analytics.recommentdation_ddb_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_concern_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE in_stock>50 and reseller_pre_discount_gm>10; + +DROP TABLE IF EXISTS raena_analytics.recommentdation_ddb_sku_ingredient ; + + +CREATE TABLE raena_analytics.recommentdation_ddb_sku_ingredient AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_ingredient_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE in_stock>50 +and reseller_pre_discount_gm >=10; + +DROP TABLE raena_analytics.recommentdation_sku_ingredient; + + +CREATE TABLE raena_analytics.recommentdation_sku_ingredient AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + Recommended_pre_discount_gm, + sale_factor , + (Ingredient_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm) sorting, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + cast(sale_factor AS decimal(22,2)) sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, tier_price::json->> 'bf645e97-8a48-4977-8367-e987489760f9' tier_price from raena_catalog_management.product) BB on AA.Recommended_sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + + +DROP TABLE raena_analytics.recommentdation_sku_ingredient_purchase; + + +CREATE TABLE raena_analytics.recommentdation_sku_ingredient_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(tier_price AS decimal(22,2))gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku, + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, tier_price::json->> 'bf645e97-8a48-4977-8367-e987489760f9' tier_price from raena_catalog_management.product) BB on AA.sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + + +DROP TABLE raena_analytics.recommentdation_sku_concern; + + +CREATE TABLE raena_analytics.recommentdation_sku_concern AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + Recommended_pre_discount_gm, + sale_factor , + concern_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm sorting, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, tier_price::json->> 'bf645e97-8a48-4977-8367-e987489760f9' tier_price from raena_catalog_management.product) BB on AA.Recommended_sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + +DROP TABLE raena_analytics.recommentdation_sku_concern_purchase; + + +CREATE TABLE raena_analytics.recommentdation_sku_concern_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, tier_price::json->> 'bf645e97-8a48-4977-8367-e987489760f9' tier_price from raena_catalog_management.product) BB on AA.sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + -----------------------------------------------Task 3 -------------------------------(completed) +--inner join (select distinct id as order_id , json_extract_path_text(reseller_info,'province',TRUE) reseller_province +-- from raena_order_management.order +-- where created_at::date>dateadd(DAY,-91,'$reportDate') and payment_status='Paid') AA on A.external_id = AA.order_id + + +DROP TABLE raena_analytics.SKU_base_recommendation_base_t3 ; + + +CREATE TABLE raena_analytics.SKU_base_recommendation_base_t3 AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + shipping_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM raena_analytics.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +WHERE transaction_date::date>cast('$reportDate' as date)-interval'90 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + and (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false' +GROUP BY 1, + 2, + 3, + 4, + 5, + 6 ; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_t3; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_t3 AS +SELECT reseller_id , + A.sku, + case when shipping_to ='Customer' then shipping_province end as dropshipping_province, + case when shipping_to ='Reseller' then shipping_province end as reseller_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin) *quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Post_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM raena_analytics.gm_dashboard A +INNER JOIN + (SELECT DISTINCT id AS order_id , + shipping_to + FROM raena_order_management.order + WHERE created_at::date>cast('$reportDate' as date)-interval'180 days' + AND payment_status='Paid') AA ON A.external_id = AA.order_id +INNER JOIN raena_analytics.recommendation_power_seller_base BB ON A.reseller_email = BB.email +WHERE transaction_date::date>cast('$reportDate' as date)-interval'180 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND reseller_id IS NOT NULL + AND order_placed_by ='Admin Panel' + AND shipping_to <> 'Marketplace' +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_stage1_t3; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_stage1_t3 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM raena_analytics.reseller_base_recommendation_base_t3; + + +drop table if exists raena_analytics.reseller_base_recommendation_base_stage1_t3_v1; + +create table raena_analytics.reseller_base_recommendation_base_stage1_t3_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm, + in_stock + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6,9; + + drop table if exists raena_analytics.reseller_base_recommendation_base_stage1_t3_v2; + +create table raena_analytics.reseller_base_recommendation_base_stage1_t3_v2 +as + SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku total_quantity_sku , + B.Pre_discount_gm + FROM (select distinct reseller_id , + sku, + dropshipping_province + from raena_analytics.reseller_base_recommendation_base_stage1_t3 + where dropshipping_province is not null ) A + LEFT JOIN raena_analytics.reseller_base_recommendation_base_stage1_t3_v1 B + ON A.dropshipping_province = B.shipping_province ; + + + +DROP TABLE raena_analytics.same_province_with_diff_sku_dropshipping; + + + +CREATE TABLE raena_analytics.same_province_with_diff_sku_dropshipping AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk +from raena_analytics.reseller_base_recommendation_base_stage1_t3_v2 A + WHERE A.sku <> A.recommended_sku and dropshipping_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + + --------------- + DROP TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM raena_analytics.same_province_with_diff_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + +drop table if exists raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1; + +create table raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +DROP TABLE IF EXISTS raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2 AS +SELECT A.reseller_id , + A.sku , + A.reseller_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm +FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A +LEFT JOIN raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1 B ON A.reseller_province = B.shipping_province +WHERE A.reseller_province IS NOT NULL; + + +DROP TABLE raena_analytics.same_province_with_diff_sku_reseller; + + +CREATE TABLE raena_analytics.same_province_with_diff_sku_reseller AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2 A +WHERE A.sku <> A.recommended_sku and A.reseller_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + +---- + + +DROP TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM raena_analytics.same_province_with_diff_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + +drop table if exists raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1; + +create table raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE IF EXISTS raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2 AS +SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm +FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A +LEFT JOIN raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1 B ON A.dropshipping_province = B.shipping_province +WHERE A.dropshipping_province IS NOT NULL; + +DROP TABLE raena_analytics.same_province_with_same_sku_dropshipping; + + +CREATE TABLE raena_analytics.same_province_with_same_sku_dropshipping AS +SELECT * +FROM + (SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition BY reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk + FROM raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2 A + WHERE sku = recommended_sku + AND A.dropshipping_province IS NOT NULL) AA +WHERE rnk<11 +ORDER BY sku , + rnk; + +DROP TABLE raena_analytics.recommendation_same_province_with_same_sku_dropshipping; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_same_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM raena_analytics.same_province_with_same_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + + +drop table if exists raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1; + +create table raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +drop table if exists raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2; + +create table raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2 +as +SELECT A.reseller_id , + A.sku , + A.reseller_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm + FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1 B + ON A.reseller_province = B.shipping_province where A.reseller_province is not null ; + + + DROP TABLE raena_analytics.same_province_with_same_sku_reseller; + + +CREATE TABLE raena_analytics.same_province_with_same_sku_reseller AS +select * from ( +SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2 A +WHERE sku = recommended_sku and A.reseller_province is not null )AA + where rnk<11 +ORDER BY sku , + rnk; + + DROP TABLE raena_analytics.recommendation_same_province_with_same_sku_reseller; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_same_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM raena_analytics.same_province_with_same_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + + +" > /home/ec2-user/cronjob/postgresql/am_recommendation/am_recommendation_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/am_recommendation/am_recommendation_etl.sql + diff --git a/postgresql/am_recommendation/whatsapp_campaign.sh b/postgresql/am_recommendation/whatsapp_campaign.sh new file mode 100644 index 0000000..53e2b9d --- /dev/null +++ b/postgresql/am_recommendation/whatsapp_campaign.sh @@ -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 + diff --git a/postgresql/am_recommendation/whatsapp_campaign.sql b/postgresql/am_recommendation/whatsapp_campaign.sql new file mode 100644 index 0000000..68d09d2 --- /dev/null +++ b/postgresql/am_recommendation/whatsapp_campaign.sql @@ -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 ; + + diff --git a/postgresql/appflyer/appflyer_etl.sh b/postgresql/appflyer/appflyer_etl.sh new file mode 100644 index 0000000..3878640 --- /dev/null +++ b/postgresql/appflyer/appflyer_etl.sh @@ -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 diff --git a/postgresql/appflyer/appflyer_etl.sql b/postgresql/appflyer/appflyer_etl.sql new file mode 100644 index 0000000..c16a8c8 --- /dev/null +++ b/postgresql/appflyer/appflyer_etl.sql @@ -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; + + diff --git a/postgresql/appflyer/etl_inappevent_appsflyer.sql b/postgresql/appflyer/etl_inappevent_appsflyer.sql new file mode 100644 index 0000000..83bdca0 --- /dev/null +++ b/postgresql/appflyer/etl_inappevent_appsflyer.sql @@ -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; + + diff --git a/postgresql/appflyer/in_app_appsflyer_etl.sh b/postgresql/appflyer/in_app_appsflyer_etl.sh new file mode 100644 index 0000000..b934967 --- /dev/null +++ b/postgresql/appflyer/in_app_appsflyer_etl.sh @@ -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 + diff --git a/postgresql/card_abandment/cart_abandment_etl.sh b/postgresql/card_abandment/cart_abandment_etl.sh new file mode 100644 index 0000000..8279fd2 --- /dev/null +++ b/postgresql/card_abandment/cart_abandment_etl.sh @@ -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 + diff --git a/postgresql/card_abandment/cart_abandment_etl.sql b/postgresql/card_abandment/cart_abandment_etl.sql new file mode 100644 index 0000000..f84a31e --- /dev/null +++ b/postgresql/card_abandment/cart_abandment_etl.sql @@ -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; + + diff --git a/postgresql/city_mapping_for_online_lead_db.sh b/postgresql/city_mapping_for_online_lead_db.sh new file mode 100644 index 0000000..14c482d --- /dev/null +++ b/postgresql/city_mapping_for_online_lead_db.sh @@ -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 + diff --git a/postgresql/city_mapping_for_online_lead_db.sql b/postgresql/city_mapping_for_online_lead_db.sql new file mode 100644 index 0000000..2c726ea --- /dev/null +++ b/postgresql/city_mapping_for_online_lead_db.sql @@ -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 ; + + + + diff --git a/postgresql/cogs_alert/cogs_increase_alert.sh b/postgresql/cogs_alert/cogs_increase_alert.sh new file mode 100644 index 0000000..1f50d99 --- /dev/null +++ b/postgresql/cogs_alert/cogs_increase_alert.sh @@ -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 diff --git a/postgresql/cogs_alert/increase_cogs_alert.sql b/postgresql/cogs_alert/increase_cogs_alert.sql new file mode 100644 index 0000000..c634e21 --- /dev/null +++ b/postgresql/cogs_alert/increase_cogs_alert.sql @@ -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; + + diff --git a/postgresql/consignement/consignment_sales_report.sh b/postgresql/consignement/consignment_sales_report.sh new file mode 100644 index 0000000..9da9218 --- /dev/null +++ b/postgresql/consignement/consignment_sales_report.sh @@ -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 diff --git a/postgresql/consignement/consignment_sales_report.sql b/postgresql/consignement/consignment_sales_report.sql new file mode 100644 index 0000000..11f5b07 --- /dev/null +++ b/postgresql/consignement/consignment_sales_report.sql @@ -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 ; + + + diff --git a/postgresql/consignement/pwd b/postgresql/consignement/pwd new file mode 100644 index 0000000..e69de29 diff --git a/postgresql/demand_forecasting/Demand_forecasting.sql b/postgresql/demand_forecasting/Demand_forecasting.sql new file mode 100644 index 0000000..a4716a2 --- /dev/null +++ b/postgresql/demand_forecasting/Demand_forecasting.sql @@ -0,0 +1,997 @@ + +--------------------------Order Level Data -------------------------- +Drop table if exists raena_analytics.Gm_final_table; + +CREATE TABLE raena_analytics.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 raena_analytics.gm_dashboard A +LEFT JOIN raena_catalog_management.product B ON A.sku = B.sku; + +Drop table if exists raena_analytics.final_order_level_data; + +CREATE TABLE raena_analytics.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 raena_analytics.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) >=(cast('2023-11-21' as date)- INTERVAL '120 day')) B ON external_id =id +WHERE discounted_price <>0 and cast(transaction_date AS date) between (cast('2023-11-21' as date)- INTERVAL '120 day') and cast('2023-11-21' as date); + + -------------------------------------------SKU Level Data --------------------------------- + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage1; + +CREATE TABLE raena_analytics.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 (cast('2023-11-21' as date)- INTERVAL '6 day') AND CAST('2023-11-21' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '14 day') AND (cast('2023-11-21' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '20 day') AND (cast('2023-11-21' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '27 day') AND (cast('2023-11-21' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '34 day') AND (cast('2023-11-21' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '41 day') AND (cast('2023-11-21' as date)- INTERVAL '35 day') 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 raena_analytics.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 (cast('2023-11-21' as date)- INTERVAL '6 day') AND CAST('2023-11-21' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '14 day') AND (cast('2023-11-21' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '20 day') AND (cast('2023-11-21' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '27 day') AND (cast('2023-11-21' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '34 day') AND (cast('2023-11-21' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '41 day') AND (cast('2023-11-21' as date)- INTERVAL '35 day') THEN 'L42D' + END Bucket, + sum(coalesce (revenue_on_after_discount,0)) Total_revenue + FROM raena_analytics.final_order_level_data + WHERE cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '41 day') AND cast('2023-11-21' as date) + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '6 day') AND CAST('2023-11-21' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '14 day') AND (cast('2023-11-21' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '20 day') AND (cast('2023-11-21' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '27 day') AND (cast('2023-11-21' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '34 day') AND (cast('2023-11-21' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '41 day') AND (cast('2023-11-21' as date)- INTERVAL '35 day') 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 (cast('2023-11-21' as date)- INTERVAL '6 day') AND CAST('2023-11-21' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '14 day') AND (cast('2023-11-21' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '20 day') AND (cast('2023-11-21' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '27 day') AND (cast('2023-11-21' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '34 day') AND (cast('2023-11-21' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '41 day') AND (cast('2023-11-21' as date)- INTERVAL '35 day') 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 (cast('2023-11-21' as date)- INTERVAL '41 day') AND cast('2023-11-21' as date) +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 (cast('2023-11-21' as date)- INTERVAL '6 day') AND CAST('2023-11-21' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '14 day') AND (cast('2023-11-21' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '20 day') AND (cast('2023-11-21' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '27 day') AND (cast('2023-11-21' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '34 day') AND (cast('2023-11-21' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '41 day') AND (cast('2023-11-21' as date)- INTERVAL '35 day') THEN 'L42D' + END , + Total_revenue; + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage2; + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage1 +WHERE warehouse IS NOT NULL +GROUP BY sku, + warehouse ; + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage3; + + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage2; + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_final; + + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage3; + +--------------------------------------Warehouse SKu Split---------------------------------- + + +DROP TABLE IF EXISTS raena_analytics.sku_level_data ; + +CREATE TABLE raena_analytics.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(cast(slashed_tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' as varchar) AS decimal(22,2)) + ELSE cast(cast(tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' as varchar) 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 raena_analytics.storage_data B ON A.sku=B.sku +left join raena_analytics.demand_forecasting_configs G on A.sku= G.sku +LEFT JOIN raena_analytics.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 raena_analytics.sku_cogs_audit sca where sku_cogs_action='CREATED') F ON A.sku= F.sku_code ; +---------------------------_basic Calculation ------------------------- + +DROP TABLE IF EXISTS raena_analytics.basic_calculation; + + +CREATE TABLE raena_analytics.basic_calculation AS +SELECT sku , + CASE + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '30 day') AND cast('2023-11-21' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '60 day') AND (cast('2023-11-21' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '90 day') AND (cast('2023-11-21' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '120 day') AND (cast('2023-11-21' as date)- INTERVAL '91 day') 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 raena_analytics.final_order_level_data A +LEFT JOIN + (SELECT CASE + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '30 day') AND cast('2023-11-21' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '60 day') AND (cast('2023-11-21' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '90 day') AND (cast('2023-11-21' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '120 day') AND (cast('2023-11-21' as date)- INTERVAL '91 day') THEN 'L120D' + END txn_period, + sum(coalesce (revenue_on_after_discount,0)) Total_revenue + FROM raena_analytics.final_order_level_data + WHERE cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '120 day') AND cast('2023-11-21' as date) + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '30 day') AND cast('2023-11-21' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '60 day') AND (cast('2023-11-21' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '90 day') AND (cast('2023-11-21' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '120 day') AND (cast('2023-11-21' as date)- INTERVAL '91 day') THEN 'L120D' + END) B ON (CASE + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '30 day') AND cast('2023-11-21' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '60 day') AND (cast('2023-11-21' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '90 day') AND (cast('2023-11-21' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '120 day') AND (cast('2023-11-21' as date)- INTERVAL '91 day') THEN 'L120D' + END)=B.txn_period +WHERE cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '120 day') AND cast('2023-11-21' as date) +GROUP BY sku, + CASE + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '30 day') AND cast('2023-11-21' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '60 day') AND (cast('2023-11-21' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '90 day') AND (cast('2023-11-21' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('2023-11-21' as date)- INTERVAL '120 day') AND (cast('2023-11-21' as date)- INTERVAL '91 day') THEN 'L120D' + END, + Total_revenue; + +select 1; + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage1; + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation + GROUP BY sku) A +LEFT JOIN +(SELECT sku , + cast(cast(tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' as varchar) AS decimal(22,2)) gold_price + FROM raena_catalog_management.product p) B ON A.sku = B.sku; + + +Drop table if exists raena_analytics.basic_calculation_stage1_v1; + +create table raena_analytics.basic_calculation_stage1_v1 as +select A.*, + abs(contribution-median_contribution) for_max_contribution +from raena_analytics.basic_calculation A +left join (select sku , + PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY contribution) median_contribution + from raena_analytics.basic_calculation group by sku ) B + on A.sku = B.sku ; + + + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage2; + + +CREATE TABLE raena_analytics.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, + PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY 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 raena_analytics.basic_calculation_stage1_v1 A +LEFT JOIN raena_analytics.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 raena_analytics.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 raena_analytics.basic_calculation_stage2 + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage3; + +/* +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation A +WHERE txn_period <> 'L120D' +GROUP BY A.SKu,txn_period; +*/ + + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation A +left join (select distinct sku , runrate from raena_analytics.basic_calculation where txn_period = 'L30D' ) B on A.sku = B.sku +WHERE txn_period <> 'L120D' +GROUP BY A.SKu; + + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage4; + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage2 A +LEFT JOIN raena_analytics.basic_calculation_stage3 B ON A.sku = B.sku; + + +--SELECT * FROM raena_analytics.basic_calculation_stage4; + +--select * from raena_analytics.basic_calculation_stage5 + +drop table if exists raena_analytics.basic_calculation_stage5; + +CREATE TABLE raena_analytics.basic_calculation_stage5 +as +select sku , percentile_cont(0.5) within +GROUP ( + ORDER BY Max_of_qty) Max_of_qty , Avg(Max_of_qty) Avg_of_quantiy +from +(SELECT sku , + qty1_median_rev_contribution Max_of_qty +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty2_avg_runrate +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty3_avg_quantity +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty4_expected_revenue_target +FROM raena_analytics.basic_calculation_stage4) A +group by sku; + +drop table if exists raena_analytics.basic_calculation_stage6; + +create table raena_analytics.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 raena_analytics.sku_warehouse_split_final B + group by sku; + +drop table if exists raena_analytics.final_basic_calucation_table; + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage4 A +LEFT JOIN raena_analytics.basic_calculation_stage6 B ON A.sku =B.sku +LEFT join raena_analytics.basic_calculation_stage5 C on A.sku=C.sku; + +drop table if exists raena_analytics.inbound_data; + +create table raena_analytics.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-21' 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 raena_analytics.warehouse_in_stock; + +create table raena_analytics.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 raena_analytics.demand_forecast_dashboard_base; + + +CREATE TABLE raena_analytics.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) + END0 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 raena_analytics.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) >=(cast('$reportDate' as date)- INTERVAL '120 day')) B ON external_id =id +WHERE discounted_price <>0 and cast(transaction_date AS date) between (cast('$reportDate' as date)- INTERVAL '120 day') and cast('$reportDate' as date); + + -------------------------------------------SKU Level Data --------------------------------- + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage1; + +CREATE TABLE raena_analytics.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 (cast('$reportDate' as date)- INTERVAL '6 day') AND CAST('$reportDate' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '14 day') AND (cast('$reportDate' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '20 day') AND (cast('$reportDate' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '27 day') AND (cast('$reportDate' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '34 day') AND (cast('$reportDate' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '41 day') AND (cast('$reportDate' as date)- INTERVAL '35 day') 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 raena_analytics.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 (cast('$reportDate' as date)- INTERVAL '6 day') AND CAST('$reportDate' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '14 day') AND (cast('$reportDate' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '20 day') AND (cast('$reportDate' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '27 day') AND (cast('$reportDate' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '34 day') AND (cast('$reportDate' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '41 day') AND (cast('$reportDate' as date)- INTERVAL '35 day') THEN 'L42D' + END Bucket, + sum(coalesce (revenue_on_after_discount,0)) Total_revenue + FROM raena_analytics.final_order_level_data + WHERE cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '41 day') AND cast('$reportDate' as date) + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '6 day') AND CAST('$reportDate' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '14 day') AND (cast('$reportDate' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '20 day') AND (cast('$reportDate' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '27 day') AND (cast('$reportDate' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '34 day') AND (cast('$reportDate' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '41 day') AND (cast('$reportDate' as date)- INTERVAL '35 day') 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 (cast('$reportDate' as date)- INTERVAL '6 day') AND CAST('$reportDate' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '14 day') AND (cast('$reportDate' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '20 day') AND (cast('$reportDate' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '27 day') AND (cast('$reportDate' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '34 day') AND (cast('$reportDate' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '41 day') AND (cast('$reportDate' as date)- INTERVAL '35 day') 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 (cast('$reportDate' as date)- INTERVAL '41 day') AND cast('$reportDate' as date) +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 (cast('$reportDate' as date)- INTERVAL '6 day') AND CAST('$reportDate' AS DATE) THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '14 day') AND (cast('$reportDate' as date)- INTERVAL '7 day') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '20 day') AND (cast('$reportDate' as date)- INTERVAL '14 day') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '27 day') AND (cast('$reportDate' as date)- INTERVAL '21 day') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '34 day') AND (cast('$reportDate' as date)- INTERVAL '28 day') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '41 day') AND (cast('$reportDate' as date)- INTERVAL '35 day') THEN 'L42D' + END , + Total_revenue; + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage2; + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage1 +WHERE warehouse IS NOT NULL +GROUP BY sku, + warehouse ; + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage3; + + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage2; + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_final; + + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage3; + +--------------------------------------Warehouse SKu Split---------------------------------- + + +DROP TABLE IF EXISTS raena_analytics.sku_level_data ; + +CREATE TABLE raena_analytics.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(cast(slashed_tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' as varchar) AS decimal(22,2)) + ELSE cast(cast(tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' as varchar) 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 raena_analytics.storage_data B ON A.sku=B.sku +left join raena_analytics.demand_forecasting_configs G on A.sku= G.sku +LEFT JOIN raena_analytics.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 raena_analytics.sku_cogs_audit sca where sku_cogs_action='CREATED') F ON A.sku= F.sku_code ; +---------------------------_basic Calculation ------------------------- + +DROP TABLE IF EXISTS raena_analytics.basic_calculation; + + +CREATE TABLE raena_analytics.basic_calculation AS +SELECT sku , + CASE + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '30 day') AND cast('$reportDate' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '60 day') AND (cast('$reportDate' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '90 day') AND (cast('$reportDate' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '120 day') AND (cast('$reportDate' as date)- INTERVAL '91 day') 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 raena_analytics.final_order_level_data A +LEFT JOIN + (SELECT CASE + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '30 day') AND cast('$reportDate' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '60 day') AND (cast('$reportDate' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '90 day') AND (cast('$reportDate' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '120 day') AND (cast('$reportDate' as date)- INTERVAL '91 day') THEN 'L120D' + END txn_period, + sum(coalesce (revenue_on_after_discount,0)) Total_revenue + FROM raena_analytics.final_order_level_data + WHERE cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '120 day') AND cast('$reportDate' as date) + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '30 day') AND cast('$reportDate' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '60 day') AND (cast('$reportDate' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '90 day') AND (cast('$reportDate' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '120 day') AND (cast('$reportDate' as date)- INTERVAL '91 day') THEN 'L120D' + END) B ON (CASE + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '30 day') AND cast('$reportDate' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '60 day') AND (cast('$reportDate' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '90 day') AND (cast('$reportDate' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '120 day') AND (cast('$reportDate' as date)- INTERVAL '91 day') THEN 'L120D' + END)=B.txn_period +WHERE cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '120 day') AND cast('$reportDate' as date) +GROUP BY sku, + CASE + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '30 day') AND cast('$reportDate' as date) THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '60 day') AND (cast('$reportDate' as date)- INTERVAL '31 day') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '90 day') AND (cast('$reportDate' as date)- INTERVAL '61 day') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN (cast('$reportDate' as date)- INTERVAL '120 day') AND (cast('$reportDate' as date)- INTERVAL '91 day') THEN 'L120D' + END, + Total_revenue; + +select 1; + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage1; + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation + GROUP BY sku) A +LEFT JOIN +(SELECT sku , + cast(cast(tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' as varchar) AS decimal(22,2)) gold_price + FROM raena_catalog_management.product p) B ON A.sku = B.sku; + + +Drop table if exists raena_analytics.basic_calculation_stage1_v1; + +create table raena_analytics.basic_calculation_stage1_v1 as +select A.*, + abs(contribution-median_contribution) for_max_contribution +from raena_analytics.basic_calculation A +left join (select sku , + PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY contribution) median_contribution + from raena_analytics.basic_calculation group by sku ) B + on A.sku = B.sku ; + + + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage2; + + +CREATE TABLE raena_analytics.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, + PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY 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 raena_analytics.basic_calculation_stage1_v1 A +LEFT JOIN raena_analytics.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 raena_analytics.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 raena_analytics.basic_calculation_stage2 + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage3; + +/* +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation A +WHERE txn_period <> 'L120D' +GROUP BY A.SKu,txn_period; +*/ + + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation A +left join (select distinct sku , runrate from raena_analytics.basic_calculation where txn_period = 'L30D' ) B on A.sku = B.sku +WHERE txn_period <> 'L120D' +GROUP BY A.SKu; + + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage4; + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage2 A +LEFT JOIN raena_analytics.basic_calculation_stage3 B ON A.sku = B.sku; + + +--SELECT * FROM raena_analytics.basic_calculation_stage4; + +--select * from raena_analytics.basic_calculation_stage5 + +drop table if exists raena_analytics.basic_calculation_stage5; + +CREATE TABLE raena_analytics.basic_calculation_stage5 +as +select sku , percentile_cont(0.5) within +GROUP ( + ORDER BY Max_of_qty) Max_of_qty , Avg(Max_of_qty) Avg_of_quantiy +from +(SELECT sku , + qty1_median_rev_contribution Max_of_qty +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty2_avg_runrate +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty3_avg_quantity +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty4_expected_revenue_target +FROM raena_analytics.basic_calculation_stage4) A +group by sku; + +drop table if exists raena_analytics.basic_calculation_stage6; + +create table raena_analytics.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 raena_analytics.sku_warehouse_split_final B + group by sku; + +drop table if exists raena_analytics.final_basic_calucation_table; + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage4 A +LEFT JOIN raena_analytics.basic_calculation_stage6 B ON A.sku =B.sku +LEFT join raena_analytics.basic_calculation_stage5 C on A.sku=C.sku; + +drop table if exists raena_analytics.inbound_data; + +create table raena_analytics.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) >'$reportDate' 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 raena_analytics.warehouse_in_stock; + +create table raena_analytics.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 raena_analytics.demand_forecast_dashboard_base; + + +CREATE TABLE raena_analytics.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 /home/ec2-user/cronjob/postgresql/demand_forecasting/Demand_forecasting.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/demand_forecasting/Demand_forecasting.sql + + + + diff --git a/postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sh b/postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sh new file mode 100644 index 0000000..967f9d5 --- /dev/null +++ b/postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sh @@ -0,0 +1,1501 @@ +!/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.sku_type_warehouse; + +create table raena_analytics.sku_type_warehouse +as +select distinct sku , sku_type from raena_analytics.final_basic_calucation_table; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1 ; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) +SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name) AA +LEFT JOIN + (SELECT DISTINCT min_price, + service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price,service_name,destination_area_id + ORDER BY min_price ,service_name ,destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON AA.service_name = BB.service_name +AND AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + E.type service_type , + EE.name partner_name , + CASE + WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount*C.Quantity payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN raena_transport_management.logistic_partner EE ON A.partner_id = EE.id +LEFT JOIN + (SELECT AA.order_id , + AA.sales_sub_order_shipment_id, + AA.sku, + CC.Quantity , + CC.discounted_price payment_amount + FROM + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + GROUP BY 1, + 2, + 3) AA + LEFT JOIN + (SELECT DISTINCT external_id , + product_sku , + quantity, + discounted_price + FROM raena_analytics.base_netsuite_final) CC ON AA.order_id = CC.external_id + AND AA.sku = CC.product_sku + AND AA.Quantity= CC.quantity ) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' + AND A.status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED'); + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3_final_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage3_final_v1 AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + service_type , + partner_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + product_sku sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM raena_analytics.warehouse_analysis_stage3 A +LEFT JOIN raena_analytics.base_netsuite_final B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM raena_analytics.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3_final; + +CREATE TABLE raena_analytics.warehouse_analysis_stage3_final AS +SELECT * +FROM raena_analytics.warehouse_analysis_stage3_final_v1 +WHERE order_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + AND payment_amount IS NOT NULL +UNION +SELECT A.Order_id, + reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + A.sku , + A.quantity, + shipping_amount, + B.payment_price +FROM raena_analytics.warehouse_analysis_stage3_final_v1 A +INNER JOIN + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity, + sum(coalesce(BB.payment_amount , AA.payment_amount)) payment_price + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + WHERE order_id IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + GROUP BY 1, + 2, + 3) B ON A.order_id = B.order_id +AND A.reference_id= B.sales_sub_order_shipment_id +AND A.sku = B.sku; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_1; + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN raena_analytics.warehouse_analysis_stage3_final A ON Z.id=A.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_2_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_2_v1 AS +SELECT BB.*, + CASE + WHEN BB.external_id = B.order_id + AND BB.sku = B.sku + AND (CASE + WHEN BB.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 +FROM + (SELECT external_id , + sku , + quantity, + item_type, + Brand_name, + discounted_price + FROM raena_analytics.gm_dashboard + LEFT JOIN + (SELECT order_id + FROM raena_analytics.warehouse_analysis_stage3_final) AA ON external_id = order_id + WHERE cast(transaction_date AS date)>='2022-04-01' + AND order_id IS NULL)BB +LEFT JOIN + (SELECT order_id, + sku, + product_class, + new_shipment_amount + FROM raena_analytics.sku_level_shipping_fee_final + WHERE new_shipment_amount<>0) B ON BB.external_id = B.order_id +AND BB.sku = B.sku +AND (CASE + WHEN BB.item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=B.product_class +LEFT JOIN + (SELECT external_id, + sku, + product_class, + new_shipment_amount + FROM raena_analytics.sku_level_shipping_fee_old_final + WHERE new_shipment_amount<>0) C ON BB.external_id = C.external_id +AND BB.sku = C.sku +AND (CASE + WHEN BB.item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=C.product_class; + + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_2; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + shipping_amount, + BB.Brand_name AS Brand_name , + shipping_to shipping_to, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + CASE + WHEN Z.provider='dsf' THEN 'DSF' + WHEN Z.provider='rdash360' THEN 'RDash' + WHEN Z.order_placed_by='merchant' THEN 'Merchant' + WHEN Z.order_placed_by='admin' THEN 'Admin Panel' + WHEN Z.order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN Z.is_campaign='true' THEN 'Campaign Order' + WHEN Z.is_consignment='true' THEN 'Consignment Order' + WHEN Z.provider='dsf' THEN 'DSF Order' + WHEN Z.order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM + (SELECT id , + status, + provider, + order_placed_by, + is_consignment, + payment_status, + is_campaign, + created_at, + shipping_to, + channel_id, + created_by + FROM raena_order_management.order + WHERE payment_status ='Paid' + AND cast(created_at AS date)>='2022-04-01') Z +INNER JOIN raena_analytics.warehouse_analysis_stage1_2_v1 BB ON Z.id = BB.external_id +LEFT JOIN raena_order_management.channel D ON Z.channel_id = D.id ; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_3_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_3_v1 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.service_type , + A.partner_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to shipping_to, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + CASE + WHEN Z.provider='dsf' THEN 'DSF' + WHEN Z.provider='rdash360' THEN 'RDash' + WHEN Z.order_placed_by='merchant' THEN 'Merchant' + WHEN Z.order_placed_by='admin' THEN 'Admin Panel' + WHEN Z.order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN Z.is_campaign='true' THEN 'Campaign Order' + WHEN Z.is_consignment='true' THEN 'Consignment Order' + WHEN Z.provider='dsf' THEN 'DSF Order' + WHEN Z.order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN raena_analytics.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN raena_analytics.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +LEFT JOIN raena_order_management.channel D ON Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_3; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_3 AS +SELECT order_id, + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + sku, + quantity, + case when b.shipping_amount is not null then ((b.shipping_amount)/(b.order_weight))*(b.final_weight) else A.shipping_amount end shipping_amount, + shipping_to, + payment_amount, + A.payment_status, + order_status, + order_placed_by, + order_type, + created_at, + is_campaign, + created_by +FROM raena_analytics.warehouse_analysis_stage1_3_v1 A +LEFT JOIN raena_analytics.OM_Logistic_final_shipping B ON A.reference_id= B.reference_id; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2_v1_final_weight; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage2_v1_final_weight AS +SELECT order_id , reference_id , service_name , + service_type , + partner_name , + actual_warehouse, + CASE + WHEN (sum(B.Final_weight*A.Quantity))<1.3 THEN 1 + WHEN (sum(B.Final_weight*A.Quantity))>=1.3 + AND (ABS(sum(B.Final_weight*A.Quantity)) - FLOOR(ABS(sum(B.Final_weight*A.Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.Final_weight*A.Quantity))+1 + ELSE FLOOR(sum(B.Final_weight*A.Quantity)) + END AS sku_weight +FROM raena_analytics.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku + group by 1,2,3,4,5,6; + + + DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage2_v1 AS +SELECT A.*, + B.brand_name, + B.Final_weight*sku_weight/sum(B.Final_weight) over(partition by A.order_id , A.reference_id ) Final_weight +FROM raena_analytics.warehouse_analysis_stage1_3 A +left join raena_analytics.warehouse_analysis_stage2_v1_final_weight C on A.order_id = C.order_id and A.reference_id = C.reference_id +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + +drop table if exists raena_analytics.calculated_ninja_jne_oct_shipping_fee; + +CREATE TABLE raena_analytics.calculated_ninja_jne_oct_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(CASE WHEN C.partner_id = 5 THEN C.insurance_fee ELSE C.insurance_fee END) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM raena_analytics.warehouse_analysis_stage2_v1 + WHERE created_at::date BETWEEN '2022-10-01' AND '2022-10-14' + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + ) AA +GROUP BY 1, + 2, + 3, + 4; + +drop table if exists raena_analytics.calculated_feb_shipping_fee; + +CREATE TABLE raena_analytics.calculated_feb_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(C.insurance_fee ) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM raena_analytics.warehouse_analysis_stage2_v1 + WHERE created_at::date BETWEEN '2022-11-01' AND '2023-02-20' + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + ) A +GROUP BY 1, + 2, + 3, + 4; + + DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2; + +CREATE TABLE raena_analytics.warehouse_analysis_stage2 AS +SELECT A.*, + (final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(final_weight) over(partition BY A.reference_id) new_shipment_amount +FROM raena_analytics.warehouse_analysis_stage2_v1 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.reference_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.reference_id = C.reference_id ; + +select count(1) from raena_analytics.warehouse_analysis_stage2 ; + + +DROP TABLE IF EXISTS raena_analytics.shipping_fee_coupon_purpose; + + +CREATE TABLE raena_analytics.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to shipping_to, + order_placed_by, + order_type, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT A.id AS order_id , + reseller_id, + A.reseller_info::json->>'tierName' tierName, + A.reseller_info::json->>'email' email, + A.reseller_info::json->>'mobile' mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + A.created_at + FROM raena_order_management.order A + left join raena_order_management.channel D on A.channel_id = D.id + WHERE A.is_archived = 'false' + AND cast(A.created_at AS date)>='2022-04-01' + ORDER BY A.created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN +(SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN raena_analytics.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management.order + WHERE payment_status= 'Paid' + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + + +select count(1) from raena_analytics.shipping_fee_coupon_purpose; + +DROP TABLE IF EXISTS raena_analytics.rate_sheet_wise_expected_warehouse ; + + +CREATE TABLE raena_analytics.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date, + DSF_flag, + brand_type +FROM + ( + SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + A.service_type , + A.partner_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + coalesce(new_shipment_amount,shipping_amount)new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at, + case when A.order_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + CASE + WHEN coalesce(Y.Name,A.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 coalesce(Y.Name,A.Brand_name) IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC' + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type + FROM raena_analytics.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM raena_analytics.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN raena_analytics.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date), + DSF_flag, + brand_type; + +update raena_analytics.rate_sheet_wise_expected_warehouse + set payment_amount =0 + from raena_analytics.gm_dashboard + where discounted_price= 0 + and raena_analytics.rate_sheet_wise_expected_warehouse.order_id = raena_analytics.gm_dashboard.external_id + and raena_analytics.rate_sheet_wise_expected_warehouse.sku = raena_analytics.gm_dashboard.sku ; + + + select count(1) from raena_analytics.rate_sheet_wise_expected_warehouse ; + +DROP TABLE IF EXISTS raena_analytics.quantity_trend_new; + + +CREATE TABLE raena_analytics.quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM raena_analytics.quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT cast(is_campaign as varchar) , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM raena_analytics.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS raena_analytics.quantity_trend; + + +CREATE TABLE raena_analytics.quantity_trend AS +SELECT * +FROM raena_analytics.quantity_trend_new; + + +DROP TABLE IF EXISTS raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM +(SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM raena_analytics.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM raena_analytics.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN raena_analytics.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + select count(1) from raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping ; + +DROP TABLE IF EXISTS raena_analytics.om_distr_actaul_origin; + + +CREATE TABLE raena_analytics.om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + B.partner_id, + B.service_id, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse='Samarinda' THEN 36700 + END AS expected_origin +FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS raena_analytics.om_distr_actaul_origin_2; + + +CREATE TABLE raena_analytics.om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM raena_analytics.om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +AND b.service_id = c.service_id +AND b.partner_id = c.partner_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN raena_analytics.om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id IS NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM raena_analytics.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM raena_analytics.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_4; + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM raena_analytics.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE raena_analytics.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM +(SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_6; + + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM raena_analytics.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM raena_analytics.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM raena_analytics.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + + --final dataset +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight=10; + + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 ; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN raena_analytics.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id and o.status <>'CANCELLED' +WHERE Sub_order_shipping_id IS NOT NULL; + +drop table if exists raena_analytics.om_shipment_jtr_shipping_fee_base_11; + +create table raena_analytics.om_shipment_jtr_shipping_fee_base_11 +as +select A.*,AA.new_sku sku,compared_weight sku_weight , +sum((compared_weight/order_weight)*expected_jtr_shipping_fee) sku_level_jtr_expected_shipping_fee, +sum((compared_weight/order_weight)*actual_jtr_shipping_fee) sku_level_jtr_actual_shipping_fee +from raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 A +left join +(SELECT order_id , sales_sub_order_id , reference_id ,new_sku, + sum(CASE + WHEN new_volume_weight>new_actual_weight THEN new_volume_weight + WHEN new_volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight /home/ec2-user/cronjob/postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.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/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sql diff --git a/postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sql b/postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sql new file mode 100644 index 0000000..e44c72a --- /dev/null +++ b/postgresql/distribution_warehouse_analysis/distributed_warehouse_etl_cron.sql @@ -0,0 +1,1486 @@ + + +drop table if exists raena_analytics.sku_type_warehouse; + +create table raena_analytics.sku_type_warehouse +as +select distinct sku , sku_type from raena_analytics.final_basic_calucation_table; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1 ; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) +SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name) AA +LEFT JOIN + (SELECT DISTINCT min_price, + service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price,service_name,destination_area_id + ORDER BY min_price ,service_name ,destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON AA.service_name = BB.service_name +AND AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + E.type service_type , + EE.name partner_name , + CASE + WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount*C.Quantity payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN raena_transport_management.logistic_partner EE ON A.partner_id = EE.id +LEFT JOIN + (SELECT AA.order_id , + AA.sales_sub_order_shipment_id, + AA.sku, + CC.Quantity , + CC.discounted_price payment_amount + FROM + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + GROUP BY 1, + 2, + 3) AA + LEFT JOIN + (SELECT DISTINCT external_id , + product_sku , + quantity, + discounted_price + FROM raena_analytics.base_netsuite_final) CC ON AA.order_id = CC.external_id + AND AA.sku = CC.product_sku + AND AA.Quantity= CC.quantity ) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' + AND A.status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED'); + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3_final_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage3_final_v1 AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + service_type , + partner_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + product_sku sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM raena_analytics.warehouse_analysis_stage3 A +LEFT JOIN raena_analytics.base_netsuite_final B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM raena_analytics.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3_final; + +CREATE TABLE raena_analytics.warehouse_analysis_stage3_final AS +SELECT * +FROM raena_analytics.warehouse_analysis_stage3_final_v1 +WHERE order_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + AND payment_amount IS NOT NULL +UNION +SELECT A.Order_id, + reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + A.sku , + A.quantity, + shipping_amount, + B.payment_price +FROM raena_analytics.warehouse_analysis_stage3_final_v1 A +INNER JOIN + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity, + sum(coalesce(BB.payment_amount , AA.payment_amount)) payment_price + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + WHERE order_id IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + GROUP BY 1, + 2, + 3) B ON A.order_id = B.order_id +AND A.reference_id= B.sales_sub_order_shipment_id +AND A.sku = B.sku; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_1; + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN raena_analytics.warehouse_analysis_stage3_final A ON Z.id=A.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_2_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_2_v1 AS +SELECT BB.*, + CASE + WHEN BB.external_id = B.order_id + AND BB.sku = B.sku + AND (CASE + WHEN BB.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 +FROM + (SELECT external_id , + sku , + quantity, + item_type, + Brand_name, + discounted_price + FROM raena_analytics.gm_dashboard + LEFT JOIN + (SELECT order_id + FROM raena_analytics.warehouse_analysis_stage3_final) AA ON external_id = order_id + WHERE cast(transaction_date AS date)>='2022-04-01' + AND order_id IS NULL)BB +LEFT JOIN + (SELECT order_id, + sku, + product_class, + new_shipment_amount + FROM raena_analytics.sku_level_shipping_fee_final + WHERE new_shipment_amount<>0) B ON BB.external_id = B.order_id +AND BB.sku = B.sku +AND (CASE + WHEN BB.item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=B.product_class +LEFT JOIN + (SELECT external_id, + sku, + product_class, + new_shipment_amount + FROM raena_analytics.sku_level_shipping_fee_old_final + WHERE new_shipment_amount<>0) C ON BB.external_id = C.external_id +AND BB.sku = C.sku +AND (CASE + WHEN BB.item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=C.product_class; + + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_2; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + shipping_amount, + BB.Brand_name AS Brand_name , + shipping_to shipping_to, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + CASE + WHEN Z.provider='dsf' THEN 'DSF' + WHEN Z.provider='rdash360' THEN 'RDash' + WHEN Z.order_placed_by='merchant' THEN 'Merchant' + WHEN Z.order_placed_by='admin' THEN 'Admin Panel' + WHEN Z.order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN Z.is_campaign='true' THEN 'Campaign Order' + WHEN Z.is_consignment='true' THEN 'Consignment Order' + WHEN Z.provider='dsf' THEN 'DSF Order' + WHEN Z.order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM + (SELECT id , + status, + provider, + order_placed_by, + is_consignment, + payment_status, + is_campaign, + created_at, + shipping_to, + channel_id, + created_by + FROM raena_order_management.order + WHERE payment_status ='Paid' + AND cast(created_at AS date)>='2022-04-01') Z +INNER JOIN raena_analytics.warehouse_analysis_stage1_2_v1 BB ON Z.id = BB.external_id +LEFT JOIN raena_order_management.channel D ON Z.channel_id = D.id ; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_3_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_3_v1 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.service_type , + A.partner_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to shipping_to, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + CASE + WHEN Z.provider='dsf' THEN 'DSF' + WHEN Z.provider='rdash360' THEN 'RDash' + WHEN Z.order_placed_by='merchant' THEN 'Merchant' + WHEN Z.order_placed_by='admin' THEN 'Admin Panel' + WHEN Z.order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN Z.is_campaign='true' THEN 'Campaign Order' + WHEN Z.is_consignment='true' THEN 'Consignment Order' + WHEN Z.provider='dsf' THEN 'DSF Order' + WHEN Z.order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN raena_analytics.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN raena_analytics.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +LEFT JOIN raena_order_management.channel D ON Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_3; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_3 AS +SELECT order_id, + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + sku, + quantity, + case when b.shipping_amount is not null then ((b.shipping_amount)/(b.order_weight))*(b.final_weight) else A.shipping_amount end shipping_amount, + shipping_to, + payment_amount, + A.payment_status, + order_status, + order_placed_by, + order_type, + created_at, + is_campaign, + created_by +FROM raena_analytics.warehouse_analysis_stage1_3_v1 A +LEFT JOIN raena_analytics.OM_Logistic_final_shipping B ON A.reference_id= B.reference_id; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2_v1_final_weight; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage2_v1_final_weight AS +SELECT order_id , reference_id , service_name , + service_type , + partner_name , + actual_warehouse, + CASE + WHEN (sum(B.Final_weight*A.Quantity))<1.3 THEN 1 + WHEN (sum(B.Final_weight*A.Quantity))>=1.3 + AND (ABS(sum(B.Final_weight*A.Quantity)) - FLOOR(ABS(sum(B.Final_weight*A.Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.Final_weight*A.Quantity))+1 + ELSE FLOOR(sum(B.Final_weight*A.Quantity)) + END AS sku_weight +FROM raena_analytics.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku + group by 1,2,3,4,5,6; + + + DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage2_v1 AS +SELECT A.*, + B.brand_name, + B.Final_weight*sku_weight/sum(B.Final_weight) over(partition by A.order_id , A.reference_id ) Final_weight +FROM raena_analytics.warehouse_analysis_stage1_3 A +left join raena_analytics.warehouse_analysis_stage2_v1_final_weight C on A.order_id = C.order_id and A.reference_id = C.reference_id +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + +drop table if exists raena_analytics.calculated_ninja_jne_oct_shipping_fee; + +CREATE TABLE raena_analytics.calculated_ninja_jne_oct_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(CASE WHEN C.partner_id = 5 THEN C.insurance_fee ELSE C.insurance_fee END) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM raena_analytics.warehouse_analysis_stage2_v1 + WHERE created_at::date BETWEEN '2022-10-01' AND '2022-10-14' + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + ) AA +GROUP BY 1, + 2, + 3, + 4; + +drop table if exists raena_analytics.calculated_feb_shipping_fee; + +CREATE TABLE raena_analytics.calculated_feb_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(C.insurance_fee ) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM raena_analytics.warehouse_analysis_stage2_v1 + WHERE created_at::date BETWEEN '2022-11-01' AND '2023-02-20' + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + ) A +GROUP BY 1, + 2, + 3, + 4; + + DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2; + +CREATE TABLE raena_analytics.warehouse_analysis_stage2 AS +SELECT A.*, + (final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(final_weight) over(partition BY A.reference_id) new_shipment_amount +FROM raena_analytics.warehouse_analysis_stage2_v1 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.reference_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.reference_id = C.reference_id ; + +select count(1) from raena_analytics.warehouse_analysis_stage2 ; + + +DROP TABLE IF EXISTS raena_analytics.shipping_fee_coupon_purpose; + + +CREATE TABLE raena_analytics.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to shipping_to, + order_placed_by, + order_type, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT A.id AS order_id , + reseller_id, + A.reseller_info::json->>'tierName' tierName, + A.reseller_info::json->>'email' email, + A.reseller_info::json->>'mobile' mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + A.created_at + FROM raena_order_management.order A + left join raena_order_management.channel D on A.channel_id = D.id + WHERE A.is_archived = 'false' + AND cast(A.created_at AS date)>='2022-04-01' + ORDER BY A.created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN +(SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN raena_analytics.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management.order + WHERE payment_status= 'Paid' + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + + +select count(1) from raena_analytics.shipping_fee_coupon_purpose; + +DROP TABLE IF EXISTS raena_analytics.rate_sheet_wise_expected_warehouse ; + + +CREATE TABLE raena_analytics.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date, + DSF_flag, + brand_type +FROM + ( + SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + A.service_type , + A.partner_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + coalesce(new_shipment_amount,shipping_amount)new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at, + case when A.order_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + CASE + WHEN coalesce(Y.Name,A.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 coalesce(Y.Name,A.Brand_name) IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC' + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type + FROM raena_analytics.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM raena_analytics.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN raena_analytics.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date), + DSF_flag, + brand_type; + +update raena_analytics.rate_sheet_wise_expected_warehouse + set payment_amount =0 + from raena_analytics.gm_dashboard + where discounted_price= 0 + and raena_analytics.rate_sheet_wise_expected_warehouse.order_id = raena_analytics.gm_dashboard.external_id + and raena_analytics.rate_sheet_wise_expected_warehouse.sku = raena_analytics.gm_dashboard.sku ; + + + select count(1) from raena_analytics.rate_sheet_wise_expected_warehouse ; + +DROP TABLE IF EXISTS raena_analytics.quantity_trend_new; + + +CREATE TABLE raena_analytics.quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM raena_analytics.quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT cast(is_campaign as varchar) , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM raena_analytics.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS raena_analytics.quantity_trend; + + +CREATE TABLE raena_analytics.quantity_trend AS +SELECT * +FROM raena_analytics.quantity_trend_new; + + +DROP TABLE IF EXISTS raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM +(SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM raena_analytics.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM raena_analytics.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN raena_analytics.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + select count(1) from raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping ; + +DROP TABLE IF EXISTS raena_analytics.om_distr_actaul_origin; + + +CREATE TABLE raena_analytics.om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + B.partner_id, + B.service_id, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse='Samarinda' THEN 36700 + END AS expected_origin +FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS raena_analytics.om_distr_actaul_origin_2; + + +CREATE TABLE raena_analytics.om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM raena_analytics.om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +AND b.service_id = c.service_id +AND b.partner_id = c.partner_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN raena_analytics.om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id IS NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM raena_analytics.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM raena_analytics.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_4; + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM raena_analytics.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE raena_analytics.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM +(SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_6; + + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM raena_analytics.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM raena_analytics.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM raena_analytics.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + + --final dataset +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight=10; + + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 ; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN raena_analytics.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id and o.status <>'CANCELLED' +WHERE Sub_order_shipping_id IS NOT NULL; + +drop table if exists raena_analytics.om_shipment_jtr_shipping_fee_base_11; + +create table raena_analytics.om_shipment_jtr_shipping_fee_base_11 +as +select A.*,AA.new_sku sku,compared_weight sku_weight , +sum((compared_weight/order_weight)*expected_jtr_shipping_fee) sku_level_jtr_expected_shipping_fee, +sum((compared_weight/order_weight)*actual_jtr_shipping_fee) sku_level_jtr_actual_shipping_fee +from raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 A +left join +(SELECT order_id , sales_sub_order_id , reference_id ,new_sku, + sum(CASE + WHEN new_volume_weight>new_actual_weight THEN new_volume_weight + WHEN new_volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight` zreie8jT1MJYp!0~Y3gyju>{5v7)xL*fw2U}5*SNhEP=5E#uE5{AOWX+t#T*Y@g~uh ziuik3;O`saql)L%K=|t-{Iq!fP$2xj0^!$)0^D+56XC~1x;p~l{}~8>DiHo3BHXRl zlxUEf|G$gyRgwR_f$+=4^G)&hnLzmWMEbG_KlUU1i<|$ef$+}+!e0^Lw~6#WAqsTU zzbwKX5&o-z@QWfmFTyYXQGfXtM7Z0&Vj%pxBHZ<;JP-(fNrb!o|JEP#=l^0LeKQdL zf(Uo(_f#PKJ0kqJsQ;cf`}6;{2scFdZGrITMfm$f_%{OK-xA?j5q{_`{`{X4;VBV* zcOYDJm%{vDi1U|$aFJ~MXDorS1jZ5=OJFR4u>{5v7)xL*fw2U}68JwW0Zmtw-KhN& z)DXq_zia=!^mavg5xfApzyT&$2k!$Xz>kBkzfDoT3O)z^2z(a&9{64G7&rskzyb^4 zYH;ByMfoy#6jZ^pS1O7Io_(vLd>otx?*va?p(uX=z5qS~J`C;wKMxLrS9dAOQ{Xe; z*TH+hKJYErZeIkS0v`e=K^(jtya>DTJa`P;3w{Nh0*Ao$;5)EezXCoAJ^)lO1+D@w z!)87YejnTcbPxyEgTI5leh0W6+y)Gg0Ph9w0ha*blkmlQ<7g4+}5R zEYi{Hy4ldnXS!O;F`dnQ$*B8P2^O156xF_mzT$f|nd*zE=F$XJ><^&DsM(@QjfUJZ4Qo$m#Cz^gl$Q4Jv1#kP=bjR>$bHv<7N9 zr4-eynktcWrdZ14@Wtt9EnBNPrqwFznq%}mm{L*&HBnMYDUr;oq@x)vt*jxtO~tUDGkjwxiXJ>GI9f&;}?~x{zNL=`u@1J&r<)JI&z`(k~VAD)|H6t75Of+1u>u18YDDN)~#;G4$&b&Wf~@j<5fz@(wd0{x&D29Qc>CrjXf*NR5^6WJDnwA(RNUu;*VVMk zRjt|9%zBGxc5kRk&*kyqR!VD95y>HrZ!7BB~rXDJvQDrr0+^nUE&Mz)sL93X!Bik8r9vW>@L9bWCHuT$3zU zd8L%rW*(o4fL67OX}e`?@TG_Mjrf;oR0{KGc^+q6TVxMBPqUI z@PlO}a_g-M-qc#D9h^I0EY3$3w7G@I+~PdG%^sYO7>f(@a|?^}2V=FxkgUgaCJWS} zZeh#Vx7hHCY*YM#QCU2oA2cG1(+9N3oVGX}S*%XaL~7dHtX^4MJYY<3TfA*H&JJ4b zc2t4bd~7yWs~C|33$f|Q-0bv1WRZ%aX3WmkwCRJ33yX8xwZDSP%jb+OC6L^%Cxs%( zUFl5=p(N9q5Vj@m*1lZDEM zM5|j9Wa3?WPek@MBYX9UFwZ|?kO^|K6_GzAO?TMXs=|sO1@-6FG6gkFmhy#_M2W}| zBaxFWRFGBFEewV=FEzerS_Ji%K@zEyx{5vjGM1l}Bqp@{5=uQ$BuN$JV0%wXUV~Le zYl0_OzcQ&KqC%_rLW!HCC)zLXf(E1KRkTSTu=uTL*$LS17F?+OGpF1}}A~mvq-D(>9M}8&oOhZv7$l)I#x#TkR#1YlI6?mjzv>N4( zQAM50aNCqw3qAkoKG>y2mG1I#*yIJhyjHW9q^x(F?Xsg)8ivBQe6pXiS7^s*T4xPJ zc8jicLpuICpWoS3!#9LVj(PBu@ypEc>4#o1Ft0Ou$3y7*3n5_r&)*(t@)&E zqVIF<>~LgKIIf#or*5`-0sYOZHx)NSLTHw*zYY#+CXrrZvN|odQ)&Qhs^mcqif}8f zAi7zrxm5@z^mNDb90X(3o1W9ar#ag=3AUMhMsNsjlM}oKe3oLaC)rg$z|Dzc7wRYo z<1}a{HZx6MY@b;O9a0)b%^{~OtT%k#dOYPDdOan;@NL^M3(63O)hu0tT1^vtS170j0;9>AF@Jql3CqWX-f<54!;4R<+`~`mx zo&XPmdq5i;2M58m;LYF__zb=V{uKNs_(iY|R=@%XgUi7`z<=;CxE<6$5gZ0T4ZaPZ z!t;RMGk6X>4n7U;1sYfYH-ZUp1Ndk7SDpb+g9iY;+rAD;APuI#Pl3z8OSot7HSjm! zLEwNz@H*}od=dN(_!OvvN$`E#DfmNhC%6SHgZRh9 zCn$8tccd}wK+b65ppYG=y7vpb7U4&?5ylvC)~#i_B-g4LF2xif;D~*XXGm59>Q~V^=sr z1#z59^7KxI?@*1M#i>!+n*$EvQet`?l8zVZ9jwPgO@m%@X*nTrh_@ns4=dOeSv;iM zH8)YZxHK$2J$SWRHcsm4y?}Vg>2#q^?1D)s)eG1vxj5+$q;=2#{SB258+$_b+Us=; z?Mx=fK7qKWjBX_p?RLNT`%*Cj#7n~LtyK-n+zyfs>w*5HJ=u5YK5O=1%q_8y4j#R- z#`O~TJIl*j78j2A9IJZTe+o<=(wiKf;M^5B@Yp+8bu2GxBI-La_MTU@jw9b+sCH2> zq#!?Ydx1R@CoS0RIhc(cf^7pI+Wu|bmPaj>u5Gpq+s2IxD58xkLb&t9ZupF_ff}9( zJ#nGih6Fn(#a1TCwhyimv9WG+3>qlcm__26WU|dNwJQ6zJQYLhVhGjka<|QPTm*S5 zEULSU_XOi+HbrlU5tw2nLQ|qTF=cI)orVOhVc(t|HKa=&o9Tw`@PqQ(iS>&vgK*@= za|?r(gyWf1ez{2Eu`n;0J!18ycMM>bNRJ!V22P{Vx-y;r!X%qHs*=#&TE{R*-K;r# zJ=I3GTSdkVuO=e%4e#WTm@Ug8HLF9Y)riOgMY6ooGkVHrrRqop3E6|EP6SW7L;12J3yDs_lvA%qQ zPhtd9PSYB2^#@h*>dNB`XcUyuffZ=@ZWJMVCz07UvGn-r z9%JLe_lQRyyi!nQ=`X_H_`t8;9)Na6O#T@oK8x3@MGQN@-lYZav1=)N4}5bY%^jcq zTU}nPrd2T;y{(7$Jh;mgmGbQp-GSznjR)R$bYuUuA?lf{Tb)fdKE0^ClYChZzjWp4 ziLXyi`6(@0r8?ci(S65BvUOpbDbkHQ4b_g8RTN zU@v$Hw)&re4}umY|H--O+LKR5;E!R6q~u(9t0w}44-5q95(u#+DFA#f#l84dU=@G!Uw{4&Ubqu?g+li+ROIoQiz0e=8) z10MkAfDMj-7`O(!4BPoUxF2)?wW(*oZt!*3(2syy0sVgf5U?9u4=%v2ei~34`?tZb zfe(QeH~~^%2K)@T8oUmfzW}}p{v134SU;dE_=l+pe1ey}SN$RFRNZ<=vt-{CI(UX0 zIGGNgHtGKY@&}si+{@l{OX;~KrBSizU8|QTFW0%eSU3^J8bb~J!%9}6kWJ%I{)W4J6W@{?%W4DPacCipXpVoG^ zk45yMQ|e&2LCjFI_-L+1s6nNzb zeHRw#HfLLbRIm;%#n$9SA~9@CA!@4KenUJ?_^TGe7!&Hi=R+)TWHOYZC&+k1Ko1Es zOX>-}FOr<}>;WsSuNPsea=H19S$Y>7Bd_QFr^h-5jCJ%nH46Anksh?N7p$fvdx#Lp z@Wt#n>^gFoEK!F)-1#CqI)L#4llzk$!f;OjC2GIeKRqS9N*cr(V`x5Bd!Cn_8a zJIRGj&^XFqS@7AKrKblOJ4r99&Tm@#8iq=+OV)k4^iHMyuPfC|yvH=)T(WPflKNfB zYYX>?vTGOqTZ&3u<}E$m*iVQP7~h`V3#G0&E}p^RUc|tcP^Pq`7D8d+FjTYqSF@gl zABfxkGnCKKyP%j$Lj4u)kItWj5WSE|kCI{~aXgjHIeNV{ zm&zAMNpl3Rs^xJBm*f(qwE~J&M@hY$T`MJW>hV$1q>YVDOg5t=NvespVkt2s31%Gn zjwSbb)7k78Nf*>&v5?QN3`sJme?v#}cs~;2TT$sPU{9}(nDuqCyfT7jtz?cD^ZDE; zWz$Q^C)Cl>&@zrBMk*$Wi_a9YnkZz7%g`{q8#zin(G7*fu#Olg)q&n*MrzE_6S9Hl+k M%xX5V$`t#*0qmar9smFU literal 0 HcmV?d00001 diff --git a/postgresql/gm_code/gm_etl_cron.sh b/postgresql/gm_code/gm_etl_cron.sh new file mode 100644 index 0000000..2ebde1f --- /dev/null +++ b/postgresql/gm_code/gm_etl_cron.sh @@ -0,0 +1,2323 @@ + #!/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.manual_bundle_sku_data ; + + +CREATE TABLE raena_analytics.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(cast(C.tier_price::json->'07030fbe-5801-4318-9e97-fe33fa169894' as varchar) AS decimal(22,2)) bronze_price, + cast(cast(C.tier_price::json->'8eb95d6e-915a-4a91-9c12-fa43db995e19' as varchar) AS decimal(22,2)) silver_price, + cast(cast(C.tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' as varchar) AS decimal(22,2)) gold_price, + cast(C.retail_price AS decimal(22,2)) child_retail_price +FROM raena_analytics.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 raena_analytics.order_level_data; + + +CREATE TABLE raena_analytics.order_level_data AS +SELECT DISTINCT external_id, + transaction_date+interval'7 hours' AS transaction_date, + discount_amount, + shipping_cost, + A.coupon_code, + replace(cast(reseller_tier_name as varchar),'\"','')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, + is_campaign, + order_placed_by +FROM + (SELECT A.id AS external_id , + (A.created_at) AS transaction_date , + A.discount_amount, + applied_shipping_amount shipping_cost, + A.coupon_code, + A.reseller_info::json->'tierName' 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, + cast(is_campaign as varchar) is_campaign, + order_placed_by + FROM raena_order_management.order A + WHERE ( payment_status='Paid' or id like 'PL%') + AND cast(A.created_at+interval '7 hours' AS date) >='$reportDate' ) A +LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code; + + +SELECT max(created_at) +FROM raena_order_management.order ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage1_V1; + + +CREATE TABLE raena_analytics.base_netsuite_stage1_V1 AS +SELECT DISTINCT transaction_date 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_wholesale_price, + CASE + WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount + ELSE B.coupon_discount + END discount_price, + CASE + WHEN discount_amount > 0 + AND A.coupon_code IS NULL + AND A.order_placed_by = 'admin' + AND coupon_applied_on IS NULL THEN 'Cart' + ELSE coupon_applied_on + END 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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type, + a.is_campaign, + B.cogs_discount_type +FROM raena_analytics.order_level_data A +LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id and status <> 'Cancelled' +LEFT JOIN raena_analytics.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, + actual_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+interval'7 hours' AS date)>='$reportDate') F ON B.id = F.sales_sub_order_id +ORDER BY 1, + 2; + + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1_base ; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1_base 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, + actual_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, + price_type, + is_campaign, + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND A.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + cogs_discount_type +FROM raena_analytics.base_netsuite_stage1_V1 A +LEFT JOIN + (SELECT external_id , + sku , + final_loyalty_point + FROM raena_analytics.final_loyalty_point) B ON A.external_id = B.external_id +AND A.sku = B.sku +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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,4) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON A.sku = D.sku +AND A.transaction_date::date BETWEEN D.created_at AND last_date; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1 AS +SELECT transaction_date, + A.external_id, + A.sku, + parent_sku, + quantity quantity, + parent_quantity, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE retail_price + END retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_retail_price + END parent_retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE effective_wholesale_price + END effective_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_wholesale_price + END parent_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE actual_wholesale_price + END actual_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_price + END discount_price, + coupon_applied_on, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_amount + END discount_amount, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE dynamic_price + END dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_dynamic_price + END parent_dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_price + END payment_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_amount + END payment_amount, + product_class, + parent_product_class, + product_type_class, + sales_sub_order_id, + loyalty_discount, + price_type, + is_campaign, + CASE + WHEN A.sku = D.sku THEN 0 + ELSE cogs + END cogs, + cogs_flag, + cogs_discount_type +FROM raena_analytics.base_netsuite_stage2_v1_base A +LEFT JOIN + (SELECT DISTINCT sku + FROM raena_catalog_management.product + WHERE lower(name) LIKE '%not for sale%' + OR lower(name) LIKE '%free item%' + OR lower(name) LIKE '%free gift%') D ON A.sku = D.sku ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage3_v1 ; + + +CREATE TABLE raena_analytics.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 + AND retail_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 discount_price + WHEN coupon_applied_on ='Cart' + AND discount_price=0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id) + END AS Final_discount , + retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + CASE + WHEN product_type_class IN ('Flash Bundle', + 'Flash') THEN 'Flash' + WHEN coupon_applied_on ='Cart' + AND price_type ='NEGOTIATED_PRICE' THEN 'Negotiated' + WHEN coupon_applied_on = 'Shipping Fee' THEN 'Shipping' + WHEN coupon_applied_on IS NOT NULL THEN 'Brand' + END discount_type +FROM raena_analytics.base_netsuite_stage2_v1 A ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage4_v1 ; + + +CREATE TABLE raena_analytics.base_netsuite_stage4_v1 AS +SELECT A.* , + CASE + WHEN (parent_product_class= 'Bundle' + OR product_type_class IN('Bundle', 'Flash Bundle')) + AND A.external_id = B.external_id + AND A.parent_sku = B.parent_sku and ttl_wholesale_price<>0 THEN (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/ttl_wholesale_price) + END AS additional_discount, + CASE + WHEN final_discount<>0 THEN final_discount-seller_margin + END AS effective_coupon_discount +FROM raena_analytics.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 + FROM raena_analytics.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 raena_analytics.base_netsuite_final +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.order_level_data); + + +INSERT INTO raena_analytics.base_netsuite_final +SELECT A.external_id, + A.transaction_date 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) -coalesce((CASE WHEN final_discount >0 THEN 0 ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity END),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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, + 0 diff, + CASE + WHEN price_type IS NOT NULL THEN price_type + ELSE discount_type + END discount_type , + CASE + WHEN final_discount >0 THEN 0 + ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity + END promo_amount, + actual_wholesale_price, + effective_wholesale_price, + A.is_campaign, + '' sales_sub_order_id, + cogs_discount_type, + parent_sku +FROM raena_analytics.order_level_data A +LEFT JOIN raena_analytics.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + + + +UPDATE raena_analytics.base_netsuite_final AA +SET discounted_price = new_discounted_price +from (SELECT external_id , + product_sku , + cast(settlement_amount/quantity AS decimal(22,2)) new_discounted_price + FROM + (SELECT A.*, + discounted_price , + (effective_wholesale_price*B.\"Settlement amount\")/sum(effective_wholesale_price) over (partition BY external_id) settlement_amount , + B.\"Settlement amount\" + FROM raena_analytics.base_netsuite_final A + INNER JOIN (select distinct * from raena_analytics.mp_orders_hn_wd) B ON external_id= B.\"Order ID \" + WHERE A.transaction_date >'2022-12-31' and effective_wholesale_price<>0 ) A) BB + where AA.external_id = BB.external_id +AND AA.product_sku = BB.product_sku +and AA.transaction_date >'2022-12-31'; + + +update raena_analytics.base_netsuite_final A +set discounted_price = B.payment_price/quantity +from (select order_id,sku, max(payment_price) payment_price from raena_analytics.order_data_update group by 1,2) B +where A.external_id = B.order_id and A.parent_sku = B.sku and product_sku = parent_sku ; + + +update raena_analytics.base_netsuite_final A +set discounted_price = new_payment_price/A.quantity +from raena_analytics.import_data_for_sku_level_payment_amount B +where A.external_id = B.order_id and A.parent_sku = B.sku and A.product_sku <> A.parent_sku; + +SELECT max(transaction_date) Netsuite_base_date +FROM raena_analytics.base_netsuite_final; + + +DROP TABLE IF EXISTS raena_analytics.gm_dimensions_stage1; + + +CREATE TABLE raena_analytics.gm_dimensions_stage1 AS +SELECT distinct A.id AS external_id , + shipping_province, + reseller_id , + replace(cast(A.reseller_info::json ->'name' AS varchar),'\"','') reseller_name, + replace(cast(A.reseller_info::json ->'email' AS varchar),'\"','')reseller_email, + replace(cast(A.reseller_info::json ->'mobile' AS varchar),'\"','') reseller_mobile, + discount_type, + replace(cast(A.reseller_info::json ->'tierName' AS varchar),'\"','') reseller_tier_name, + shipping_to, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + cast(is_campaign AS varchar) is_campaign +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 +LEFT JOIN raena_order_management.channel D ON A.channel_id = D.id +WHERE (payment_status ='Paid' or A.id like 'PL%') + AND cast(A.created_at+interval'7 hours' AS date) >='$reportDate'; + +drop table if exists raena_analytics.sku_gm_mapping; + +create table raena_analytics.sku_gm_mapping +as +select A.sku ,A.name sku_name,B.name brand_name,C.name as category_name,product_type + 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 ; + + +DELETE +FROM raena_analytics.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.GM_dashboard +SELECT A.external_id , + B.transaction_date AS transaction_date, + B.order_discount_amount , + B.shipping_cost , + A.shipping_province, + B.coupon_code , + A.discount_type , + B.coupon_applied_on , + brand_name, + category_name , + product_type , + product_sku sku, + sku_name , + A.reseller_name , + A.reseller_email , + A.reseller_mobile , + reseller_tier_name tier_name, + A.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 , + coalesce(effective_cogs,coalesce(C.applied_cogs,D.cogs-coalesce(promo,0))) 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type , + Customer_type, + BB.gm_target, + B.discount_type discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + coalesce(case when cogs_type ='REGULAR' then 'Promo' else cogs_type end ,CASE WHEN B.product_sku = D.sku + AND B.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + A.is_campaign, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END AS DSF_Flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender, + cohort +FROM raena_analytics.gm_dimensions_stage1 A +INNER JOIN raena_analytics.base_netsuite_final B ON A.external_id =B.external_id +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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,4) C ON B.product_sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON B.product_sku = D.sku +AND B.transaction_date::date BETWEEN D.created_at AND last_date +left join (select distinct order_id , + created_at+interval'7 hours' AS created_date, + parent_sku sku, + effective_cogs + from raena_order_management.sales_sub_order + where price_type is not null and parent_sku not like 'BAZ%')XX + on A.external_Id=XX.order_id and B.product_sku= XX.sku +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 B.product_sku = BB.sku +AND A.reseller_tier_name =BB.tierName +LEFT JOIN raena_analytics.sku_gm_mapping AAA ON B.product_sku = AAA.sku +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) BBB on B.product_sku=BBB.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where payment_status = 'Paid' + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid') ; + + + + +SELECT max(transaction_date) gm_dashboard_date +FROM raena_analytics.GM_dashboard; + +DROP TABLE IF EXISTS raena_analytics.GM_dashboard_v1; + + +CREATE TABLE raena_analytics.GM_dashboard_v1 AS +SELECT * +FROM raena_analytics.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +DELETE +FROM raena_analytics.GM_dashboard +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.GM_dashboard +SELECT external_id, + transaction_date, + order_discount_amount, + shipping_cost, + shipping_province, + coupon_code, + discount_type, + coupon_applied_on, + brand_name, + category_name, + product_type, + sku, + sku_name, + reseller_name, + reseller_email, + reseller_mobile, + tier_name, + reseller_id, + quantity, + 0 retail_price, + 0 seller_margin, + 0 , + 0 , + 0, + 0, + item_type, + order_placed_by, + 0 cogs, + order_recipient, + brand_type, + customer_type, + gm_target, + discount_promo_type, + 0 promo_amount, + 0, + 0, + cogs_flag, + is_campaign, + dsf_flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender +FROM raena_analytics.GM_dashboard_v1 +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + + +UPDATE raena_analytics.gm_dashboard AA +SET cogs = 0 +from (SELECT distinct order_id, sku + from raena_analytics.beau_cogs_update) BB + where AA.external_id = BB.order_id +AND AA.sku = BB.sku; + + +DROP TABLE IF EXISTS raena_analytics.business_dimensions_stage1; + + +CREATE TABLE raena_analytics.business_dimensions_stage1 AS +SELECT distinct A.id AS external_id , + reseller_id, + customer_id, + cart_id, + A.flash_sale_id, + shipping_to, + payment_status, + A.status, + A.stock_type, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN E.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + 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 , + replace(cast(A.reseller_info::json->'city' AS varchar),'\"','') reseller_city, + replace(cast(A.reseller_info::json->'province' AS varchar),'\"','') reseller_province, + replace(cast(A.reseller_info::json->'name' AS varchar),'\"','') reseller_name, + replace(cast(A.reseller_info::json->'email' AS varchar),'\"','') reseller_email, + replace(cast(A.reseller_info::json->'mobile' AS varchar),'\"','') reseller_mobile, + replace(cast(A.reseller_info::json->'store' AS varchar),'\"','') reseller_store, + replace(cast(A.reseller_info::json->'tierName' AS varchar),'\"','') reseller_tier_name, + shipping_address_line1 , + shipping_address_line2 , + shipping_pincode , + shipping_district, + shipping_city , + shipping_province , + cast(is_bank_transfer AS varchar) is_bank_transfer , + cast(is_campaign AS varchar)is_campaign , + discount_type, + shipping_coupon_discount +FROM raena_order_management.order A +LEFT JOIN raena_analytics.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 +LEFT JOIN raena_order_management.channel E ON A.channel_id = E.id +WHERE cast(A.created_at+interval'7 hours' AS date) >='$reportDate' ; + +DELETE +FROM raena_analytics.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + +INSERT INTO raena_analytics.business_report +SELECT DISTINCT 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, + A.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 , + a.is_campaign , + C.Country, + B.quantity , + B.retail_price, + seller_margin, + discounted_price, + loyalty_discount, + effective_coupon_discount , + additional_discount, + item_type, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END DSF_flag, + CASE + WHEN D.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 D.name IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type, + A.order_type, + F.is_offender, + cohort +FROM raena_analytics.business_dimensions_stage1 A +LEFT JOIN raena_analytics.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 +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) F on B.product_sku= F.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where payment_status = 'Paid' or id like 'PL%' + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid' ); + +DROP TABLE IF EXISTS raena_analytics.business_report_v1; + + +CREATE TABLE raena_analytics.business_report_v1 AS +SELECT * +FROM raena_analytics.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + +DELETE +FROM raena_analytics.business_report USING + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B +WHERE raena_analytics.business_report.external_id = B.external_id + AND raena_analytics.business_report.sku=B.sku + AND raena_analytics.business_report.external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + +INSERT INTO raena_analytics.business_report +SELECT external_id, + created_date, + order_discount_amount, + shipping_cost, + coupon_code, + brand_name, + product_type, + category_name, + sku_name, + sku, + reseller_id, + customer_id, + cart_id, + flash_sale_id, + shipping_to, + payment_status, + status, + stock_type, + order_placed_by, + order_warehouse, + channel_id, + medium, + marketplace, + provider, + 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, + country, + quantity, + 0 retail_price, + 0 seller_margin, + 0 discount_price, + 0 loyalty_discount, + 0 effective_coupon_discount, + 0 additional_discount, + item_type, + discount_promo_type, + 0 promo_amount, + 0 actual_wholesale_price, + 0 effective_wholesale_price, + reseller_province, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort +FROM + (SELECT raena_analytics.business_report_v1.* + FROM raena_analytics.business_report_v1 + INNER JOIN + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B ON raena_analytics.business_report_v1.external_id = B.external_id + AND raena_analytics.business_report_v1.sku=B.sku) A +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + +SELECT max(created_date) business_date +FROM raena_analytics.business_report; + + +DELETE +FROM raena_analytics.base_netsuite_final +WHERE external_id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + +DELETE +FROM raena_analytics.business_report +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + DELETE +FROM raena_analytics.gm_dashboard +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + +insert into raena_analytics.base_netsuite_final +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +''coupon_applied_on, +''tier, +sku product_sku, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +(pre_discount_rev-post_discount_revenue)/quantity coupon_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 loyalty_discount, +0 additional_discount, +'' item_type, +0 diff, +'' discount_type, +0 promo_amount, +pre_discount_rev/quantity actual_wholesale_price, +pre_discount_rev/quantity effective_wholesale_price, +false is_campaign, +'' sales_sub_order_id, +'' cogs_discount_type, +'' parent_sku +from raena_analytics.sk_product; + + +update raena_analytics.gm_dashboard +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.gm_dashboard +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + + +update raena_analytics.gm_dashboard +set effective_coupon_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and effective_coupon_discount >0; + + +update raena_analytics.OM_GM_DB_Product_category +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.OM_GM_DB_Product_category +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + + +insert into raena_analytics.gm_dashboard +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +B.province shipping_province, +'SK Product'coupon_code, +'' discount_type, +'' coupon_applied_on, +brand_name, +category_name, +product_type, +A.sku, +sku_name, +B.name reseller_name, +reseller reseller_email, +B.mobile reseller_mobile, +D.name tier_name, +B.id reseller_id, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 additional_discount, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +'' item_type, +'Admin Panel'order_placed_by, +cogs/quantity cogs, +'' order_recipient, +'' brand_type, +'SK product' customer_type, +gm_target, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +'' cogs_flag, +false is_campaign, +false dsf_flag, +'' cogs_discount_type, +'SK product' order_type, +'' parent_sku, +'' is_offender, +'' cohort +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.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 D.name =BB.tierName ; + + +insert into raena_analytics.business_report +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +brand_name, +product_type, +category_name, +sku_name, +A.sku, +B.id reseller_id, +null customer_id, +null cart_id, +null flash_sale_id, +null shipping_to, +'Paid' payment_status, +'' status, +'' stock_type, +'Admin Panel' order_placed_by, +'' order_warehouse, +'' channel_id, +'' medium, +'' marketplace, +'' provider, +D.name tier, +city reseller_city, +B.name reseller_name, +reseller reseller_email, +mobile reseller_mobile, +store reseller_store, +address_line1 shipping_address_line1, +address_line2 shipping_address_line2, +pincode shipping_pincode, +district shipping_district, +city shipping_city, +province shipping_province, +false is_bank_transfer, +false is_campaign, +'' country, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 additional_discount, +'' item_type, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +province reseller_province, +false dsf_flag, +case when \"el/pl_flag\"= 'Yes' then 'EL/PL' end brand_type, +'SK Product 'order_type, +'' cohort +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.id ; + + + + + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.OM_GM_DB_Product_category +SELECT external_id, transaction_date, + \"Time\", + order_discount_amount, + shipping_cost, + coupon_code , + discount_type , + coupon_applied_on , + brand_name, + category_name , + product_type , + sku, + sku_name , + reseller_name , + reseller_email , + base.reseller_mobile , + tier_name , + reseller_id , + quantity , + retail_price , + seller_margin , + discounted_price , + additional_discount, + item_type , + order_placed_by , + cogs, + shipping_province , + Flag , + wholesale_price, + order_recipient, + brand_type, + Customer_type, + gm_target, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + CASE WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 28 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=28 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=28 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + is_campaign, + dsf_flag, + cogs_flag, + cogs_discount_type, + order_type, + parent_sku, + First_transaction_date, + is_offender, + cohort +FROM + (SELECT A.external_id , + cast(transaction_date AS date) 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + is_campaign, + A.dsf_flag, + A.cogs_flag, + A.cogs_discount_type, + A.order_type, + parent_sku, + is_offender, + cohort + FROM raena_analytics.GM_dashboard A + LEFT JOIN + (SELECT DISTINCT external_id + FROM + (SELECT external_id, + discounted_price , + sku , + (((retail_price-seller_margin)*quantity) -(cogs*quantity))/(CASE WHEN retail_price<>0 THEN (retail_price-seller_margin)*quantity END) Pre_discount, + CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/(discounted_price*quantity) ELSE 0 END Post_discount + FROM raena_analytics.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 raena_analytics.pricing_sheet P ON A.sku=p.skucode + WHERE cast(transaction_date AS date) >='$reportDate' + AND A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid')) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(transaction_date) om_gm_db_product_date +FROM raena_analytics.OM_GM_DB_Product_category; + +update raena_analytics.gm_dashboard + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.gm_dashboard where sku like '______X'); + + update raena_analytics.om_gm_db_product_category + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.om_gm_db_product_category where sku like '______X'); + + + +update raena_analytics.OM_GM_DB_Product_category +set retail_price=0, + seller_margin=0, + additional_discount=0, + wholesale_price=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 + where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + +update raena_analytics.gm_dashboard +set retail_price=0, + seller_margin=0, + loyalty_discount=0, + effective_coupon_discount=0, + additional_discount=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 +where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + + DELETE +FROM raena_analytics.GM_GROWTH_TAB1 +WHERE created_date >='$reportDate'; + +INSERT INTO raena_analytics.GM_GROWTH_TAB1 +SELECT tier, + 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, + base.reseller_mobile, + Number_of_Orders , + RSP , + wholesale_price , + Payment_Price , + Effective_Coupon_Discount, + Total_Loyalty_point, + additional_discount, + seller_margin, + AOV, + quantity, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + CASE + WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + external_id, + order_type, + First_transaction_date, + is_offender, + cohort +FROM + (SELECT tier, + 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, + reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + external_id, + order_type, + is_offender, + cohort + FROM + (SELECT DISTINCT coalesce(a.tier,'NA') AS tier, + 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, + reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + row_number() over (partition BY external_id + ORDER BY external_id) AS R + FROM raena_analytics.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 (payment_status='Paid' or a.external_id like 'PL%') + AND created_date >= '$reportDate') A + GROUP BY tier, + 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, + reseller_mobile, + external_id, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(created_date) gm_growth_date +FROM raena_analytics.GM_GROWTH_TAB1; + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB2; + + +CREATE TABLE raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'quarter' AS frequency, + To_char(created_date,'quarter') AS month_name, + date_part('quarter',created_date) AS sort , + * +FROM raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'month' AS frequency, + To_char(created_date,'month') AS month_name, + date_part('month',created_date) AS sort , + * +FROM raena_analytics.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 raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +ORDER BY 3; + + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB3; +CREATE TABLE raena_analytics.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +ORDER BY 3; + +DROP TABLE raena_analytics.shipping_fee_base_table; + + +CREATE TABLE raena_analytics.shipping_fee_base_table AS +SELECT A.order_id , + sum(CASE WHEN A.sales_sub_order_shipment_id = C.reference_id THEN ((C.shipping_amount)/(C.order_weight))*(C.final_weight) ELSE B.shipping_amount END) shipping_amount +FROM + (SELECT DISTINCT order_id , + sales_sub_order_shipment_id + FROM raena_order_management.sales_sub_order) A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.sales_sub_order_shipment_id = B.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping C ON A.sales_sub_order_shipment_id = C.reference_id +GROUP BY A.order_id; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage1 ; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + + +DROP TABLE raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.shipping_fee_sku_base_stage2_v1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2_v1 AS +SELECT order_id , + sales_sub_order_shipment_id, + CASE + WHEN (sum(B.weight*Quantity))<1.3 THEN 1 + WHEN (sum(B.weight*Quantity))>=1.3 + AND (ABS(sum(B.weight*Quantity)) - FLOOR(ABS(sum(B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.weight*Quantity))+1 + ELSE FLOOR(sum(B.weight*Quantity)) + END AS sku_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +GROUP BY 1, + 2; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2 AS +SELECT A.*, + B.weight*sku_weight/sum(B.weight) over(partition BY A.order_id , A.sales_sub_order_shipment_id) Final_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.shipping_fee_sku_base_stage2_v1 C ON A.order_id = C.order_id +AND A.sales_sub_order_shipment_id = C.sales_sub_order_shipment_id +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage3; + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage3 AS +SELECT A.*, + (Final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(Final_weight) over(partition BY A.sales_sub_order_shipment_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage2 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.sales_sub_order_shipment_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.sales_sub_order_shipment_id = C.reference_id ; + +DROP TABLE raena_analytics.sku_level_shipping_fee_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM raena_analytics.gm_dashboard A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +where A.order_type <> 'SK product'; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage1 ; + + +DROP TABLE raena_analytics.sku_level_shipping_fee_old_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3 +union + select distinct order_id , sku , 'Product' , + sum(shipping_fee_sku_level) + from raena_analytics.sk_product +group by 1,2 ; + + + +DROP TABLE IF EXISTS raena_analytics.payment_order_stage; + + +CREATE TABLE raena_analytics.payment_order_stage AS +SELECT id, + max(created_at) created_at, + replace(cast(jsonb_array_elements(order_ids) AS varchar),'\"','') AS order_id, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_order_management.payment +where payment_status ='Paid' +GROUP BY 1, + 3, + 4, + 5, + 6, + 7; + + +DROP TABLE IF EXISTS raena_analytics.payment_order; + + +CREATE TABLE raena_analytics.payment_order AS +SELECT A.id , + A.created_at, + A.order_id, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_analytics.payment_order_stage A +INNER JOIN + (SELECT order_id , + max(created_at) max_created_at + FROM raena_analytics.payment_order_stage + GROUP BY order_id) B ON A.created_at= B.max_created_at +AND A.order_id = B.order_id; + +select count(1) from raena_analytics.payment_order; + +drop table raena_analytics.om_app_admin_dump_table; + + +CREATE TABLE raena_analytics.om_app_admin_dump_table AS +/*SELECT raena_order_management.order.id AS order_id, + (raena_order_management.order.created_at+interval '7 hours') AS order_date, + (raena_order_management.order.created_at+interval '7 hours') AS id_order_date, + status AS order_status, + Payment_status, + D.name AS channel_name, + cast(is_bank_transfer AS varchar) AS bank_transfer, + cast(is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + payment_amount, + shipping_amount AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + Coupon_code, + replace(cast(reseller_info::json->'name' as varchar),'\"','') AS Reseller_name, + replace(cast(reseller_info::json->'email'as varchar),'\"','') AS Reseller_email, + replace(cast(reseller_info::json->'mobile'as varchar),'\"','') AS Reseller_mobile, + concat(concat(concat(replace(cast(reseller_info::json->'addressLine1'as varchar),'\"','') || ' ,',replace(cast(reseller_info::json->'addressLine2'as varchar),'\"','') || ', '), concat(replace(cast(reseller_info::json->'city'as varchar),'\"','') || ' ,',replace(cast(reseller_info::json->'pincode'as varchar),'\"','') || ' ,')), replace(cast(reseller_info::json->'province'as varchar),'\"','')) AS Reseller_address, + replace(cast(reseller_info::json->'store'as varchar),'\"','') AS store_name, + replace(cast(reseller_info::json->'tierName'as varchar),'\"','') AS tier, + replace(cast(customer_info::json->'name'as varchar),'\"','') AS customer_name, + replace(cast(customer_info::json->'email'as varchar),'\"','') AS customer_email, + concat(concat(concat(replace(cast(customer_info::json->'addressLine1'as varchar),'\"','') || ' ,',replace(cast(customer_info::json->'addressLine2'as varchar),'\"','') || ', '), concat(replace(cast(customer_info::json->'city'as varchar),'\"','') || ' ,',replace(cast(customer_info::json->'pincode'as varchar),'\"','') || ' ,')), replace(cast(customer_info::json->'province'as varchar),'\"','')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN order_placed_by ='Raena' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + raena_order_management.order.stock_type AS product_stock_type, + sku AS product_sku, + oi.name AS product_name, + product_code, + date_key, + product_type, + batch, + country, + quantity, + wholesale_price , + retail_price, + total_price, + Total_earnings, + raena_order_management.order.is_slash_price_enabled , + Brand_name, + category_name, + product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + 0 total_cogs, + 'NA' bundle_sku, + 0 sku_level_shipping_fee +FROM raena_order_management.order +INNER JOIN raena_order_management.order_item oi ON raena_order_management.order .id=oi.order_id +left join raena_analytics.payment_order C on raena_order_management.order.id=C.order_id +left join raena_order_management.channel D on raena_order_management.order.channel_id = D.id +WHERE raena_order_management.order.created_at <'2021-10-28' +UNION +*/ +SELECT A.id AS order_id, + (A.created_at+interval '7 hours') AS order_date, + (A.created_at+interval '7 hours') AS id_order_date, + A.status AS order_status, + A.Payment_status, + D.name AS channel_name, + cast(A.is_bank_transfer AS varchar) AS bank_transfer, + cast(A.is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + GB.payment_amount, + coalesce(GB.new_shipping_amount_final,shipping_amount) AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + A.Coupon_code, + replace(cast(reseller_info::json->'name' as varchar),'\"','') AS Reseller_name, + replace(cast(reseller_info::json->'email'as varchar),'\"','') AS Reseller_email, + replace(cast(reseller_info::json->'mobile'as varchar),'\"','') AS Reseller_mobile, + concat(concat(concat(replace(cast(reseller_info::json->'addressLine1'as varchar),'\"','') || ' ,',replace(cast(reseller_info::json->'addressLine2'as varchar),'\"','') || ', '), concat(replace(cast(reseller_info::json->'city'as varchar),'\"','') || ' ,',replace(cast(reseller_info::json->'pincode'as varchar),'\"','') || ' ,')), replace(cast(reseller_info::json->'province'as varchar),'\"','')) AS Reseller_address, + replace(cast(reseller_info::json->'store'as varchar),'\"','') AS store_name, + replace(cast(reseller_info::json->'tierName'as varchar),'\"','') AS tier, + replace(cast(customer_info::json->'name'as varchar),'\"','') AS customer_name, + replace(cast(customer_info::json->'email'as varchar),'\"','') AS customer_email, + concat(concat(concat(replace(cast(customer_info::json->'addressLine1'as varchar),'\"','') || ' ,',replace(cast(customer_info::json->'addressLine2'as varchar),'\"','') || ', '), concat(replace(cast(customer_info::json->'city'as varchar),'\"','') || ' ,',replace(cast(customer_info::json->'pincode'as varchar),'\"','') || ' ,')), replace(cast(customer_info::json->'province'as varchar),'\"','')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + A.shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN A.order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + A.stock_type AS product_stock_type, + coalesce(GB.sku,oi.sku) AS product_sku, + coalesce(gb.sku_name,oi.name) AS product_name, + product_code, + date_key, + coalesce(GB.product_type,oi.product_type) product_type, + batch, + country, + coalesce(GB.quantity,oi.quantity) quantity, + coalesce(GB.actual_wholesale_price,oi.wholesale_price) wholesale_price, + coalesce(GB.retail_price,oi.retail_price) retail_price, + ((GB.retail_price*GB.quantity)-(GB.seller_margin*GB.quantity)) total_price, + Total_earnings, + A.is_slash_price_enabled , + coalesce(gb.brand_name,oi.Brand_name) brand_name, + coalesce(gb.category_name,oi.category_name) category_name, + oi.product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + (GB.cogs) as total_cogs, + case when oi.parent_sku is null then 'NA' else oi.parent_sku end bundle_sku, + new_shipping_amount sku_level_shipping_amount, + GB.discounted_price*GB.quantity sku_level_payment_amount, + is_cash_on_delivery, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_order_management.order A +inner join (select AD.* , + sum(new_shipping_amount) over(partition by AD.external_id) new_shipping_amount_final , + sum(discounted_price*quantity) over(partition by AD.external_id) payment_amount + from (SELECT A.*, + discounted_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_discounted_price, + actual_wholesale_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_actual_wholesale_price , + retail_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_retail_price, + seller_margin/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_seller_margin , + cogs/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_cogs, + (case when (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)/count(1) over(partition BY A.external_id,A.sku,A.item_type) new_shipping_amount + 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) AD + )GB + ON A.id = GB.external_id AND GB.sku not like 'BAZ%' + left JOIN (select A.*,B.sku parent_sku + from raena_order_management.order_item A + left join raena_order_management.order_item B on A.parent_item_id = B.id ) oi + ON GB.external_id=oi.order_id + AND OI.SKU not like 'BAZ%' + AND GB.sku=oi.sku + and GB.parent_sku= oi.parent_sku +left join raena_analytics.payment_order C on A.id=C.order_id +left join raena_order_management.channel D on A.channel_id = D.id +WHERE cast(A.created_at+interval'7 hours' as date)>='2021-10-28'; + +DROP TABLE IF EXISTS raena_analytics.remove_duplicate_from_app_admin; + + +CREATE TABLE raena_analytics.remove_duplicate_from_app_admin AS +SELECT A.external_id, + wholesale_price_gm, + wholesale_price_app_admin, + wholesale_price_gm-wholesale_price_app_admin diff +FROM + (SELECT external_id , + sum(discounted_price*quantity) P1, + sum((retail_price*quantity)-(seller_margin*quantity)) wholesale_price_gm + FROM raena_analytics.gm_dashboard gd + GROUP BY external_id) A +FULL OUTER JOIN + (SELECT order_id , + payment_amount p2, + sum(total_price) wholesale_price_app_admin + FROM raena_analytics.om_app_admin_dump_table oaadt + GROUP BY order_id, + payment_amount) B ON A.external_id= B.order_id +WHERE wholesale_price_gm-wholesale_price_app_admin<>0; + +DROP TABLE IF EXISTS raena_analytics.dubplicate_record_app_admin; + +CREATE TABLE raena_analytics.dubplicate_record_app_admin AS +SELECT DISTINCT * +FROM raena_analytics.om_app_admin_dump_table oaadt +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + +DELETE +FROM raena_analytics.om_app_admin_dump_table +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + +INSERT INTO raena_analytics.om_app_admin_dump_table +SELECT * +FROM raena_analytics.dubplicate_record_app_admin; + +" > /home/ec2-user/cronjob/postgresql/gm_code/gm_etl_cron.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/gm_code/gm_etl_cron.sql diff --git a/postgresql/gm_code/gm_etl_cron.sql b/postgresql/gm_code/gm_etl_cron.sql new file mode 100644 index 0000000..1a8fe74 --- /dev/null +++ b/postgresql/gm_code/gm_etl_cron.sql @@ -0,0 +1,2306 @@ + + + +DROP TABLE IF EXISTS raena_analytics.manual_bundle_sku_data ; + + +CREATE TABLE raena_analytics.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(cast(C.tier_price::json->'07030fbe-5801-4318-9e97-fe33fa169894' as varchar) AS decimal(22,2)) bronze_price, + cast(cast(C.tier_price::json->'8eb95d6e-915a-4a91-9c12-fa43db995e19' as varchar) AS decimal(22,2)) silver_price, + cast(cast(C.tier_price::json->'bf645e97-8a48-4977-8367-e987489760f9' as varchar) AS decimal(22,2)) gold_price, + cast(C.retail_price AS decimal(22,2)) child_retail_price +FROM raena_analytics.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 raena_analytics.order_level_data; + + +CREATE TABLE raena_analytics.order_level_data AS +SELECT DISTINCT external_id, + transaction_date+interval'7 hours' AS transaction_date, + discount_amount, + shipping_cost, + A.coupon_code, + replace(cast(reseller_tier_name as varchar),'"','')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, + is_campaign, + order_placed_by +FROM + (SELECT A.id AS external_id , + (A.created_at) AS transaction_date , + A.discount_amount, + applied_shipping_amount shipping_cost, + A.coupon_code, + A.reseller_info::json->'tierName' 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, + cast(is_campaign as varchar) is_campaign, + order_placed_by + FROM raena_order_management.order A + WHERE ( payment_status='Paid' or id like 'PL%') + AND cast(A.created_at+interval '7 hours' AS date) >='2024-02-25' ) A +LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code; + + +SELECT max(created_at) +FROM raena_order_management.order ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage1_V1; + + +CREATE TABLE raena_analytics.base_netsuite_stage1_V1 AS +SELECT DISTINCT transaction_date 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_wholesale_price, + CASE + WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount + ELSE B.coupon_discount + END discount_price, + CASE + WHEN discount_amount > 0 + AND A.coupon_code IS NULL + AND A.order_placed_by = 'admin' + AND coupon_applied_on IS NULL THEN 'Cart' + ELSE coupon_applied_on + END 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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type, + a.is_campaign, + B.cogs_discount_type +FROM raena_analytics.order_level_data A +LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id and status <> 'Cancelled' +LEFT JOIN raena_analytics.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, + actual_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+interval'7 hours' AS date)>='2024-02-25') F ON B.id = F.sales_sub_order_id +ORDER BY 1, + 2; + + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1_base ; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1_base 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, + actual_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, + price_type, + is_campaign, + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND A.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + cogs_discount_type +FROM raena_analytics.base_netsuite_stage1_V1 A +LEFT JOIN + (SELECT external_id , + sku , + final_loyalty_point + FROM raena_analytics.final_loyalty_point) B ON A.external_id = B.external_id +AND A.sku = B.sku +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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,4) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON A.sku = D.sku +AND A.transaction_date::date BETWEEN D.created_at AND last_date; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1 AS +SELECT transaction_date, + A.external_id, + A.sku, + parent_sku, + quantity quantity, + parent_quantity, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE retail_price + END retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_retail_price + END parent_retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE effective_wholesale_price + END effective_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_wholesale_price + END parent_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE actual_wholesale_price + END actual_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_price + END discount_price, + coupon_applied_on, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_amount + END discount_amount, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE dynamic_price + END dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_dynamic_price + END parent_dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_price + END payment_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_amount + END payment_amount, + product_class, + parent_product_class, + product_type_class, + sales_sub_order_id, + loyalty_discount, + price_type, + is_campaign, + CASE + WHEN A.sku = D.sku THEN 0 + ELSE cogs + END cogs, + cogs_flag, + cogs_discount_type +FROM raena_analytics.base_netsuite_stage2_v1_base A +LEFT JOIN + (SELECT DISTINCT sku + FROM raena_catalog_management.product + WHERE lower(name) LIKE '%not for sale%' + OR lower(name) LIKE '%free item%' + OR lower(name) LIKE '%free gift%') D ON A.sku = D.sku ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage3_v1 ; + + +CREATE TABLE raena_analytics.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 + AND retail_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 discount_price + WHEN coupon_applied_on ='Cart' + AND discount_price=0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id) + END AS Final_discount , + retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + CASE + WHEN product_type_class IN ('Flash Bundle', + 'Flash') THEN 'Flash' + WHEN coupon_applied_on ='Cart' + AND price_type ='NEGOTIATED_PRICE' THEN 'Negotiated' + WHEN coupon_applied_on = 'Shipping Fee' THEN 'Shipping' + WHEN coupon_applied_on IS NOT NULL THEN 'Brand' + END discount_type +FROM raena_analytics.base_netsuite_stage2_v1 A ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage4_v1 ; + + +CREATE TABLE raena_analytics.base_netsuite_stage4_v1 AS +SELECT A.* , + CASE + WHEN (parent_product_class= 'Bundle' + OR product_type_class IN('Bundle', 'Flash Bundle')) + AND A.external_id = B.external_id + AND A.parent_sku = B.parent_sku and ttl_wholesale_price<>0 THEN (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/ttl_wholesale_price) + END AS additional_discount, + CASE + WHEN final_discount<>0 THEN final_discount-seller_margin + END AS effective_coupon_discount +FROM raena_analytics.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 + FROM raena_analytics.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 raena_analytics.base_netsuite_final +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.order_level_data); + + +INSERT INTO raena_analytics.base_netsuite_final +SELECT A.external_id, + A.transaction_date 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) -coalesce((CASE WHEN final_discount >0 THEN 0 ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity END),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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, + 0 diff, + CASE + WHEN price_type IS NOT NULL THEN price_type + ELSE discount_type + END discount_type , + CASE + WHEN final_discount >0 THEN 0 + ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity + END promo_amount, + actual_wholesale_price, + effective_wholesale_price, + A.is_campaign, + '' sales_sub_order_id, + cogs_discount_type, + parent_sku +FROM raena_analytics.order_level_data A +LEFT JOIN raena_analytics.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + + + +UPDATE raena_analytics.base_netsuite_final AA +SET discounted_price = new_discounted_price +from (SELECT external_id , + product_sku , + cast(settlement_amount/quantity AS decimal(22,2)) new_discounted_price + FROM + (SELECT A.*, + discounted_price , + (effective_wholesale_price*B."Settlement amount")/sum(effective_wholesale_price) over (partition BY external_id) settlement_amount , + B."Settlement amount" + FROM raena_analytics.base_netsuite_final A + INNER JOIN (select distinct * from raena_analytics.mp_orders_hn_wd) B ON external_id= B."Order ID " + WHERE A.transaction_date >'2022-12-31' and effective_wholesale_price<>0 ) A) BB + where AA.external_id = BB.external_id +AND AA.product_sku = BB.product_sku +and AA.transaction_date >'2022-12-31'; + + +update raena_analytics.base_netsuite_final A +set discounted_price = B.payment_price/quantity +from (select order_id,sku, max(payment_price) payment_price from raena_analytics.order_data_update group by 1,2) B +where A.external_id = B.order_id and A.parent_sku = B.sku and product_sku = parent_sku ; + + +update raena_analytics.base_netsuite_final A +set discounted_price = new_payment_price/A.quantity +from raena_analytics.import_data_for_sku_level_payment_amount B +where A.external_id = B.order_id and A.parent_sku = B.sku and A.product_sku <> A.parent_sku; + +SELECT max(transaction_date) Netsuite_base_date +FROM raena_analytics.base_netsuite_final; + + +DROP TABLE IF EXISTS raena_analytics.gm_dimensions_stage1; + + +CREATE TABLE raena_analytics.gm_dimensions_stage1 AS +SELECT distinct A.id AS external_id , + shipping_province, + reseller_id , + replace(cast(A.reseller_info::json ->'name' AS varchar),'"','') reseller_name, + replace(cast(A.reseller_info::json ->'email' AS varchar),'"','')reseller_email, + replace(cast(A.reseller_info::json ->'mobile' AS varchar),'"','') reseller_mobile, + discount_type, + replace(cast(A.reseller_info::json ->'tierName' AS varchar),'"','') reseller_tier_name, + shipping_to, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + cast(is_campaign AS varchar) is_campaign +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 +LEFT JOIN raena_order_management.channel D ON A.channel_id = D.id +WHERE (payment_status ='Paid' or A.id like 'PL%') + AND cast(A.created_at+interval'7 hours' AS date) >='2024-02-25'; + +drop table if exists raena_analytics.sku_gm_mapping; + +create table raena_analytics.sku_gm_mapping +as +select A.sku ,A.name sku_name,B.name brand_name,C.name as category_name,product_type + 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 ; + + +DELETE +FROM raena_analytics.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.GM_dashboard +SELECT A.external_id , + B.transaction_date AS transaction_date, + B.order_discount_amount , + B.shipping_cost , + A.shipping_province, + B.coupon_code , + A.discount_type , + B.coupon_applied_on , + brand_name, + category_name , + product_type , + product_sku sku, + sku_name , + A.reseller_name , + A.reseller_email , + A.reseller_mobile , + reseller_tier_name tier_name, + A.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 , + coalesce(effective_cogs,coalesce(C.applied_cogs,D.cogs-coalesce(promo,0))) 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type , + Customer_type, + BB.gm_target, + B.discount_type discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + coalesce(case when cogs_type ='REGULAR' then 'Promo' else cogs_type end ,CASE WHEN B.product_sku = D.sku + AND B.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + A.is_campaign, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END AS DSF_Flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender, + cohort +FROM raena_analytics.gm_dimensions_stage1 A +INNER JOIN raena_analytics.base_netsuite_final B ON A.external_id =B.external_id +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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,4) C ON B.product_sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON B.product_sku = D.sku +AND B.transaction_date::date BETWEEN D.created_at AND last_date +left join (select distinct order_id , + created_at+interval'7 hours' AS created_date, + parent_sku sku, + effective_cogs + from raena_order_management.sales_sub_order + where price_type is not null and parent_sku not like 'BAZ%')XX + on A.external_Id=XX.order_id and B.product_sku= XX.sku +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 B.product_sku = BB.sku +AND A.reseller_tier_name =BB.tierName +LEFT JOIN raena_analytics.sku_gm_mapping AAA ON B.product_sku = AAA.sku +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) BBB on B.product_sku=BBB.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where payment_status = 'Paid' + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid') ; + + + + +SELECT max(transaction_date) gm_dashboard_date +FROM raena_analytics.GM_dashboard; + +DROP TABLE IF EXISTS raena_analytics.GM_dashboard_v1; + + +CREATE TABLE raena_analytics.GM_dashboard_v1 AS +SELECT * +FROM raena_analytics.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +DELETE +FROM raena_analytics.GM_dashboard +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.GM_dashboard +SELECT external_id, + transaction_date, + order_discount_amount, + shipping_cost, + shipping_province, + coupon_code, + discount_type, + coupon_applied_on, + brand_name, + category_name, + product_type, + sku, + sku_name, + reseller_name, + reseller_email, + reseller_mobile, + tier_name, + reseller_id, + quantity, + 0 retail_price, + 0 seller_margin, + 0 , + 0 , + 0, + 0, + item_type, + order_placed_by, + 0 cogs, + order_recipient, + brand_type, + customer_type, + gm_target, + discount_promo_type, + 0 promo_amount, + 0, + 0, + cogs_flag, + is_campaign, + dsf_flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender +FROM raena_analytics.GM_dashboard_v1 +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + + +UPDATE raena_analytics.gm_dashboard AA +SET cogs = 0 +from (SELECT distinct order_id, sku + from raena_analytics.beau_cogs_update) BB + where AA.external_id = BB.order_id +AND AA.sku = BB.sku; + + +DROP TABLE IF EXISTS raena_analytics.business_dimensions_stage1; + + +CREATE TABLE raena_analytics.business_dimensions_stage1 AS +SELECT distinct A.id AS external_id , + reseller_id, + customer_id, + cart_id, + A.flash_sale_id, + shipping_to, + payment_status, + A.status, + A.stock_type, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN E.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + 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 , + replace(cast(A.reseller_info::json->'city' AS varchar),'"','') reseller_city, + replace(cast(A.reseller_info::json->'province' AS varchar),'"','') reseller_province, + replace(cast(A.reseller_info::json->'name' AS varchar),'"','') reseller_name, + replace(cast(A.reseller_info::json->'email' AS varchar),'"','') reseller_email, + replace(cast(A.reseller_info::json->'mobile' AS varchar),'"','') reseller_mobile, + replace(cast(A.reseller_info::json->'store' AS varchar),'"','') reseller_store, + replace(cast(A.reseller_info::json->'tierName' AS varchar),'"','') reseller_tier_name, + shipping_address_line1 , + shipping_address_line2 , + shipping_pincode , + shipping_district, + shipping_city , + shipping_province , + cast(is_bank_transfer AS varchar) is_bank_transfer , + cast(is_campaign AS varchar)is_campaign , + discount_type, + shipping_coupon_discount +FROM raena_order_management.order A +LEFT JOIN raena_analytics.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 +LEFT JOIN raena_order_management.channel E ON A.channel_id = E.id +WHERE cast(A.created_at+interval'7 hours' AS date) >='2024-02-25' ; + +DELETE +FROM raena_analytics.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + +INSERT INTO raena_analytics.business_report +SELECT DISTINCT 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, + A.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 , + a.is_campaign , + C.Country, + B.quantity , + B.retail_price, + seller_margin, + discounted_price, + loyalty_discount, + effective_coupon_discount , + additional_discount, + item_type, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END DSF_flag, + CASE + WHEN D.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 D.name IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type, + A.order_type, + F.is_offender, + cohort +FROM raena_analytics.business_dimensions_stage1 A +LEFT JOIN raena_analytics.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 +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) F on B.product_sku= F.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where payment_status = 'Paid' or id like 'PL%' + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid' ); + +DROP TABLE IF EXISTS raena_analytics.business_report_v1; + + +CREATE TABLE raena_analytics.business_report_v1 AS +SELECT * +FROM raena_analytics.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + +DELETE +FROM raena_analytics.business_report USING + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B +WHERE raena_analytics.business_report.external_id = B.external_id + AND raena_analytics.business_report.sku=B.sku + AND raena_analytics.business_report.external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + +INSERT INTO raena_analytics.business_report +SELECT external_id, + created_date, + order_discount_amount, + shipping_cost, + coupon_code, + brand_name, + product_type, + category_name, + sku_name, + sku, + reseller_id, + customer_id, + cart_id, + flash_sale_id, + shipping_to, + payment_status, + status, + stock_type, + order_placed_by, + order_warehouse, + channel_id, + medium, + marketplace, + provider, + 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, + country, + quantity, + 0 retail_price, + 0 seller_margin, + 0 discount_price, + 0 loyalty_discount, + 0 effective_coupon_discount, + 0 additional_discount, + item_type, + discount_promo_type, + 0 promo_amount, + 0 actual_wholesale_price, + 0 effective_wholesale_price, + reseller_province, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort +FROM + (SELECT raena_analytics.business_report_v1.* + FROM raena_analytics.business_report_v1 + INNER JOIN + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B ON raena_analytics.business_report_v1.external_id = B.external_id + AND raena_analytics.business_report_v1.sku=B.sku) A +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + +SELECT max(created_date) business_date +FROM raena_analytics.business_report; + + +DELETE +FROM raena_analytics.base_netsuite_final +WHERE external_id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + +DELETE +FROM raena_analytics.business_report +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + DELETE +FROM raena_analytics.gm_dashboard +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + +insert into raena_analytics.base_netsuite_final +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +''coupon_applied_on, +''tier, +sku product_sku, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +(pre_discount_rev-post_discount_revenue)/quantity coupon_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 loyalty_discount, +0 additional_discount, +'' item_type, +0 diff, +'' discount_type, +0 promo_amount, +pre_discount_rev/quantity actual_wholesale_price, +pre_discount_rev/quantity effective_wholesale_price, +false is_campaign, +'' sales_sub_order_id, +'' cogs_discount_type, +'' parent_sku +from raena_analytics.sk_product; + + +update raena_analytics.gm_dashboard +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.gm_dashboard +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + + +update raena_analytics.gm_dashboard +set effective_coupon_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and effective_coupon_discount >0; + + +update raena_analytics.OM_GM_DB_Product_category +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.OM_GM_DB_Product_category +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + + +insert into raena_analytics.gm_dashboard +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +B.province shipping_province, +'SK Product'coupon_code, +'' discount_type, +'' coupon_applied_on, +brand_name, +category_name, +product_type, +A.sku, +sku_name, +B.name reseller_name, +reseller reseller_email, +B.mobile reseller_mobile, +D.name tier_name, +B.id reseller_id, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 additional_discount, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +'' item_type, +'Admin Panel'order_placed_by, +cogs/quantity cogs, +'' order_recipient, +'' brand_type, +'SK product' customer_type, +gm_target, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +'' cogs_flag, +false is_campaign, +false dsf_flag, +'' cogs_discount_type, +'SK product' order_type, +'' parent_sku, +'' is_offender, +'' cohort +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.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 D.name =BB.tierName ; + + +insert into raena_analytics.business_report +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +brand_name, +product_type, +category_name, +sku_name, +A.sku, +B.id reseller_id, +null customer_id, +null cart_id, +null flash_sale_id, +null shipping_to, +'Paid' payment_status, +'' status, +'' stock_type, +'Admin Panel' order_placed_by, +'' order_warehouse, +'' channel_id, +'' medium, +'' marketplace, +'' provider, +D.name tier, +city reseller_city, +B.name reseller_name, +reseller reseller_email, +mobile reseller_mobile, +store reseller_store, +address_line1 shipping_address_line1, +address_line2 shipping_address_line2, +pincode shipping_pincode, +district shipping_district, +city shipping_city, +province shipping_province, +false is_bank_transfer, +false is_campaign, +'' country, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 additional_discount, +'' item_type, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +province reseller_province, +false dsf_flag, +case when "el/pl_flag"= 'Yes' then 'EL/PL' end brand_type, +'SK Product 'order_type, +'' cohort +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.id ; + + + + + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.OM_GM_DB_Product_category +SELECT external_id, transaction_date, + "Time", + order_discount_amount, + shipping_cost, + coupon_code , + discount_type , + coupon_applied_on , + brand_name, + category_name , + product_type , + sku, + sku_name , + reseller_name , + reseller_email , + base.reseller_mobile , + tier_name , + reseller_id , + quantity , + retail_price , + seller_margin , + discounted_price , + additional_discount, + item_type , + order_placed_by , + cogs, + shipping_province , + Flag , + wholesale_price, + order_recipient, + brand_type, + Customer_type, + gm_target, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + CASE WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 28 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=28 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=28 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + is_campaign, + dsf_flag, + cogs_flag, + cogs_discount_type, + order_type, + parent_sku, + First_transaction_date, + is_offender, + cohort +FROM + (SELECT A.external_id , + cast(transaction_date AS date) 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + is_campaign, + A.dsf_flag, + A.cogs_flag, + A.cogs_discount_type, + A.order_type, + parent_sku, + is_offender, + cohort + FROM raena_analytics.GM_dashboard A + LEFT JOIN + (SELECT DISTINCT external_id + FROM + (SELECT external_id, + discounted_price , + sku , + (((retail_price-seller_margin)*quantity) -(cogs*quantity))/(CASE WHEN retail_price<>0 THEN (retail_price-seller_margin)*quantity END) Pre_discount, + CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/(discounted_price*quantity) ELSE 0 END Post_discount + FROM raena_analytics.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 raena_analytics.pricing_sheet P ON A.sku=p.skucode + WHERE cast(transaction_date AS date) >='2024-02-25' + AND A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid')) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(transaction_date) om_gm_db_product_date +FROM raena_analytics.OM_GM_DB_Product_category; + +update raena_analytics.gm_dashboard + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.gm_dashboard where sku like '______X'); + + update raena_analytics.om_gm_db_product_category + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.om_gm_db_product_category where sku like '______X'); + + + +update raena_analytics.OM_GM_DB_Product_category +set retail_price=0, + seller_margin=0, + additional_discount=0, + wholesale_price=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 + where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + +update raena_analytics.gm_dashboard +set retail_price=0, + seller_margin=0, + loyalty_discount=0, + effective_coupon_discount=0, + additional_discount=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 +where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + + DELETE +FROM raena_analytics.GM_GROWTH_TAB1 +WHERE created_date >='2024-02-25'; + +INSERT INTO raena_analytics.GM_GROWTH_TAB1 +SELECT tier, + 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, + base.reseller_mobile, + Number_of_Orders , + RSP , + wholesale_price , + Payment_Price , + Effective_Coupon_Discount, + Total_Loyalty_point, + additional_discount, + seller_margin, + AOV, + quantity, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + CASE + WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + external_id, + order_type, + First_transaction_date, + is_offender, + cohort +FROM + (SELECT tier, + 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, + reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + external_id, + order_type, + is_offender, + cohort + FROM + (SELECT DISTINCT coalesce(a.tier,'NA') AS tier, + 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, + reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + row_number() over (partition BY external_id + ORDER BY external_id) AS R + FROM raena_analytics.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) >= '2024-02-25' + AND channel_id IS NOT NULL) b ON a.external_id=b.id + WHERE (payment_status='Paid' or a.external_id like 'PL%') + AND created_date >= '2024-02-25') A + GROUP BY tier, + 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, + reseller_mobile, + external_id, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(created_date) gm_growth_date +FROM raena_analytics.GM_GROWTH_TAB1; + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB2; + + +CREATE TABLE raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'quarter' AS frequency, + To_char(created_date,'quarter') AS month_name, + date_part('quarter',created_date) AS sort , + * +FROM raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'month' AS frequency, + To_char(created_date,'month') AS month_name, + date_part('month',created_date) AS sort , + * +FROM raena_analytics.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 raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +ORDER BY 3; + + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB3; +CREATE TABLE raena_analytics.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +ORDER BY 3; + +DROP TABLE raena_analytics.shipping_fee_base_table; + + +CREATE TABLE raena_analytics.shipping_fee_base_table AS +SELECT A.order_id , + sum(CASE WHEN A.sales_sub_order_shipment_id = C.reference_id THEN ((C.shipping_amount)/(C.order_weight))*(C.final_weight) ELSE B.shipping_amount END) shipping_amount +FROM + (SELECT DISTINCT order_id , + sales_sub_order_shipment_id + FROM raena_order_management.sales_sub_order) A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.sales_sub_order_shipment_id = B.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping C ON A.sales_sub_order_shipment_id = C.reference_id +GROUP BY A.order_id; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage1 ; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + + +DROP TABLE raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.shipping_fee_sku_base_stage2_v1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2_v1 AS +SELECT order_id , + sales_sub_order_shipment_id, + CASE + WHEN (sum(B.weight*Quantity))<1.3 THEN 1 + WHEN (sum(B.weight*Quantity))>=1.3 + AND (ABS(sum(B.weight*Quantity)) - FLOOR(ABS(sum(B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.weight*Quantity))+1 + ELSE FLOOR(sum(B.weight*Quantity)) + END AS sku_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +GROUP BY 1, + 2; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2 AS +SELECT A.*, + B.weight*sku_weight/sum(B.weight) over(partition BY A.order_id , A.sales_sub_order_shipment_id) Final_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.shipping_fee_sku_base_stage2_v1 C ON A.order_id = C.order_id +AND A.sales_sub_order_shipment_id = C.sales_sub_order_shipment_id +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage3; + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage3 AS +SELECT A.*, + (Final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(Final_weight) over(partition BY A.sales_sub_order_shipment_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage2 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.sales_sub_order_shipment_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.sales_sub_order_shipment_id = C.reference_id ; + +DROP TABLE raena_analytics.sku_level_shipping_fee_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM raena_analytics.gm_dashboard A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +where A.order_type <> 'SK product'; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage1 ; + + +DROP TABLE raena_analytics.sku_level_shipping_fee_old_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3 +union + select distinct order_id , sku , 'Product' , + sum(shipping_fee_sku_level) + from raena_analytics.sk_product +group by 1,2 ; + + + +DROP TABLE IF EXISTS raena_analytics.payment_order_stage; + + +CREATE TABLE raena_analytics.payment_order_stage AS +SELECT id, + max(created_at) created_at, + replace(cast(jsonb_array_elements(order_ids) AS varchar),'"','') AS order_id, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_order_management.payment +where payment_status ='Paid' +GROUP BY 1, + 3, + 4, + 5, + 6, + 7; + + +DROP TABLE IF EXISTS raena_analytics.payment_order; + + +CREATE TABLE raena_analytics.payment_order AS +SELECT A.id , + A.created_at, + A.order_id, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_analytics.payment_order_stage A +INNER JOIN + (SELECT order_id , + max(created_at) max_created_at + FROM raena_analytics.payment_order_stage + GROUP BY order_id) B ON A.created_at= B.max_created_at +AND A.order_id = B.order_id; + +select count(1) from raena_analytics.payment_order; + +drop table raena_analytics.om_app_admin_dump_table; + + +CREATE TABLE raena_analytics.om_app_admin_dump_table AS +/*SELECT raena_order_management.order.id AS order_id, + (raena_order_management.order.created_at+interval '7 hours') AS order_date, + (raena_order_management.order.created_at+interval '7 hours') AS id_order_date, + status AS order_status, + Payment_status, + D.name AS channel_name, + cast(is_bank_transfer AS varchar) AS bank_transfer, + cast(is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + payment_amount, + shipping_amount AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + Coupon_code, + replace(cast(reseller_info::json->'name' as varchar),'"','') AS Reseller_name, + replace(cast(reseller_info::json->'email'as varchar),'"','') AS Reseller_email, + replace(cast(reseller_info::json->'mobile'as varchar),'"','') AS Reseller_mobile, + concat(concat(concat(replace(cast(reseller_info::json->'addressLine1'as varchar),'"','') || ' ,',replace(cast(reseller_info::json->'addressLine2'as varchar),'"','') || ', '), concat(replace(cast(reseller_info::json->'city'as varchar),'"','') || ' ,',replace(cast(reseller_info::json->'pincode'as varchar),'"','') || ' ,')), replace(cast(reseller_info::json->'province'as varchar),'"','')) AS Reseller_address, + replace(cast(reseller_info::json->'store'as varchar),'"','') AS store_name, + replace(cast(reseller_info::json->'tierName'as varchar),'"','') AS tier, + replace(cast(customer_info::json->'name'as varchar),'"','') AS customer_name, + replace(cast(customer_info::json->'email'as varchar),'"','') AS customer_email, + concat(concat(concat(replace(cast(customer_info::json->'addressLine1'as varchar),'"','') || ' ,',replace(cast(customer_info::json->'addressLine2'as varchar),'"','') || ', '), concat(replace(cast(customer_info::json->'city'as varchar),'"','') || ' ,',replace(cast(customer_info::json->'pincode'as varchar),'"','') || ' ,')), replace(cast(customer_info::json->'province'as varchar),'"','')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN order_placed_by ='Raena' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + raena_order_management.order.stock_type AS product_stock_type, + sku AS product_sku, + oi.name AS product_name, + product_code, + date_key, + product_type, + batch, + country, + quantity, + wholesale_price , + retail_price, + total_price, + Total_earnings, + raena_order_management.order.is_slash_price_enabled , + Brand_name, + category_name, + product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + 0 total_cogs, + 'NA' bundle_sku, + 0 sku_level_shipping_fee +FROM raena_order_management.order +INNER JOIN raena_order_management.order_item oi ON raena_order_management.order .id=oi.order_id +left join raena_analytics.payment_order C on raena_order_management.order.id=C.order_id +left join raena_order_management.channel D on raena_order_management.order.channel_id = D.id +WHERE raena_order_management.order.created_at <'2021-10-28' +UNION +*/ +SELECT A.id AS order_id, + (A.created_at+interval '7 hours') AS order_date, + (A.created_at+interval '7 hours') AS id_order_date, + A.status AS order_status, + A.Payment_status, + D.name AS channel_name, + cast(A.is_bank_transfer AS varchar) AS bank_transfer, + cast(A.is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + GB.payment_amount, + coalesce(GB.new_shipping_amount_final,shipping_amount) AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + A.Coupon_code, + replace(cast(reseller_info::json->'name' as varchar),'"','') AS Reseller_name, + replace(cast(reseller_info::json->'email'as varchar),'"','') AS Reseller_email, + replace(cast(reseller_info::json->'mobile'as varchar),'"','') AS Reseller_mobile, + concat(concat(concat(replace(cast(reseller_info::json->'addressLine1'as varchar),'"','') || ' ,',replace(cast(reseller_info::json->'addressLine2'as varchar),'"','') || ', '), concat(replace(cast(reseller_info::json->'city'as varchar),'"','') || ' ,',replace(cast(reseller_info::json->'pincode'as varchar),'"','') || ' ,')), replace(cast(reseller_info::json->'province'as varchar),'"','')) AS Reseller_address, + replace(cast(reseller_info::json->'store'as varchar),'"','') AS store_name, + replace(cast(reseller_info::json->'tierName'as varchar),'"','') AS tier, + replace(cast(customer_info::json->'name'as varchar),'"','') AS customer_name, + replace(cast(customer_info::json->'email'as varchar),'"','') AS customer_email, + concat(concat(concat(replace(cast(customer_info::json->'addressLine1'as varchar),'"','') || ' ,',replace(cast(customer_info::json->'addressLine2'as varchar),'"','') || ', '), concat(replace(cast(customer_info::json->'city'as varchar),'"','') || ' ,',replace(cast(customer_info::json->'pincode'as varchar),'"','') || ' ,')), replace(cast(customer_info::json->'province'as varchar),'"','')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + A.shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN A.order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + A.stock_type AS product_stock_type, + coalesce(GB.sku,oi.sku) AS product_sku, + coalesce(gb.sku_name,oi.name) AS product_name, + product_code, + date_key, + coalesce(GB.product_type,oi.product_type) product_type, + batch, + country, + coalesce(GB.quantity,oi.quantity) quantity, + coalesce(GB.actual_wholesale_price,oi.wholesale_price) wholesale_price, + coalesce(GB.retail_price,oi.retail_price) retail_price, + ((GB.retail_price*GB.quantity)-(GB.seller_margin*GB.quantity)) total_price, + Total_earnings, + A.is_slash_price_enabled , + coalesce(gb.brand_name,oi.Brand_name) brand_name, + coalesce(gb.category_name,oi.category_name) category_name, + oi.product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + (GB.cogs) as total_cogs, + case when oi.parent_sku is null then 'NA' else oi.parent_sku end bundle_sku, + new_shipping_amount sku_level_shipping_amount, + GB.discounted_price*GB.quantity sku_level_payment_amount, + is_cash_on_delivery, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_order_management.order A +inner join (select AD.* , + sum(new_shipping_amount) over(partition by AD.external_id) new_shipping_amount_final , + sum(discounted_price*quantity) over(partition by AD.external_id) payment_amount + from (SELECT A.*, + discounted_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_discounted_price, + actual_wholesale_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_actual_wholesale_price , + retail_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_retail_price, + seller_margin/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_seller_margin , + cogs/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_cogs, + (case when (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)/count(1) over(partition BY A.external_id,A.sku,A.item_type) new_shipping_amount + 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) AD + )GB + ON A.id = GB.external_id AND GB.sku not like 'BAZ%' + left JOIN (select A.*,B.sku parent_sku + from raena_order_management.order_item A + left join raena_order_management.order_item B on A.parent_item_id = B.id ) oi + ON GB.external_id=oi.order_id + AND OI.SKU not like 'BAZ%' + AND GB.sku=oi.sku + and GB.parent_sku= oi.parent_sku +left join raena_analytics.payment_order C on A.id=C.order_id +left join raena_order_management.channel D on A.channel_id = D.id +WHERE cast(A.created_at+interval'7 hours' as date)>='2021-10-28'; + +DROP TABLE IF EXISTS raena_analytics.remove_duplicate_from_app_admin; + + +CREATE TABLE raena_analytics.remove_duplicate_from_app_admin AS +SELECT A.external_id, + wholesale_price_gm, + wholesale_price_app_admin, + wholesale_price_gm-wholesale_price_app_admin diff +FROM + (SELECT external_id , + sum(discounted_price*quantity) P1, + sum((retail_price*quantity)-(seller_margin*quantity)) wholesale_price_gm + FROM raena_analytics.gm_dashboard gd + GROUP BY external_id) A +FULL OUTER JOIN + (SELECT order_id , + payment_amount p2, + sum(total_price) wholesale_price_app_admin + FROM raena_analytics.om_app_admin_dump_table oaadt + GROUP BY order_id, + payment_amount) B ON A.external_id= B.order_id +WHERE wholesale_price_gm-wholesale_price_app_admin<>0; + +DROP TABLE IF EXISTS raena_analytics.dubplicate_record_app_admin; + +CREATE TABLE raena_analytics.dubplicate_record_app_admin AS +SELECT DISTINCT * +FROM raena_analytics.om_app_admin_dump_table oaadt +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + +DELETE +FROM raena_analytics.om_app_admin_dump_table +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + +INSERT INTO raena_analytics.om_app_admin_dump_table +SELECT * +FROM raena_analytics.dubplicate_record_app_admin; + + diff --git a/postgresql/h b/postgresql/h new file mode 100644 index 0000000..e69de29 diff --git a/postgresql/home_page_pdp_flash/home_page_pdp_flash.sh b/postgresql/home_page_pdp_flash/home_page_pdp_flash.sh new file mode 100644 index 0000000..585616f --- /dev/null +++ b/postgresql/home_page_pdp_flash/home_page_pdp_flash.sh @@ -0,0 +1,748 @@ +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.homepage_base_stage1_v1; + +create table raena_analytics.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 raena_analytics.homepage_base_stage1 +where ts::date >= '$reportDate'; + +insert into raena_analytics.homepage_base_stage1 +SELECT ts, + events, + name , + email, + phone, + objectid, + profiledata::json ->>'tier' tier, + profiledata::json ->>'isguestuser' isguestuser, + split_part(replace(event_props,'\\\xa0',' ')::json->>'url' ,'/',2) collection, + split_part(replace(event_props,'\\\xa0',' ')::json->>'url' ,'/',3) collection_id, + replace(event_props,'\\\xa0',' ')::json->>'userId' userId, + replace(event_props,'\\\xa0',' ')::json->>'sectionName' sectionName, + replace(event_props,'\\\xa0',' ')::json->>'guest_user' guest_user, + replace(event_props,'\\\xa0',' ')::json->>'tier' tier_id, + replace(event_props,'\\\xa0',' ')::json->>'categoryName' categoryName, + replace(event_props,'\\\xa0',' ')::json->>'brand_code' brand_code, + replace(event_props,'\\\xa0',' ')::json->>'brand_name' brand_name, + case when events ='home_product_infinite' then replace(event_props,'\\\xa0',' ')::json->'brand' end brand_id, + case when events ='home_product_Caraousel' then replace(event_props,'\\\xa0',' ')::json->'brand' end brand, + replace(event_props,'\\\xa0',' ')::json->>'productId' productId, + replace(event_props,'\\\xa0',' ')::json->>'videoId' videoId, + replace(event_props,'\\\xa0',' ')::json->>'pageName' pageName, + replace(event_props,'\\\xa0',' ')::json->>'SKU_code' SKU_code +FROM raena_analytics.homepage_base_stage1_v1; + +drop table if exists raena_analytics.homepage_base; + +create table raena_analytics.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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11; + + + drop table if exists raena_analytics.pdp_base_stage1_v1; + +create table raena_analytics.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 raena_analytics.pdp_base_stage1 +where ts::date >= '$reportDate' ; + +insert into raena_analytics.pdp_base_stage1 +SELECT ts, + events, + name , + email, + phone, + objectid, + profiledata::json ->'tier' tier, + profiledata::json ->'isguestuser' isguestuser, + replace(event_props,'\\\xa0',' ')::json->'item_brand' brand_name, + replace(event_props,'\\\xa0',' ')::json->'item_id' SKU_code, + replace(event_props,'\\\xa0',' ')::json->'categoryPageId' categoryPageId, + replace(event_props,'\\\xa0',' ')::json->'screen' screen +FROM raena_analytics.pdp_base_stage1_v1; + + + +drop table if exists raena_analytics.pdp_base; + +create table raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9; + + +drop table if exists raena_analytics.flash_sale_order_stage_v1; + +create table raena_analytics.flash_sale_order_stage_v1 +as +SELECT BB.*,AA.flash_sale_id + FROM (select external_id , + sku, + brand_name, + tier_name, + sum(quantity)quantity, + sum((discounted_price*quantity)) discounted_price, + raena_analytics.gm_dashboard.reseller_id, + transaction_date::date transaction_date + from raena_analytics.gm_dashboard + where item_type like 'Flash%' + group by 1,2,3,4,7,8) BB + INNER JOIN (select id,flash_sale_id from raena_order_management.order where flash_sale_id IS NOT NULL)AA ON external_id = id ; + +DROP TABLE IF EXISTS raena_analytics.flash_sale_order; + + +CREATE TABLE raena_analytics.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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +delete from raena_analytics.flash_base_stage1 +where ts::date >= '$reportDate'; + +insert into raena_analytics.flash_base_stage1 +SELECT ts, + name , + email, + events, + phone, + objectid, + profiledata::json->'tier' tier, + replace(event_props,'\\\xa0',' ')::json->>'item_brand' brand_name, + replace(event_props,'\\\xa0',' ')::json->>'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 raena_analytics.flash_sale_event_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.flash_base_stage1 +GROUP BY 1, + 2, + 3, + 4, + 5; + + +" > /home/ec2-user/cronjob/postgresql/home_page_pdp_flash/home_page_pdp_flash.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/home_page_pdp_flash/home_page_pdp_flash.sql + diff --git a/postgresql/home_page_pdp_flash/home_page_pdp_flash.sql b/postgresql/home_page_pdp_flash/home_page_pdp_flash.sql new file mode 100644 index 0000000..5396115 --- /dev/null +++ b/postgresql/home_page_pdp_flash/home_page_pdp_flash.sql @@ -0,0 +1,735 @@ + + +drop table if exists raena_analytics.homepage_base_stage1_v1; + +create table raena_analytics.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-11-21' ; + +delete from raena_analytics.homepage_base_stage1 +where ts::date >= '2023-11-21'; + +insert into raena_analytics.homepage_base_stage1 +SELECT ts, + events, + name , + email, + phone, + objectid, + profiledata::json ->>'tier' tier, + profiledata::json ->>'isguestuser' isguestuser, + split_part(replace(event_props,'\\xa0',' ')::json->>'url' ,'/',2) collection, + split_part(replace(event_props,'\\xa0',' ')::json->>'url' ,'/',3) collection_id, + replace(event_props,'\\xa0',' ')::json->>'userId' userId, + replace(event_props,'\\xa0',' ')::json->>'sectionName' sectionName, + replace(event_props,'\\xa0',' ')::json->>'guest_user' guest_user, + replace(event_props,'\\xa0',' ')::json->>'tier' tier_id, + replace(event_props,'\\xa0',' ')::json->>'categoryName' categoryName, + replace(event_props,'\\xa0',' ')::json->>'brand_code' brand_code, + replace(event_props,'\\xa0',' ')::json->>'brand_name' brand_name, + case when events ='home_product_infinite' then replace(event_props,'\\xa0',' ')::json->'brand' end brand_id, + case when events ='home_product_Caraousel' then replace(event_props,'\\xa0',' ')::json->'brand' end brand, + replace(event_props,'\\xa0',' ')::json->>'productId' productId, + replace(event_props,'\\xa0',' ')::json->>'videoId' videoId, + replace(event_props,'\\xa0',' ')::json->>'pageName' pageName, + replace(event_props,'\\xa0',' ')::json->>'SKU_code' SKU_code +FROM raena_analytics.homepage_base_stage1_v1; + +drop table if exists raena_analytics.homepage_base; + +create table raena_analytics.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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11; + + + drop table if exists raena_analytics.pdp_base_stage1_v1; + +create table raena_analytics.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-11-21' ; + + +delete from raena_analytics.pdp_base_stage1 +where ts::date >= '2023-11-21' ; + +insert into raena_analytics.pdp_base_stage1 +SELECT ts, + events, + name , + email, + phone, + objectid, + profiledata::json ->'tier' tier, + profiledata::json ->'isguestuser' isguestuser, + replace(event_props,'\\xa0',' ')::json->'item_brand' brand_name, + replace(event_props,'\\xa0',' ')::json->'item_id' SKU_code, + replace(event_props,'\\xa0',' ')::json->'categoryPageId' categoryPageId, + replace(event_props,'\\xa0',' ')::json->'screen' screen +FROM raena_analytics.pdp_base_stage1_v1; + + + +drop table if exists raena_analytics.pdp_base; + +create table raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9; + + +drop table if exists raena_analytics.flash_sale_order_stage_v1; + +create table raena_analytics.flash_sale_order_stage_v1 +as +SELECT BB.*,AA.flash_sale_id + FROM (select external_id , + sku, + brand_name, + tier_name, + sum(quantity)quantity, + sum((discounted_price*quantity)) discounted_price, + raena_analytics.gm_dashboard.reseller_id, + transaction_date::date transaction_date + from raena_analytics.gm_dashboard + where item_type like 'Flash%' + group by 1,2,3,4,7,8) BB + INNER JOIN (select id,flash_sale_id from raena_order_management.order where flash_sale_id IS NOT NULL)AA ON external_id = id ; + +DROP TABLE IF EXISTS raena_analytics.flash_sale_order; + + +CREATE TABLE raena_analytics.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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +delete from raena_analytics.flash_base_stage1 +where ts::date >= '2023-11-21'; + +insert into raena_analytics.flash_base_stage1 +SELECT ts, + name , + email, + events, + phone, + objectid, + profiledata::json->'tier' tier, + replace(event_props,'\\xa0',' ')::json->>'item_brand' brand_name, + replace(event_props,'\\xa0',' ')::json->>'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-11-21' ; + +DROP TABLE IF EXISTS raena_analytics.flash_sale_event_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.flash_base_stage1 +GROUP BY 1, + 2, + 3, + 4, + 5; + + + diff --git a/postgresql/loyalty_point/loyalty_point_etl.sh b/postgresql/loyalty_point/loyalty_point_etl.sh new file mode 100644 index 0000000..232d861 --- /dev/null +++ b/postgresql/loyalty_point/loyalty_point_etl.sh @@ -0,0 +1,137 @@ + #!/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.loyalty_reseller_stage1; + +CREATE TABLE raena_analytics.loyalty_reseller_stage1 AS +SELECT 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 (select distinct * from raena_analytics.lms_transactions) A +LEFT JOIN raena_user_management.user B ON A.resellerid = cast(B.id as varchar) +LEFT JOIN raena_user_management.tier C ON A.tierid = cast(C.id as varchar) ; + + +DROP TABLE IF EXISTS raena_analytics.loyalty_total_orders; + +CREATE TABLE raena_analytics.loyalty_total_orders AS +SELECT cast(A.created_at+interval'7 Hours' as date) AS created_date , + A.reseller_id , + A.reseller_info::json->>'name' reseller_name, + A.reseller_info::json->>'email' reseller_email, + A.reseller_info::json->>'mobile' reseller_mobile, + A.reseller_info::json->>'tierName' 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 >='2021-01-01'; + + +drop table if exists raena_analytics.loyalty_base_1; + + +create table raena_analytics.loyalty_base_1 +as +select B.created_date, + coalesce(B.resellerid,A.reseller_id) reseller_id, + A.reseller_name, + coalesce(B.email,A.reseller_email) reseller_email, + coalesce(B.mobile,A.reseller_mobile) reseller_mobile, + coalesce(B.tier_name,A.reseller_tier_name) reseller_tier_name, + A.payment_status, + A.order_id, + A.payment_price, + A.order_status, + B.transactionid, + 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 raena_analytics.loyalty_reseller_stage1 B left join raena_analytics.loyalty_total_orders A + on A.order_id = B.order_id; + + + + +/*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.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 raena_analytics.loyalty_total_orders A left join raena_analytics.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.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 raena_analytics.loyalty_reseller_stage1 B +where B.order_id is null; +*/ + +" > /home/ec2-user/cronjob/postgresql/loyalty_point/loyalty_point_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/loyalty_point/loyalty_point_etl.sql diff --git a/postgresql/loyalty_point/loyalty_point_etl.sql b/postgresql/loyalty_point/loyalty_point_etl.sql new file mode 100644 index 0000000..51f4073 --- /dev/null +++ b/postgresql/loyalty_point/loyalty_point_etl.sql @@ -0,0 +1,120 @@ + + +DROP TABLE IF EXISTS raena_analytics.loyalty_reseller_stage1; + +CREATE TABLE raena_analytics.loyalty_reseller_stage1 AS +SELECT 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 (select distinct * from raena_analytics.lms_transactions) A +LEFT JOIN raena_user_management.user B ON A.resellerid = cast(B.id as varchar) +LEFT JOIN raena_user_management.tier C ON A.tierid = cast(C.id as varchar) ; + + +DROP TABLE IF EXISTS raena_analytics.loyalty_total_orders; + +CREATE TABLE raena_analytics.loyalty_total_orders AS +SELECT cast(A.created_at+interval'7 Hours' as date) AS created_date , + A.reseller_id , + A.reseller_info::json->>'name' reseller_name, + A.reseller_info::json->>'email' reseller_email, + A.reseller_info::json->>'mobile' reseller_mobile, + A.reseller_info::json->>'tierName' 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 >='2021-01-01'; + + +drop table if exists raena_analytics.loyalty_base_1; + + +create table raena_analytics.loyalty_base_1 +as +select B.created_date, + coalesce(B.resellerid,A.reseller_id) reseller_id, + A.reseller_name, + coalesce(B.email,A.reseller_email) reseller_email, + coalesce(B.mobile,A.reseller_mobile) reseller_mobile, + coalesce(B.tier_name,A.reseller_tier_name) reseller_tier_name, + A.payment_status, + A.order_id, + A.payment_price, + A.order_status, + B.transactionid, + 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 raena_analytics.loyalty_reseller_stage1 B left join raena_analytics.loyalty_total_orders A + on A.order_id = B.order_id; + + + + +/*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.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 raena_analytics.loyalty_total_orders A left join raena_analytics.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.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 raena_analytics.loyalty_reseller_stage1 B +where B.order_id is null; +*/ + + diff --git a/postgresql/new_user_funnel/new_user_funnel_etl.sh b/postgresql/new_user_funnel/new_user_funnel_etl.sh new file mode 100644 index 0000000..10e3d37 --- /dev/null +++ b/postgresql/new_user_funnel/new_user_funnel_etl.sh @@ -0,0 +1,274 @@ + #!/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.om_clevertap_install_jan_may; + + +CREATE TABLE raena_analytics.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 cast('2022-04-01' as date) AS App_install_date, + email, + phone, + email AS user_id + FROM raena_analytics.clevertap_april_csv + UNION SELECT '2022-03-30' AS App_install_date, + email, + phone, + email AS user_id + FROM raena_analytics.clevertap_march_csv + WHERE phone NOT IN + (SELECT DISTINCT profile_phone + FROM raena_analytics.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)A)B +WHERE R=1 ; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_2; + +CREATE TABLE raena_analytics.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 raena_analytics.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)A + 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)A; + + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_3; + + +CREATE TABLE raena_analytics.om_clevertap_install_jan_may_3 AS +SELECT a.*, + cast(b.created_at AS date) AS transaction_date, + transacted_email +FROM raena_analytics.om_clevertap_install_jan_may_2 a +LEFT JOIN + (SELECT reseller_email AS transacted_email , + min(transaction_date) AS created_at + FROM raena_analytics.gm_dashboard + GROUP BY 1) b ON a.email=transacted_email; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_final; + + +CREATE TABLE raena_analytics.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<=app_install_date+interval'30 day' THEN 'Yes' + ELSE 'No' + END AS transacted_flag +FROM raena_analytics.om_clevertap_install_jan_may_3 ; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_order; + + +CREATE TABLE raena_analytics.om_clevertap_install_jan_may_order AS +SELECT * +FROM raena_analytics.om_clevertap_install_jan_may_final +WHERE transacted_email NOTNULL + AND rtrim(ltrim(transacted_email)) != ''; ; + +INSERT INTO raena_analytics.Test_1_week_om_clevertap_install_jan_may_final +SELECT *, + CURRENT_DATE-1 AS Report_date +FROM raena_analytics.om_clevertap_install_jan_may_final; + + +DROP TABLE IF EXISTS raena_analytics.om_reseller_info; + + +CREATE TABLE raena_analytics.om_reseller_info AS +SELECT DISTINCT reseller_info::json->>'mobile' AS reseller_mobile , + reseller_info::json->>'email' AS reseller_email, + reseller_id +FROM raena_order_management.order +WHERE id LIKE '%DSF'; + +DROP TABLE IF EXISTS raena_analytics.OM_Order_id_payment_id; + +CREATE TABLE raena_analytics.OM_Order_id_payment_id AS +SELECT id, + max(created_at) created_at, + replace(unnest(replace(replace(replace(cast(order_ids as varchar),'\"',''''),'[','{'),']','}')::varchar[]),'''','') order_id +FROM raena_order_management.payment B +GROUP BY 1, + 3; + +delete from raena_analytics.OM_Events_user_type +where cast(ts as date)>='$reportDate'; + +insert into raena_analytics.OM_Events_user_type +SELECT A.*, + CASE + WHEN date_trunc('Month',A.ts)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.ts)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.ts)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.ts)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag +FROM (select * from clevertap.clevertap_master_data 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') and ts::date >= '$reportDate') A +LEFT JOIN raena_analytics.user_type_table flag ON replace(A.phone,'+','')=flag.reseller_mobile; + + +DROP TABLE IF EXISTS raena_analytics.om_order_brand_type; + + +DROP TABLE IF EXISTS raena_analytics.om_order_brand_type; + + +CREATE TABLE raena_analytics.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 (select distinct id,order_id,parent_sku from raena_order_management.sales_sub_order) sso +LEFT JOIN (select distinct sales_sub_order_id,sku from 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/postgresql/new_user_funnel/new_user_funnel_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/new_user_funnel/new_user_funnel_etl.sql diff --git a/postgresql/new_user_funnel/new_user_funnel_etl.sql b/postgresql/new_user_funnel/new_user_funnel_etl.sql new file mode 100644 index 0000000..1badadb --- /dev/null +++ b/postgresql/new_user_funnel/new_user_funnel_etl.sql @@ -0,0 +1,257 @@ + + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may; + + +CREATE TABLE raena_analytics.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 cast('2022-04-01' as date) AS App_install_date, + email, + phone, + email AS user_id + FROM raena_analytics.clevertap_april_csv + UNION SELECT '2022-03-30' AS App_install_date, + email, + phone, + email AS user_id + FROM raena_analytics.clevertap_march_csv + WHERE phone NOT IN + (SELECT DISTINCT profile_phone + FROM raena_analytics.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)A)B +WHERE R=1 ; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_2; + +CREATE TABLE raena_analytics.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 raena_analytics.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)A + 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)A; + + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_3; + + +CREATE TABLE raena_analytics.om_clevertap_install_jan_may_3 AS +SELECT a.*, + cast(b.created_at AS date) AS transaction_date, + transacted_email +FROM raena_analytics.om_clevertap_install_jan_may_2 a +LEFT JOIN + (SELECT reseller_email AS transacted_email , + min(transaction_date) AS created_at + FROM raena_analytics.gm_dashboard + GROUP BY 1) b ON a.email=transacted_email; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_final; + + +CREATE TABLE raena_analytics.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<=app_install_date+interval'30 day' THEN 'Yes' + ELSE 'No' + END AS transacted_flag +FROM raena_analytics.om_clevertap_install_jan_may_3 ; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_order; + + +CREATE TABLE raena_analytics.om_clevertap_install_jan_may_order AS +SELECT * +FROM raena_analytics.om_clevertap_install_jan_may_final +WHERE transacted_email NOTNULL + AND rtrim(ltrim(transacted_email)) != ''; ; + +INSERT INTO raena_analytics.Test_1_week_om_clevertap_install_jan_may_final +SELECT *, + CURRENT_DATE-1 AS Report_date +FROM raena_analytics.om_clevertap_install_jan_may_final; + + +DROP TABLE IF EXISTS raena_analytics.om_reseller_info; + + +CREATE TABLE raena_analytics.om_reseller_info AS +SELECT DISTINCT reseller_info::json->>'mobile' AS reseller_mobile , + reseller_info::json->>'email' AS reseller_email, + reseller_id +FROM raena_order_management.order +WHERE id LIKE '%DSF'; + +DROP TABLE IF EXISTS raena_analytics.OM_Order_id_payment_id; + +CREATE TABLE raena_analytics.OM_Order_id_payment_id AS +SELECT id, + max(created_at) created_at, + replace(unnest(replace(replace(replace(cast(order_ids as varchar),'"',''''),'[','{'),']','}')::varchar[]),'''','') order_id +FROM raena_order_management.payment B +GROUP BY 1, + 3; + +delete from raena_analytics.OM_Events_user_type +where cast(ts as date)>='2023-11-20'; + +insert into raena_analytics.OM_Events_user_type +SELECT A.*, + CASE + WHEN date_trunc('Month',A.ts)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.ts)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.ts)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.ts)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag +FROM (select * from clevertap.clevertap_master_data 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') and ts::date >= '2023-11-20') A +LEFT JOIN raena_analytics.user_type_table flag ON replace(A.phone,'+','')=flag.reseller_mobile; + + +DROP TABLE IF EXISTS raena_analytics.om_order_brand_type; + + +DROP TABLE IF EXISTS raena_analytics.om_order_brand_type; + + +CREATE TABLE raena_analytics.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 (select distinct id,order_id,parent_sku from raena_order_management.sales_sub_order) sso +LEFT JOIN (select distinct sales_sub_order_id,sku from 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; + + + diff --git a/postgresql/notification_dashboard_anchor_brand/notification_dashboard_anchor_brand.sh b/postgresql/notification_dashboard_anchor_brand/notification_dashboard_anchor_brand.sh new file mode 100644 index 0000000..48dd03a --- /dev/null +++ b/postgresql/notification_dashboard_anchor_brand/notification_dashboard_anchor_brand.sh @@ -0,0 +1,625 @@ + #!/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 " + +------- Notification code + +drop table raena_analytics.notification_session_base_event; + +create table raena_analytics.notification_session_base_event +as +SELECT DISTINCT ts::date created_date, + events, + email, + phone, + event_props::jsonb->>'CT Session Id' sessionid, + CASE + WHEN event_props::jsonb->>'url' LIKE '%source=email%' THEN 'email' + WHEN event_props::jsonb->>'url' LIKE '%source=push%' THEN 'push' + WHEN event_props::jsonb->>'url' LIKE '%source=whatsapp%' THEN 'whatsapp' + END SOURCE, + split_part((event_props::jsonb->>'url')::TEXT,'&requestId=', 2) requestid +FROM clevertap.clevertap_master_data cmd +WHERE events IN ('opened_using_link') + AND (event_props::jsonb->>'url' LIKE '%source=email%' + OR event_props::jsonb->>'url' LIKE '%source=push%' + OR event_props::jsonb->>'url' LIKE '%source=whatsapp%') + AND ts::date >='2023-06-16'; + + +drop table raena_analytics.clevertab_session_id_base_data ; + +CREATE TABLE raena_analytics.clevertab_session_id_base_data AS +SELECT created_date, + email, + phone, + sessionid, + count(CASE WHEN events ='view_item' THEN email END) view_item, + count(CASE WHEN events ='view_cart' THEN email END) view_cart, + count(CASE WHEN events ='add_to_cart' THEN email END) add_to_cart, + count(CASE WHEN events ='begin_checkout' THEN email END) begin_checkout, + count(CASE WHEN events ='page_load_checkout_continue' THEN email END) page_load_checkout_continue, + count(CASE WHEN events ='page_load_finish_checkout' THEN email END) page_load_finish_checkout, + count(CASE WHEN events ='finish_checkout' THEN email END) finish_checkout, + count(CASE WHEN events ='Charged' THEN email END) Charged +FROM + (SELECT ts::date created_date, + events, + email, + phone, + event_props::jsonb->>'CT Session Id' sessionid --identity + + FROM clevertap.clevertap_master_data cmd + WHERE events IN ('add_to_cart', + 'begin_checkout', + 'finish_checkout', + 'page_load_checkout_continue', + 'page_load_finish_checkout', + 'view_cart', + 'view_item', + 'Charged') + AND ts::date >='2023-06-16') A +GROUP BY 1, + 2, + 3, + 4; + + +drop table raena_analytics.clevertab_charged_base_data ; + +CREATE TABLE raena_analytics.clevertab_charged_base_data AS +SELECT created_date, + email, + phone, + count(CASE WHEN events ='Charged' THEN email END) Charged +FROM + (SELECT ts::date created_date, + events, + email, + phone + FROM clevertap.clevertap_master_data cmd + WHERE events IN ('Charged') + AND ts::date >='2023-06-16') A +GROUP BY 1, + 2, + 3; + + +drop table raena_analytics.notification_base_table ; + +create table raena_analytics.notification_base_table +as +select created_at::date created_at, + id notification_id, + user_id , + event_name, + notification_type, + request_id, + (event_param->>'eventDetail')::json->>'sku' sku, + (event_param->>'eventDetail')::json->>'price' price, + ((event_param->>'eventDetail')::json->>'price')::json->>'07030fbe-5801-4318-9e97-fe33fa169894' bronze_price, + ((event_param->>'eventDetail')::json->>'price')::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19' silver_price, + ((event_param->>'eventDetail')::json->>'price')::json->>'bf645e97-8a48-4977-8367-e987489760f9' gold_price, + (event_param->>'eventDetail')::json->>'margin' margin, + (event_param->>'eventDetail')::json->>'flashSaleId' flashSaleId, + (event_param->>'eventDetail')::json->>'newQty' newQty, + (event_param->>'eventDetail')::json->>'oldQty' oldQty, + (event_param->>'eventDetail')::json->>'retailPrice' retailPrice, + (event_param->>'eventDetail')::json->>'applicablePrice' applicablePrice, + ((event_param->>'eventDetail')::json->>'applicablePrice')::json->>'new' new_applicablePrice, + ((event_param->>'eventDetail')::json->>'applicablePrice')::json->>'old' old_applicablePrice, + (event_param->>'eventDetail')::json->>'percentageChange' percentageChange, + (event_param->>'userContactDetail')::json->>'name' user_name, + (event_param->>'userContactDetail')::json->>'email'user_email, + (event_param->>'userContactDetail')::json->>'mobile'user_mobile, + (event_param->>'userContactDetail')::json->>'tierId'user_tierid, + (event_param->>'userContactDetail')::json->>'lastLoggedInAt' user_last_loggedin_at +from raena_notification_management.notifications_log A +left join (select Notification_type, id template_id from raena_notification_management.notification_template) B on A.template_id = B.template_id +where created_at ::date >='2023-06-01' +and event_name in ('flash-sale-start','back-in-stock','price-drop'); + + +delete from raena_analytics.notification_funnel_report +where created_at >='2023-06-30'; + + +INSERT INTO raena_analytics.notification_funnel_report +SELECT A.created_at, + A.event_name, + A.notification_type, + A.user_name, + A.User_mobile, + A.user_email, + count(DISTINCT CASE WHEN unique_user>0 THEN 1 END) unique_user, + count(DISTINCT CASE WHEN view_item>0 THEN 1 END) view_item, + count(DISTINCT CASE WHEN view_cart>0 THEN 1 END) view_cart, + count(DISTINCT CASE WHEN add_to_cart>0 THEN 1 END) add_to_cart, + count(DISTINCT CASE WHEN begin_checkout>0 THEN 1 END) begin_checkout, + count(DISTINCT CASE WHEN page_load_checkout_continue>0 THEN 1 END) page_load_checkout_continue, + count(DISTINCT CASE WHEN page_load_finish_checkout>0 THEN 1 END) page_load_finish_checkout, + count(DISTINCT CASE WHEN finish_checkout>0 THEN 1 END) finish_checkout, + count(DISTINCT CASE WHEN Charged>0 THEN 1 END) Charged +FROM + (SELECT DISTINCT created_at , + event_name, + notification_type, + user_name, + User_mobile, + user_email, + request_id + FROM raena_analytics.notification_base_table + WHERE created_at>='2023-06-30') A +LEFT JOIN + (SELECT A.created_date, + A.email, + A.phone, + A.source, + A.requestid, + count(DISTINCT A.email) unique_user, + count(DISTINCT CASE WHEN view_item >0 THEN A.email END) view_item, + count(DISTINCT CASE WHEN view_cart >0 THEN A.email END) view_cart, + count(DISTINCT CASE WHEN add_to_cart >0 THEN A.email END) add_to_cart, + count(DISTINCT CASE WHEN begin_checkout >0 THEN A.email END) begin_checkout, + count(DISTINCT CASE WHEN page_load_checkout_continue >0 THEN A.email END) page_load_checkout_continue, + count(DISTINCT CASE WHEN page_load_finish_checkout >0 THEN A.email END) page_load_finish_checkout, + count(DISTINCT CASE WHEN finish_checkout >0 THEN A.email END) finish_checkout, + count(DISTINCT CASE WHEN C.Charged >0 THEN A.email END) Charged + FROM raena_analytics.notification_session_base_event A + LEFT JOIN raena_analytics.clevertab_session_id_base_data B ON A.sessionid = B.sessionid + LEFT JOIN raena_analytics.clevertab_charged_base_data C ON A.email = C.email + AND A.phone = C.phone + AND A.created_date = C.created_date + WHERE A.created_date >='2023-06-30' + GROUP BY 1, + 2, + 3, + 4, + 5) B ON --(replace(A.User_mobile,'+','') = B.phone +A.user_email = B.email +AND A.request_id = B.requestid +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +DROP TABLE raena_analytics.notification_order_base ; + + +CREATE TABLE raena_analytics.notification_order_base AS +SELECT reseller_id, + reseller_mobile , + transaction_date::date transaction_date, + external_id , + sku , + brand_name , + category_name , + discounted_price*quantity payment_amount , + quantity , + sum(discounted_price*quantity) over(partition BY external_id) order_level_amount +FROM raena_analytics.gm_dashboard gd +WHERE transaction_date ::date >='2023-06-15' + AND order_placed_by <>'Admin Panel'; + + +DROP TABLE raena_analytics.segement_raw_data; +CREATE TABLE raena_analytics.segement_raw_data AS +SELECT id notification_id, + user_id , + event_name, + notification_type, + request_id, + (event_param->>'eventDetail')::json->>'sku' sku, + (event_param->>'userContactDetail')::json->>'segment'segment +FROM raena_notification_management.notifications_log A +LEFT JOIN + (SELECT Notification_type, + template_id + FROM raena_notification_management.notification_event_template_mapping) B ON A.template_id = B.template_id +WHERE created_at ::date >='2023-06-30' + AND event_name IN ('flash-sale-start', + 'back-in-stock', + 'price-drop') + AND (event_param->>'userContactDetail')::json->>'segment' IS NOT NULL ; + + +DROP TABLE IF EXISTS raena_analytics.user_bucket; + +CREATE TABLE raena_analytics.user_bucket AS +SELECT reseller_id , + max(payment_amount) max_payment_amount +FROM + (SELECT reseller_id , + id , + sum(payment_amount)payment_amount + FROM raena_order_management.order + WHERE payment_status ='Paid' + GROUP BY 1, + 2) A +GROUP BY 1; + +delete from raena_analytics.transaction_notification_mapping +where created_at >='2023-06-30'; + +INSERT INTO raena_analytics.transaction_notification_mapping +SELECT D.name brand_name, + A.*, + B.external_id , + B.quantity, + CASE + WHEN A.sku = B.sku THEN payment_amount + END payment_amount, + CASE + WHEN A.sku IS NOT NULL + AND B.sku IS NOT NULL + AND A.sku = B.sku + AND payment_amount>0 THEN order_level_amount + END order_level_amount, + payment_amount indirect_payment_amount, + CASE + WHEN date_trunc('Month',A.created_at)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.created_at)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.created_at)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.created_at)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + DD.source, + XX.segment, + case when max_payment_amount between 0 and 2000000 then '0to2M' + when max_payment_amount between 2000000.00001 and 10000000 then '2Mto10M' + when max_payment_amount >= 10000000.000001 then 'greaterthan10M' end bucket +FROM raena_analytics.notification_base_table A +INNER JOIN raena_catalog_management.product C ON A.sku = C.sku +INNER JOIN raena_catalog_management.brand D ON C.brand_id = D.id +left join raena_analytics.segement_raw_data XX on A.notification_id = XX.notification_id +and A.user_id = XX.user_id +and A.event_name = XX.event_name +and A.notification_type = XX.notification_type +and A.request_id = XX.request_id +and A.sku = XX.sku +left join raena_analytics.user_bucket YY +on cast(A.user_id as varchar) = YY.reseller_id +LEFT JOIN raena_analytics.user_type_table flag ON replace(A.user_mobile,'+','')=flag.reseller_mobile +LEFT JOIN raena_analytics.notification_session_base_event dd ON replace(A.user_mobile,'+','')=DD.phone +AND --(date_part('day',A.created_at::date)-date_part('Day',created_date::date) = 1) and (date_part('Month',A.created_at::date)-date_part('Month',created_date::date) = 0 ) and + lower(A.notification_type) =lower("source") +AND A.request_id = dd.requestid +LEFT JOIN raena_analytics.notification_order_base B ON cast(A.user_id AS varchar) = B.reseller_id +and (transaction_date::date-A.created_at::date) between 0 and 3 +AND replace(B.reseller_mobile,'+','')=DD.phone +WHERE A.created_at >='2023-06-30'; + +drop table raena_analytics.anchor_brand_level_data ; + +CREATE TABLE raena_analytics.anchor_brand_level_data AS +SELECT * , + rank()over (partition BY external_id + ORDER BY brand_contribution DESC) rnk +FROM + (SELECT * , + CASE + WHEN order_level_payment_amount <2000000 THEN '0 to 2M' + WHEN order_level_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' + WHEN order_level_payment_amount >10000000 THEN '>10M' + END bucket, + (brand_payment_amount/nullif(order_level_payment_amount,0))*100 AS brand_contribution + FROM + (SELECT *, + sum(brand_payment_amount) over(partition BY external_id) order_level_payment_amount + FROM + (SELECT date_trunc('Year',transaction_date)::date transaction_date , + external_id , + brand_name, + sum(discounted_price*quantity) brand_payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2, + 3) A + WHERE brand_payment_amount>10) B) C +WHERE brand_contribution<100; + + drop table raena_analytics.anchor_brand_data; + +CREATE TABLE raena_analytics.anchor_brand_data AS +SELECT cast(date_part('Year',transaction_date) AS varchar) transaction_date2, + bucket, + brand_name , + avg(brand_contribution) avg_brand_contribution, + avg(brand_payment_amount) avg_payment_amount , + anchor_brand_name , + avg(anchor_brand_contribution) avg_anchor_brand_contribution , + avg(anchor_payment_amount) avg_anchor_payment_amount +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +GROUP BY 1, + 2, + 3, + 6 +ORDER BY transaction_date2 , + bucket, + brand_name , + avg_anchor_brand_contribution DESC; + + +DROP TABLE raena_analytics.reseller_level_anchor_brand_level_data ; + +CREATE TABLE raena_analytics.reseller_level_anchor_brand_level_data AS +SELECT * , + rank()over (partition BY external_id + ORDER BY brand_contribution DESC) rnk +FROM + (SELECT B.* , + CASE + WHEN bucket_level_payment_amount <2000000 THEN '0 to 2M' + WHEN bucket_level_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' + WHEN bucket_level_payment_amount >10000000 THEN '>10M' + END bucket, + (brand_payment_amount/nullif(order_level_payment_amount,0))*100 AS brand_contribution + FROM + (SELECT *, + sum(brand_payment_amount) over(partition BY external_id) order_level_payment_amount + FROM + (SELECT date_trunc('Year',transaction_date)::date transaction_date, + reseller_id, + reseller_mobile , + reseller_email , + external_id , + brand_name, + sum(discounted_price*quantity) brand_payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) A + WHERE brand_payment_amount>10) B + LEFT JOIN + (SELECT reseller_id, + max(payment_amount) bucket_level_payment_amount + FROM + (SELECT reseller_id , + external_id , + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2) A + GROUP BY 1) C ON B.reseller_id = C.reseller_id) D +WHERE brand_contribution<100; + +drop table raena_analytics.reseller_level_anchor_brand_data; + +CREATE TABLE raena_analytics.reseller_level_anchor_brand_data AS +SELECT reseller_id , + reseller_email , + reseller_mobile , + cast(date_part('Year',transaction_date) AS varchar) transaction_date2, + bucket, + brand_name , + avg(brand_contribution) avg_brand_contribution, + avg(brand_payment_amount) avg_payment_amount , + anchor_brand_name , + avg(anchor_brand_contribution) avg_anchor_brand_contribution , + avg(anchor_payment_amount) avg_anchor_payment_amount +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 9 +ORDER BY transaction_date2 , + bucket, + brand_name , + avg_anchor_brand_contribution DESC; + +DROP TABLE raena_analytics.anchor_analysis; + + +CREATE TABLE raena_analytics.anchor_analysis AS +SELECT BB.reseller_id , + brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_2023, + avg(CASE WHEN transaction_date <'2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_less_2023, + last_transaction_date +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +GROUP BY 1, + 2 , + 3, + 5, + 8 +ORDER BY 1, + 2, + 4; + +DROP TABLE raena_analytics.anchor_bucket_wise_for2023; + + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date BETWEEN '2023-01-01' AND '2023-05-31' + AND transaction_date ='2023-01-01' +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + +DROP TABLE raena_analytics.anchor_bucket_wise_for2023_churn_before2023; + + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023_churn_before2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date <'2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_before_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date <'2023-01-01' + AND transaction_date <'2023-01-01' +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + + +drop table raena_analytics.anchor_bucket_wise_for2023_churn_in2023; + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023_churn_in2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_in_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date ='2023-01-01' + AND brand_name IN + (SELECT DISTINCT brand_name + FROM raena_analytics.anchor_bucket_wise_for2023_churn_before2023) +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + + +" > /home/ec2-user/cronjob/postgresql/notification_dashboard_anchor_brand//notification_doashboard_anchor_brand.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/notification_dashboard_anchor_brand//notification_doashboard_anchor_brand.sql + + + diff --git a/postgresql/notification_dashboard_anchor_brand/notification_doashboard_anchor_brand.sql b/postgresql/notification_dashboard_anchor_brand/notification_doashboard_anchor_brand.sql new file mode 100644 index 0000000..e8602b4 --- /dev/null +++ b/postgresql/notification_dashboard_anchor_brand/notification_doashboard_anchor_brand.sql @@ -0,0 +1,605 @@ + + +------- Notification code + +drop table raena_analytics.notification_session_base_event; + +create table raena_analytics.notification_session_base_event +as +SELECT DISTINCT ts::date created_date, + events, + email, + phone, + event_props::jsonb->>'CT Session Id' sessionid, + CASE + WHEN event_props::jsonb->>'url' LIKE '%source=email%' THEN 'email' + WHEN event_props::jsonb->>'url' LIKE '%source=push%' THEN 'push' + WHEN event_props::jsonb->>'url' LIKE '%source=whatsapp%' THEN 'whatsapp' + END SOURCE, + split_part((event_props::jsonb->>'url')::TEXT,'&requestId=', 2) requestid +FROM clevertap.clevertap_master_data cmd +WHERE events IN ('opened_using_link') + AND (event_props::jsonb->>'url' LIKE '%source=email%' + OR event_props::jsonb->>'url' LIKE '%source=push%' + OR event_props::jsonb->>'url' LIKE '%source=whatsapp%') + AND ts::date >='2023-06-16'; + + +drop table raena_analytics.clevertab_session_id_base_data ; + +CREATE TABLE raena_analytics.clevertab_session_id_base_data AS +SELECT created_date, + email, + phone, + sessionid, + count(CASE WHEN events ='view_item' THEN email END) view_item, + count(CASE WHEN events ='view_cart' THEN email END) view_cart, + count(CASE WHEN events ='add_to_cart' THEN email END) add_to_cart, + count(CASE WHEN events ='begin_checkout' THEN email END) begin_checkout, + count(CASE WHEN events ='page_load_checkout_continue' THEN email END) page_load_checkout_continue, + count(CASE WHEN events ='page_load_finish_checkout' THEN email END) page_load_finish_checkout, + count(CASE WHEN events ='finish_checkout' THEN email END) finish_checkout, + count(CASE WHEN events ='Charged' THEN email END) Charged +FROM + (SELECT ts::date created_date, + events, + email, + phone, + event_props::jsonb->>'CT Session Id' sessionid --identity + + FROM clevertap.clevertap_master_data cmd + WHERE events IN ('add_to_cart', + 'begin_checkout', + 'finish_checkout', + 'page_load_checkout_continue', + 'page_load_finish_checkout', + 'view_cart', + 'view_item', + 'Charged') + AND ts::date >='2023-06-16') A +GROUP BY 1, + 2, + 3, + 4; + + +drop table raena_analytics.clevertab_charged_base_data ; + +CREATE TABLE raena_analytics.clevertab_charged_base_data AS +SELECT created_date, + email, + phone, + count(CASE WHEN events ='Charged' THEN email END) Charged +FROM + (SELECT ts::date created_date, + events, + email, + phone + FROM clevertap.clevertap_master_data cmd + WHERE events IN ('Charged') + AND ts::date >='2023-06-16') A +GROUP BY 1, + 2, + 3; + + +drop table raena_analytics.notification_base_table ; + +create table raena_analytics.notification_base_table +as +select created_at::date created_at, + id notification_id, + user_id , + event_name, + notification_type, + request_id, + (event_param->>'eventDetail')::json->>'sku' sku, + (event_param->>'eventDetail')::json->>'price' price, + ((event_param->>'eventDetail')::json->>'price')::json->>'07030fbe-5801-4318-9e97-fe33fa169894' bronze_price, + ((event_param->>'eventDetail')::json->>'price')::json->>'8eb95d6e-915a-4a91-9c12-fa43db995e19' silver_price, + ((event_param->>'eventDetail')::json->>'price')::json->>'bf645e97-8a48-4977-8367-e987489760f9' gold_price, + (event_param->>'eventDetail')::json->>'margin' margin, + (event_param->>'eventDetail')::json->>'flashSaleId' flashSaleId, + (event_param->>'eventDetail')::json->>'newQty' newQty, + (event_param->>'eventDetail')::json->>'oldQty' oldQty, + (event_param->>'eventDetail')::json->>'retailPrice' retailPrice, + (event_param->>'eventDetail')::json->>'applicablePrice' applicablePrice, + ((event_param->>'eventDetail')::json->>'applicablePrice')::json->>'new' new_applicablePrice, + ((event_param->>'eventDetail')::json->>'applicablePrice')::json->>'old' old_applicablePrice, + (event_param->>'eventDetail')::json->>'percentageChange' percentageChange, + (event_param->>'userContactDetail')::json->>'name' user_name, + (event_param->>'userContactDetail')::json->>'email'user_email, + (event_param->>'userContactDetail')::json->>'mobile'user_mobile, + (event_param->>'userContactDetail')::json->>'tierId'user_tierid, + (event_param->>'userContactDetail')::json->>'lastLoggedInAt' user_last_loggedin_at +from raena_notification_management.notifications_log A +left join (select Notification_type, id template_id from raena_notification_management.notification_template) B on A.template_id = B.template_id +where created_at ::date >='2023-06-01' +and event_name in ('flash-sale-start','back-in-stock','price-drop'); + + +delete from raena_analytics.notification_funnel_report +where created_at >='2023-06-30'; + + +INSERT INTO raena_analytics.notification_funnel_report +SELECT A.created_at, + A.event_name, + A.notification_type, + A.user_name, + A.User_mobile, + A.user_email, + count(DISTINCT CASE WHEN unique_user>0 THEN 1 END) unique_user, + count(DISTINCT CASE WHEN view_item>0 THEN 1 END) view_item, + count(DISTINCT CASE WHEN view_cart>0 THEN 1 END) view_cart, + count(DISTINCT CASE WHEN add_to_cart>0 THEN 1 END) add_to_cart, + count(DISTINCT CASE WHEN begin_checkout>0 THEN 1 END) begin_checkout, + count(DISTINCT CASE WHEN page_load_checkout_continue>0 THEN 1 END) page_load_checkout_continue, + count(DISTINCT CASE WHEN page_load_finish_checkout>0 THEN 1 END) page_load_finish_checkout, + count(DISTINCT CASE WHEN finish_checkout>0 THEN 1 END) finish_checkout, + count(DISTINCT CASE WHEN Charged>0 THEN 1 END) Charged +FROM + (SELECT DISTINCT created_at , + event_name, + notification_type, + user_name, + User_mobile, + user_email, + request_id + FROM raena_analytics.notification_base_table + WHERE created_at>='2023-06-30') A +LEFT JOIN + (SELECT A.created_date, + A.email, + A.phone, + A.source, + A.requestid, + count(DISTINCT A.email) unique_user, + count(DISTINCT CASE WHEN view_item >0 THEN A.email END) view_item, + count(DISTINCT CASE WHEN view_cart >0 THEN A.email END) view_cart, + count(DISTINCT CASE WHEN add_to_cart >0 THEN A.email END) add_to_cart, + count(DISTINCT CASE WHEN begin_checkout >0 THEN A.email END) begin_checkout, + count(DISTINCT CASE WHEN page_load_checkout_continue >0 THEN A.email END) page_load_checkout_continue, + count(DISTINCT CASE WHEN page_load_finish_checkout >0 THEN A.email END) page_load_finish_checkout, + count(DISTINCT CASE WHEN finish_checkout >0 THEN A.email END) finish_checkout, + count(DISTINCT CASE WHEN C.Charged >0 THEN A.email END) Charged + FROM raena_analytics.notification_session_base_event A + LEFT JOIN raena_analytics.clevertab_session_id_base_data B ON A.sessionid = B.sessionid + LEFT JOIN raena_analytics.clevertab_charged_base_data C ON A.email = C.email + AND A.phone = C.phone + AND A.created_date = C.created_date + WHERE A.created_date >='2023-06-30' + GROUP BY 1, + 2, + 3, + 4, + 5) B ON --(replace(A.User_mobile,'+','') = B.phone +A.user_email = B.email +AND A.request_id = B.requestid +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +DROP TABLE raena_analytics.notification_order_base ; + + +CREATE TABLE raena_analytics.notification_order_base AS +SELECT reseller_id, + reseller_mobile , + transaction_date::date transaction_date, + external_id , + sku , + brand_name , + category_name , + discounted_price*quantity payment_amount , + quantity , + sum(discounted_price*quantity) over(partition BY external_id) order_level_amount +FROM raena_analytics.gm_dashboard gd +WHERE transaction_date ::date >='2023-06-15' + AND order_placed_by <>'Admin Panel'; + + +DROP TABLE raena_analytics.segement_raw_data; +CREATE TABLE raena_analytics.segement_raw_data AS +SELECT id notification_id, + user_id , + event_name, + notification_type, + request_id, + (event_param->>'eventDetail')::json->>'sku' sku, + (event_param->>'userContactDetail')::json->>'segment'segment +FROM raena_notification_management.notifications_log A +LEFT JOIN + (SELECT Notification_type, + template_id + FROM raena_notification_management.notification_event_template_mapping) B ON A.template_id = B.template_id +WHERE created_at ::date >='2023-06-30' + AND event_name IN ('flash-sale-start', + 'back-in-stock', + 'price-drop') + AND (event_param->>'userContactDetail')::json->>'segment' IS NOT NULL ; + + +DROP TABLE IF EXISTS raena_analytics.user_bucket; + +CREATE TABLE raena_analytics.user_bucket AS +SELECT reseller_id , + max(payment_amount) max_payment_amount +FROM + (SELECT reseller_id , + id , + sum(payment_amount)payment_amount + FROM raena_order_management.order + WHERE payment_status ='Paid' + GROUP BY 1, + 2) A +GROUP BY 1; + +delete from raena_analytics.transaction_notification_mapping +where created_at >='2023-06-30'; + +INSERT INTO raena_analytics.transaction_notification_mapping +SELECT D.name brand_name, + A.*, + B.external_id , + B.quantity, + CASE + WHEN A.sku = B.sku THEN payment_amount + END payment_amount, + CASE + WHEN A.sku IS NOT NULL + AND B.sku IS NOT NULL + AND A.sku = B.sku + AND payment_amount>0 THEN order_level_amount + END order_level_amount, + payment_amount indirect_payment_amount, + CASE + WHEN date_trunc('Month',A.created_at)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.created_at)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.created_at)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.created_at)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + DD.source, + XX.segment, + case when max_payment_amount between 0 and 2000000 then '0to2M' + when max_payment_amount between 2000000.00001 and 10000000 then '2Mto10M' + when max_payment_amount >= 10000000.000001 then 'greaterthan10M' end bucket +FROM raena_analytics.notification_base_table A +INNER JOIN raena_catalog_management.product C ON A.sku = C.sku +INNER JOIN raena_catalog_management.brand D ON C.brand_id = D.id +left join raena_analytics.segement_raw_data XX on A.notification_id = XX.notification_id +and A.user_id = XX.user_id +and A.event_name = XX.event_name +and A.notification_type = XX.notification_type +and A.request_id = XX.request_id +and A.sku = XX.sku +left join raena_analytics.user_bucket YY +on cast(A.user_id as varchar) = YY.reseller_id +LEFT JOIN raena_analytics.user_type_table flag ON replace(A.user_mobile,'+','')=flag.reseller_mobile +LEFT JOIN raena_analytics.notification_session_base_event dd ON replace(A.user_mobile,'+','')=DD.phone +AND --(date_part('day',A.created_at::date)-date_part('Day',created_date::date) = 1) and (date_part('Month',A.created_at::date)-date_part('Month',created_date::date) = 0 ) and + lower(A.notification_type) =lower(source) +AND A.request_id = dd.requestid +LEFT JOIN raena_analytics.notification_order_base B ON cast(A.user_id AS varchar) = B.reseller_id +and (transaction_date::date-A.created_at::date) between 0 and 3 +AND replace(B.reseller_mobile,'+','')=DD.phone +WHERE A.created_at >='2023-06-30'; + +drop table raena_analytics.anchor_brand_level_data ; + +CREATE TABLE raena_analytics.anchor_brand_level_data AS +SELECT * , + rank()over (partition BY external_id + ORDER BY brand_contribution DESC) rnk +FROM + (SELECT * , + CASE + WHEN order_level_payment_amount <2000000 THEN '0 to 2M' + WHEN order_level_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' + WHEN order_level_payment_amount >10000000 THEN '>10M' + END bucket, + (brand_payment_amount/nullif(order_level_payment_amount,0))*100 AS brand_contribution + FROM + (SELECT *, + sum(brand_payment_amount) over(partition BY external_id) order_level_payment_amount + FROM + (SELECT date_trunc('Year',transaction_date)::date transaction_date , + external_id , + brand_name, + sum(discounted_price*quantity) brand_payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2, + 3) A + WHERE brand_payment_amount>10) B) C +WHERE brand_contribution<100; + + drop table raena_analytics.anchor_brand_data; + +CREATE TABLE raena_analytics.anchor_brand_data AS +SELECT cast(date_part('Year',transaction_date) AS varchar) transaction_date2, + bucket, + brand_name , + avg(brand_contribution) avg_brand_contribution, + avg(brand_payment_amount) avg_payment_amount , + anchor_brand_name , + avg(anchor_brand_contribution) avg_anchor_brand_contribution , + avg(anchor_payment_amount) avg_anchor_payment_amount +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +GROUP BY 1, + 2, + 3, + 6 +ORDER BY transaction_date2 , + bucket, + brand_name , + avg_anchor_brand_contribution DESC; + + +DROP TABLE raena_analytics.reseller_level_anchor_brand_level_data ; + +CREATE TABLE raena_analytics.reseller_level_anchor_brand_level_data AS +SELECT * , + rank()over (partition BY external_id + ORDER BY brand_contribution DESC) rnk +FROM + (SELECT B.* , + CASE + WHEN bucket_level_payment_amount <2000000 THEN '0 to 2M' + WHEN bucket_level_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' + WHEN bucket_level_payment_amount >10000000 THEN '>10M' + END bucket, + (brand_payment_amount/nullif(order_level_payment_amount,0))*100 AS brand_contribution + FROM + (SELECT *, + sum(brand_payment_amount) over(partition BY external_id) order_level_payment_amount + FROM + (SELECT date_trunc('Year',transaction_date)::date transaction_date, + reseller_id, + reseller_mobile , + reseller_email , + external_id , + brand_name, + sum(discounted_price*quantity) brand_payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) A + WHERE brand_payment_amount>10) B + LEFT JOIN + (SELECT reseller_id, + max(payment_amount) bucket_level_payment_amount + FROM + (SELECT reseller_id , + external_id , + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2) A + GROUP BY 1) C ON B.reseller_id = C.reseller_id) D +WHERE brand_contribution<100; + +drop table raena_analytics.reseller_level_anchor_brand_data; + +CREATE TABLE raena_analytics.reseller_level_anchor_brand_data AS +SELECT reseller_id , + reseller_email , + reseller_mobile , + cast(date_part('Year',transaction_date) AS varchar) transaction_date2, + bucket, + brand_name , + avg(brand_contribution) avg_brand_contribution, + avg(brand_payment_amount) avg_payment_amount , + anchor_brand_name , + avg(anchor_brand_contribution) avg_anchor_brand_contribution , + avg(anchor_payment_amount) avg_anchor_payment_amount +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 9 +ORDER BY transaction_date2 , + bucket, + brand_name , + avg_anchor_brand_contribution DESC; + +DROP TABLE raena_analytics.anchor_analysis; + + +CREATE TABLE raena_analytics.anchor_analysis AS +SELECT BB.reseller_id , + brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_2023, + avg(CASE WHEN transaction_date <'2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_less_2023, + last_transaction_date +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +GROUP BY 1, + 2 , + 3, + 5, + 8 +ORDER BY 1, + 2, + 4; + +DROP TABLE raena_analytics.anchor_bucket_wise_for2023; + + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date BETWEEN '2023-01-01' AND '2023-05-31' + AND transaction_date ='2023-01-01' +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + +DROP TABLE raena_analytics.anchor_bucket_wise_for2023_churn_before2023; + + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023_churn_before2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date <'2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_before_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date <'2023-01-01' + AND transaction_date <'2023-01-01' +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + + +drop table raena_analytics.anchor_bucket_wise_for2023_churn_in2023; + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023_churn_in2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_in_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date ='2023-01-01' + AND brand_name IN + (SELECT DISTINCT brand_name + FROM raena_analytics.anchor_bucket_wise_for2023_churn_before2023) +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + + + diff --git a/postgresql/offender_dashboard/offender_dashboard.sh b/postgresql/offender_dashboard/offender_dashboard.sh new file mode 100644 index 0000000..c43a6c8 --- /dev/null +++ b/postgresql/offender_dashboard/offender_dashboard.sh @@ -0,0 +1,953 @@ + #!/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.base_analytics_dump; + + +CREATE TABLE raena_analytics.base_analytics_dump AS +SELECT ts, + events, + phone, + event_props::jsonb->>'CT Session Id' sessionid , + event_props::jsonb->>'page_name' pagename +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events IN ('load_home_page', + 'page_load', + 'add_to_cart', + 'begin_checkout', + 'finish_checkout', + 'Charged', + 'page_load_checkout_continue', + 'page_load_finish_checkout', + 'confirm_payment', + 'view_cart', + 'view_item' ) + AND event_props NOT LIKE '%Focallure%'; + +DROP TABLE raena_analytics.add_to_cart_base_1; + + +CREATE TABLE raena_analytics.add_to_cart_base_1 AS +SELECT DISTINCT ts::date, + phone, + event_props::jsonb->>'CT Session Id' sessionid , + event_props::jsonb->>'screen' pagename, + event_props::jsonb->>'item_id' sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='add_to_cart' + AND event_props::jsonb->>'item_id' IS NOT NULL ; + + +DROP TABLE raena_analytics.view_item_base_1; + + +CREATE TABLE raena_analytics.view_item_base_1 AS +SELECT DISTINCT ts::date, + phone, + event_props::jsonb->>'CT Session Id' sessionid , + event_props::jsonb->>'screen' pagename, + event_props::jsonb->>'item_id' sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='view_item' + AND event_props::jsonb->>'item_id' IS NOT NULL ; + + +DROP TABLE raena_analytics.begin_checkout_base_1; + + +CREATE TABLE raena_analytics.begin_checkout_base_1 AS +SELECT DISTINCT ts::date, + phone, + event_props::jsonb->>'CT Session Id' sessionid , + replace(replace(unnest(regexp_split_to_array(event_props::jsonb ->>'items',',')),']',''),'[','') sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='begin_checkout'; + + + +drop table raena_analytics.finish_checkout_base_1; + +create table raena_analytics.finish_checkout_base_1 +as +select A.*,P.sku +from +(select distinct ts::date, phone,event_props::jsonb->>'CT Session Id' sessionid , +replace(replace(unnest(regexp_split_to_array(event_props::jsonb ->>'items',',')),']',''),'[','') pagename +from clevertap.clevertap_master_data cmd +where ts::date >='2023-01-01' +and events ='finish_checkout') A +left join raena_catalog_management.product P on A.pagename = cast(P.id as varchar) ; + +drop table raena_analytics.pagename_homepage_data; + +create table raena_analytics.pagename_homepage_data +as +select distinct sessionid ,phone,pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Home'; + + +drop table if exists raena_analytics.pagename_homepage_data_remaining_funnel_stage1; + +CREATE TABLE raena_analytics.pagename_homepage_data_remaining_funnel_stage1 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_homepage_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Home' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + +drop table raena_analytics.offender_funnel_report; + +CREATE TABLE raena_analytics.offender_funnel_report AS +SELECT ts, + pagename, + C.is_offender , + D.name, + count(phone) count_view_item, + count(add_to_cart_phone) count_add_to_cart, + count(begin_checkout_cart) begin_checkout_cart, + count(finish_checkout_cart) finish_checkout_cart +FROM + (SELECT DISTINCT A.ts, + A.sessionid, + A.phone, + A.pagename , + A.sku , + B.phone add_to_cart_phone , + C.phone begin_checkout_cart, + D.phone finish_checkout_cart + FROM raena_analytics.view_item_base_1 A + LEFT JOIN raena_analytics.add_to_cart_base_1 B ON A.sessionid = B.sessionid + AND A.sku = B.sku + LEFT JOIN raena_analytics.begin_checkout_base_1 C ON A.sessionid = C.sessionid + AND A.sku = C.sku + LEFT JOIN raena_analytics.finish_checkout_base_1 D ON A.sessionid = D.sessionid + AND A.sku = C.sku) AA +LEFT JOIN raena_catalog_management.product C ON AA.sku = C.sku +LEFT JOIN raena_catalog_management.brand D ON C.brand_id = D.id +GROUP BY 1, + 2, + 3, + 4; + +drop table raena_analytics.pagename_Brands_data; + +create table raena_analytics.pagename_Brands_data +as +select distinct sessionid ,phone,pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Brands'; + + +drop table if exists raena_analytics.pagename_brand_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_brand_data_remaining_funnel_stage2 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_brands_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Brands' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + + +drop table raena_analytics.pagename_search_data; + +create table raena_analytics.pagename_search_data +as +select distinct sessionid ,phone,'Search' pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename like '%Search%'; + + +drop table if exists raena_analytics.pagename_search_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_search_data_remaining_funnel_stage2 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_search_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename LIKE '%Search%' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + +-----CategoryList---- + + + +drop table raena_analytics.pagename_CategoryList_data; + +create table raena_analytics.pagename_CategoryList_data +as +select distinct sessionid ,phone,pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'CategoryList'; + + +drop table if exists raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_CategoryList_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'CategoryList' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + + +-----Cart----- + + + +drop table raena_analytics.pagename_Cart_data; + +create table raena_analytics.pagename_Cart_data +as +select distinct sessionid ,phone,pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Cart'; + + +drop table if exists raena_analytics.pagename_Cart_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_Cart_data_remaining_funnel_stage2 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_Cart_data A + INNER JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Cart' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + +drop table if exists raena_analytics.offender_dashboard_base_table; + +CREATE TABLE raena_analytics.offender_dashboard_base_table AS +SELECT gd.transaction_date , + category_name , + product_type , + brand_name , + gd.sku , + gd.shipping_province , + order_recipient , + gd.order_placed_by , + order_type , + tier_name , + gd.external_id , + sum(quantity) quantity , + sum(discounted_price*quantity) payment_price, + sum(cogs*quantity) total_cogs, + sum((retail_price*quantity)-(seller_margin*quantity)) total_wholesale_price , + C.name channel_name , + O.status, + payment_amount order_level_payment_amount, + reseller_mobile , + reseller_email , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END bucket, + is_offender, + sum(CASE WHEN gd.external_id = BB.order_id + AND gd.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount +FROM raena_analytics.gm_dashboard gd +LEFT JOIN raena_order_management.order O ON gd.external_id = O.id +LEFT JOIN raena_order_management.channel C ON O.channel_id = C.id +LEFT JOIN + (SELECT reseller_id , + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1, + 2) A + GROUP BY 1) D ON gd.reseller_id = D.reseller_id +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON gd.external_id = BB.order_id +AND gd.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON gd.external_id = CC.external_id +AND gd.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +GROUP BY gd.transaction_date , + category_name , + product_type , + brand_name , + gd.sku , + gd.shipping_province , + order_recipient , + gd.order_placed_by , + order_type , + tier_name , + gd.external_id , + C.name , + O.status, + payment_amount , + reseller_mobile , + reseller_email , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END , + is_offender; + + +drop table if exists raena_analytics.rentention_bucket_wise ; + +create table raena_analytics.rentention_bucket_wise +as +select month_sort , bucket, + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user, + count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user, + count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user, + count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user, + count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user, + count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user, + count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user, + count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user, + count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user +from ( +SELECT mobile , month_sort , bucket,is_offender,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end bucket, + is_offender, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT * + FROM + (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + is_offender, + discounted_price*quantity payment_amount + FROM raena_analytics.gm_dashboard gd + ) A + GROUP BY 1, + 2,3) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') AS mobile, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3,4) BB + group by 1,2 + order by 1,2; + + + +drop table if exists raena_analytics.rentention_overall ; + +create table raena_analytics.rentention_overall +as +select month_sort , + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user, + count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user, + count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user, + count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user, + count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user, + count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user, + count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user, + count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user, + count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user +from ( +SELECT mobile , month_sort,is_offender,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + is_offender, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT * + FROM + (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + is_offender, + discounted_price*quantity payment_amount + FROM raena_analytics.gm_dashboard gd + ) A + GROUP BY 1, + 2,3) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') AS mobile, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3) BB + group by 1 + order by 1; + + + + +drop table if exists raena_analytics.rentention_brand_wise ; + +create table raena_analytics.rentention_brand_wise +as +select month_sort ,bucket,brand_name,is_offender, + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user +from ( +SELECT mobile , month_sort ,bucket,brand_name,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end bucket, + base_1.brand_name, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT distinct * + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date)::date created_date, + brand_name, + discounted_price*quantity payment_amount, + row_number() over(partition BY reseller_mobile + ORDER BY cast(transaction_date AS Date)) created_rnk + FROM raena_analytics.gm_dashboard o + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + discounted_price*quantity payment_amount, + row_number() over(partition BY replace(cast(reseller_mobile AS varchar),'\"',''), date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date)) + ORDER BY date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date))) created_rnk + FROM raena_analytics.gm_dashboard o + ) A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_mobile AS mobile, + date_trunc('month',transaction_date)::date AS created_date, + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard o + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3,4) BB + left join (select distinct name, is_offender from raena_catalog_management.brand ) CC on BB.brand_name = CC.name + group by 1,2,3,4 + order by 1,2,3; + + + drop table raena_analytics.churn_reseller_data; + + CREATE TABLE raena_analytics.churn_reseller_data AS +SELECT date_trunc('Month',A.first_transaction_date)::date , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END bucket, + count(DISTINCT A.mobile) M0_user, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' THEN B.mobile END) churn, + count(DISTINCT CASE WHEN created_date ='2023-05-01' THEN base_2.mobile END) may_retention, + count(DISTINCT CASE WHEN created_date ='2023-06-01' THEN base_2.mobile END) jun_retention, + count(DISTINCT CASE WHEN created_date ='2023-07-01' THEN base_2.mobile END) july_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-05-01' THEN base_2.mobile END) from_churn_may_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-06-01' THEN base_2.mobile END) from_churn_jun_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-07-01' THEN base_2.mobile END) from_churn_july_retention, + count(DISTINCT CASE WHEN created_date ='2023-05-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_may_retention, + count(DISTINCT CASE WHEN created_date ='2023-06-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_jun_retention, + count(DISTINCT CASE WHEN created_date ='2023-07-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_july_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-05-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_may_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-06-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_jun_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-07-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_july_retention +FROM + (SELECT DISTINCT mobile, + created_date first_transaction_date, + payment_amount + FROM + (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') + ORDER BY cast(created_at AS Date)::date) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' ) a + WHERE a.created_rnk=1) A +LEFT JOIN + (SELECT DISTINCT mobile, + created_date last_transaction_date, + payment_amount + FROM + (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"','') + ORDER BY cast(created_at AS Date)::date DESC) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + AND cast(created_at AS Date)::date <='2023-07-17' ) a + WHERE a.created_rnk=1) B ON A.mobile = B.mobile +LEFT JOIN + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + discounted_price*quantity payment_amount, + p.is_offender + FROM raena_analytics.gm_dashboard o + LEFT JOIN raena_catalog_management.product P ON o.sku = P.sku + WHERE cast(transaction_date AS Date) >= date_trunc('Month',CURRENT_DATE)::date +interval'-4 Month' ) A + GROUP BY 1, + 2, + 3) base_2 ON A.mobile=base_2.mobile +LEFT JOIN + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_mobile AS mobile, + date_trunc('month',transaction_date)::date AS created_date, + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard o + GROUP BY 1, + 2) A + GROUP BY 1) base_3 ON A.mobile=base_3.mobile +GROUP BY 1, + 2; +" > /home/ec2-user/cronjob/postgresql/offender_dashboard/offender_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/offender_dashboard/offender_dashboard.sql + + diff --git a/postgresql/offender_dashboard/offender_dashboard.sql b/postgresql/offender_dashboard/offender_dashboard.sql new file mode 100644 index 0000000..46948d1 --- /dev/null +++ b/postgresql/offender_dashboard/offender_dashboard.sql @@ -0,0 +1,934 @@ + +DROP TABLE raena_analytics.base_analytics_dump; + + +CREATE TABLE raena_analytics.base_analytics_dump AS +SELECT ts, + events, + phone, + event_props::jsonb->>'CT Session Id' sessionid , + event_props::jsonb->>'page_name' pagename +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events IN ('load_home_page', + 'page_load', + 'add_to_cart', + 'begin_checkout', + 'finish_checkout', + 'Charged', + 'page_load_checkout_continue', + 'page_load_finish_checkout', + 'confirm_payment', + 'view_cart', + 'view_item' ) + AND event_props NOT LIKE '%Focallure%'; + +DROP TABLE raena_analytics.add_to_cart_base_1; + + +CREATE TABLE raena_analytics.add_to_cart_base_1 AS +SELECT DISTINCT ts::date, + phone, + event_props::jsonb->>'CT Session Id' sessionid , + event_props::jsonb->>'screen' pagename, + event_props::jsonb->>'item_id' sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='add_to_cart' + AND event_props::jsonb->>'item_id' IS NOT NULL ; + + +DROP TABLE raena_analytics.view_item_base_1; + + +CREATE TABLE raena_analytics.view_item_base_1 AS +SELECT DISTINCT ts::date, + phone, + event_props::jsonb->>'CT Session Id' sessionid , + event_props::jsonb->>'screen' pagename, + event_props::jsonb->>'item_id' sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='view_item' + AND event_props::jsonb->>'item_id' IS NOT NULL ; + + +DROP TABLE raena_analytics.begin_checkout_base_1; + + +CREATE TABLE raena_analytics.begin_checkout_base_1 AS +SELECT DISTINCT ts::date, + phone, + event_props::jsonb->>'CT Session Id' sessionid , + replace(replace(unnest(regexp_split_to_array(event_props::jsonb ->>'items',',')),']',''),'[','') sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='begin_checkout'; + + + +drop table raena_analytics.finish_checkout_base_1; + +create table raena_analytics.finish_checkout_base_1 +as +select A.*,P.sku +from +(select distinct ts::date, phone,event_props::jsonb->>'CT Session Id' sessionid , +replace(replace(unnest(regexp_split_to_array(event_props::jsonb ->>'items',',')),']',''),'[','') pagename +from clevertap.clevertap_master_data cmd +where ts::date >='2023-01-01' +and events ='finish_checkout') A +left join raena_catalog_management.product P on A.pagename = cast(P.id as varchar) ; + +drop table raena_analytics.pagename_homepage_data; + +create table raena_analytics.pagename_homepage_data +as +select distinct sessionid ,phone,pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Home'; + + +drop table if exists raena_analytics.pagename_homepage_data_remaining_funnel_stage1; + +CREATE TABLE raena_analytics.pagename_homepage_data_remaining_funnel_stage1 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_homepage_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Home' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + +drop table raena_analytics.offender_funnel_report; + +CREATE TABLE raena_analytics.offender_funnel_report AS +SELECT ts, + pagename, + C.is_offender , + D.name, + count(phone) count_view_item, + count(add_to_cart_phone) count_add_to_cart, + count(begin_checkout_cart) begin_checkout_cart, + count(finish_checkout_cart) finish_checkout_cart +FROM + (SELECT DISTINCT A.ts, + A.sessionid, + A.phone, + A.pagename , + A.sku , + B.phone add_to_cart_phone , + C.phone begin_checkout_cart, + D.phone finish_checkout_cart + FROM raena_analytics.view_item_base_1 A + LEFT JOIN raena_analytics.add_to_cart_base_1 B ON A.sessionid = B.sessionid + AND A.sku = B.sku + LEFT JOIN raena_analytics.begin_checkout_base_1 C ON A.sessionid = C.sessionid + AND A.sku = C.sku + LEFT JOIN raena_analytics.finish_checkout_base_1 D ON A.sessionid = D.sessionid + AND A.sku = C.sku) AA +LEFT JOIN raena_catalog_management.product C ON AA.sku = C.sku +LEFT JOIN raena_catalog_management.brand D ON C.brand_id = D.id +GROUP BY 1, + 2, + 3, + 4; + +drop table raena_analytics.pagename_Brands_data; + +create table raena_analytics.pagename_Brands_data +as +select distinct sessionid ,phone,pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Brands'; + + +drop table if exists raena_analytics.pagename_brand_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_brand_data_remaining_funnel_stage2 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_brands_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Brands' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + + +drop table raena_analytics.pagename_search_data; + +create table raena_analytics.pagename_search_data +as +select distinct sessionid ,phone,'Search' pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename like '%Search%'; + + +drop table if exists raena_analytics.pagename_search_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_search_data_remaining_funnel_stage2 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_search_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename LIKE '%Search%' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + +-----CategoryList---- + + + +drop table raena_analytics.pagename_CategoryList_data; + +create table raena_analytics.pagename_CategoryList_data +as +select distinct sessionid ,phone,pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'CategoryList'; + + +drop table if exists raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_CategoryList_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'CategoryList' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + + +-----Cart----- + + + +drop table raena_analytics.pagename_Cart_data; + +create table raena_analytics.pagename_Cart_data +as +select distinct sessionid ,phone,pagename,ts::date ts +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Cart'; + + +drop table if exists raena_analytics.pagename_Cart_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_Cart_data_remaining_funnel_stage2 AS +SELECT AA.ts, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_Cart_data A + INNER JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Cart' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + +drop table if exists raena_analytics.offender_dashboard_base_table; + +CREATE TABLE raena_analytics.offender_dashboard_base_table AS +SELECT gd.transaction_date , + category_name , + product_type , + brand_name , + gd.sku , + gd.shipping_province , + order_recipient , + gd.order_placed_by , + order_type , + tier_name , + gd.external_id , + sum(quantity) quantity , + sum(discounted_price*quantity) payment_price, + sum(cogs*quantity) total_cogs, + sum((retail_price*quantity)-(seller_margin*quantity)) total_wholesale_price , + C.name channel_name , + O.status, + payment_amount order_level_payment_amount, + reseller_mobile , + reseller_email , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END bucket, + is_offender, + sum(CASE WHEN gd.external_id = BB.order_id + AND gd.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount +FROM raena_analytics.gm_dashboard gd +LEFT JOIN raena_order_management.order O ON gd.external_id = O.id +LEFT JOIN raena_order_management.channel C ON O.channel_id = C.id +LEFT JOIN + (SELECT reseller_id , + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1, + 2) A + GROUP BY 1) D ON gd.reseller_id = D.reseller_id +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON gd.external_id = BB.order_id +AND gd.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON gd.external_id = CC.external_id +AND gd.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +GROUP BY gd.transaction_date , + category_name , + product_type , + brand_name , + gd.sku , + gd.shipping_province , + order_recipient , + gd.order_placed_by , + order_type , + tier_name , + gd.external_id , + C.name , + O.status, + payment_amount , + reseller_mobile , + reseller_email , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END , + is_offender; + + +drop table if exists raena_analytics.rentention_bucket_wise ; + +create table raena_analytics.rentention_bucket_wise +as +select month_sort , bucket, + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user, + count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user, + count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user, + count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user, + count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user, + count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user, + count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user, + count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user, + count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user +from ( +SELECT mobile , month_sort , bucket,is_offender,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end bucket, + is_offender, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT * + FROM + (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + is_offender, + discounted_price*quantity payment_amount + FROM raena_analytics.gm_dashboard gd + ) A + GROUP BY 1, + 2,3) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3,4) BB + group by 1,2 + order by 1,2; + + + +drop table if exists raena_analytics.rentention_overall ; + +create table raena_analytics.rentention_overall +as +select month_sort , + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user, + count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user, + count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user, + count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user, + count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user, + count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user, + count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user, + count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user, + count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user +from ( +SELECT mobile , month_sort,is_offender,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + is_offender, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT * + FROM + (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + is_offender, + discounted_price*quantity payment_amount + FROM raena_analytics.gm_dashboard gd + ) A + GROUP BY 1, + 2,3) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3) BB + group by 1 + order by 1; + + + + +drop table if exists raena_analytics.rentention_brand_wise ; + +create table raena_analytics.rentention_brand_wise +as +select month_sort ,bucket,brand_name,is_offender, + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user +from ( +SELECT mobile , month_sort ,bucket,brand_name,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end bucket, + base_1.brand_name, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT distinct * + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date)::date created_date, + brand_name, + discounted_price*quantity payment_amount, + row_number() over(partition BY reseller_mobile + ORDER BY cast(transaction_date AS Date)) created_rnk + FROM raena_analytics.gm_dashboard o + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + discounted_price*quantity payment_amount, + row_number() over(partition BY replace(cast(reseller_mobile AS varchar),'"',''), date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date)) + ORDER BY date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date))) created_rnk + FROM raena_analytics.gm_dashboard o + ) A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_mobile AS mobile, + date_trunc('month',transaction_date)::date AS created_date, + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard o + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3,4) BB + left join (select distinct name, is_offender from raena_catalog_management.brand ) CC on BB.brand_name = CC.name + group by 1,2,3,4 + order by 1,2,3; + + + drop table raena_analytics.churn_reseller_data; + + CREATE TABLE raena_analytics.churn_reseller_data AS +SELECT date_trunc('Month',A.first_transaction_date)::date , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END bucket, + count(DISTINCT A.mobile) M0_user, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' THEN B.mobile END) churn, + count(DISTINCT CASE WHEN created_date ='2023-05-01' THEN base_2.mobile END) may_retention, + count(DISTINCT CASE WHEN created_date ='2023-06-01' THEN base_2.mobile END) jun_retention, + count(DISTINCT CASE WHEN created_date ='2023-07-01' THEN base_2.mobile END) july_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-05-01' THEN base_2.mobile END) from_churn_may_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-06-01' THEN base_2.mobile END) from_churn_jun_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-07-01' THEN base_2.mobile END) from_churn_july_retention, + count(DISTINCT CASE WHEN created_date ='2023-05-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_may_retention, + count(DISTINCT CASE WHEN created_date ='2023-06-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_jun_retention, + count(DISTINCT CASE WHEN created_date ='2023-07-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_july_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-05-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_may_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-06-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_jun_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-07-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_july_retention +FROM + (SELECT DISTINCT mobile, + created_date first_transaction_date, + payment_amount + FROM + (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') + ORDER BY cast(created_at AS Date)::date) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' ) a + WHERE a.created_rnk=1) A +LEFT JOIN + (SELECT DISTINCT mobile, + created_date last_transaction_date, + payment_amount + FROM + (SELECT DISTINCT replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(o.reseller_info::json->'mobile' AS varchar),'"','') + ORDER BY cast(created_at AS Date)::date DESC) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + AND cast(created_at AS Date)::date <='2023-07-17' ) a + WHERE a.created_rnk=1) B ON A.mobile = B.mobile +LEFT JOIN + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + discounted_price*quantity payment_amount, + p.is_offender + FROM raena_analytics.gm_dashboard o + LEFT JOIN raena_catalog_management.product P ON o.sku = P.sku + WHERE cast(transaction_date AS Date) >= date_trunc('Month',CURRENT_DATE)::date +interval'-4 Month' ) A + GROUP BY 1, + 2, + 3) base_2 ON A.mobile=base_2.mobile +LEFT JOIN + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_mobile AS mobile, + date_trunc('month',transaction_date)::date AS created_date, + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard o + GROUP BY 1, + 2) A + GROUP BY 1) base_3 ON A.mobile=base_3.mobile +GROUP BY 1, + 2; + diff --git a/postgresql/oos_restock/oos_restock_etl.sh b/postgresql/oos_restock/oos_restock_etl.sh new file mode 100644 index 0000000..f83eb71 --- /dev/null +++ b/postgresql/oos_restock/oos_restock_etl.sh @@ -0,0 +1,654 @@ + #!/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 " + +--------------------------OOS ------------------------- +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_1; + + +CREATE TABLE raena_analytics.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, + to_timestamp(snp_date || ' ' || snp_time,'YY-MM-DD HH24:MI:SS') +interval'7 hours' AS snp_date_time, + sku_code, + inventory + FROM raena_analytics.inventory_snapshot + where snp_time+interval'7 hours'>='23:30' + )A ) B +WHERE R=1; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_2; + +CREATE TABLE raena_analytics.om_oos_soldout_2 AS +SELECT *, + (date_part('Year',CURRENT_DATE)-date_part('year',cast(snp_date_time AS date)))*12*30 + + (date_part('Month',CURRENT_DATE)-date_part('Month',cast(snp_date_time AS date)))*30 + + (date_part('day',CURRENT_DATE)-date_part('day',cast(snp_date_time AS date))) AS age +FROM raena_analytics.om_oos_soldout_1; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_2_med; + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_soldout_2 +WHERE age>0; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_3; + + +CREATE TABLE raena_analytics.om_oos_soldout_3 AS +SELECT a.*, + b.inventory AS Maximum_occurred_inventory +FROM raena_analytics.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 raena_analytics.om_oos_soldout_2_med + GROUP BY inventory, + age_group, + sku_code + ORDER BY 4 DESC,1) A) AA + 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 raena_analytics.om_oos_soldout_3_med; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_soldout_3 + GROUP BY 1 + ORDER BY sku_code) b ON a.sku_code=b.sku_code ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_3_med_2; + + +CREATE TABLE raena_analytics.om_oos_soldout_3_med_2 AS +SELECT a.*, + b.sold_out_fl +FROM raena_analytics.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 + FROM raena_analytics.om_oos_soldout_3_med + )A) B + WHERE R=1) b ON a.sku_code=b.sku_code ; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_4; + + +CREATE TABLE raena_analytics.om_oos_soldout_4 AS +SELECT a.*, + b.brand_name, + b.\"EL/PL Flag\", b.give_away, + b.active_status +FROM raena_analytics.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 b.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 raena_analytics.demand_forecasting_configs c ON a.sku=c.sku) b ON a.sku_code=b.sku ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_5; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_soldout_4 a +LEFT JOIN raena_analytics.demand_forecast_dashboard_final b ON a.sku_code=b.sku +LEFT JOIN + (SELECT DISTINCT sku , + CASE + WHEN is_slash_price='true' THEN slashed_tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9' + ELSE tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9' + 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 raena_analytics.om_oos_soldout_6; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 IS NULL + AND payment_amount is NOT NULL + 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 raena_analytics.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 raena_analytics.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')AAA) AA + 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 raena_analytics.om_oos_restock_1; + + +CREATE TABLE raena_analytics.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\", + (date_part('Year',CURRENT_DATE)-date_part('year',cast(report_date AS date)))*12*30 + + (date_part('Month',CURRENT_DATE)-date_part('Month',cast(report_date AS date)))*30 + + (date_part('day',CURRENT_DATE)-date_part('day',cast(report_date AS date))) AS age, + c.gold_price +FROM raena_analytics.OM_OOS_Base a +LEFT JOIN + (SELECT DISTINCT sku , + CASE + WHEN is_slash_price='true' THEN slashed_tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9' + ELSE tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9' + END AS gold_price + FROM raena_catalog_management.product p) c ON a.sku=c.sku; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_1_med; + +CREATE TABLE raena_analytics.om_oos_restock_1_med AS +SELECT * +FROM raena_analytics.om_oos_restock_1 C +CROSS JOIN LATERAL ( + VALUES (cikarang_project_quantity_to_order,'cikarang_project_quantity_to_order'), + (surabaya_project_quantity_to_order, 'surabaya_project_quantity_to_order'), + (semarang_project_quantity_to_order, 'semarang_project_quantity_to_order'), + (medan_project_quantity_to_order, 'medan_project_quantity_to_order'), + (makassar_project_quantity_to_order,'makassar_project_quantity_to_order'), + (semarandi_project_quantity_to_order,'semarandi_project_quantity_to_order')) AS t(cnt,warehouse); + + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_2; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_restock_1_med +WHERE age>0; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_3; + + +CREATE TABLE raena_analytics.om_oos_restock_3 AS +SELECT a.*, + b.restock AS Maximum_occurred_inventory +FROM raena_analytics.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 raena_analytics.om_oos_restock_2 + GROUP BY cnt, + age_group, + warehouse, + sku + ORDER BY 5 DESC,1) A) B + 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 raena_analytics.om_oos_restock_4; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_restock_4_stage1; + +create table raena_analytics.om_oos_restock_4_stage1 +as + 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 s OLD 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 + FROM raena_analytics.om_oos_restock_4 + )A )B + WHERE R=1 ; + +drop table if exists raena_analytics.om_oos_restock_4_stage2; +create table raena_analytics.om_oos_restock_4_stage2 +as + SELECT sku, + sum(quantity)/count(DISTINCT cast(transaction_date AS date)) AS runrate + FROM raena_analytics.gm_dashboard + WHERE cast(transaction_date AS date)>=cast((CURRENT_DATE-90) AS date) + GROUP BY sku; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_5; + +CREATE TABLE raena_analytics.om_oos_restock_5 AS +SELECT a.*, + b.sold_out_fl, + c.runrate AS \"90_days_runrate\" +FROM raena_analytics.om_oos_restock_4 a +INNER JOIN raena_analytics.om_oos_restock_4_stage1 b ON a.sku=b.sku +AND a.warehouse=b.warehouse +LEFT JOIN raena_analytics.om_oos_restock_4_stage2 c ON a.sku=c.sku; + + +DROP TABLE IF EXISTS raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_restock_5_stage1; + +CREATE TABLE raena_analytics.om_oos_restock_5_stage1 AS + SELECT sku, + sum(payment_amount)payment_amount, + sum(quantity)quantity, + sub_order_shipping_id, + sum(shipping_amount)shipping_amount + FROM raena_analytics.rate_sheet_wise_expected_warehouse + WHERE created_date>=(CURRENT_DATE-90) + AND is_campaign='false' + group by 1,4; + + + + +drop table if exists raena_analytics.om_oos_restock_5_stage2; + +CREATE TABLE raena_analytics.om_oos_restock_5_stage2 AS +SELECT DISTINCT A.sku, + A.payment_amount, + A.quantity, + A.sub_order_shipping_id, + A.shipping_amount \"Actual Shipping Fee\", + DD.Expected_shipping_fee, + jsf.expected_JTR_shipping_fee +FROM + raena_analytics.om_oos_restock_5_stage1 A + LEFT JOIN + (SELECT DISTINCT sub_order_shipping_id, + Expected_shipping_fee + FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping_final + WHERE Expected_shipping_fee IS NOT NULL ) DD ON A.sub_order_shipping_id=DD.sub_order_shipping_id + LEFT join + (SELECT sub_order_shipping_id , + expected_JTR_shipping_fee + FROM raena_analytics.om_shipment_jtr_shipping_fee_base_11 + WHERE expected_JTR_shipping_fee IS NOT NULL) jsf ON A.sub_order_shipping_id=jsf.sub_order_shipping_id; + + DROP TABLE IF EXISTS raena_analytics.om_shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.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, + payment_amount, + quantity, + sub_order_shipping_id, + coalesce(expected_JTR_shipping_fee,Expected_shipping_fee,\"Actual Shipping Fee\") AS Expected_shipping_fee, + \"Actual Shipping Fee\" + from raena_analytics.om_oos_restock_5_stage2) A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + +DROP TABLE IF EXISTS raena_analytics.om_shipping_fee_sku_base_stage3; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_shipping_fee_sku_base_stage2; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_6; + +CREATE TABLE raena_analytics.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, + \"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 raena_analytics.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 raena_analytics.om_oos_restock_5 + WHERE sold_out_fl NOT LIKE '%stocked' + GROUP BY 1, + 2) b ON a.sku=b.sku +LEFT JOIN raena_analytics.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, + (payment_amount/quantity) AS payment_amount, + (new_shipment_amount/quantity) AS \"Actual Shipping Fee\", (new_expected_shipment_amount/quantity) AS new_expected_shipment_amount + FROM raena_analytics.om_shipping_fee_sku_base_stage3 + )A) B + WHERE payment_amount!=0 + GROUP BY 1) d ON a.sku=d.sku; + + + " > /home/ec2-user/cronjob/postgresql/oos_restock/oos_restock_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/oos_restock/oos_restock_etl.sql diff --git a/postgresql/oos_restock/oos_restock_etl.sql b/postgresql/oos_restock/oos_restock_etl.sql new file mode 100644 index 0000000..2857b7f --- /dev/null +++ b/postgresql/oos_restock/oos_restock_etl.sql @@ -0,0 +1,637 @@ + + +--------------------------OOS ------------------------- +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_1; + + +CREATE TABLE raena_analytics.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, + to_timestamp(snp_date || ' ' || snp_time,'YY-MM-DD HH24:MI:SS') +interval'7 hours' AS snp_date_time, + sku_code, + inventory + FROM raena_analytics.inventory_snapshot + where snp_time+interval'7 hours'>='23:30' + )A ) B +WHERE R=1; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_2; + +CREATE TABLE raena_analytics.om_oos_soldout_2 AS +SELECT *, + (date_part('Year',CURRENT_DATE)-date_part('year',cast(snp_date_time AS date)))*12*30 + + (date_part('Month',CURRENT_DATE)-date_part('Month',cast(snp_date_time AS date)))*30 + + (date_part('day',CURRENT_DATE)-date_part('day',cast(snp_date_time AS date))) AS age +FROM raena_analytics.om_oos_soldout_1; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_2_med; + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_soldout_2 +WHERE age>0; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_3; + + +CREATE TABLE raena_analytics.om_oos_soldout_3 AS +SELECT a.*, + b.inventory AS Maximum_occurred_inventory +FROM raena_analytics.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 raena_analytics.om_oos_soldout_2_med + GROUP BY inventory, + age_group, + sku_code + ORDER BY 4 DESC,1) A) AA + 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 raena_analytics.om_oos_soldout_3_med; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_soldout_3 + GROUP BY 1 + ORDER BY sku_code) b ON a.sku_code=b.sku_code ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_3_med_2; + + +CREATE TABLE raena_analytics.om_oos_soldout_3_med_2 AS +SELECT a.*, + b.sold_out_fl +FROM raena_analytics.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 + FROM raena_analytics.om_oos_soldout_3_med + )A) B + WHERE R=1) b ON a.sku_code=b.sku_code ; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_4; + + +CREATE TABLE raena_analytics.om_oos_soldout_4 AS +SELECT a.*, + b.brand_name, + b."EL/PL Flag", b.give_away, + b.active_status +FROM raena_analytics.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 b.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 raena_analytics.demand_forecasting_configs c ON a.sku=c.sku) b ON a.sku_code=b.sku ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_5; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_soldout_4 a +LEFT JOIN raena_analytics.demand_forecast_dashboard_final b ON a.sku_code=b.sku +LEFT JOIN + (SELECT DISTINCT sku , + CASE + WHEN is_slash_price='true' THEN slashed_tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9' + ELSE tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9' + 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 raena_analytics.om_oos_soldout_6; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 IS NULL + AND payment_amount is NOT NULL + 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 raena_analytics.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 raena_analytics.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')AAA) AA + 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 raena_analytics.om_oos_restock_1; + + +CREATE TABLE raena_analytics.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", + (date_part('Year',CURRENT_DATE)-date_part('year',cast(report_date AS date)))*12*30 + + (date_part('Month',CURRENT_DATE)-date_part('Month',cast(report_date AS date)))*30 + + (date_part('day',CURRENT_DATE)-date_part('day',cast(report_date AS date))) AS age, + c.gold_price +FROM raena_analytics.OM_OOS_Base a +LEFT JOIN + (SELECT DISTINCT sku , + CASE + WHEN is_slash_price='true' THEN slashed_tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9' + ELSE tier_price::json->>'bf645e97-8a48-4977-8367-e987489760f9' + END AS gold_price + FROM raena_catalog_management.product p) c ON a.sku=c.sku; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_1_med; + +CREATE TABLE raena_analytics.om_oos_restock_1_med AS +SELECT * +FROM raena_analytics.om_oos_restock_1 C +CROSS JOIN LATERAL ( + VALUES (cikarang_project_quantity_to_order,'cikarang_project_quantity_to_order'), + (surabaya_project_quantity_to_order, 'surabaya_project_quantity_to_order'), + (semarang_project_quantity_to_order, 'semarang_project_quantity_to_order'), + (medan_project_quantity_to_order, 'medan_project_quantity_to_order'), + (makassar_project_quantity_to_order,'makassar_project_quantity_to_order'), + (semarandi_project_quantity_to_order,'semarandi_project_quantity_to_order')) AS t(cnt,warehouse); + + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_2; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_restock_1_med +WHERE age>0; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_3; + + +CREATE TABLE raena_analytics.om_oos_restock_3 AS +SELECT a.*, + b.restock AS Maximum_occurred_inventory +FROM raena_analytics.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 raena_analytics.om_oos_restock_2 + GROUP BY cnt, + age_group, + warehouse, + sku + ORDER BY 5 DESC,1) A) B + 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 raena_analytics.om_oos_restock_4; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_restock_4_stage1; + +create table raena_analytics.om_oos_restock_4_stage1 +as + 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 s OLD 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 + FROM raena_analytics.om_oos_restock_4 + )A )B + WHERE R=1 ; + +drop table if exists raena_analytics.om_oos_restock_4_stage2; +create table raena_analytics.om_oos_restock_4_stage2 +as + SELECT sku, + sum(quantity)/count(DISTINCT cast(transaction_date AS date)) AS runrate + FROM raena_analytics.gm_dashboard + WHERE cast(transaction_date AS date)>=cast((CURRENT_DATE-90) AS date) + GROUP BY sku; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_5; + +CREATE TABLE raena_analytics.om_oos_restock_5 AS +SELECT a.*, + b.sold_out_fl, + c.runrate AS "90_days_runrate" +FROM raena_analytics.om_oos_restock_4 a +INNER JOIN raena_analytics.om_oos_restock_4_stage1 b ON a.sku=b.sku +AND a.warehouse=b.warehouse +LEFT JOIN raena_analytics.om_oos_restock_4_stage2 c ON a.sku=c.sku; + + +DROP TABLE IF EXISTS raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_restock_5_stage1; + +CREATE TABLE raena_analytics.om_oos_restock_5_stage1 AS + SELECT sku, + sum(payment_amount)payment_amount, + sum(quantity)quantity, + sub_order_shipping_id, + sum(shipping_amount)shipping_amount + FROM raena_analytics.rate_sheet_wise_expected_warehouse + WHERE created_date>=(CURRENT_DATE-90) + AND is_campaign='false' + group by 1,4; + + + + +drop table if exists raena_analytics.om_oos_restock_5_stage2; + +CREATE TABLE raena_analytics.om_oos_restock_5_stage2 AS +SELECT DISTINCT A.sku, + A.payment_amount, + A.quantity, + A.sub_order_shipping_id, + A.shipping_amount "Actual Shipping Fee", + DD.Expected_shipping_fee, + jsf.expected_JTR_shipping_fee +FROM + raena_analytics.om_oos_restock_5_stage1 A + LEFT JOIN + (SELECT DISTINCT sub_order_shipping_id, + Expected_shipping_fee + FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping_final + WHERE Expected_shipping_fee IS NOT NULL ) DD ON A.sub_order_shipping_id=DD.sub_order_shipping_id + LEFT join + (SELECT sub_order_shipping_id , + expected_JTR_shipping_fee + FROM raena_analytics.om_shipment_jtr_shipping_fee_base_11 + WHERE expected_JTR_shipping_fee IS NOT NULL) jsf ON A.sub_order_shipping_id=jsf.sub_order_shipping_id; + + DROP TABLE IF EXISTS raena_analytics.om_shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.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, + payment_amount, + quantity, + sub_order_shipping_id, + coalesce(expected_JTR_shipping_fee,Expected_shipping_fee,"Actual Shipping Fee") AS Expected_shipping_fee, + "Actual Shipping Fee" + from raena_analytics.om_oos_restock_5_stage2) A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + +DROP TABLE IF EXISTS raena_analytics.om_shipping_fee_sku_base_stage3; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_shipping_fee_sku_base_stage2; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_6; + +CREATE TABLE raena_analytics.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, + "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 raena_analytics.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 raena_analytics.om_oos_restock_5 + WHERE sold_out_fl NOT LIKE '%stocked' + GROUP BY 1, + 2) b ON a.sku=b.sku +LEFT JOIN raena_analytics.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, + (payment_amount/quantity) AS payment_amount, + (new_shipment_amount/quantity) AS "Actual Shipping Fee", (new_expected_shipment_amount/quantity) AS new_expected_shipment_amount + FROM raena_analytics.om_shipping_fee_sku_base_stage3 + )A) B + WHERE payment_amount!=0 + GROUP BY 1) d ON a.sku=d.sku; + + + diff --git a/postgresql/product_matrics/product_matrics_etl.sh b/postgresql/product_matrics/product_matrics_etl.sh new file mode 100644 index 0000000..d09e602 --- /dev/null +++ b/postgresql/product_matrics/product_matrics_etl.sh @@ -0,0 +1,1049 @@ + +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.daily_recommendation_ingredients_split; + + create table raena_analytics.daily_recommendation_ingredients_split + as + select reseller_email ,sku,replace(replace(replace(\" Top Marketing Ingredients \",'{',''),'}',''),'\"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_ingredients; + +drop table raena_analytics.daily_recommendation_concerns_split; + + create table raena_analytics.daily_recommendation_concerns_split + as + select reseller_email ,sku,replace(replace(replace(\" Concern(s) \" ,'{',''),'}',''),'\"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_concerns drc ; + +DROP TABLE raena_analytics.product_metrics_calendar; + +CREATE TABLE raena_analytics.product_metrics_calendar AS +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type +FROM raena_order_management.order; + + +drop table if exists raena_analytics.order_base_for_user_type; + +create table raena_analytics.order_base_for_user_type +as +SELECT replace(reseller_info::json->>'mobile','+','') reseller_mobile, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' + GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.user_type_table ; + + +CREATE TABLE raena_analytics.user_type_table AS +SELECT DISTINCT profile_phone reseller_mobile, + First_transaction_date, + min_created_date first_install_date +FROM raena_analytics.order_base_for_user_type A +INNER JOIN + (SELECT profile_phone, + min(created_at) min_created_date + FROM + (SELECT profile_phone, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) created_at + FROM clevertap.app_installed + GROUP BY profile_phone + UNION + SELECT replace(mobile,'+','') profile_phone, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1) A + GROUP BY 1) B ON A.reseller_mobile=B.profile_phone; + + + +DROP TABLE IF EXISTS raena_analytics.total_uninstalled; + + +CREATE TABLE raena_analytics.total_uninstalled AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM (SELECT profile_objectid, + CASE + WHEN profile_phone<>'' THEN profile_phone + END profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_uninstalled) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + +DROP TABLE IF EXISTS raena_analytics.total_installed; + + +CREATE TABLE raena_analytics.total_installed AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + DROP TABLE raena_analytics.Active_user; + + +CREATE TABLE raena_analytics.Active_user AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',coalesce(first_install_date,D.created_at::date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND (date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date + OR First_transaction_date IS NULL) THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND first_transaction_date IS NULL THEN 'New' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.order_base_for_user_type B ON A.profile_phone = B.reseller_mobile +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + +DROP TABLE IF EXISTS raena_analytics.total_view_item; + + +CREATE TABLE raena_analytics.total_view_item AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 4; + + +drop table if exists raena_analytics.order_base_user_transaction; + +create table raena_analytics.order_base_user_transaction +as +SELECT reseller_id , + replace(reseller_info::json->>'mobile','+','') profile_phone, + created_at::date transaction_date, + count(DISTINCT id) total_orders, + sum(payment_amount) AS revenue + FROM raena_order_management.order + WHERE payment_status='Paid' + AND is_campaign = 'false' + AND cast(is_archived AS varchar)='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2, + 3; + + +DROP TABLE IF EXISTS raena_analytics.total_first_transacting_user; + + +CREATE TABLE raena_analytics.total_first_transacting_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + profile_phone, + count(1) total_event , + Count(DISTINCT A.reseller_id) Total_users, + sum(total_orders) total_orders, + sum(revenue) revenue +FROM raena_analytics.order_base_user_transaction A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 3; + +DROP TABLE IF EXISTS raena_analytics.post_discount_gm; + + +CREATE TABLE raena_analytics.post_discount_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(discounted_price*quantity) discounted_price, + sum(cogs*quantity) cogs, + count(DISTINCT external_id) AS Number_of_orders +FROM raena_analytics.OM_GM_DB_Product_category A +LEFT JOIN raena_analytics.user_type_table B ON replace(A.reseller_mobile,'+','') =B.reseller_mobile +WHERE is_campaign='false' + AND order_placed_by='App' +GROUP BY 1, + 2; + +drop table if exists raena_analytics.Active_user_for_mau_stage; + +CREATE TABLE raena_analytics.Active_user_for_mau_stage AS + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed + union + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + 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; + + + +DROP TABLE IF EXISTS raena_analytics.Active_user_for_mau; + + +CREATE TABLE raena_analytics.Active_user_for_mau AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',(first_install_date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date>=date_trunc('Month',first_install_date)::date + AND (date_trunc('Month',A.transaction_date)::date <= date_trunc('Month',First_transaction_date)::date + OR date_trunc('Month',First_transaction_date)::date IS NULL) THEN 'Activation' + ELSE 'Existing' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM raena_analytics.Active_user_for_mau_stage A +LEFT JOIN + (SELECT profile_objectid profile_objectid, + cast(min(transaction_date) AS DATE) First_transaction_date + FROM + (SELECT profile_objectid, (cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) transaction_date + FROM clevertap.charged) A + GROUP BY 1) B ON A.profile_objectid = B.profile_objectid +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid --LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + +/* +DROP TABLE IF EXISTS raena_analytics.lm_revenue_gm; + + +CREATE TABLE raena_analytics.lm_revenue_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(CASE WHEN a.external_id = BB.order_id + AND a.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount, + sum(a.discounted_price*a.quantity) discounted_price +FROM + (SELECT * + FROM raena_analytics.OM_GM_DB_Product_category + WHERE order_placed_by ='reseller' + AND is_campaign='false') A +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON a.external_id = BB.order_id +AND a.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON a.external_id = CC.external_id +AND a.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + +DROP TABLE IF EXISTS raena_analytics.gm_order_count; + + +CREATE TABLE raena_analytics.gm_order_count AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(Customer_order) Customer_order, + sum(Marketplace_order)Marketplace_order, + sum(total_dropship_order) total_dropship_order, + sum(total_order) total_order +FROM + (SELECT transaction_date, + reseller_mobile, + count(DISTINCT CASE WHEN order_recipient= 'Customer' THEN external_id END) AS Customer_order, + count(DISTINCT CASE WHEN order_recipient= 'Marketplace' THEN external_id END) AS Marketplace_order, + count(DISTINCT CASE WHEN order_recipient IN ('Customer', 'Marketplace') THEN external_id END) total_dropship_order, + count(DISTINCT external_id) total_order + FROM OM_GM_DB_Product_category + WHERE is_campaign='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2) A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; +*/ + +DROP TABLE IF EXISTS raena_analytics.post_discount_dsf_revenue; + + +CREATE TABLE raena_analytics.post_discount_dsf_revenue AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(payment_amount) discounted_price, + count(DISTINCT id) AS Number_of_orders, + count(DISTINCT reseller_id) AS total_users +FROM + (SELECT reseller_id , + external_id id , + discounted_price*quantity payment_amount , + reseller_mobile, + transaction_date + FROM raena_analytics.gm_dashboard + WHERE is_campaign='false' + AND lower(external_id) LIKE '%dsf') A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile =B.reseller_mobile +GROUP BY 1, + 2; + +----OM_Events_user_type;created in db_funnel report + + + +select max(transaction_date) uninstalled_td from raena_analytics.total_uninstalled; +select max(transaction_date) installed_td from raena_analytics.total_installed; +select max(transaction_date) launched_td from raena_analytics.Active_user; +select max(transaction_date) view_td from raena_analytics.total_view_item; +select max(transaction_date) gm_td from raena_analytics.lm_revenue_gm; + + + +DROP TABLE IF EXISTS raena_analytics.overall_conversion_trend; + + +CREATE TABLE raena_analytics.overall_conversion_trend AS +SELECT date_trunc('day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Year'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS raena_analytics.overall_converting_user_trend; + + +CREATE TABLE raena_analytics.overall_converting_user_trend AS +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN(SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Day'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 ; + +DROP TABLE IF EXISTS raena_analytics.revenue_trend; + + +CREATE TABLE raena_analytics.revenue_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + sum(revenue) Total_revenue +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE IF EXISTS raena_analytics.arpu_trend; + + +CREATE TABLE raena_analytics.arpu_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 ; + + +DROP TABLE IF EXISTS raena_analytics.install_uninstall_ratio_trend; + + +CREATE TABLE raena_analytics.install_uninstall_ratio_trend AS +SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_year, + 'Year'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Quarter'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Month'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Week'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Day'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS raena_analytics.mau_trend; + + +CREATE TABLE raena_analytics.mau_trend AS +SELECT date_trunc('Year',transaction_date)::date AS transaction_date, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL + +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Day'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4; + + +" > /home/ec2-user/cronjob/postgresql/product_matrics/product_matrics_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/product_matrics/product_matrics_etl.sql + diff --git a/postgresql/product_matrics/product_matrics_etl.sql b/postgresql/product_matrics/product_matrics_etl.sql new file mode 100644 index 0000000..a9bd4b1 --- /dev/null +++ b/postgresql/product_matrics/product_matrics_etl.sql @@ -0,0 +1,1033 @@ + + +drop table raena_analytics.daily_recommendation_ingredients_split; + + create table raena_analytics.daily_recommendation_ingredients_split + as + select reseller_email ,sku,replace(replace(replace(" Top Marketing Ingredients ",'{',''),'}',''),'"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_ingredients; + +drop table raena_analytics.daily_recommendation_concerns_split; + + create table raena_analytics.daily_recommendation_concerns_split + as + select reseller_email ,sku,replace(replace(replace(" Concern(s) " ,'{',''),'}',''),'"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_concerns drc ; + +DROP TABLE raena_analytics.product_metrics_calendar; + +CREATE TABLE raena_analytics.product_metrics_calendar AS +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type +FROM raena_order_management.order; + + +drop table if exists raena_analytics.order_base_for_user_type; + +create table raena_analytics.order_base_for_user_type +as +SELECT replace(reseller_info::json->>'mobile','+','') reseller_mobile, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' + GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.user_type_table ; + + +CREATE TABLE raena_analytics.user_type_table AS +SELECT DISTINCT profile_phone reseller_mobile, + First_transaction_date, + min_created_date first_install_date +FROM raena_analytics.order_base_for_user_type A +INNER JOIN + (SELECT profile_phone, + min(created_at) min_created_date + FROM + (SELECT profile_phone, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) created_at + FROM clevertap.app_installed + GROUP BY profile_phone + UNION + SELECT replace(mobile,'+','') profile_phone, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1) A + GROUP BY 1) B ON A.reseller_mobile=B.profile_phone; + + + +DROP TABLE IF EXISTS raena_analytics.total_uninstalled; + + +CREATE TABLE raena_analytics.total_uninstalled AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM (SELECT profile_objectid, + CASE + WHEN profile_phone<>'' THEN profile_phone + END profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_uninstalled) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + +DROP TABLE IF EXISTS raena_analytics.total_installed; + + +CREATE TABLE raena_analytics.total_installed AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + DROP TABLE raena_analytics.Active_user; + + +CREATE TABLE raena_analytics.Active_user AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',coalesce(first_install_date,D.created_at::date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND (date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date + OR First_transaction_date IS NULL) THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND first_transaction_date IS NULL THEN 'New' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.order_base_for_user_type B ON A.profile_phone = B.reseller_mobile +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + +DROP TABLE IF EXISTS raena_analytics.total_view_item; + + +CREATE TABLE raena_analytics.total_view_item AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 4; + + +drop table if exists raena_analytics.order_base_user_transaction; + +create table raena_analytics.order_base_user_transaction +as +SELECT reseller_id , + replace(reseller_info::json->>'mobile','+','') profile_phone, + created_at::date transaction_date, + count(DISTINCT id) total_orders, + sum(payment_amount) AS revenue + FROM raena_order_management.order + WHERE payment_status='Paid' + AND is_campaign = 'false' + AND cast(is_archived AS varchar)='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2, + 3; + + +DROP TABLE IF EXISTS raena_analytics.total_first_transacting_user; + + +CREATE TABLE raena_analytics.total_first_transacting_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + profile_phone, + count(1) total_event , + Count(DISTINCT A.reseller_id) Total_users, + sum(total_orders) total_orders, + sum(revenue) revenue +FROM raena_analytics.order_base_user_transaction A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 3; + +DROP TABLE IF EXISTS raena_analytics.post_discount_gm; + + +CREATE TABLE raena_analytics.post_discount_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(discounted_price*quantity) discounted_price, + sum(cogs*quantity) cogs, + count(DISTINCT external_id) AS Number_of_orders +FROM raena_analytics.OM_GM_DB_Product_category A +LEFT JOIN raena_analytics.user_type_table B ON replace(A.reseller_mobile,'+','') =B.reseller_mobile +WHERE is_campaign='false' + AND order_placed_by='App' +GROUP BY 1, + 2; + +drop table if exists raena_analytics.Active_user_for_mau_stage; + +CREATE TABLE raena_analytics.Active_user_for_mau_stage AS + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed + union + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + 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; + + + +DROP TABLE IF EXISTS raena_analytics.Active_user_for_mau; + + +CREATE TABLE raena_analytics.Active_user_for_mau AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',(first_install_date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date>=date_trunc('Month',first_install_date)::date + AND (date_trunc('Month',A.transaction_date)::date <= date_trunc('Month',First_transaction_date)::date + OR date_trunc('Month',First_transaction_date)::date IS NULL) THEN 'Activation' + ELSE 'Existing' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM raena_analytics.Active_user_for_mau_stage A +LEFT JOIN + (SELECT profile_objectid profile_objectid, + cast(min(transaction_date) AS DATE) First_transaction_date + FROM + (SELECT profile_objectid, (cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) transaction_date + FROM clevertap.charged) A + GROUP BY 1) B ON A.profile_objectid = B.profile_objectid +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid --LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + +/* +DROP TABLE IF EXISTS raena_analytics.lm_revenue_gm; + + +CREATE TABLE raena_analytics.lm_revenue_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(CASE WHEN a.external_id = BB.order_id + AND a.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount, + sum(a.discounted_price*a.quantity) discounted_price +FROM + (SELECT * + FROM raena_analytics.OM_GM_DB_Product_category + WHERE order_placed_by ='reseller' + AND is_campaign='false') A +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON a.external_id = BB.order_id +AND a.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON a.external_id = CC.external_id +AND a.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + +DROP TABLE IF EXISTS raena_analytics.gm_order_count; + + +CREATE TABLE raena_analytics.gm_order_count AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(Customer_order) Customer_order, + sum(Marketplace_order)Marketplace_order, + sum(total_dropship_order) total_dropship_order, + sum(total_order) total_order +FROM + (SELECT transaction_date, + reseller_mobile, + count(DISTINCT CASE WHEN order_recipient= 'Customer' THEN external_id END) AS Customer_order, + count(DISTINCT CASE WHEN order_recipient= 'Marketplace' THEN external_id END) AS Marketplace_order, + count(DISTINCT CASE WHEN order_recipient IN ('Customer', 'Marketplace') THEN external_id END) total_dropship_order, + count(DISTINCT external_id) total_order + FROM OM_GM_DB_Product_category + WHERE is_campaign='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2) A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; +*/ + +DROP TABLE IF EXISTS raena_analytics.post_discount_dsf_revenue; + + +CREATE TABLE raena_analytics.post_discount_dsf_revenue AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(payment_amount) discounted_price, + count(DISTINCT id) AS Number_of_orders, + count(DISTINCT reseller_id) AS total_users +FROM + (SELECT reseller_id , + external_id id , + discounted_price*quantity payment_amount , + reseller_mobile, + transaction_date + FROM raena_analytics.gm_dashboard + WHERE is_campaign='false' + AND lower(external_id) LIKE '%dsf') A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile =B.reseller_mobile +GROUP BY 1, + 2; + +----OM_Events_user_type;created in db_funnel report + + + +select max(transaction_date) uninstalled_td from raena_analytics.total_uninstalled; +select max(transaction_date) installed_td from raena_analytics.total_installed; +select max(transaction_date) launched_td from raena_analytics.Active_user; +select max(transaction_date) view_td from raena_analytics.total_view_item; +select max(transaction_date) gm_td from raena_analytics.lm_revenue_gm; + + + +DROP TABLE IF EXISTS raena_analytics.overall_conversion_trend; + + +CREATE TABLE raena_analytics.overall_conversion_trend AS +SELECT date_trunc('day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Year'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS raena_analytics.overall_converting_user_trend; + + +CREATE TABLE raena_analytics.overall_converting_user_trend AS +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN(SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Day'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 ; + +DROP TABLE IF EXISTS raena_analytics.revenue_trend; + + +CREATE TABLE raena_analytics.revenue_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + sum(revenue) Total_revenue +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE IF EXISTS raena_analytics.arpu_trend; + + +CREATE TABLE raena_analytics.arpu_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 ; + + +DROP TABLE IF EXISTS raena_analytics.install_uninstall_ratio_trend; + + +CREATE TABLE raena_analytics.install_uninstall_ratio_trend AS +SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_year, + 'Year'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Quarter'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Month'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Week'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Day'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS raena_analytics.mau_trend; + + +CREATE TABLE raena_analytics.mau_trend AS +SELECT date_trunc('Year',transaction_date)::date AS transaction_date, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL + +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Day'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4; + + + diff --git a/postgresql/retention_code/retention_code_etl.sh b/postgresql/retention_code/retention_code_etl.sh new file mode 100644 index 0000000..b5674e6 --- /dev/null +++ b/postgresql/retention_code/retention_code_etl.sh @@ -0,0 +1,578 @@ +!/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.OM_Reseller_Wise_latest_tier; + + +CREATE TABLE raena_analytics.OM_Reseller_Wise_latest_tier AS +SELECT mobile, + tier from + ( SELECT mobile, + tier, + row_number() over (partition BY mobile ORDER BY created_date DESC) AS R + FROM + ( SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' as varchar),'\"',''),'+','') AS mobile, + replace(cast(o.reseller_info::json ->'tierName' AS varchar),'\"','') AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o) A ) B +WHERE R=1 + AND mobile is NOT NULL; + +DROP TABLE IF EXISTS raena_analytics.Retention_new_filters_install; + +CREATE TABLE raena_analytics.Retention_new_filters_install AS +SELECT DISTINCT TO_CHAR(o.created_at, 'Month') AS MONTH, + cast(date_part('year',o.created_at) AS varchar) AS YEAR, + CASE + WHEN shipping_to='Reseller' THEN 'Reseller' + ELSE 'Dropshipper' + END AS customer_type, + order_placed_by, + CASE + WHEN order_placed_by='admin' THEN 'admin' + ELSE marketplace + END AS Sales_channel, + provider, + c.channel_name AS channel_name, + replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') mobile, + verification_status, + tier +FROM raena_user_management.user u +LEFT JOIN raena_order_management.order o ON replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','')=replace(u.mobile,'+','') +LEFT JOIN raena_analytics.OM_channel c ON cast(c.id AS text)=cast(o.channel_id AS text) +LEFT JOIN + (SELECT mobile, + tier + FROM + (SELECT mobile, + tier, + row_number() over (partition BY mobile + ORDER BY created_date DESC) AS R + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + replace(cast(o.reseller_info::json ->'tierName' AS varchar),'\"','') AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o)A) B + WHERE R=1 + AND mobile IS NOT NULL) t ON u.mobile=t.mobile; + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION; + + + +CREATE TABLE raena_analytics.DAU_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS conversion_year, + TO_CHAR(base_1.created_date, 'Month') AS conversion_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =0 + THEN base_1.mobile + END AS M0, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=1 THEN base_1.mobile + END AS M1, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=2 THEN base_1.mobile + END AS M2, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=3 THEN base_1.mobile + END AS M3, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=4 THEN base_1.mobile + END AS M4, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=5 THEN base_1.mobile + END AS M5, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=6 THEN base_1.mobile + END AS M6, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+',''), 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 o + WHERE payment_status = 'Paid') b + 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 <> '' ) c ) base_3 ON base_1.mobile=base_3.active_user) bucket_final; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_REVENUE_RETENTION; + + +CREATE TABLE raena_analytics.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 (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=0 THEN conversion_base.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=1 THEN conversion_base.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=2 THEN conversion_base.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=3 THEN conversion_base.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=4 THEN conversion_base.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=5 THEN conversion_base.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=6 THEN conversion_base.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)>6 THEN conversion_base.payment_amount + END AS GT_M6 + FROM + (SELECT DISTINCT base.Active_user, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid') A + GROUP BY 1, + 2)B) C) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','')AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.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(DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=0 THEN install_base.Active_user + END AS M0, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=1 THEN install_base.Active_user + END AS M1, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=2 THEN install_base.Active_user + END AS M2, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=3 THEN install_base.Active_user + END AS M3, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=4 THEN install_base.Active_user + END AS M4, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=5 THEN install_base.Active_user + END AS M5, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=6 THEN install_base.Active_user + END AS M6, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)>6 THEN install_base.Active_user + END AS GT_M6 + FROM + (SELECT DISTINCT base.Active_user, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','')AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid')A + GROUP BY 1, + 2)B)C) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT; + + +CREATE TABLE raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT AS +SELECT * +FROM + (SELECT install_base.*, + bucket.bucket + FROM + (SELECT DISTINCT a.Active_user, + date_part('YEAR',a.created_date) AS acquisition_year, + TO_CHAR(a.created_date, 'Month') AS acquisition_month, + date_part('MONTH',a.created_date) AS month_sort, + a.created_date, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=0 THEN A.Active_user END AS M0, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=1 THEN A.Active_user END AS M1, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=2 THEN A.Active_user END AS M2, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=3 THEN A.Active_user END AS M3, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=4 THEN A.Active_user END AS M4, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=5 THEN A.Active_user END AS M5, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=6 THEN A.Active_user END AS M6, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',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') 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') 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1) A ) 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 raena_analytics.OMREVENUE_RETENTION; + + +CREATE TABLE raena_analytics.OMREVENUE_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+',''), 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 o + WHERE payment_status = 'Paid') A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + +DROP TABLE IF EXISTS raena_analytics.RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.RESELLER_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =0 THEN base_1.mobile + END AS M0, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =1 THEN base_1.mobile + END AS M1, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =2 THEN base_1.mobile + END AS M2, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =3 THEN base_1.mobile + END AS M3, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =4 THEN base_1.mobile + END AS M4, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =5 THEN base_1.mobile + END AS M5, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =6 THEN base_1.mobile + END AS M6, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+',''), 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 o + WHERE payment_status = 'Paid')A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + +" > /home/ec2-user/cronjob/postgresql/retention_code/retention_code_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/retention_code/retention_code_etl.sql diff --git a/postgresql/retention_code/retention_code_etl.sql b/postgresql/retention_code/retention_code_etl.sql new file mode 100644 index 0000000..3025aa7 --- /dev/null +++ b/postgresql/retention_code/retention_code_etl.sql @@ -0,0 +1,563 @@ + + +DROP TABLE IF EXISTS raena_analytics.OM_Reseller_Wise_latest_tier; + + +CREATE TABLE raena_analytics.OM_Reseller_Wise_latest_tier AS +SELECT mobile, + tier from + ( SELECT mobile, + tier, + row_number() over (partition BY mobile ORDER BY created_date DESC) AS R + FROM + ( SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' as varchar),'"',''),'+','') AS mobile, + replace(cast(o.reseller_info::json ->'tierName' AS varchar),'"','') AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o) A ) B +WHERE R=1 + AND mobile is NOT NULL; + +DROP TABLE IF EXISTS raena_analytics.Retention_new_filters_install; + +CREATE TABLE raena_analytics.Retention_new_filters_install AS +SELECT DISTINCT TO_CHAR(o.created_at, 'Month') AS MONTH, + cast(date_part('year',o.created_at) AS varchar) AS YEAR, + CASE + WHEN shipping_to='Reseller' THEN 'Reseller' + ELSE 'Dropshipper' + END AS customer_type, + order_placed_by, + CASE + WHEN order_placed_by='admin' THEN 'admin' + ELSE marketplace + END AS Sales_channel, + provider, + c.channel_name AS channel_name, + replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') mobile, + verification_status, + tier +FROM raena_user_management.user u +LEFT JOIN raena_order_management.order o ON replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','')=replace(u.mobile,'+','') +LEFT JOIN raena_analytics.OM_channel c ON cast(c.id AS text)=cast(o.channel_id AS text) +LEFT JOIN + (SELECT mobile, + tier + FROM + (SELECT mobile, + tier, + row_number() over (partition BY mobile + ORDER BY created_date DESC) AS R + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + replace(cast(o.reseller_info::json ->'tierName' AS varchar),'"','') AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o)A) B + WHERE R=1 + AND mobile IS NOT NULL) t ON u.mobile=t.mobile; + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION; + + + +CREATE TABLE raena_analytics.DAU_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS conversion_year, + TO_CHAR(base_1.created_date, 'Month') AS conversion_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =0 + THEN base_1.mobile + END AS M0, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=1 THEN base_1.mobile + END AS M1, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=2 THEN base_1.mobile + END AS M2, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=3 THEN base_1.mobile + END AS M3, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=4 THEN base_1.mobile + END AS M4, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=5 THEN base_1.mobile + END AS M5, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',base_3.created_date)=6 THEN base_1.mobile + END AS M6, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_3.created_date)) * 12 +date_part('Month',base_1.created_date)-date_part('Month',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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+',''), 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 o + WHERE payment_status = 'Paid') b + 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 <> '' ) c ) base_3 ON base_1.mobile=base_3.active_user) bucket_final; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_REVENUE_RETENTION; + + +CREATE TABLE raena_analytics.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 (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=0 THEN conversion_base.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=1 THEN conversion_base.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=2 THEN conversion_base.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=3 THEN conversion_base.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=4 THEN conversion_base.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=5 THEN conversion_base.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=6 THEN conversion_base.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)>6 THEN conversion_base.payment_amount + END AS GT_M6 + FROM + (SELECT DISTINCT base.Active_user, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid') A + GROUP BY 1, + 2)B) C) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','')AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.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(DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=0 THEN install_base.Active_user + END AS M0, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=1 THEN install_base.Active_user + END AS M1, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=2 THEN install_base.Active_user + END AS M2, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=3 THEN install_base.Active_user + END AS M3, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=4 THEN install_base.Active_user + END AS M4, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=5 THEN install_base.Active_user + END AS M5, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)=6 THEN install_base.Active_user + END AS M6, + CASE + WHEN (DATE_PART('year', install_base.created_date) - DATE_PART('year', conversion_base.created_date)) * 12 + + date_part('Month',install_base.created_date)-date_part('Month',conversion_base.created_date)>6 THEN install_base.Active_user + END AS GT_M6 + FROM + (SELECT DISTINCT base.Active_user, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','')AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid')A + GROUP BY 1, + 2)B)C) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT; + + +CREATE TABLE raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT AS +SELECT * +FROM + (SELECT install_base.*, + bucket.bucket + FROM + (SELECT DISTINCT a.Active_user, + date_part('YEAR',a.created_date) AS acquisition_year, + TO_CHAR(a.created_date, 'Month') AS acquisition_month, + date_part('MONTH',a.created_date) AS month_sort, + a.created_date, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=0 THEN A.Active_user END AS M0, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=1 THEN A.Active_user END AS M1, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=2 THEN A.Active_user END AS M2, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=3 THEN A.Active_user END AS M3, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=4 THEN A.Active_user END AS M4, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=5 THEN A.Active_user END AS M5, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',B.created_date)=6 THEN A.Active_user END AS M6, + CASE WHEN (DATE_PART('year', A.created_date) - DATE_PART('year', B.created_date)) * 12 + + date_part('Month',A.created_date)-date_part('Month',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') 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') 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1) A ) 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 raena_analytics.OMREVENUE_RETENTION; + + +CREATE TABLE raena_analytics.OMREVENUE_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+',''), 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 o + WHERE payment_status = 'Paid') A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + +DROP TABLE IF EXISTS raena_analytics.RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.RESELLER_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =0 THEN base_1.mobile + END AS M0, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =1 THEN base_1.mobile + END AS M1, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =2 THEN base_1.mobile + END AS M2, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =3 THEN base_1.mobile + END AS M3, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =4 THEN base_1.mobile + END AS M4, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =5 THEN base_1.mobile + END AS M5, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',base_2.created_date) =6 THEN base_1.mobile + END AS M6, + CASE + WHEN (DATE_PART('year', base_1.created_date) - DATE_PART('year', base_2.created_date)) * 12 + + date_part('Month',base_1.created_date)-date_part('Month',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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'"',''),'+',''), 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 o + WHERE payment_status = 'Paid')A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + + diff --git a/postgresql/retention_code/retention_code_etl_new.sh b/postgresql/retention_code/retention_code_etl_new.sh new file mode 100644 index 0000000..43af9bf --- /dev/null +++ b/postgresql/retention_code/retention_code_etl_new.sh @@ -0,0 +1,587 @@ +!/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.OM_Reseller_Wise_latest_tier; + + +CREATE TABLE raena_analytics.OM_Reseller_Wise_latest_tier AS +SELECT mobile, + tier from + ( SELECT mobile, + tier, + row_number() over (partition BY mobile ORDER BY created_date DESC) AS R + FROM + ( SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' as varchar),'\"',''),'+','') AS mobile, + replace(cast(o.reseller_info::json ->'tierName' AS varchar),'\"','') AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o) A ) B +WHERE R=1 + AND mobile is NOT NULL; + +DROP TABLE IF EXISTS raena_analytics.Retention_new_filters_install; + +CREATE TABLE raena_analytics.Retention_new_filters_install AS +SELECT DISTINCT TO_CHAR(o.created_at, 'Month') AS MONTH, + cast(date_part('year',o.created_at) AS varchar) AS YEAR, + CASE + WHEN shipping_to='Reseller' THEN 'Reseller' + ELSE 'Dropshipper' + END AS customer_type, + order_placed_by, + CASE + WHEN order_placed_by='admin' THEN 'admin' + ELSE marketplace + END AS Sales_channel, + provider, + c.channel_name AS channel_name, + replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') mobile, + verification_status, + tier +FROM raena_user_management.user u +LEFT JOIN raena_order_management.order o ON replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','')=replace(u.mobile,'+','') +LEFT JOIN raena_analytics.OM_channel c ON cast(c.id AS text)=cast(o.channel_id AS text) +LEFT JOIN + (SELECT mobile, + tier + FROM + (SELECT mobile, + tier, + row_number() over (partition BY mobile + ORDER BY created_date DESC) AS R + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + replace(cast(o.reseller_info::json ->'tierName' AS varchar),'\"','') AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o)A) B + WHERE R=1 + AND mobile IS NOT NULL) t ON u.mobile=t.mobile; + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION; + + + +CREATE TABLE raena_analytics.DAU_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS conversion_year, + TO_CHAR(base_1.created_date, 'Month') AS conversion_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 + THEN base_1.mobile + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =1 THEN base_1.mobile + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =2 THEN base_1.mobile + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =3 THEN base_1.mobile + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =4 THEN base_1.mobile + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =5 THEN base_1.mobile + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =6 THEN base_1.mobile + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) >6 THEN base_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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+',''), 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 o + WHERE payment_status = 'Paid') b + 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 <> '' ) c ) base_3 ON base_1.mobile=base_3.active_user) bucket_final; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_REVENUE_RETENTION; + + +CREATE TABLE raena_analytics.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 (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=0 THEN conversion_base.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=1 THEN conversion_base.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=2 THEN conversion_base.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=3 THEN conversion_base.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=4 THEN conversion_base.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=5 THEN conversion_base.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=6 THEN conversion_base.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)>6 THEN conversion_base.payment_amount + END AS GT_M6 + FROM + (SELECT DISTINCT base.Active_user, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid') A + GROUP BY 1, + 2)B) C) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','')AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.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 + (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=0 THEN install_base.Active_user + END AS M0, + CASE when + (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=1 THEN install_base.Active_user + END AS M1, + CASE when + (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=2 THEN install_base.Active_user + END AS M2, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=3 THEN install_base.Active_user + END AS M3, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=4 THEN install_base.Active_user + END AS M4, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=5 THEN install_base.Active_user + END AS M5, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)=6 THEN install_base.Active_user + END AS M6, + CASE + WHEN (DATE_PART('year', conversion_base.created_date) - DATE_PART('year', install_base.created_date)) * 12 + + date_part('Month',conversion_base.created_date)-date_part('Month',install_base.created_date)>6 THEN install_base.Active_user + END AS GT_M6 + FROM + (SELECT DISTINCT base.Active_user, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','')AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid')A + GROUP BY 1, + 2)B)C) 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT; + + +CREATE TABLE raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT AS +SELECT * +FROM + (SELECT install_base.*, + bucket.bucket + FROM + (SELECT DISTINCT a.Active_user, + date_part('YEAR',a.created_date) AS acquisition_year, + TO_CHAR(a.created_date, 'Month') AS acquisition_month, + date_part('MONTH',a.created_date) AS month_sort, + a.created_date, + CASE WHEN (DATE_PART('year', B.created_date) - DATE_PART('year', A.created_date)) * 12 + + date_part('Month',B.created_date)-date_part('Month',A.created_date)=0 THEN A.Active_user END AS M0, + CASE WHEN (DATE_PART('year', B.created_date) - DATE_PART('year', A.created_date)) * 12 + + date_part('Month',B.created_date)-date_part('Month',A.created_date)=1 THEN A.Active_user END AS M1, + CASE WHEN (DATE_PART('year', B.created_date) - DATE_PART('year', A.created_date)) * 12 + + date_part('Month',B.created_date)-date_part('Month',A.created_date)=2 THEN A.Active_user END AS M2, + CASE WHEN (DATE_PART('year', B.created_date) - DATE_PART('year', A.created_date)) * 12 + + date_part('Month',B.created_date)-date_part('Month',A.created_date)=3 THEN A.Active_user END AS M3, + CASE WHEN (DATE_PART('year', B.created_date) - DATE_PART('year', A.created_date)) * 12 + + date_part('Month',B.created_date)-date_part('Month',A.created_date)=4 THEN A.Active_user END AS M4, + CASE WHEN (DATE_PART('year', B.created_date) - DATE_PART('year', A.created_date)) * 12 + + date_part('Month',B.created_date)-date_part('Month',A.created_date)=5 THEN A.Active_user END AS M5, + CASE WHEN (DATE_PART('year', B.created_date) - DATE_PART('year', A.created_date)) * 12 + + date_part('Month',B.created_date)-date_part('Month',A.created_date)=6 THEN A.Active_user END AS M6, + CASE WHEN (DATE_PART('year', B.created_date) - DATE_PART('year', A.created_date)) * 12 + + date_part('Month',B.created_date)-date_part('Month',A.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') 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') 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1) A ) 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 raena_analytics.OMREVENUE_RETENTION; + + +CREATE TABLE raena_analytics.OMREVENUE_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT *, + 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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+',''), 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 o + WHERE payment_status = 'Paid') A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + +DROP TABLE IF EXISTS raena_analytics.RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.RESELLER_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('MONTH',base_1.created_date) AS month_sort, + base_1.created_date, + base_1.bucket, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_1.mobile + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =1 THEN base_1.mobile + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =2 THEN base_1.mobile + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =3 THEN base_1.mobile + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =4 THEN base_1.mobile + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =5 THEN base_1.mobile + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =6 THEN base_1.mobile + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) >6 THEN base_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(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid') a + WHERE a.created_rnk=1) base_1 + INNER JOIN + (SELECT mobile, + date_trunc('month',created_date) AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount, + row_number() over(partition BY replace(replace(cast(o.reseller_info::json->'mobile' AS varchar),'\"',''),'+',''), 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 o + WHERE payment_status = 'Paid')A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + +" > /home/ec2-user/cronjob/postgresql/retention_code/retention_code_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/retention_code/retention_code_etl.sql + diff --git a/postgresql/revenue_leakage/revenue_leakage_etl.sh b/postgresql/revenue_leakage/revenue_leakage_etl.sh new file mode 100644 index 0000000..3f10eda --- /dev/null +++ b/postgresql/revenue_leakage/revenue_leakage_etl.sh @@ -0,0 +1,639 @@ + #!/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.revenue_leakage_new_stage1; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage1 AS +SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at, + payment_amount, + payment_status_sort, + quantity +FROM + (SELECT *, + row_number() over (partition BY reseller_id,sku,payment_status,cast(created_at AS date) + ORDER BY created_at,payment_status_sort,payment_amount DESC) AS R + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.payment_price AS payment_amount, + CASE WHEN payment_status='Paid' THEN 1 WHEN payment_status='Expired' THEN 2 ELSE 3 END AS payment_status_sort, + oi.quantity + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status IN ('Expired', + 'Failed') + AND o.status='Order_Placed' + AND cast(o.created_at AS date) >='2022-01-01') inn) m +WHERE R=1 ; + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage2; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage2 AS +SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.quantity, + oi.payment_price AS payment_amount +FROM + (SELECT id, + order_placed_by , + reseller_id, + customer_id, + payment_status, + created_at + FROM RAENA_ORDER_MANAGEMENT.order + WHERE payment_status='Paid' + AND cast(created_at AS date) >='2022-01-01') o +INNER JOIN + (SELECT order_id, + sku, + quantity, + payment_price + FROM RAENA_ORDER_MANAGEMENT.order_item + WHERE cast(created_at AS date) >='2022-01-01') oi ON o.id=oi.order_id; + + +DROP TABLE IF EXISTS raena_analytics.om_revenue_leakage_qty_match; + + +CREATE TABLE raena_analytics.om_revenue_leakage_qty_match AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at AS date) AS concat1 +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_amount AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + LEFT JOIN raena_analytics.revenue_leakage_new_stage2 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND a.quantity=b.quantity + AND a.customer_id IS NULL + AND b.customer_id IS NULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS' + WHERE a.customer_id IS NULL) c +WHERE b_reseller_id IS NULL +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount) AS payment_amount, + a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at AS date) +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_amount AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + LEFT JOIN raena_analytics.revenue_leakage_new_stage2 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND a.quantity=b.quantity + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS' + WHERE a.customer_id IS NOT NULL) c +WHERE b_reseller_id IS NULL; + + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage3; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage3 AS +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price +FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >='2022-01-01' + AND customer_id IS NOT NULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist +INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >='2022-01-01' + AND customer_id IS NOT NULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by +AND ist.reseller_id=nxt.reseller_id +AND ist.customer_id=nxt.customer_id +AND ist.sku=nxt.sku +AND ist.created_at=cast(nxt.created_at AS date); + + +DROP TABLE IF EXISTS public.om_revenue_leakage_qty_not_match; + + +CREATE TABLE public.om_revenue_leakage_qty_not_match AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount)-(b_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at AS date) AS concat2 +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + INNER JOIN raena_analytics.revenue_leakage_new_stage3 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_amount>b.payment_price + AND a.customer_id ISNULL + AND b.customer_id ISNULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount)-(b_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at AS date) +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + INNER JOIN raena_analytics.revenue_leakage_new_stage3 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_amount>b.payment_price + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +ORDER BY 2, + 4; + + + +drop table if exists raena_analytics.revenue_leakage_new_stage4; + +create table raena_analytics.revenue_leakage_new_stage4 +as +SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at, + payment_price, + payment_status_sort, + quantity + FROM + (SELECT *, + row_number() over (partition BY reseller_id,sku,payment_status,cast(created_at AS date) + ORDER BY created_at,payment_status_sort,payment_price DESC) AS R + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.payment_price, + CASE WHEN payment_status='Paid' THEN 1 WHEN payment_status='Expired' THEN 2 ELSE 3 END AS payment_status_sort, + oi.quantity + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status!='Paid' + AND payment_status!='Pending' + AND o.status='Order_Placed' + AND cast(o.created_at AS date) >= '2022-01-01') inn) m + WHERE R=1; + +drop table if exists raena_analytics.revenue_leakage_new_stage5; + +create table raena_analytics.revenue_leakage_new_stage5 +as +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id ISNULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist + INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id ISNULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by + AND ist.reseller_id=nxt.reseller_id + AND ist.sku=nxt.sku + AND ist.created_at=cast(nxt.created_at AS date); + + +drop table raena_analytics.revenue_leakage_new_stage6; + +create table raena_analytics.revenue_leakage_new_stage6 +as +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id NOTNULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist + INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id NOTNULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by + AND ist.reseller_id=nxt.reseller_id + AND ist.customer_id=nxt.customer_id + AND ist.sku=nxt.sku + AND ist.created_at=cast(nxt.created_at AS date); + + +DROP TABLE IF EXISTS raena_analytics.OM_Revenue_leakage; + + +CREATE TABLE raena_analytics.OM_Revenue_leakage AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(revenue_leakage) AS revenue_leakage +FROM + (SELECT * + FROM + (SELECT qm.*, + qnm.concat2 + FROM raena_analytics.om_revenue_leakage_qty_match qm + LEFT JOIN raena_analytics.om_revenue_leakage_qty_not_match qnm ON qm.concat1=qnm.concat2) n + WHERE concat2 ISNULL) s +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(a_payment_amount)-sum(b_payment_amount) AS revenue_leakage +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_price AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + from raena_analytics.revenue_leakage_new_stage4 a + INNER join raena_analytics.revenue_leakage_new_stage5 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_price>b.payment_price + AND a.customer_id ISNULL + AND b.customer_id ISNULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(a_payment_amount)-sum(b_payment_amount) AS revenue_leakage +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_price AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + from raena_analytics.revenue_leakage_new_stage4 a + INNER JOIN raena_analytics.revenue_leakage_new_stage6 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_price>b.payment_price + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +GROUP BY 1, + 2, + 3, + 4, + 5 +ORDER BY 2, + 4; + + DROP TABLE IF EXISTS raena_analytics.OM_Revenue_leakage_dump; + + +CREATE TABLE raena_analytics.OM_Revenue_leakage_dump AS +SELECT * +FROM raena_analytics.om_revenue_leakage_qty_match +UNION +SELECT * +FROM raena_analytics.om_revenue_leakage_qty_not_match; + +DROP TABLE IF EXISTS raena_analytics.om_conversion_order_data_1; + + +CREATE TABLE raena_analytics.om_conversion_order_data_1 AS +SELECT DISTINCT cast(created_at + interval '7 Hours' AS date) AS created_date, + date_part('year',created_at + interval '7 Hours') AS order_year, + date_part('month',created_at + interval '7 Hours') AS order_month, + To_char(created_at + interval '7 Hours','month') Month_name, + a.reseller_id, + id AS order_id, + reseller_info::json->>'email' AS transacted_email, + replace(reseller_info::json->>'mobile','+','') 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 + interval '7 Hours'),date_part('month',created_at + interval '7 Hours')) AS R +FROM raena_order_management.ORDER a +LEFT 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 + interval '7 Hours') AS order_year, + date_part('month',created_at + interval '7 Hours') 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 + interval '7 Hours'), + date_part('month',created_at + interval '7 Hours'), + reseller_id ) AA + WHERE reseller_id is not NULL) b ON a.reseller_id=b.reseller_id +AND date_part('year',a.created_at + interval '7 Hours') =b.order_year +AND date_part('month',a.created_at + interval '7 Hours')=b.order_month +LEFT 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 ) BB + WHERE reseller_id is not NULL) c ON a.reseller_id=c.reseller_id +WHERE payment_status='Paid' + AND cast(is_archived AS varchar)='false' + AND cast(is_campaign AS varchar)='false' +ORDER BY 1, + 3; + + +" > /home/ec2-user/cronjob/postgresql/revenue_leakage/revenue_leakage_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/revenue_leakage/revenue_leakage_etl.sql + diff --git a/postgresql/revenue_leakage/revenue_leakage_etl.sql b/postgresql/revenue_leakage/revenue_leakage_etl.sql new file mode 100644 index 0000000..0d0cf65 --- /dev/null +++ b/postgresql/revenue_leakage/revenue_leakage_etl.sql @@ -0,0 +1,621 @@ + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage1; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage1 AS +SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at, + payment_amount, + payment_status_sort, + quantity +FROM + (SELECT *, + row_number() over (partition BY reseller_id,sku,payment_status,cast(created_at AS date) + ORDER BY created_at,payment_status_sort,payment_amount DESC) AS R + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.payment_price AS payment_amount, + CASE WHEN payment_status='Paid' THEN 1 WHEN payment_status='Expired' THEN 2 ELSE 3 END AS payment_status_sort, + oi.quantity + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status IN ('Expired', + 'Failed') + AND o.status='Order_Placed' + AND cast(o.created_at AS date) >='2022-01-01') inn) m +WHERE R=1 ; + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage2; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage2 AS +SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.quantity, + oi.payment_price AS payment_amount +FROM + (SELECT id, + order_placed_by , + reseller_id, + customer_id, + payment_status, + created_at + FROM RAENA_ORDER_MANAGEMENT.order + WHERE payment_status='Paid' + AND cast(created_at AS date) >='2022-01-01') o +INNER JOIN + (SELECT order_id, + sku, + quantity, + payment_price + FROM RAENA_ORDER_MANAGEMENT.order_item + WHERE cast(created_at AS date) >='2022-01-01') oi ON o.id=oi.order_id; + + +DROP TABLE IF EXISTS raena_analytics.om_revenue_leakage_qty_match; + + +CREATE TABLE raena_analytics.om_revenue_leakage_qty_match AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at AS date) AS concat1 +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_amount AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + LEFT JOIN raena_analytics.revenue_leakage_new_stage2 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND a.quantity=b.quantity + AND a.customer_id IS NULL + AND b.customer_id IS NULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS' + WHERE a.customer_id IS NULL) c +WHERE b_reseller_id IS NULL +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount) AS payment_amount, + a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at AS date) +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_amount AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + LEFT JOIN raena_analytics.revenue_leakage_new_stage2 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND a.quantity=b.quantity + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS' + WHERE a.customer_id IS NOT NULL) c +WHERE b_reseller_id IS NULL; + + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage3; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage3 AS +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price +FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >='2022-01-01' + AND customer_id IS NOT NULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist +INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >='2022-01-01' + AND customer_id IS NOT NULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by +AND ist.reseller_id=nxt.reseller_id +AND ist.customer_id=nxt.customer_id +AND ist.sku=nxt.sku +AND ist.created_at=cast(nxt.created_at AS date); + + +DROP TABLE IF EXISTS public.om_revenue_leakage_qty_not_match; + + +CREATE TABLE public.om_revenue_leakage_qty_not_match AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount)-(b_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at AS date) AS concat2 +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + INNER JOIN raena_analytics.revenue_leakage_new_stage3 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_amount>b.payment_price + AND a.customer_id ISNULL + AND b.customer_id ISNULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount)-(b_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at AS date) +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + INNER JOIN raena_analytics.revenue_leakage_new_stage3 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_amount>b.payment_price + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +ORDER BY 2, + 4; + + + +drop table if exists raena_analytics.revenue_leakage_new_stage4; + +create table raena_analytics.revenue_leakage_new_stage4 +as +SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at, + payment_price, + payment_status_sort, + quantity + FROM + (SELECT *, + row_number() over (partition BY reseller_id,sku,payment_status,cast(created_at AS date) + ORDER BY created_at,payment_status_sort,payment_price DESC) AS R + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.payment_price, + CASE WHEN payment_status='Paid' THEN 1 WHEN payment_status='Expired' THEN 2 ELSE 3 END AS payment_status_sort, + oi.quantity + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status!='Paid' + AND payment_status!='Pending' + AND o.status='Order_Placed' + AND cast(o.created_at AS date) >= '2022-01-01') inn) m + WHERE R=1; + +drop table if exists raena_analytics.revenue_leakage_new_stage5; + +create table raena_analytics.revenue_leakage_new_stage5 +as +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id ISNULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist + INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id ISNULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by + AND ist.reseller_id=nxt.reseller_id + AND ist.sku=nxt.sku + AND ist.created_at=cast(nxt.created_at AS date); + + +drop table raena_analytics.revenue_leakage_new_stage6; + +create table raena_analytics.revenue_leakage_new_stage6 +as +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id NOTNULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist + INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id NOTNULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by + AND ist.reseller_id=nxt.reseller_id + AND ist.customer_id=nxt.customer_id + AND ist.sku=nxt.sku + AND ist.created_at=cast(nxt.created_at AS date); + + +DROP TABLE IF EXISTS raena_analytics.OM_Revenue_leakage; + + +CREATE TABLE raena_analytics.OM_Revenue_leakage AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(revenue_leakage) AS revenue_leakage +FROM + (SELECT * + FROM + (SELECT qm.*, + qnm.concat2 + FROM raena_analytics.om_revenue_leakage_qty_match qm + LEFT JOIN raena_analytics.om_revenue_leakage_qty_not_match qnm ON qm.concat1=qnm.concat2) n + WHERE concat2 ISNULL) s +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(a_payment_amount)-sum(b_payment_amount) AS revenue_leakage +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_price AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + from raena_analytics.revenue_leakage_new_stage4 a + INNER join raena_analytics.revenue_leakage_new_stage5 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_price>b.payment_price + AND a.customer_id ISNULL + AND b.customer_id ISNULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(a_payment_amount)-sum(b_payment_amount) AS revenue_leakage +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_price AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + from raena_analytics.revenue_leakage_new_stage4 a + INNER JOIN raena_analytics.revenue_leakage_new_stage6 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_price>b.payment_price + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +GROUP BY 1, + 2, + 3, + 4, + 5 +ORDER BY 2, + 4; + + DROP TABLE IF EXISTS raena_analytics.OM_Revenue_leakage_dump; + + +CREATE TABLE raena_analytics.OM_Revenue_leakage_dump AS +SELECT * +FROM raena_analytics.om_revenue_leakage_qty_match +UNION +SELECT * +FROM raena_analytics.om_revenue_leakage_qty_not_match; + +DROP TABLE IF EXISTS raena_analytics.om_conversion_order_data_1; + + +CREATE TABLE raena_analytics.om_conversion_order_data_1 AS +SELECT DISTINCT cast(created_at + interval '7 Hours' AS date) AS created_date, + date_part('year',created_at + interval '7 Hours') AS order_year, + date_part('month',created_at + interval '7 Hours') AS order_month, + To_char(created_at + interval '7 Hours','month') Month_name, + a.reseller_id, + id AS order_id, + reseller_info::json->>'email' AS transacted_email, + replace(reseller_info::json->>'mobile','+','') 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 + interval '7 Hours'),date_part('month',created_at + interval '7 Hours')) AS R +FROM raena_order_management.ORDER a +LEFT 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 + interval '7 Hours') AS order_year, + date_part('month',created_at + interval '7 Hours') 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 + interval '7 Hours'), + date_part('month',created_at + interval '7 Hours'), + reseller_id ) AA + WHERE reseller_id is not NULL) b ON a.reseller_id=b.reseller_id +AND date_part('year',a.created_at + interval '7 Hours') =b.order_year +AND date_part('month',a.created_at + interval '7 Hours')=b.order_month +LEFT 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 ) BB + WHERE reseller_id is not NULL) c ON a.reseller_id=c.reseller_id +WHERE payment_status='Paid' + AND cast(is_archived AS varchar)='false' + AND cast(is_campaign AS varchar)='false' +ORDER BY 1, + 3; + + + diff --git a/postgresql/sold_product/sold_product.sh b/postgresql/sold_product/sold_product.sh new file mode 100644 index 0000000..eb1bfbb --- /dev/null +++ b/postgresql/sold_product/sold_product.sh @@ -0,0 +1,123 @@ + #!/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.monthly_sold_product; + +create table raena_analytics.monthly_sold_product +as +select brand_name , brand_page_url , + sum(product_sold) product_sold +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id +where product_sold is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url +order by sum(product_sold) desc; + + +drop table if exists raena_analytics.avg_sold_product; + +create table raena_analytics.avg_sold_product +as +select distinct brand_name , brand_page_url , + avg(ratings) avg_ratings +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id +where ratings is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url +order by avg(ratings) desc; + +drop table if exists raena_analytics.total_sold_product; + +create table raena_analytics.total_sold_product +as +select brand_name , brand_page_url , + rceb.rce_source_id, + sum(product_sold_total) product_sold_total, + sum(product_sold) product_sold, + avg(ratings) avg_ratings, + count(distinct rcers.id) number_of_reseller_store_selling, + CASE + WHEN LOWER(brand_name) = LOWER(b.name) then true + ELSE false + END ss_present_in_Raena, + b.name raena_brand_name, + number_of_products_in_raena +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id and rceb.rce_source_id= rcep.rce_source_id +left join +( + select A.name , count(distinct B.sku) number_of_products_in_raena + from raena_catalog_management.brand A + left join raena_catalog_management.product B on A.id = B.brand_id + group by 1 +) b on LOWER(b.name) = LOWER(rceb.brand_name) +left join +( + select * + from raena_spider_management.rce_reseller_store +) rcers on rcers.id = rcep.rce_store_id +where product_sold_total is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url,rceb.rce_source_id,CASE + WHEN LOWER(brand_name) = LOWER(b.name) then true + ELSE false + end,b.name,number_of_products_in_raena +order by sum(product_sold_total) desc; + +drop table if exists raena_analytics.inventory_snapshot_dashboard_table ; + +create table raena_analytics.inventory_snapshot_dashboard_table +as +SELECT snp_date, sku_code,case WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' end warehouse , min(inventory) inventory_remaining +FROM raena_analytics.inventory_snapshot +GROUP BY 1,2,3 +ORDER BY 1,2,3; + + +" > /home/ec2-user/cronjob/postgresql/sold_product/sold_product.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/sold_product/sold_product.sql + + diff --git a/postgresql/sold_product/sold_product.sql b/postgresql/sold_product/sold_product.sql new file mode 100644 index 0000000..90f020c --- /dev/null +++ b/postgresql/sold_product/sold_product.sql @@ -0,0 +1,104 @@ + + +drop table if exists raena_analytics.monthly_sold_product; + +create table raena_analytics.monthly_sold_product +as +select brand_name , brand_page_url , + sum(product_sold) product_sold +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id +where product_sold is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url +order by sum(product_sold) desc; + + +drop table if exists raena_analytics.avg_sold_product; + +create table raena_analytics.avg_sold_product +as +select distinct brand_name , brand_page_url , + avg(ratings) avg_ratings +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id +where ratings is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url +order by avg(ratings) desc; + +drop table if exists raena_analytics.total_sold_product; + +create table raena_analytics.total_sold_product +as +select brand_name , brand_page_url , + rceb.rce_source_id, + sum(product_sold_total) product_sold_total, + sum(product_sold) product_sold, + avg(ratings) avg_ratings, + count(distinct rcers.id) number_of_reseller_store_selling, + CASE + WHEN LOWER(brand_name) = LOWER(b.name) then true + ELSE false + END ss_present_in_Raena, + b.name raena_brand_name, + number_of_products_in_raena +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id and rceb.rce_source_id= rcep.rce_source_id +left join +( + select A.name , count(distinct B.sku) number_of_products_in_raena + from raena_catalog_management.brand A + left join raena_catalog_management.product B on A.id = B.brand_id + group by 1 +) b on LOWER(b.name) = LOWER(rceb.brand_name) +left join +( + select * + from raena_spider_management.rce_reseller_store +) rcers on rcers.id = rcep.rce_store_id +where product_sold_total is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url,rceb.rce_source_id,CASE + WHEN LOWER(brand_name) = LOWER(b.name) then true + ELSE false + end,b.name,number_of_products_in_raena +order by sum(product_sold_total) desc; + +drop table if exists raena_analytics.inventory_snapshot_dashboard_table ; + +create table raena_analytics.inventory_snapshot_dashboard_table +as +SELECT snp_date, sku_code,case WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' end warehouse , min(inventory) inventory_remaining +FROM raena_analytics.inventory_snapshot +GROUP BY 1,2,3 +ORDER BY 1,2,3; + + + diff --git a/product_metrics/product_metrics.sh b/product_metrics/product_metrics.sh new file mode 100644 index 0000000..97905ab --- /dev/null +++ b/product_metrics/product_metrics.sh @@ -0,0 +1,1061 @@ + #!/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.daily_recommendation_ingredients_split; + + create table raena_analytics.daily_recommendation_ingredients_split + as + select reseller_email ,sku,replace(replace(replace(\" Top Marketing Ingredients \",'{',''),'}',''),'\"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_ingredients; + +drop table raena_analytics.daily_recommendation_concerns_split; + + create table raena_analytics.daily_recommendation_concerns_split + as + select reseller_email ,sku,replace(replace(replace(\" Concern(s) \" ,'{',''),'}',''),'\"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_concerns drc ; + + + +DROP TABLE public.product_metrics_calendar; + +CREATE TABLE public.product_metrics_calendar AS +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type +FROM raena_order_management.order; + + +DROP TABLE IF EXISTS public.user_type_table ; + + +CREATE TABLE public.user_type_table AS +SELECT DISTINCT profile_phone reseller_mobile, + First_transaction_date, + min_created_date first_install_date +FROM + (SELECT replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') reseller_mobile, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' + GROUP BY 1) A +INNER JOIN + (SELECT profile_phone, + min(created_at) min_created_date + FROM + (SELECT profile_phone, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) created_at + FROM clevertap.app_installed + GROUP BY profile_phone + UNION SELECT replace(mobile,'+','') profile_phone, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1) A + GROUP BY 1) B ON A.reseller_mobile=B.profile_phone; + + + +DROP TABLE IF EXISTS public.total_uninstalled; + + +CREATE TABLE public.total_uninstalled AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + CASE + WHEN profile_phone<>'' THEN profile_phone + END profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_uninstalled) A +LEFT JOIN public.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + +DROP TABLE IF EXISTS public.total_installed; + + +CREATE TABLE public.total_installed AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed) A +LEFT JOIN public.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + drop table public.Active_user; + +CREATE TABLE public.Active_user AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',coalesce(first_install_date,D.created_at::date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND ( date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date + OR First_transaction_date IS NULL) THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND first_transaction_date IS NULL THEN 'New' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN + (SELECT replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') reseller_mobile, + cast(min(created_at) AS DATE) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' + GROUP BY 1) B ON A.profile_phone = B.reseller_mobile +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + + +DROP TABLE IF EXISTS public.total_view_item; + + +CREATE TABLE public.total_view_item AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN public.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 4; + + +DROP TABLE IF EXISTS public.total_first_transacting_user; + + +CREATE TABLE public.total_first_transacting_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + profile_phone, + count(1) total_event , + Count(DISTINCT A.reseller_id) Total_users, + sum(total_orders) total_orders, + sum(revenue) revenue +FROM + (SELECT reseller_id , + replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') profile_phone, + created_at::date transaction_date, + count(DISTINCT id) total_orders, + sum(payment_amount) AS revenue + FROM raena_order_management.order + WHERE payment_status='Paid' + AND is_campaign = 'false' + AND cast(is_archived AS varchar)='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2, + 3) A +LEFT JOIN public.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 3; + +DROP TABLE IF EXISTS public.post_discount_gm; + + +CREATE TABLE public.post_discount_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(discounted_price*quantity) discounted_price, + sum(cogs*quantity) cogs, + count(DISTINCT external_id) AS Number_of_orders +FROM OM_GM_DB_Product_category A +LEFT JOIN public.user_type_table B ON A.reseller_mobile =B.reseller_mobile +WHERE is_campaign='false' + AND order_placed_by='reseller' +GROUP BY 1, + 2; + +drop table if exists public.Active_user_for_mau_stage; + +CREATE TABLE public.Active_user_for_mau_stage AS + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed + union + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + 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; + + + +drop table if exists public.Active_user_for_mau; + +CREATE TABLE public.Active_user_for_mau AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',(first_install_date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date>=date_trunc('Month',first_install_date)::date + AND ( date_trunc('Month',A.transaction_date)::date <= date_trunc('Month',First_transaction_date)::date + or date_trunc('Month',First_transaction_date)::date is null ) THEN 'Activation' + else 'Existing' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM public.Active_user_for_mau_stage A +LEFT JOIN + (SELECT profile_objectid profile_objectid, + cast(min(transaction_date) AS DATE) First_transaction_date + FROM (SELECT profile_objectid, + (cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) transaction_date + FROM clevertap.charged ) + GROUP BY 1) B ON A.profile_objectid = B.profile_objectid +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +--LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + +DROP TABLE IF EXISTS public.lm_revenue_gm; + + +CREATE TABLE public.lm_revenue_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(CASE WHEN a.external_id = BB.order_id + AND a.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount, + sum(a.discounted_price*a.quantity) discounted_price +FROM + (SELECT * + FROM OM_GM_DB_Product_category + WHERE order_placed_by ='reseller' + AND is_campaign='false') A +LEFT JOIN public.sku_level_shipping_fee_final BB ON a.external_id = BB.order_id +AND a.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN public.sku_level_shipping_fee_old_final CC ON a.external_id = CC.external_id +AND a.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +LEFT JOIN public.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + +DROP TABLE IF EXISTS public.gm_order_count; + + +CREATE TABLE public.gm_order_count AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(Customer_order) Customer_order, + sum(Marketplace_order)Marketplace_order, + sum(total_dropship_order) total_dropship_order, + sum(total_order) total_order +FROM + (SELECT transaction_date, + reseller_mobile, + count(DISTINCT CASE WHEN order_recipient= 'Customer' THEN external_id END) AS Customer_order, + count(DISTINCT CASE WHEN order_recipient= 'Marketplace' THEN external_id END) AS Marketplace_order, + count(DISTINCT CASE WHEN order_recipient IN ('Customer', 'Marketplace') THEN external_id END) total_dropship_order, + count(DISTINCT external_id) total_order + FROM OM_GM_DB_Product_category + WHERE is_campaign='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2) A +LEFT JOIN public.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + + +DROP TABLE IF EXISTS public.post_discount_dsf_revenue; + + +CREATE TABLE public.post_discount_dsf_revenue AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(payment_amount) discounted_price, + count(DISTINCT id) AS Number_of_orders, + count(DISTINCT reseller_id) AS total_users +FROM + (SELECT reseller_id , + id , + payment_amount , + replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') reseller_mobile, + created_at::date transaction_date + FROM raena_order_management.order + WHERE is_campaign='false' + AND lower(id) LIKE '%dsf' + AND payment_status='Paid') A +LEFT JOIN public.user_type_table B ON A.reseller_mobile =B.reseller_mobile +GROUP BY 1, + 2; + +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; + + +select max(transaction_date) uninstalled_td from public.total_uninstalled; +select max(transaction_date) installed_td from public.total_installed; +select max(transaction_date) launched_td from public.Active_user; +select max(transaction_date) view_td from public.total_view_item; +select max(transaction_date) gm_td from public.lm_revenue_gm; + + + +DROP TABLE IF EXISTS public.overall_conversion_trend; + + +CREATE TABLE public.overall_conversion_trend AS +SELECT date_trunc('day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',public.product_metrics_calendar.transaction_date)::date transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('day',public.product_metrics_calendar.transaction_date)::date transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Month',public.product_metrics_calendar.transaction_date)::date transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Month',public.product_metrics_calendar.transaction_date)::date transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Year'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Year',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS public.overall_converting_user_trend; + + +CREATE TABLE public.overall_converting_user_trend AS +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('year',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('month',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('month',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Day'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Day',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Day',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 ; + +DROP TABLE IF EXISTS public.revenue_trend; + + +CREATE TABLE public.revenue_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + sum(revenue) Total_revenue +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + sum(revenue) +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + sum(revenue) +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + sum(revenue) +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + sum(revenue) +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE IF EXISTS public.arpu_trend; + + +CREATE TABLE public.arpu_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 ; + + +DROP TABLE IF EXISTS public.install_uninstall_ratio_trend; + + +CREATE TABLE public.install_uninstall_ratio_trend AS +SELECT date_trunc('Year',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_year, + 'Year'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_time, + 'Quarter'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_time, + 'Month'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_time, + 'Week'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_time, + 'Day'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS public.mau_trend; + + +CREATE TABLE public.mau_trend AS +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Day'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4; + + +" > /home/ec2-user/cronjob/product_metrics/product_metrics.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/product_metrics/product_metrics.sql + + diff --git a/product_metrics/product_metrics.sql b/product_metrics/product_metrics.sql new file mode 100644 index 0000000..d4bd362 --- /dev/null +++ b/product_metrics/product_metrics.sql @@ -0,0 +1,1042 @@ + + +drop table raena_analytics.daily_recommendation_ingredients_split; + + create table raena_analytics.daily_recommendation_ingredients_split + as + select reseller_email ,sku,replace(replace(replace(" Top Marketing Ingredients ",'{',''),'}',''),'"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_ingredients; + +drop table raena_analytics.daily_recommendation_concerns_split; + + create table raena_analytics.daily_recommendation_concerns_split + as + select reseller_email ,sku,replace(replace(replace(" Concern(s) " ,'{',''),'}',''),'"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_concerns drc ; + + + +DROP TABLE public.product_metrics_calendar; + +CREATE TABLE public.product_metrics_calendar AS +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type +FROM raena_order_management.order; + + +DROP TABLE IF EXISTS public.user_type_table ; + + +CREATE TABLE public.user_type_table AS +SELECT DISTINCT profile_phone reseller_mobile, + First_transaction_date, + min_created_date first_install_date +FROM + (SELECT replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') reseller_mobile, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' + GROUP BY 1) A +INNER JOIN + (SELECT profile_phone, + min(created_at) min_created_date + FROM + (SELECT profile_phone, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) created_at + FROM clevertap.app_installed + GROUP BY profile_phone + UNION SELECT replace(mobile,'+','') profile_phone, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1) A + GROUP BY 1) B ON A.reseller_mobile=B.profile_phone; + + + +DROP TABLE IF EXISTS public.total_uninstalled; + + +CREATE TABLE public.total_uninstalled AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + CASE + WHEN profile_phone<>'' THEN profile_phone + END profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_uninstalled) A +LEFT JOIN public.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + +DROP TABLE IF EXISTS public.total_installed; + + +CREATE TABLE public.total_installed AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed) A +LEFT JOIN public.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + drop table public.Active_user; + +CREATE TABLE public.Active_user AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',coalesce(first_install_date,D.created_at::date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND ( date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date + OR First_transaction_date IS NULL) THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND first_transaction_date IS NULL THEN 'New' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN + (SELECT replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') reseller_mobile, + cast(min(created_at) AS DATE) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' + GROUP BY 1) B ON A.profile_phone = B.reseller_mobile +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + + +DROP TABLE IF EXISTS public.total_view_item; + + +CREATE TABLE public.total_view_item AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN public.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 4; + + +DROP TABLE IF EXISTS public.total_first_transacting_user; + + +CREATE TABLE public.total_first_transacting_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + profile_phone, + count(1) total_event , + Count(DISTINCT A.reseller_id) Total_users, + sum(total_orders) total_orders, + sum(revenue) revenue +FROM + (SELECT reseller_id , + replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') profile_phone, + created_at::date transaction_date, + count(DISTINCT id) total_orders, + sum(payment_amount) AS revenue + FROM raena_order_management.order + WHERE payment_status='Paid' + AND is_campaign = 'false' + AND cast(is_archived AS varchar)='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2, + 3) A +LEFT JOIN public.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 3; + +DROP TABLE IF EXISTS public.post_discount_gm; + + +CREATE TABLE public.post_discount_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(discounted_price*quantity) discounted_price, + sum(cogs*quantity) cogs, + count(DISTINCT external_id) AS Number_of_orders +FROM OM_GM_DB_Product_category A +LEFT JOIN public.user_type_table B ON A.reseller_mobile =B.reseller_mobile +WHERE is_campaign='false' + AND order_placed_by='reseller' +GROUP BY 1, + 2; + +drop table if exists public.Active_user_for_mau_stage; + +CREATE TABLE public.Active_user_for_mau_stage AS + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed + union + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + 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; + + + +drop table if exists public.Active_user_for_mau; + +CREATE TABLE public.Active_user_for_mau AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',(first_install_date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date>=date_trunc('Month',first_install_date)::date + AND ( date_trunc('Month',A.transaction_date)::date <= date_trunc('Month',First_transaction_date)::date + or date_trunc('Month',First_transaction_date)::date is null ) THEN 'Activation' + else 'Existing' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM public.Active_user_for_mau_stage A +LEFT JOIN + (SELECT profile_objectid profile_objectid, + cast(min(transaction_date) AS DATE) First_transaction_date + FROM (SELECT profile_objectid, + (cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) transaction_date + FROM clevertap.charged ) + GROUP BY 1) B ON A.profile_objectid = B.profile_objectid +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +--LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + +DROP TABLE IF EXISTS public.lm_revenue_gm; + + +CREATE TABLE public.lm_revenue_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(CASE WHEN a.external_id = BB.order_id + AND a.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount, + sum(a.discounted_price*a.quantity) discounted_price +FROM + (SELECT * + FROM OM_GM_DB_Product_category + WHERE order_placed_by ='reseller' + AND is_campaign='false') A +LEFT JOIN public.sku_level_shipping_fee_final BB ON a.external_id = BB.order_id +AND a.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN public.sku_level_shipping_fee_old_final CC ON a.external_id = CC.external_id +AND a.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +LEFT JOIN public.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + +DROP TABLE IF EXISTS public.gm_order_count; + + +CREATE TABLE public.gm_order_count AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(Customer_order) Customer_order, + sum(Marketplace_order)Marketplace_order, + sum(total_dropship_order) total_dropship_order, + sum(total_order) total_order +FROM + (SELECT transaction_date, + reseller_mobile, + count(DISTINCT CASE WHEN order_recipient= 'Customer' THEN external_id END) AS Customer_order, + count(DISTINCT CASE WHEN order_recipient= 'Marketplace' THEN external_id END) AS Marketplace_order, + count(DISTINCT CASE WHEN order_recipient IN ('Customer', 'Marketplace') THEN external_id END) total_dropship_order, + count(DISTINCT external_id) total_order + FROM OM_GM_DB_Product_category + WHERE is_campaign='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2) A +LEFT JOIN public.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + + +DROP TABLE IF EXISTS public.post_discount_dsf_revenue; + + +CREATE TABLE public.post_discount_dsf_revenue AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(payment_amount) discounted_price, + count(DISTINCT id) AS Number_of_orders, + count(DISTINCT reseller_id) AS total_users +FROM + (SELECT reseller_id , + id , + payment_amount , + replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') reseller_mobile, + created_at::date transaction_date + FROM raena_order_management.order + WHERE is_campaign='false' + AND lower(id) LIKE '%dsf' + AND payment_status='Paid') A +LEFT JOIN public.user_type_table B ON A.reseller_mobile =B.reseller_mobile +GROUP BY 1, + 2; + +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; + + +select max(transaction_date) uninstalled_td from public.total_uninstalled; +select max(transaction_date) installed_td from public.total_installed; +select max(transaction_date) launched_td from public.Active_user; +select max(transaction_date) view_td from public.total_view_item; +select max(transaction_date) gm_td from public.lm_revenue_gm; + + + +DROP TABLE IF EXISTS public.overall_conversion_trend; + + +CREATE TABLE public.overall_conversion_trend AS +SELECT date_trunc('day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',public.product_metrics_calendar.transaction_date)::date transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('day',public.product_metrics_calendar.transaction_date)::date transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Month',public.product_metrics_calendar.transaction_date)::date transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Month',public.product_metrics_calendar.transaction_date)::date transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Year'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Year',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS public.overall_converting_user_trend; + + +CREATE TABLE public.overall_converting_user_trend AS +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('year',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('month',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('month',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Day'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Day',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM public.product_metrics_calendar + LEFT JOIN public.active_user ON public.product_metrics_calendar.transaction_date =public.active_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Day',public.product_metrics_calendar.transaction_date) transaction_date, + public.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM public.product_metrics_calendar + LEFT JOIN public.total_first_transacting_user ON public.product_metrics_calendar.transaction_date =public.total_first_transacting_user.transaction_date + AND public.product_metrics_calendar.user_type =coalesce(public.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 ; + +DROP TABLE IF EXISTS public.revenue_trend; + + +CREATE TABLE public.revenue_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + sum(revenue) Total_revenue +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + sum(revenue) +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + sum(revenue) +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + sum(revenue) +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + sum(revenue) +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE IF EXISTS public.arpu_trend; + + +CREATE TABLE public.arpu_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM public.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 ; + + +DROP TABLE IF EXISTS public.install_uninstall_ratio_trend; + + +CREATE TABLE public.install_uninstall_ratio_trend AS +SELECT date_trunc('Year',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_year, + 'Year'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_time, + 'Quarter'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_time, + 'Month'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_time, + 'Week'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',public.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',public.product_metrics_calendar.transaction_date) AS transaction_time, + 'Day'frequency, + public.product_metrics_calendar.user_type , + cast(Sum(coalesce(public.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN public.total_installed.total_event>0 THEN coalesce(public.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM public.product_metrics_calendar +LEFT JOIN public.total_uninstalled ON public.product_metrics_calendar.Transaction_date = public.total_uninstalled.Transaction_date +AND public.product_metrics_calendar.User_type = coalesce(public.total_uninstalled.user_type,'Na') +LEFT JOIN public.total_installed ON public.product_metrics_calendar.Transaction_date = public.total_installed.Transaction_date +AND public.product_metrics_calendar.User_type =coalesce(public.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS public.mau_trend; + + +CREATE TABLE public.mau_trend AS +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Day'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM public.active_user +GROUP BY 1, + 2, + 3, + 4; + + + diff --git a/query.log b/query.log new file mode 100644 index 0000000..f906f85 --- /dev/null +++ b/query.log @@ -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 diff --git a/rdash/Mongo_rdash360_dashboard.sh b/rdash/Mongo_rdash360_dashboard.sh new file mode 100644 index 0000000..4659ff1 --- /dev/null +++ b/rdash/Mongo_rdash360_dashboard.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +echo " + +--------------------------Rdash ------------------------- + +drop table if exists om_mongo_brand_category; + +create table om_mongo_brand_category +as +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'name') as actual_category_name +from public.mongo_baseproducts +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'name') as actual_category_name +from public.mongo_baseproducts mo +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'name') as actual_category_name +from public.mongo_baseproducts mo +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'name') as actual_category_name +from public.mongo_baseproducts mo; + +drop table if exists om_mongo_Order_item_details; + +create table om_mongo_Order_item_details +as +select orderid,orderitemid,quantity,originalprice,rsellviewid,productsource,sku +from public.mongo_orderitems mo +order by orderid ; + +drop table if exists om_mongo_Order_details; + +create table om_mongo_Order_details +as +select createdat, +orderid, +json_extract_path_text(storeinfo, 'name') as store, +json_extract_path_text(storeinfo, 'storeOwner') as sellername, +json_extract_path_text(storeinfo, 'storeOwnerPhone') as Sellerphone, +storename, +marketplaceorderid, +totalamount ,orderStatus +from public.mongo_orders; + + +drop table if exists om_mongo_orders_summary_metabase; + +create table 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 public.om_mongo_Order_details mod +left join om_mongo_Order_item_details moid on mod.orderid=moid.orderid +left join om_mongo_brand_category mbc on moid.rsellviewid=mbc.rsellviewid; + +drop table if exists OM_Mongo_orders_dump_metabase; + +create table 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, +json_extract_path_text(storeinfo, 'storeOwner') as sellername, +json_extract_path_text(storeinfo, 'storeOwnerPhone') as Sellerphone +from public.mongo_orders; + + + + " > /home/ec2-user/cronjob/rdash/mongo_rdash360.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/rdash/mongo_rdash360.sql > mongo_rdash360.log diff --git a/rdash/mongo_rdash360.log b/rdash/mongo_rdash360.log new file mode 100644 index 0000000..9c83159 --- /dev/null +++ b/rdash/mongo_rdash360.log @@ -0,0 +1,10 @@ +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT diff --git a/rdash/mongo_rdash360.sql b/rdash/mongo_rdash360.sql new file mode 100644 index 0000000..b7ea3bb --- /dev/null +++ b/rdash/mongo_rdash360.sql @@ -0,0 +1,85 @@ + + +--------------------------Rdash ------------------------- + +drop table if exists om_mongo_brand_category; + +create table om_mongo_brand_category +as +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'name') as actual_category_name +from public.mongo_baseproducts +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'name') as actual_category_name +from public.mongo_baseproducts mo +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'name') as actual_category_name +from public.mongo_baseproducts mo +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'name') as actual_category_name +from public.mongo_baseproducts mo; + +drop table if exists om_mongo_Order_item_details; + +create table om_mongo_Order_item_details +as +select orderid,orderitemid,quantity,originalprice,rsellviewid,productsource,sku +from public.mongo_orderitems mo +order by orderid ; + +drop table if exists om_mongo_Order_details; + +create table om_mongo_Order_details +as +select createdat, +orderid, +json_extract_path_text(storeinfo, 'name') as store, +json_extract_path_text(storeinfo, 'storeOwner') as sellername, +json_extract_path_text(storeinfo, 'storeOwnerPhone') as Sellerphone, +storename, +marketplaceorderid, +totalamount ,orderStatus +from public.mongo_orders; + + +drop table if exists om_mongo_orders_summary_metabase; + +create table 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 public.om_mongo_Order_details mod +left join om_mongo_Order_item_details moid on mod.orderid=moid.orderid +left join om_mongo_brand_category mbc on moid.rsellviewid=mbc.rsellviewid; + +drop table if exists OM_Mongo_orders_dump_metabase; + +create table 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, +json_extract_path_text(storeinfo, 'storeOwner') as sellername, +json_extract_path_text(storeinfo, 'storeOwnerPhone') as Sellerphone +from public.mongo_orders; + + + + diff --git a/redshift/.gm_dashboard.sh.swp b/redshift/.gm_dashboard.sh.swp new file mode 100644 index 0000000000000000000000000000000000000000..70c33cf0acef781ef5e2b5a05affacd56ac61a1c GIT binary patch literal 53248 zcmeI53w$J1ndlo)xd@6Q7e!^)A~JRY-I-)&n3*^mq?2@J!X%xT2Qb3j(CMxu&2+j$ zcV}ke1JqU5i>qFAUp`i4UFEvFSA?r5?y8Hf-n)ARFYNV+BHXLX#Roo6R0MYa->Fko zr>d)ybQpHswfgt_cX!o!)Hz?BIE7-_b!mB}VRx4*i7VX* zw-svD%H`F$Z8f`CUo6cxwk<5p7W4JRxoW;v?5;1aeZANdcPq zy*(20`5k9kr#@q*%}8SJu>@iX#1e=l5KADIKrDe+0Q3H)1@Kx6rg#8qVA2|5#b z{d-*K_n-7h%9&-PscE3=&|3k?A zA?;q&?p-=VZv4N{?qk~hjUo4+YxkFH_pgQAf2Q4Mwflv~JAd5xPuAyOpnpFSaz9C* zKS%%G8FGJ$KEF@@KIw_hA2@iX#1e=l5KADIKrDe+0Q2|R8R$QKidGl>1KWgvt8Kd-O!!&4H8AHe;v z0u3m^BD@3+!WQ_((-MiVz{lZc_!!&(AA;-Q3Rs3J?15*%kDr=I{BQUm6yO^So-cxb zU~qahTnZ<^#~2iU6mEj6;7WJ}jKVg!mqGF8;5v9c^uTjL##T4OHBf^w=!OK`&A9Fp z@E&+2ybLaZ6r2KgF|Pb9RN+Ff-~{;p3{bxS*TEa%AiM}phj%lmn}&a4koPD&0{;k~ zhFjoo;MMRdcqQzI)8Iko818{p=z~+?A@0NNAot_1V6Bf8`V3~KvWd_+YoL2MU$ZNX z+4`Xs%m15d@|evHS@>yokQlS0Q`taNO~J+2MfJwpuIN zwOLM2iw9f|t+Wnbz;2{7km}5obvR!uEaq!nM6BG0%v_o7>Iy^Mg~d|2C|Pd0PNa}B z-S*LjU905Fq%xJ5%8q6Sr>&upsp*j%e;T!XrJgS|O4SNxDh<2oL@F^jnaxaRt?A6b zXx6Ib?Mi+&Pjst|QlZ{Gm#^Ei6}wSiDK+fbdLzGJ_s(9}Yh|Xmy+f1Z6FS_;u$8@- zi%xAchP{Lzo*W<3{vA0lap47fiF@9yPFQ>03@zo4b{1+jIVjHN8(jn*%??jn7mSbO zTE$Q-)eF@XiVxq*)rvJ}jpwWkRy}gW$)e3I88nagIwH?doYbwtvvIA6As zyRF6QO0Awm2$smS%Q~+=c~;F{uGShu94D=Ypm4!lN0$; zF= zqqZVf?ve|yFW8KD(;r9n%jJ9l;m)ll-1C+r1$Dip<$P(OV&&_8c{a|QPoA6p{E`UY zbZJx@l+xmACBIZEP{xY0jS>&na;;Rb1ODW!d-nnkDuSBb$d}4aFss?wxo*IrTwTqV z8>?QNa^sv#hh5C*=F1{wsS4DScC zD?IJhjE!Xag_?3Y)l?%W(RJ#IBqLQKo{UNccPU>+F>yrGx)?d`Ey|1IsGmwVuhoi~ zX2(nGg5|hxw0LRXtqPeLS<9kkdWtA;j`R9O%<(79*F>#aT%kBMR+j^%!to*Bs)Y)a zNo!=v%8gH3xtY;Xr+rEeOPNh3{7}xn$d-c&OCI@7Rn;|@P4ih#B`@trrjp67W^c{+ zsbxWS($ccnI?ihpUB^SXMe{1=K)oFH)!Z@~C3>AUP~S)UuubiiZ{&F%Hrn`wgG#G6 zlFMZ$Ewkw=5NWx(Fl#h_%T9T48m*P(WxK|MOQ%4(5zgVDI^5e{bOX``y3x(8REkt? zfmjDrtY*t>^aULMA_7V33YB@qKH@ZW&b`s!szeP^9U2w2w3T+Dw3IJ*Zr`5T-qmF} z9UaqCA`lA)DxwzE?O?ajdL-xOckMja?%SQ-li#@~y|ZsO|90%!owob-?B2PjZ}+aA z`98NEZcfTVl`dCPJLIHh<7wDq&-I;K+-0Zxde6%yC>Zzf|<8>?3mB@?&{mqx3je*<}h!5 zsl8?@2G+Z=x?Gue)~h9w1F8@ujsE;>fl90H`N(u%v68PeN{v-rLfk{o$Bg+nc->{1 zCEaE@<=ZUX6aF}2oo8K_Yd>(iPkf6A{dh1epYq6uti9c;y9oD6lwE3}yq=QdSnIVQX znSJrh$YgfN8Xlh<%S>CQ$8`GA3JFvRbuX4k=&Sah)vKMFuVG~d2eT6lIrh;SjSW!0 zjSq9xgHzT(mg_LOkSovRoRn~e=4@nezfRD^_~f)SX4B(%SKcBLr7DN#pSf+WRN2O; zWHBM*|DQ1?e?DV*8UHhkRi9ptd*Cb(w+@Iuu>@iX#1e=l5KADIKrDe+0Q3B(eJ zB@j#C-?Ida2{n}2^()F&$0FK-es}eHx=>!Jqt}wv?!_xEKX=dK{L!7uyTdEtUGLrb zrTj~)mHd&qERMHig?)c+=>0E;OJE!>fEPjsoD4q^oj-gYJ_dgRuY?j@1TTUY zz{&6g3kZH!7}u~!|3|2hpXWbWZ)O*`2P~#3`d~|IoJ(n!WMWkJj&$B zgK!JH16E-Mdf+tpDLVh#;C&$W07WN7OIi8xPBtsXA zocav;3Fsd}Zu-#Ewk%cWO63-MAKf4rpM8rc&h1^@f})OJHf4tLqx^UuotSe)iuLwZOj_lIm?>E z(xSL#EO}4!DigXoFRr51v)Cw9#rOohTwW-3P17c)&w`%mheeDB(pD74mhq+mz8tr&k_~yQGRiX8gi%WPv)%k123E9422ZVpT$0 zj!sh>%J?B89K+$m6}2CDT3Km~YMYDYDuy%Rz7u5+7nD@${z z1FQ41<6?qV4}|reu;zxrb82b~JeP3Q-m1!PuI5`*?cV9A2Y!tz9L;l6g~>Vd?zXI+ zxm2XVM07Rmn!LCbuUqRnb*gF3qAFEgJ_=Q*V%3LHmFsY+R>x{1WM${&AfnV9~YTvJu#w>hB- z+l15Ite*>ne57e5Kjt+MB~R=c`>OWC3k49UW8WdhAr>&8gc zF`m#v&5?IL*cN%G0}WARD->#iyoN@G7NY)>LLvHqis z;qQdMg15mDn1e|ef+xTy8Pi__uYx@6g{Q$ijPY-SkHWiQ2F`$QFy?qvWpE+P zz%)D$eiMEJ?q?q04tNX9z$kP=2OJN#GAHnMxCWMB0WN@N!nc_R_&R(HZiGLC0$A`Y zcz}6;@53GNMtCvohqK^xcosYpWWL~T<^^tm>*1|XhZ)!fXTo=x8@LXB8=eU29yZSr z1kdQIKcY{QDJTv>mq%8UgFjn3q%ov$UEysM5m z{ap21m%&)IJ5+Srh{#(pK$oCVGOUD?nQ~cKoqHW8-;x+hycT^2Gn7}EcqgMfSe|pM zZkFZwFgsk@0Lzh*^l0X5_7y93rLdYxbgsS7VBgs6zRB^6ruWaX5Mc*lH@3PU7)-3J z662FY*dGlXw07)`wqp#0>2+J5v)w=8JB6`TGG%rko9?z>Yi=y8d4rdvucmv!uK47N zA?eNJM#giS&raE%Z*)#qXJsXdw~j)Jbt-wpwhtw{QoU>BYOR2h&Zs=7XpdPYj3-cC zEaHa9W9Q$9=p;K~RZgSQ>^MIjiKZ)1W14-F$#z4xOt|aem?b@2j-6_6lMg|Qz~)on zRC8|mR{=tKS#yR2kg?Z@rO=lB9%oS?DMhYSkk=(Q@3p zNXd^>D4gd*(`Tbv-x~eUtBKjtW0uN3V98Ua`4(?uK`Y|^ZIM8PPFn8HD*>0Uc{lE zXV5X&{S3MV`k=}wvLmWoLQT4J&Rvxgp6zb#bR<*&{T1%A1RTBgJ$yDO*66ldU7%&V z-EyAVe7MzRXpUzlq#gApo>k0hb(~%nCxx8FZ&qOS8?}@wdh4%#%bRN#&4qAk5u3Ui zU+RKdE5TwUW6=xcTppP~H2rg|SW38I^%6W)ow*szH>7HDp4V{N{eZ-<)+#z(NV=>c zNdZe<`~>z}LxCiCX|bfNnOAUUp=9Bv=gV8y=$~3EA*vijtikZ&8|s$JVoUGqb^Mq; zU;2;h3px8qa+B#zNvlO+u6sDWMq>TiZf>Pss@V1VtYg=1qbB}B$#fp)=8Dqy z7BQpYJF0)tm8$P4YQDpFRsX#DFuJ1keXl$q)|ng$`R4MTGQ}+@AuparHfri5 z;ooH1wa<-Z_&VGQpN1RZeelQd8mPbw^g|MkgFBfkxCPz^*T4!~0>kh^ z=z$a9LFNtq7Cr=XZ~*#YE1U**Ghgsu;rBr15`GJw52wQx`0vaSh^>I^A@F**9L%|a zXL5Wma{w|wa0AF50$0J6upeZOU<*9JT)>CnFzkXKGxom;4ujYYJOTa(WBlKRVUWE8 zJ_E0X3*aR9UySKr4})+5sC(qxp5S-rMz4DqYBr5pYTcmP1MAqARL5%*VI#}oMlbC8 zb9+0kvT76Q2uvPsBD|@h+C*@7VsjHgy$UMO!nmSY@7J5|*u<5iI@Ftp#>;@}R6dpn zRI}Wwa%?RLw{N(KONVr8t*>Fam_5l&Trjn zev3J;*6A8z1C;5kn><}gN|-d&tjjS)&^c72Z)3T&R4jDm=^(NRjH+d*pPY_7dywc@ z)DK?NLTp^s3T!LjD)VgSbr4s0Ww$Fqt00$K?skvaWTJnNfkT%y*lv{S8DT7>;&UDt zB2(MhB#f+!MFU22vQ>^{rIwv>fm@xV2ApGe%JsTSaK!>}wc@5}VW=?B{Yt7 z46mLbNuJ3P=e>?l3du$GY?@M#UdDJhkQckMVn^pkSA6c(?9lL=lp%5!&qLq!O;sQwdc<*qo+@TEe}Tfx(q=axPyvu!Zxiz_ahPbeNlwF8CCw)Rxn?*`@eYVV?dAsi(I34uW8||w%m8dtW zHQO(j>M)gX=F9xk?);heP`Q6*_2ikZ9=y|1Eu<1E%icLB%X-4WnKeSxN@U6)kpt{% z57`Z6(5ddjl3gsVEIGHtn?g~C&f|K7mHX_rROedBjIuMI$YY)=n=%eG*t zgxWLAlYpF2%>#1xjGMd~-jNyd@aoP~!tCD|)m75yHxL(gYF;Usx7D5Mro;>5bRQ&|@Z-|rkyaBY#=MCctB&7&HoZ-Tnw6-|O;TNb-S*HPQGqw8=Nr|g z-bQGah(=uI{&acjA8=acoXlqJr7@B5|MiTUe+FXz&k$LC{**EPY4G=q*G12NP>zD%{?uyI9P-qGnc&p= zy0eH%3N7Y)1L=sTds#6jQ#js~#jnhCuZ;iqpliMx9kh)93;JQbm$CmHGX95~;d=O8 zI1EE@9DJBLf%m|FhL=Gd^6+AK9(;%SfiHrr3Ahg421nrt9EM5Q1$Qy`AnyRU8LDtP zJj_JIFX18h1$+@+3*B%soCHsSucEX61lUl79dIA|`?tVhSb+xAVFWU;3(kViqRYP$ zE`xn=E{LxGRCpphh#vn3@NM`4+yw80tKleAAPYM|)&+bIJ^xqW4e$z>g#)k~I^kqE z4(>zu|5bP&RNyi=0DaH_C&EMAkMDrom%HF6+@lYGymwIU*ZtT7+y)bjBsLuC z!$TieZ^1qM-V?*n+hB4V%!5*s)%jg^gL{M<&=RGMnlkDE1GcE0}{z-_Ge7%tz zh13mE+6F^^)|AT@v+Djee35NPPu4;~w~qSEouNOr+OuYjhoSM&D_I7*kCB4O*P>{( zirE!G|77Z=L&_9+eAKHxeEScjSaPM}DvEp^aZu^Pi(Fi-uBS(F&-~h`WWup%MJU&_4tMX)~ z9V3R_eSJMW-90^m}7)>)=|x9Xd(Mrb|IVTcQ}2A9*k+`=T$3WwEis`=O}xZ zW7<(@KeDF2#yTAc&vwYILff*xG3z(e{DwB&?XSwqM?{>iu}D;l-Ku?-@&A8etSj%& zlkva%e)(TAzW+R23vY#^Ao~F9f|KB@jPGTyzgK__PX*c6?-qCulwc7u@LYHfJR6e&<$T8k1C&1`Nf}D0Q3B(eJB@jy>mcYNV1b$tNQ&}?LzX^A~#Lk%r9(9HK zB-FQ|A&pw!pkDCmEpNdrwp6#oq8MvdXBkP8X45sGZDVPSIa(uDy&guc@YvF*LLZ9+ zuD^DN#0Ip@ZOQVOr88`e?5-^O7fc;Rq}2*2SMCKDI ztEP-&RhT6I#4Bn6wfn@(=%{(VTs^|uIo*lsE^Bn;K-NlbW$P>3S}4sow)%T163be? zWlq8K?JS0C;k{CASo6FsTBcCE5RtV{pgLGEcE*rgd7r2(dyeXln?G%6(NqzrfjmB) zx0}j)xf2{kCaL*e;?<3C1NZA}Rlb$||0KF=*$-dF|NC@1cspbK_ruFz9;RRm+`~Bk zFX0U^4O1X%{9gdifJYeLe-r)=J_3IVZv@f#&%;Y#7`8(PoD4r_ynhee0iS~F;LY%I zSb~GFANImlI0b&rxc_eWJNR>W1JvMR7=TVV1%Ao6|GV%t_zZjiWbOYUco94ce!;xJ z?eIak3J$>Y;9+F)Ww;Tpg+Bn1)2rcPNQ2l5h^@f4km0A{kHCiAa3cH&egE5F4#pq_ zKSjU)6Sx(wgZG2j0=x@WKYQoB($bT^B)z24&lbS5xH%hN(cn}?7ofs+&63q)IjI1}UUDv^SScl!<)jgpA{(^Pw zPsJ*W*7b2fN$Q^qEm_wN5jHGg7iax;_$=8`OW3{3`Y|~R*qa?f3)RChn$E0H*UnzK z)JxREr`;v$;q$@W9lcnb#nxfZfh;;&N9{#D4d#1u0!!7kCwIBJKJ>TC^K#)l%Eq#F z?bSJNtXJ3P%{A-ZDQDffJ`As0cYV1V3K$zthSsiYpWf?MGxir7^tQy=Si_2)g> zne-6-|Lu%Z-^LhL#{cKY0FcjZjP+juyWwYy>u-Q_;CT2HWA-AP3J)+A|7*AbUIXXB zNpKfq@At#+K@N7nbKxh9w{Hj0%}>I!LH6F0arn0wbIW`DZh~t;-r*-U`}<)Z$X@=Z z!6T&gZnymcYMt3DAj*>_Top6|;^W#_WYRP28#OdgO&e z{akBiY9zNW;b{vWuTF1hJIA#q5QE!;C-jRT&R+YKF`;as6Kvzn56yvj`-75%)9vo; z#E5y7O)ML-fhGHh_3LfKnCT)X<;Qd1IJB4 zWCZEHKQZ)*nS8^2aU!eLgCkCTJzsli>oq7y3D3r;+S__o@)NxJQvRq{eB9I$nA5Y7 ziB`L4y0_Q3gg26QQ|0|4`$|N7)_J`>&PQ)}$?=%8-=$u8=*1BF(<(xDLC{HwSa<(7@LqTgTnh5x z;f%Z?aTkWBGEy?3{zTk`iMueWg_AF%z(jZ0;w}vRTW3FAPiUb(t$G}B7lw_~op;E^ zT^M=}3$~2e+}_tQ?ZTXRVxcNaRtt8gx#XG6BE0j1-R@=sY7>c}Zuv=j&UCDns;M4E zd&-&=YXRR@V7S}pd^UIGGu=+r5p5(OFOc7meL(1`N{&nqXD1oMPVdj=+zsy&Ax|w2 zljFH8A(>1_HYd1Ms3hJeM~2!7F_yV_aC9U$GT2U_f$Zq$@aXtOWUr0H4UbP|bA#hk z?S$DsGBrIuGMTk?5np3A7VrAe)()n$ENc!o)z% z-Dq1)I$UH8O=hR2CdbFeS_Fx@zsUpRQ&|kKr^d1qxTx zk&CCs$8+smn<8u-%(fRsuCYJU)@24JXL5oyk(nHs+D8c+$+cB5xeb$oy6Nhb3>zvV_xyfEgZent? z%PMG9eAZU9)<+dVT2%aNi;4o`1K%&S3Q}KxAGB&K+Ex*(e--KXH?K42b#rcZfm(Av zpKosNIWu$S%$YOu`~BuOGZXD+4eSiIw6z!bJXrqd&$+XOLV2>Yb$O}0Xpd!mR~A}3 z+FMWB*t)s3bJLc#(n9Of{Bmu7xwKSk?P!~vpIcb2mD?(X=h*}|OfJq>&z+yxuvng2 z+FzNjZJ3!GpDHcwpO`N#PK_VvXj|H!b+NVMYbCH&0;?>6V|H)s>`<@zlh!>kIPRgl zSDDUQw6zjgD}l8VSSx|G5?CvNwGvn>fwdA?D}mp~5~wXar0{$){(ns7_n7bd#J=xq zes4G5dt>MS$o!r$-t=3Eo5&lI?g{}6M0qxt^(*!hQ=;}19A-;13$hu6N= zN?@%7)=FTl1lCGmtpwIeV66n!N?@%7)=FTl1b$CTpfpt|h${MAQ4~=kt^R*I3g^$@ zr*IGszk?1QJnaqwLf*Kfjy;ClEVybs)gh#=@qFa9n&WCYmh6lsXD5$T)Rq#5PfE~~YkA+`gGx#c8 z1FwXq!$aUhlF~%vXcnV0b9##v?<1ad4^}bOpWs@2!PEKHWoo!6fb*O$1&nmulsy z@lx$6m1?cLc%U>J6i*KJ&o3`76@%{4V2abt!M3jA@r&h!`Ndivzl+U5cR>;gKayl+ zX`EE|k8|;<@@(ZmdGQb#DApF2iP2mL0}qb`O$((%auagTwdExVH&CtADw3I^7c?Mj zWhxjPIHNx(?ixIysM0z6^!}0lpedF}S8-RVqGN9#8Qz&yY^oe1H-Hn&fy!ige7aor zE?t_Mt5oG!i+4~zgoWACB!xC{$Umh@RF%QpLa8!SmC`#Y$=rH zAfVnh-gm5?CzPN|jk1El8f$*NvQ* zoj+8XtsQb{%5CVoj-T<9o1T>?zf_tnk5{Jr6Xz~b9m?m|7E6=0@r6=t|9FjG)V#&= zQh9ck%vY+@^DRZWyP?utx!5u`vb(=IaHrL6Qwov0j?{Aha;1fZ*$R&azoUhPN_A%3 z+@bqDIlmMs=kD+|N5(W^m0{ZD#btRE*xt*R@;sZ*0zi0RdFzvx$pWtt`Xfaa?W#La$9R=8;1w`iqg zvR<;4;?;i#bHz@1MOTQo^4q#X0{t}RyB6oCmU(4shZZ7l-U?CWG_CVc5NQyCfze=S zcq|y&JvbPQ$?4*DX)q!!ef^YsyGQ#A^@Dh4>8qrPZc=S>vMKSK=9c2Vb;Xuqu{jy6 zff}0GAbFWe`!};)N(<~Avn{@9-;z0vH&m{BkM!>GP%Q=1l=UI53=Gkxd2Lxsp?9Re zd#v9`HE~(SRJ-zlGW}MmR-%xRVseQ=@ur_NY`<%bpbJba*KN$?7uQK(%}@ zB5&yDsOn}f{h-%F(|JslCo6NM*{06Umd@tpU~+$Dc52)YRPd^S`d1~C(a-j_d16Cx zV)~>_CziLI+`756X>;qQEhqEe#*mIMN{dQJK5uz^k_L0hh3Ia3_HwCOtJDse8e)IC5WD@?LDwScF!G3b!*%`k zYxgd9F+$aS)lIz)BRBfOXCxqcPkD0xe6Y^`Q}ncsE>8t1i11YiPj(&Oq{Ok-sddLs zlqS#XD;+|0ITVy;=GO)5o^rz8))VGhPncTQtkZ8T2kU};)mHx#^hw)>x!)J1*OuH!q*spK_+?%v-1U9_t^=m~fB(7p|C z=c;>0gPwk_L&GXp-aVvq!V$XCf!@tsl{(cR-8qSAd5WW86=%?%kfa-jO%Y#XG=QP<=|@)?Vo9*80|c7UYv6{aTgs129V z|JcC&gQNYbc(rvwr&_RQ(drOP8 zIugAk(cPwt))`Rbtx?+RiV=OYmZ%e4H#NKJna0H9U|Z0h2%P%FqzGN)jAtbUV(OX6 zl#P+Nh6K%9$yQ1$HNQP=7$Zc+Jh*?J+AH=KU4cvklYTb&GRl4D4J$Wg?msR0p`b{2vvG<>c*R)s|7Gs@4?W5R#a3lHl<@`RL$DYr0w;^Go2Z^?0)?D$8p(9*7xT1ee( z5gTgUz#2EfCWe?+u*GByA#3Za=>NkgwD(2V75#t4D7WuN*MBxV4ITskfsTIzya5(q z7qr5C;h)j6;EV8nxC}0W3Fw96;TZTiHiU1%r{Ep%8h9?0LHq_< z;h}Ip_#8Hd3t%st3eE6v_$O=)e+OewfFEFExEXGMSHnwT9>$>?eulk4Y!J7=M?h>5 zkAwTe&Db0+fOA0R6Li2j5QmIgc^+Sb&%(9vc9;cuPX83ML6}G8eaY*P_frT1IxT;; zT$*ZXN*VfhV*|~noFeA5R_hZ`>=0{OF=z=onuB>Purh$Zi20Fm(4c2;5Pcc)Bprm8 z_ge)F3v=`Sfywz9W%aaUtg01pt`P;TRC+HMf`6}3GAmQw%?H7zX6qqA8Y);c*Fn&! zF1%x8c=s;3sScA5xrSAGB|E{X+mtp*i-gTCa>g6yl54wsX89t-O%5^~ByJIb8iHUf z!~B+!H%u?jPFH4Um2btA8d``9J;*rK%#t!#d4;pIJjcL^e<$mkq%G5p zuI(PSwd?-Y{?-k5M}T^o==^7{#&DDg#6I;N_eeSHO4U4l{W#T_jNFq~ zI-#S@bVbJFtp~%xJQcgy7IL)q^!UxSS4Jr*cxNRKatk)Yewu!cQ!|&@gxd?GI0Yn! z{MCcCmWYgTTdB(G6-ybB-6gMHQ9sn;(0sMk9^mm}R-)E0raR+5r$Bx5D;V#_bOVB= zdhG0e#(R;`oTrh}TNur#DGv&JkFIrc6|^O+PP#^Pk&O4wx){BYs@uY~$Ze}Tt=p7# z8BF82yMZ(}24y>mmYoRCw%*v%b!kWeDDkDcEgr_+mjqFtw5BRj+4S4h(^kKxdB%!( znai-YG~3Om2F1?y&d%2M&eqP&&a&o9C-H{IbK0S}+{k}y)X9iwHbittD&uIxbm|Ju zT2!MZHj{q3tu*7Pb%Ki-kQ25|cWL_&W5ajpb|)9hWG&0Akg2`L6Y#YncP{myxuR`t zUEst46OcEb=r)`_1%*so_%v-^_?>@1F|1!h00Cjl`AlT5>=05qo(KMU-e0?%bbZ9 zjPi{OJ52R6?AhIa3IB?Wq2oSzvWKPKDQUvEqn^4gDE5}Blj5O>M`=7aD?Rlr$-<5+ zrF)L(v$LK9O+goF^e^vrs=sO&3Q;c2cQp*qqoqLC5@tJjmDNyK*tM2d(EQixxjc8^XBAs5y7Kk9DbVd#~j|J(WJXQ1;x7LJD7 z(DAQ?cfi}B0{dYGHozmG0Jo#}e-}Ol?}oR+%V0m822X+q!0*uie*m9?OW+WQZD1#C zf``FRumgy%z?

I0$FM2KW>B0lNP;KX74lji{D8cE_ z1O@mWH~&ev7A}S7!`ZL}o&d+euhILz3Gao!fLVAJ?0_QNg&p8l_zYYRm%|I;9M}p0 zJOKV3d%#U_4ZH>Z91g;HupK%efMen3*az%9gumzim%uEX1y6$xcnJImTfrCM1MosP zA4;$pj)5QYT(5+8fY=b;2IAW=4P|JB0Oa|Lzk_^CRZ`NRb6j4J_EOj7C#B!sNcVqu zJ-z zOKC()NqI6P2f+y^N|7DOx0jYQRFkDnc7>zoz78SzOYdk@_S`8yQAt?lblmNklZ?AL z3{j0vor>tPawl>$(krN&kfVP7RiV_&yk9_RU1v&fnc0!m>9*>0QKpZvA@V3`a{t+K zrPgqx=lwE~M|zJuQ$L$g#Zhvgo|ny~I$e;%{^y>kcsi5HZ8KeCl?<7@CPNkc?8^W5 zRF+rBYvlC3m&pueUkXYUsgr0k9KY^~(#*CzNY!vT%&5T8^Y>awudFcY0#6TjuVve` zeOXo-S5MNz-D}y-rF`oG&kTC6W!{u!mfjy}>-<3XT88~hBzt`S?7fmjQ=SQ1i#?FE z!k8HMjsfuSGyoBMMFBX>K?I`%1p32BUs6wyXBnCk)nJW3RFop)D><>WJXNV-Sd!>> z%g5zO9$#qHY;I|4>fJpu(mymdE)x@yR?g<|kRF|FHlwydIJP3=|EE*D7t5G3`u`wb z^7$fq|KGw3UE$H}9fxFP*Z-y&j9{S<6=@GjA9-Gl;J2 za&2WG)#FQ6&=7-Hbk13rV(d~bGdK^}83TTE+0x#;-rh-b5DvhZiQaRi#hFT#K;Eg$ zJV%3hBg?Ve+d=8}dh&?NRnVGk=nexRr!}kVhqoDdjC$DxLil%*cygQ$OVXA$Q`4h@ zAH&vq8oVx@4WSDby1$wqZ;H*JV@2!TsgMa^eDd=sGrbv^9?zLpiRlHKLC6A(G4FI7Po1$s5<5(?bwvwG@BniP-mS z6{YoAWRZ)hCUF$eFL6vvcQ0UbbFG$`(U6*s|1!j)`=?wzB@xup86P&)(G90^jM zgUee}cz2l(vX!1f7Me+Y-pQWFJ;L?kUM4eN<0+fNu9s=vkdj4ztr`Db{bwxRRNn7` z4(I)C{0)j+Rv8NIUDSD%H}|k%3-2$)z|{o|#Wsh$t%`j1$+L_U4rW|C%YH&E|HK6Gqi#Y<-C|-9eL% zY+G#CsQC?m-|D%yDU`(0%{48VZ*nNl$;%pPe%$3N2fg>dmZga)GsxuQ`bM9{SN-Yvmtw1*2RS#ho?k%^LC8PQKL%10R(s zy%HpEZJWI}cN41A{6cB4q9w0kFA*8-$w*Z@Iy6v$)gx=*k7Um;`-UF~&Oh1DBclS@ z?$f`pd*~}ypy;#F&c61RbC%tBz){}1py5MO`-d>P$e=KVhl0=OUi6y5(;5PQJiz;oakAoBno z3o^$4PLOo~${=I>t?*#D3%kG_@I|-+E`%q;32;0-34Vcn;B)Zz@H*&+E_ez&5DsD= z7=b6k)z}E00UaP~0R8}*!1v)_;Gg0Ba1lHNuE9R=0eC;W3`XHN`Hd~$YPbw!ErGA1 z`~NLeVG6c@tRZj%Hh{l^*TAdcVh~^cL6GtG^Pv~Ez_D;MalVE%BliqIE*A zB4?bXV=5=I)sd?)wu+3qVfjd9aYx60f2vCImhSOd5-+wRQb+Wg7U}eOR^Ai{sY0@H zhFu#dP^YEhnKQ^S!GtXeOE&r|X#_p0{fWPVk^vUE za)FghvmsogsDYM;*(%m9v+5FvHkw{JnY*`7$-ccU^1cRhrSlO3_1&r)kkwASpFwJ4 zrqiHZEsdMzbp|uXLO|=%!2(`&3QWBCyyTs&u+hR&ElSXxVmaHUX92Ld*2a$FR&9<7 z7p;-nLA46kY2korxJ?aAJb*Nn8Bv5;P%B+s)yz!MMub9^*K7{Rj>&>v|4l7_YabW2 zu2!ki04{V<7`lk^&GjRHr?L)1O zX02h}ZR=GGDVTz~3-w3YauSKVnwvD}s8%7~T6hWiZ)%I~?Q9mj8zOh3{bR{Y>qaM$ z{O(<9C9ilJ7qw5C=P9j?_SW;48mw#hP?i!iKhMu(%%YTGFxKrw(#c!K0h~}L0t8YV z3n_XBb+NJFi}mdeOfXq+(H%4oT0|oAZ`l5Ml|YSc+LDX9$6=%>rg-anky+Ps zXPjAMy|@_`h_FtGszkMzn~x6-s2X_WMtQ<3O2SH^Qgy;P4F*=Fis;cg7rx0Q#q?!g zCmq-*c9GI9*i-6TU0wA#+BlW4i-qUHh<+a0c{!%1T`@};iqmWS!J3@ZaaT{bdWP2j zX5~j}2NA-|m=a7ic+$(HMbIUXc|mO&TftD4dA+1zOAVYqo^mGncJA>zyD<2Kx>*!zug;QOgr!V|KA~cDdYbW(%kX68GZjIxE#(0 z*#qEI*b1k>AA_v@|0%c%rr|O0C3OC4LDuy@6T~;*F>o9DzN`mOgKe-59s%D*$A1e{ zq1EX7Uq!F~F#H8nK<4@1gnoZ9JPN*wZhte}1aE@BfhwE_kAR<`-@g-{3D1Ciup2hQ zAHjX$o9OzVfY-tjh_C)OD8jMuGj#v&z-Qn+@J5ii{Z$x)E$}e76I;M-a1&eym%#I2 z0=9wOJK$RE0%9+CDeQxGkTnOtgKa?89DF(Kg*Nyt&qC}4@=V??&xMb!QJH^>g9Ez; zyLSx?72PYI6}H$tIJkSHJ4e7>BmFxEM0V@x@7_JupCiWT83RKd?d>d#x?+a6clXL7 zn!9%Q3=Z_>2;Vc(Jvw|=o`7VQ0oLxG9AQVhhX(fMxiMLC{;WI!r3A(Xw#RQtz1mRok zAa_sxRs*sgzj<>qoFLQ#bb-9MbMq6GS-sxBO2Io-o@0-NFlFzIstqnRk?NvHZ2uv; z7`Tx3xb!Pw!uY5$Hnn&tmPlCfTzr|}7aweMCq18xX!ZGllEj{|TSF3vUk!$Wz3~Hqb zUX-}{CF#%1u_U_MmqWU|sm1vP6Sy*snvQzEv_do)pk8VJ8n=jzQb);fV*8q{1sZHR zzNRK;;nDMc%?>g&$UG)WcD8a}*=#!_$DDa6ZPOZM;ZS-}#uhjMd*C>~5_6xrA@-(^ zvs+BHTAsE3tLCR({-sZz@`@?wB&8X*%iWl2W=`Zt!o9P)5wWwGO_O3a7K$_$zSltF zp45TRbfSz1o&qV4$t|<#9p^!-k0>)bO=+!MD<k#5 zsVerG!p?}NFY0>9uHsx-cJry$th2#R^E>OpsAGb%SfUtMDF zAX&brOYfXyl#$->M7`)nDUZc9M`ICvaU?8ccfXO6Rt1?T5Zi~^d+T0UNEUx0ma=&gk*mzRAz2Zn;eCH8$b76$&!*WUc#pOD^TDVwM#_k86 zG|=!)NA5+8)cm{`(h`f=#lSwQaedT1+`Ne?87H2WI@oYh(%sm-65`@@ylzYQXtVgY+JTK0iGC*6&LwHl>7?=+ zl($+}RvCPVizv;(klh(h*)C=9w!!g3@&8}Xy}Uy7O!WT|zT|TY`u&w~A&fy2{1*NG zEAUoWh9Ni}?nJ-;GF$-{Kp#9B{uBLP#{XXoz3`{-pXm6Xgjc~7oD2_ye@4gu0LVUn zqVtc!Hh2j97M=gwa6Oy>zd^@;JG>fR48w2=tb_Z*9q9MB!WZEq@JbN82Ar#_wR$O+xIEB8VXph`Kt$4LW>@I!v!+^Tuin~hzJd~>bMp{)Zz}!d-fE94(4r+ znl)mlhxyV*>!M@X-kTpLmDah(^cZh^F6xPevh2;O+7#}8s?;A*hV>Yr8@LOyW6bZz zIm49`TEaZ`xGIW;I~KOp?vQR%BZ6a{K|hiI5u8v?#u>esajEc7jFvSt?<8S4sAT4- zgMeOz=mgR@!OGfu%S$enFF$9EAWF)7%+Si*ynWxijRSWb#xj*hpks0EW?1P}u_{Yi zS8=3&$L_)Ik)nQQiYBw_jR}MC>;i9b%Xs_ax+f`$u$zueg>a8(g%MSx<>XjuPq^!; z%D7WBbZB+VT>uvm-h>*n_sU@{>|I`}&7+P*hZvKrTj^!9eQtd!!E7U_DX9s;F{iCBJ!^1msT+6cSp)sSVE5?hIRf{v!gG#byLOKZb??lR(KGfA<$2Ra`+J9n#6>5^#kaHJ zsO-U)w*ZDu-?=wuS!ImnrM+iG%0TVVHMqwg&U0iKW$XhkD%{g4X=d@;B@#oy8cI@3ftf(==dLkb3w-W??k`< zIeZH~46lcC+@HZf1{Lcm%=idcSg=Tmt{2JT9zrk1G6Yws0 z4ZH|yAnWq&fgX4=$eMh=;Mx2?kZ1H!c~0IZQW+;xyIdV4>~v!tk`@Eq*mC4-dveh; z9X#Cz?1qM;iPZZQTJI#A?f%bpycE;sx3@>83%SG-!@8c0&;?5F74JgD%%1Le)P(5{ zN6e9KXsn$P!>=kUx&(47Sb}1If}#FRG&Vaq7BkD7?gv}JjxGgc9Rg&MLzyO)!jz9vHH{y>83bm; zw^W%z4mArj=wdQszRI;HTpHTcFR#$_+vS;J)q#RsD^Z^r&((=X6=_-0H>}J-;1+sd z^Oc?+>?ggwz1j!BwO80j*!ZNkmtCh#v#LA-cNWT7R(o1VDpcxA7N)s*crgl--#&l$j57o%hk!I;@o_-w!i4Fv}BCSm_MQhYkOh) z*u0_@TZ#YEwyknM(X0EjN^EY4%wacMMjtlOlu@~TB_G+i8Y8zU=k7r8*f zH6t`6O6NiwWPD$ktJ5tLQ^GVPdtGu^ZEqq?k~eDXgqncsw#ly{#!97#ojufelPqJ6 z@n5Uq~wx3$jx$j@8zX| zSz6-KvcQv{gUE4w8>TBW+@?g1e`?xEgRA>@7!T$0FRz1rOPqKxdd6;luN@+LSxj%2 zX$;k~h&I6TEZmMmTq*GpJ6FU4mKMmxk_W0U?*wb{iI4a}3{OwbR;uM-#I)a;W)C_H zo~Km>cwpk!;z=~_s{TJ|IgqLU(JWN&rW`bK^*)@!lt3Hvg*arIL*pwY&pE8x7mrK11K=Ut*#Vh9ky zM?U|CzW-?uAAmQ)OJNSqgw619_%${FnFH_%co)13WNm=0a4g8W09V5$@CsOlF_3)# z*1;1YfL~!F_%VD2-UtiubP(Ht>Y``8!04zj-Bv!EUR6C1-d@H&w7|7Sq_37!Ivg!_T49dMqpHT)xXgcrey z@FQ#muY&_{7Mu#};DI1E1o0aX+rb>{1ep(T2X+9N3m|rZJJIiDf4^(sD)@6K!Lwi! zJREM}*~s$=pSJ$b4t-@QYA(ELyOTp3(vpkdgYUQ5dP{zxf*bkWHJg#PufA$GvAjeh zMN_|6mI=ewxz@ix`ds!5zuI_u*0okk#k$w>b`DE-Gtf7goILMdq?Md4`=yJ-FIT$* z>QUGJT+Wv6*%(hrv+Ss+r7L#>?|1lmXLQ=qiEGbIbNimKBahU%N}4T?mH)7JJaK_g z3<)I(GayR4BeND!6;|vhL#4r)8_ZqVGO!ic{*I0eshjb8JAHH=)mh8#kl5;m4Du$EyCmKMwV z=V`{=@zA+AV#}0wAfE?!|C$H4o@~A5!Hx1$JQ5zz?go!MHM0^5j#Pc7YQAA4bNj% z{{tDBh_3CciZRO@uAt6_e|TPT_c!-0*ihOEm^kbi@e=1!yx?qVx~@cCc6&`sT-;7S zchK)|fJ;E;0Gtgc!Eta0y8YMSBX9})8HnD0I;@AI;NQ^eZv@f*Uk?|8=>8|dBjCT# zm z2HXUXfcwB#(Bpd62M>mCqr<-zM6Z|m{l7t%{|w07fR};#*qXG`cX2f?U@p;O zeqXRKPHv~LFiL&zFlC;zlkQ|Dd_(SF;Z+XPFDy(bw}V(1CBLUw7$>*OSQurMzGGnm zd7a3@82P=)!Z?k%m4&hL`Im*^b32-aQSy15h2a};Jqsf>=z|tU%yuBj#OD#fy=DWtA{e-X&JT7gm^+(e#z0J}C2G3I zph`1kNtk5(@GfJv=Fq6+GMLzZr{GSRf2`zemzhakFWn>2rPG6VO^u~qVq5rl=dG7w zO8$D=dfEP@wip`tT)dLMo?c4SY(uw8!dWD2l}Sm)g^8NJlWB;C9kGEYgCvaM$soSf z+p~(-59pc4T|oCbMD5eb^D&ciGCJ5E{e50c~Z&aofFJTwN z8%Ruf!IO(IiT?j&6wq@;4@Lh!Lu;dj8`1M$2PN15GS>egcr!c?#$gA1A6;Jd0hBoa z&G2>f_t(G(91Sl)Ul-l|(eP39^C}F%BjITHE;{)eVH&o;2hqnbg9+FJ-LL^JLoXNK z|FhsO^zmxwEAIUQQ{0&$Gh zl9gQ!L66)!J9E@CMhaGY5_qdEc*_9E`FaE2v3c4OJjcfuT2dEQNK3=J$IC7 zu72tP6@SqHwlz>oE$Fv9awhhEyDVop0mWYO|4EB6h;k?m*pR;%gIa*u=^08f?NIwz z=~d$!i*xuZG3X~3KCrbJ3PG?oL&3ez>Yue43U-xHyYdmu-VvLjusu0HA=nstjTP#%%Jv6V_wT_RIbQ3{M+!LqkaYctBxFm?UPl=$e$Q_F1g z(JR7TLXuEjM!Z!I2kp~#c}QFSg@m2d=uef>HP@30y`<|nRTQE%UQf8l8V=81>cys_ z+^(o3@lo<*Ow&Qyh% z5&gdn<@FSFThae7G>YsO(fL0Km%=Nc0-ewS?I1P*SrhP1Yydxi&%%}P0vLfJ{1$t_ zzrt7HH826Ka5Tu+|5xE=xC!0{VmG)D#$f78e3S|9(XTboh2iZ&TcI*!_H{j#&4tNzj7tVuopck6q zX!tQUiI0M;QE&*(gf_Sf`@|REQn&!lhE6ydevCch^YCV)|N430uWi;GJ+0hz;X~@LU*yE)aXhtvuWB!8hP)xD;Lh zy&&rhbb~y9nQNdvsg2w4&T%Hx_VtYKQmt>ndg2#Uy-JO;bRLFq)l{=1(PG3sO_nrd zIm3);+*T1alV4psJiuh8A>S1}rR_Bn4{Y}C!Q9b@W#*1m)5fcIu}-z-SXh9mWf!vp zMdmO2ihOh6LW*8r&s{AHxEiN5tx35@p>LMb6=grZl()N*Gh#`n+@&T4`m>9fTyLl0 z>!s~-B;@BHwxg1+isUI{F25Ylm~}tBSS|-MmFXJu^GQH`RtRlo^Xss>bTSf;P3hM$ zQlEOn|8Pc@Y)|`mMUW9fBm>V9ov`$9UiF{hGI^3vYL=4ElAt!h9M5aLdS5g0$wf0| zpgK{Ka!(y;AY~B~F6(`=gcTR0uFT>>=|(GEba8>Sw7Wu}Om9M$OgQm@1~$6kLKsH> z*PdgAxM&<^Tu;`POS9uy3(EPy+v~X^>VS4wRXT%oheRE&&R{Sr!{iwc_U|<|ARMb8 z?KA|3O0%^?Za1$;0NE~Ui4<63Q(1<+lZU%3jY`!U5m<{*7?XS%Al&dyebxd z1T}5oG(LmHO;mBo=vO&8PemV34vMDC6o?IgW{i8fr{E2D9-&zR<|VZ)z5I;z3TT2z z3~s*|clQ*csMk@?;gmr~Z>-V9bvHCQzf^Nab-B4$D&|7si!*L^gV)D3IOFv06M=FG zJHFLGFVd1jLJGwrBl51$u|_VEDg{MEtAeTwsZ!AjLXIyItq?SnhZ+=yQpJuOl8{$L z^bM-JbbtKXZsip>rCGfesTYpPJHhTzboo$N*H@|^b5)Z2F7sK;G)&kfGr@{k3=KenirePSmVG9&N=KlWxef}zV6I=wcmZ10*?1Qav3dsEb zhr(;jvJFcc8<+ z9S%SX+=AYI9b5~tpTGy;FW@EcTo4^!bbDE2@CtY(JO`#=4-A0pH?RrTgXsFdL9f3R z{t-S5?}Jx>>^*Qgbim`_%joqVhrff1;R2Y0GhsWl!QcWvxLz(P>wO_5!=TS`nFlBf>Dh!-46RLUHsAYqc&$w&YPsFlWnk0a^ksPM26J zycmQ6uRRixifnA{e%Z9JSPm0fEFbXpxC||t3E8z?v@CfyF5Hq4cL%;O>rJSM6oFfX z#oTVuTl<*OvSs8gz9@FqaJytZ##-$5w!lergv6XC zBxeZXsaFYa#FkpAbE$m8oVIM~iA|OfvRnWmKME{y=$A_NuMn zNyN%CeyS#YcWYMGORgGeGlI7XZnfpfTA@IeA?8&^f zpr@CLxEpj8w~y^;Z|~5)PVr0_FP6YATvqIC@7&nhep0KAU=Ezz+P=9+E^Dcm?7}I_ zucECHFX84^TNbwRHy8BO)x0#=!f@s6ECs6$F@0$ zzlB|TVL|C)m9vn%-SVR=v*JfH5R;Q;57jD@OXzWS$YZOPoFd|p2vR9hP%()67)f82 zDhg%yl40#nh(6hoRwMXmS;lTC9~D0`q=Pg;;)qHrjVBgso9NEd= z9q8w>?*HxZY4|8y4VQ!L`}bS)@qdA@!RO$EAiDgO@OpSL9E3?Y6GWHa00G);ajbJz)+K-LI&B0K>;hJJq%{21N+4e(rOhYipMt?>WR*M9}S1X&aC zWAGYyHT)So17u8J)&^{X+tJ%^gzMoFcqRlOWB6}|XF&k+%)Tejt==bS@MBveY2{O9 zkgSxNl~`jtkgpWYo%i)7cQEHarb!Q5%vI#yw(?3PTSa_NZ(Bv|m`Y~uyT0{&-?}k| z^Y^VwwVWqJr24k>>~?~h+^VvZ+R#elm*P{Pqa~`1!ga27W_ial{W2%zY74$!W|yr0h=YA3>p7BCZ=&8xe!)3u z%1{H&klvm94y!@SLTnT2NCh;%!YnJS1fcLWH;81Imb_x7qXTG(l0noLVNvDbwtYxNlu~cEiG~nlQd^ z9h0tPpF6Y6y57S~t+O6A2PTZ6+@-eq*nEj@VXQt0iPtmf)f@4z8*er009kT_`ni;W zL=G~nOrGo=>NW|nJx#T^TSfUyYos>w=?UDax|??}Ho9*gYv4Knd!(hH=3=|!dgz;8 zO3l9x!`mb1F=iD~7sUk=Anu*s($keKC+0}V{ z%bsOd*-ljfsgZulJu=EKzs-zjc_iNGK0o#A>W%D2jO*sKH^5I6e{f&@)C&OWJwO=# zIHY!Bwd4J2<5ySqGOVwT#0K@%u}Qn|pZ?`fQBGR6jnSDt!q}rDt!LaC?Tb-fb7hrE zS*Dg}_y=J)j*WZLhs$`psZRce3A#&;PaH_?(+_vc1obfKWbV1G5|GgB{+waopODPd z{s>O);GY%yPLXPu$&n`FLI_@N|$h`@V->F248TbH5J0 ziS8}__5X#QE&lTlfES}rKN4jBzT@Eo=*}JRDRkm}a0@!{C*U%e1ld#XKgj$$LFVXp z!foio?}lY)2X$YOn&?;Wtp8WxlbVr~wAZnHoSj!xMwH}XJNlkHQ~yY0sJ?#C+LNK`A5m#!FZxt3a_G`fFI3vOi0gw5AakE& zVeg^x!SV9>YUSUV*m6{6393@)z%G%cvy+Jdc6Y0tdUq?arMAT%ZnGH=ZB2`jULQ=9 zZC#z4*>PFcUhX%!3$xMSyKuo45-wD%CaI#!;B-^b+-yqh zpsN#MPv2)GY{qR8oL%XwQIGu9lx=NQ%bAe!)>s1gU-#r zSH-)Us=Kj0v+AlEb@!I=Mzv0Xt-`B%Uyo>=x(9AKh~D!(`Wn%IM&BP-@^$_R*Gy=z zE+FHl#7Jq6bj#~rwK{o;oC*yii9z2;xEw#SY(F`oMuH&ZVh^%zKao>8emMKPPDb0+ za>R=S@syp&@f<%Qd$u($>}b7vb-C8Uj^_)QBfF6xhg}7>vljW`j9Mt0Om%Wq<3{*J zG+x}g$eg)m6?u2AldFiE;(pfnE!YZgox@g1y%|;!)!FzM5NW*V zoR{Y+;wllzUEbI&8_$AEK@AtIA|EaRJv35?ywEv*SbN|qE=^Ri_A*wD{{AKq{r}-g z`^OI*o&Q$20=9to0@%6!U%&?NZun~u-+*VrPUwI~g3SB>9(*3&3$p*8O5dct_O(_5 zYbCH&0&69(Rsw4!uvP+VC9qZkYbCH&0&67@DS^OK22*uxr#r^9=XAZ;g-%g+-MOS1 z>rN#URO3RCs(~7Bl~9503)h>BI~KFJpuY@7M3e0^o`-UAUpuew>T7K(NhjVc_gv?& zBXc1mInX91m!h_7MnX)^SGj>EajR$=8y?5uXt60iver_ZQ{!&UEgPF#ltow%IvpyP z7K<(M&@8LSP+#cS8y2Hl_Awq@0!y0g=qtNmW5{KYNDY#yd|EG$=xWR@dReCb2Eg~U A^Z)<= literal 0 HcmV?d00001 diff --git a/redshift/Appflyer_etl.sh b/redshift/Appflyer_etl.sh new file mode 100644 index 0000000..0bda622 --- /dev/null +++ b/redshift/Appflyer_etl.sh @@ -0,0 +1,289 @@ +!/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/redshift/sql_code/appflyer_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/appflyer_etl.sql + diff --git a/redshift/Product_metrics.sh b/redshift/Product_metrics.sh new file mode 100644 index 0000000..d89cc61 --- /dev/null +++ b/redshift/Product_metrics.sh @@ -0,0 +1,1051 @@ + +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.daily_recommendation_ingredients_split; + + create table raena_analytics.daily_recommendation_ingredients_split + as + select reseller_email ,sku,replace(replace(replace("ingredients",'{',''),'}',''),'\"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_ingredients; + +drop table raena_analytics.daily_recommendation_concerns_split; + + create table raena_analytics.daily_recommendation_concerns_split + as + select reseller_email ,sku,replace(replace(replace("Concerns" ,'{',''),'}',''),'\"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_concerns drc ; + + +DROP TABLE raena_analytics.product_metrics_calendar; + +CREATE TABLE raena_analytics.product_metrics_calendar AS +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type +FROM raena_order_management.order; + + +drop table if exists raena_analytics.order_base_for_user_type; + +create table raena_analytics.order_base_for_user_type +as +SELECT replace(json_extract_path_text(reseller_info,'mobile',true),'+','') reseller_mobile, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.user_type_table ; + + +CREATE TABLE raena_analytics.user_type_table AS +SELECT DISTINCT profile_phone reseller_mobile, + First_transaction_date, + min_created_date first_install_date +FROM raena_analytics.order_base_for_user_type A +INNER JOIN + (SELECT profile_phone, + min(created_at) min_created_date + FROM + (SELECT profile_phone, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) created_at + FROM clevertap.app_installed + GROUP BY profile_phone + UNION + SELECT replace(mobile,'+','') profile_phone, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1) A + GROUP BY 1) B ON A.reseller_mobile=B.profile_phone; + + + +DROP TABLE IF EXISTS raena_analytics.total_uninstalled; + + +CREATE TABLE raena_analytics.total_uninstalled AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM (SELECT profile_objectid, + CASE + WHEN profile_phone<>'' THEN profile_phone + END profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_uninstalled) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + +DROP TABLE IF EXISTS raena_analytics.total_installed; + + +CREATE TABLE raena_analytics.total_installed AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2; + + DROP TABLE raena_analytics.Active_user; + + +CREATE TABLE raena_analytics.Active_user AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',coalesce(first_install_date,D.created_at::date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND (date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date + OR First_transaction_date IS NULL) THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND first_transaction_date IS NULL THEN 'New' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.order_base_for_user_type B ON A.profile_phone = B.reseller_mobile +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + +DROP TABLE IF EXISTS raena_analytics.total_view_item; + + +CREATE TABLE raena_analytics.total_view_item AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + count(1) total_event , + A.profile_objectid +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 4; + + +drop table if exists raena_analytics.order_base_user_transaction; + +create table raena_analytics.order_base_user_transaction +as +SELECT reseller_id , + replace(json_extract_path_text(reseller_info,'mobile',true),'+','') profile_phone, + created_at::date transaction_date, + count(DISTINCT id) total_orders, + sum(payment_amount) AS revenue + FROM raena_order_management.order + WHERE payment_status='Paid' + AND is_campaign = 'false' + AND cast(is_archived AS varchar)='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2, + 3; + + +DROP TABLE IF EXISTS raena_analytics.total_first_transacting_user; + + +CREATE TABLE raena_analytics.total_first_transacting_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + profile_phone, + count(1) total_event , + Count(DISTINCT A.reseller_id) Total_users, + sum(total_orders) total_orders, + sum(revenue) revenue +FROM raena_analytics.order_base_user_transaction A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +GROUP BY 1, + 2, + 3; + +DROP TABLE IF EXISTS raena_analytics.post_discount_gm; + + +CREATE TABLE raena_analytics.post_discount_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',B.first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',B.first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',B.First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(discounted_price*quantity) discounted_price, + sum(cogs*quantity) cogs, + count(DISTINCT external_id) AS Number_of_orders +FROM raena_analytics.OM_GM_DB_Product_category A +LEFT JOIN raena_analytics.user_type_table B ON replace(A.reseller_mobile,'+','') =B.reseller_mobile +WHERE is_campaign='false' + AND order_placed_by='App' +GROUP BY 1, + 2; + +drop table if exists raena_analytics.Active_user_for_mau_stage; + +CREATE TABLE raena_analytics.Active_user_for_mau_stage AS + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed + union + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + 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; + + + +DROP TABLE IF EXISTS raena_analytics.Active_user_for_mau; + + +CREATE TABLE raena_analytics.Active_user_for_mau AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',(first_install_date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date>=date_trunc('Month',first_install_date)::date + AND (date_trunc('Month',A.transaction_date)::date <= date_trunc('Month',First_transaction_date)::date + OR date_trunc('Month',First_transaction_date)::date IS NULL) THEN 'Activation' + ELSE 'Existing' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM raena_analytics.Active_user_for_mau_stage A +LEFT JOIN + (SELECT profile_objectid profile_objectid, + cast(min(transaction_date) AS DATE) First_transaction_date + FROM + (SELECT profile_objectid, (cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) transaction_date + FROM clevertap.charged) A + GROUP BY 1) B ON A.profile_objectid = B.profile_objectid +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid --LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + +/* +DROP TABLE IF EXISTS raena_analytics.lm_revenue_gm; + + +CREATE TABLE raena_analytics.lm_revenue_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(CASE WHEN a.external_id = BB.order_id + AND a.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount, + sum(a.discounted_price*a.quantity) discounted_price +FROM + (SELECT * + FROM raena_analytics.OM_GM_DB_Product_category + WHERE order_placed_by ='reseller' + AND is_campaign='false') A +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON a.external_id = BB.order_id +AND a.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON a.external_id = CC.external_id +AND a.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + +DROP TABLE IF EXISTS raena_analytics.gm_order_count; + + +CREATE TABLE raena_analytics.gm_order_count AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(Customer_order) Customer_order, + sum(Marketplace_order)Marketplace_order, + sum(total_dropship_order) total_dropship_order, + sum(total_order) total_order +FROM + (SELECT transaction_date, + reseller_mobile, + count(DISTINCT CASE WHEN order_recipient= 'Customer' THEN external_id END) AS Customer_order, + count(DISTINCT CASE WHEN order_recipient= 'Marketplace' THEN external_id END) AS Marketplace_order, + count(DISTINCT CASE WHEN order_recipient IN ('Customer', 'Marketplace') THEN external_id END) total_dropship_order, + count(DISTINCT external_id) total_order + FROM OM_GM_DB_Product_category + WHERE is_campaign='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2) A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; +*/ + +DROP TABLE IF EXISTS raena_analytics.post_discount_dsf_revenue; + + +CREATE TABLE raena_analytics.post_discount_dsf_revenue AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(payment_amount) discounted_price, + count(DISTINCT id) AS Number_of_orders, + count(DISTINCT reseller_id) AS total_users +FROM + (SELECT reseller_id , + external_id id , + discounted_price*quantity payment_amount , + reseller_mobile, + transaction_date + FROM raena_analytics.gm_dashboard + WHERE is_campaign='false' + AND lower(external_id) LIKE '%dsf') A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile =B.reseller_mobile +GROUP BY 1, + 2; + +----OM_Events_user_type;created in db_funnel report + + + +select max(transaction_date) uninstalled_td from raena_analytics.total_uninstalled; +select max(transaction_date) installed_td from raena_analytics.total_installed; +select max(transaction_date) launched_td from raena_analytics.Active_user; +select max(transaction_date) view_td from raena_analytics.total_view_item; +select max(transaction_date) gm_td from raena_analytics.lm_revenue_gm; + + + +DROP TABLE IF EXISTS raena_analytics.overall_conversion_trend; + + +CREATE TABLE raena_analytics.overall_conversion_trend AS +SELECT date_trunc('day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Year'frequency, + A.user_type , + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2 ) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS raena_analytics.overall_converting_user_trend; + + +CREATE TABLE raena_analytics.overall_converting_user_trend AS +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Quarter'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN(SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Month'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Week'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_time, + 'Day'frequency, + A.user_type , + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + GROUP BY 1, + 2) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +GROUP BY 1, + 2, + 3, + 4 ; + +DROP TABLE IF EXISTS raena_analytics.revenue_trend; + + +CREATE TABLE raena_analytics.revenue_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + sum(revenue) Total_revenue +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + sum(revenue) +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE IF EXISTS raena_analytics.arpu_trend; + + +CREATE TABLE raena_analytics.arpu_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(sum(revenue) AS decimal(22,4)) total_revenue, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4 ; + + +DROP TABLE IF EXISTS raena_analytics.install_uninstall_ratio_trend; + + +CREATE TABLE raena_analytics.install_uninstall_ratio_trend AS +SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_year, + 'Year'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Quarter'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Month'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Week'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Day'frequency, + raena_analytics.product_metrics_calendar.user_type , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +GROUP BY 1, + 2, + 3, + 4; + + +DROP TABLE IF EXISTS raena_analytics.mau_trend; + + +CREATE TABLE raena_analytics.mau_trend AS +SELECT date_trunc('Year',transaction_date)::date AS transaction_date, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL + +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4 +UNION ALL +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Day'frequency, + user_type , + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) total_revenue +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4; + + +" > /home/ec2-user/cronjob/redshift/sql_code/product_matrics_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/product_matrics_etl.sql + + diff --git a/redshift/Product_metrics_2.sh b/redshift/Product_metrics_2.sh new file mode 100644 index 0000000..297b262 --- /dev/null +++ b/redshift/Product_metrics_2.sh @@ -0,0 +1,1882 @@ + +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.daily_recommendation_ingredients_split; + + create table raena_analytics.daily_recommendation_ingredients_split + as + select reseller_email ,sku,replace(replace(replace("ingredients",'{',''),'}',''),'\"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_ingredients; + +drop table raena_analytics.daily_recommendation_concerns_split; + + create table raena_analytics.daily_recommendation_concerns_split + as + select reseller_email ,sku,replace(replace(replace("Concerns" ,'{',''),'}',''),'\"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_concerns drc ; + + +DROP TABLE raena_analytics.product_metrics_calendar; + +CREATE TABLE raena_analytics.product_metrics_calendar AS +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type, + '<=2M'bucket +FROM raena_order_management.order +union +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type, + '>10M'bucket +FROM raena_order_management.order +union +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type, + '2M-10M'bucket +FROM raena_order_management.order +union +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type, + 'Na'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type, + '<=2M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type, + '>10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type, + '2M-10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type, + 'Na'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type, + '<=2M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type, + '>10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type, + '2M-10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type, + 'Na'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type, + '<=2M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type, + '>10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type, + '2M-10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type, + 'Na'bucket +FROM raena_order_management.order; + +DROP TABLE raena_analytics.cohort_level_reseller; + + +CREATE TABLE raena_analytics.cohort_level_reseller AS +SELECT reseller_id , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M-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',created_at)::date trnsaction_mount, + sum(payment_amount)payment_amount + FROM raena_order_management.order + GROUP BY 1, + 2) A + GROUP BY 1) B ; + + +drop table if exists raena_analytics.order_base_for_user_type; + +create table raena_analytics.order_base_for_user_type +as +SELECT replace(json_extract_path_text(reseller_info,'mobile',true),'+','') reseller_mobile, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.user_type_table ; + + +CREATE TABLE raena_analytics.user_type_table AS +SELECT DISTINCT profile_phone reseller_mobile, + First_transaction_date, + min_created_date first_install_date +FROM raena_analytics.order_base_for_user_type A +INNER JOIN + (SELECT profile_phone, + min(created_at) min_created_date + FROM + (SELECT profile_phone, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) created_at + FROM clevertap.app_installed + GROUP BY profile_phone + UNION + SELECT replace(mobile,'+','') profile_phone, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1) A + GROUP BY 1) B ON A.reseller_mobile=B.profile_phone; + + +DROP TABLE IF EXISTS raena_analytics.total_registered_user; + + +CREATE TABLE raena_analytics.total_registered_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + bucket, + transaction_date, + Count(DISTINCT id) Total_users +FROM (SELECT distinct id ,replace(mobile,'+','') mobile, + created_at::date transaction_date + FROM raena_user_management.user) A +LEFT JOIN raena_analytics.user_type_table B ON A.mobile = B.reseller_mobile +left join raena_analytics.cohort_level_reseller on A.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2,3; + + + +DROP TABLE IF EXISTS raena_analytics.total_uninstalled; + + +CREATE TABLE raena_analytics.total_uninstalled AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + bucket, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM (SELECT profile_objectid, + CASE + WHEN profile_phone<>'' THEN profile_phone + END profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_uninstalled) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_user_management.user C on A.profile_phone = replace(C.mobile,'+','') +left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2,3; + + +DROP TABLE IF EXISTS raena_analytics.total_installed; + + +CREATE TABLE raena_analytics.total_installed AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + bucket, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_user_management.user C on A.profile_phone = replace(C.mobile,'+','') +left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2,3; + + DROP TABLE raena_analytics.Active_user; + + +CREATE TABLE raena_analytics.Active_user AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',coalesce(first_install_date,D.created_at::date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND (date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date + OR First_transaction_date IS NULL) THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND first_transaction_date IS NULL THEN 'New' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END user_type, + transaction_date, + count(distinct profile_phone) total_event , + A.profile_phone, + A.profile_objectid, + bucket +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.order_base_for_user_type B ON A.profile_phone = B.reseller_mobile +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +left join raena_analytics.cohort_level_reseller on D.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8,9; + +DROP TABLE IF EXISTS raena_analytics.total_view_item; + + +CREATE TABLE raena_analytics.total_view_item AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + bucket, + count(distinct profile_objectid) total_event, + profile_phone +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_user_management.user C on A.profile_phone = replace(C.mobile,'+','') +left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2, + 3,5; + + + +DROP TABLE IF EXISTS raena_analytics.total_checkout; + + +CREATE TABLE raena_analytics.total_checkout AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + bucket, + count(distinct profile_objectid) total_event, + profile_phone +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_user_management.user C on A.profile_phone = replace(C.mobile,'+','') +left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2, + 3,5; + + +drop table if exists raena_analytics.order_base_user_transaction; + +create table raena_analytics.order_base_user_transaction +as +SELECT reseller_id , + replace(reseller_mobile,'+','') profile_phone, + transaction_date::date transaction_date, + count(DISTINCT external_id ) total_orders, + sum(quantity*discounted_price) AS revenue, + sum((retail_price*quantity)-(seller_margin*quantity)) as Pre_discount_revenue, + sum(quantity) total_quantity + FROM raena_analytics.gm_dashboard + WHERE is_campaign = 'false' + AND order_placed_by ='App' + GROUP BY 1, + 2, + 3; + + +DROP TABLE IF EXISTS raena_analytics.total_first_transacting_user; + + +CREATE TABLE raena_analytics.total_first_transacting_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + profile_phone, + bucket, + count(1) total_event , + Count(DISTINCT A.reseller_id) Total_users, + sum(total_orders) total_orders, + sum(revenue) revenue, + sum(Pre_discount_revenue)Pre_discount_revenue, + sum(total_quantity) total_quantity +FROM raena_analytics.order_base_user_transaction A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_analytics.cohort_level_reseller on A.reseller_id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2, + 3,4; + + +drop table if exists raena_analytics.base_matrics_table; + +CREATE TABLE raena_analytics.base_matrics_table AS +SELECT A.transaction_date, + A.user_type, + A.bucket, + A.install , + B.register, + avg_dau, + avg_wau, + avg_mau, + pdp_unique, + add_to_cart, + checkout, + transacting_user, + gmv, + uninstall +FROM + (SELECT date_trunc('Month',transaction_date)::date transaction_date , + user_type, + bucket, + Sum(Total_users)install + FROM raena_analytics.total_installed + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + Sum(total_users) register + FROM raena_analytics.total_registered_user + GROUP BY 1, + 2, + 3)B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + sum(total_user) /count(DISTINCT transaction_date) avg_dau + FROM + (SELECT transaction_date , + user_type, + bucket , + count(DISTINCT profile_phone) total_user + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A + GROUP BY 1, + 2, + 3) C ON A.transaction_date = C.transaction_date +AND A.user_type = C.user_type +AND A.bucket = C.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + sum(total_user) /count(DISTINCT transaction_date) avg_wau + FROM + (SELECT date_trunc('Week',transaction_date)::date transaction_date, + user_type, + bucket , + count(DISTINCT profile_phone) total_user + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) + GROUP BY 1, + 2, + 3) D ON A.transaction_date = D.transaction_date +AND A.user_type = D.user_type +AND A.bucket = D.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket , + count(DISTINCT profile_phone) avg_mau + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) E ON A.transaction_date = E.transaction_date +AND A.user_type = E.user_type +AND A.bucket = E.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + count(distinct profile_phone) pdp_unique + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) F ON A.transaction_date = F.transaction_date +AND A.user_type = F.user_type +AND A.bucket = F.bucket +LEFT JOIN + (SELECT date_trunc('Month',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) add_to_cart + FROM raena_analytics.OM_Events_user_type + LEFT JOIN raena_user_management.user C ON OM_Events_user_type.phone = replace(C.mobile,'+','') + LEFT JOIN raena_analytics.cohort_level_reseller ON C.id = cohort_level_reseller.reseller_id + WHERE events = 'add_to_cart' + GROUP BY 1, + 2, + 3) G ON A.transaction_date = G.transaction_date +AND A.user_type = G.user_type +AND A.bucket = G.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + count(distinct profile_phone) checkout + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) H ON A.transaction_date = H.transaction_date +AND A.user_type = H.user_type +AND A.bucket = H.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) transacting_user, + sum(Pre_discount_revenue) gmv + FROM raena_analytics.total_first_transacting_user + GROUP BY 1, + 2, + 3) I ON A.transaction_date = I.transaction_date +AND A.user_type = I.user_type +AND A.bucket = I.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date , + user_type, + bucket, + Sum(Total_users)uninstall + FROM raena_analytics.total_uninstalled + GROUP BY 1, + 2, + 3) J ON A.transaction_date = J.transaction_date +AND A.user_type = j.user_type +AND A.bucket = j.bucket; + +DROP TABLE IF EXISTS raena_analytics.post_discount_gm; + + +CREATE TABLE raena_analytics.post_discount_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',B.first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',B.first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',B.First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + cohort bucket, + sum(discounted_price*quantity) discounted_price, + sum(cogs*quantity) cogs, + count(DISTINCT external_id) AS Number_of_orders +FROM raena_analytics.OM_GM_DB_Product_category A +LEFT JOIN raena_analytics.user_type_table B ON replace(A.reseller_mobile,'+','') =B.reseller_mobile +WHERE is_campaign='false' + AND order_placed_by='App' +GROUP BY 1, + 2,3; + +drop table if exists raena_analytics.Active_user_for_mau_stage; + +CREATE TABLE raena_analytics.Active_user_for_mau_stage AS + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed + union + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + 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; + + + +DROP TABLE IF EXISTS raena_analytics.Active_user_for_mau; + + +CREATE TABLE raena_analytics.Active_user_for_mau AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',(first_install_date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date>=date_trunc('Month',first_install_date)::date + AND (date_trunc('Month',A.transaction_date)::date <= date_trunc('Month',First_transaction_date)::date + OR date_trunc('Month',First_transaction_date)::date IS NULL) THEN 'Activation' + ELSE 'Existing' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM raena_analytics.Active_user_for_mau_stage A +LEFT JOIN + (SELECT profile_objectid profile_objectid, + cast(min(transaction_date) AS DATE) First_transaction_date + FROM + (SELECT profile_objectid, (cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) transaction_date + FROM clevertap.charged) A + GROUP BY 1) B ON A.profile_objectid = B.profile_objectid +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid --LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + +/* +DROP TABLE IF EXISTS raena_analytics.lm_revenue_gm; + + +CREATE TABLE raena_analytics.lm_revenue_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(CASE WHEN a.external_id = BB.order_id + AND a.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount, + sum(a.discounted_price*a.quantity) discounted_price +FROM + (SELECT * + FROM raena_analytics.OM_GM_DB_Product_category + WHERE order_placed_by ='reseller' + AND is_campaign='false') A +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON a.external_id = BB.order_id +AND a.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON a.external_id = CC.external_id +AND a.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + +DROP TABLE IF EXISTS raena_analytics.gm_order_count; + + +CREATE TABLE raena_analytics.gm_order_count AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(Customer_order) Customer_order, + sum(Marketplace_order)Marketplace_order, + sum(total_dropship_order) total_dropship_order, + sum(total_order) total_order +FROM + (SELECT transaction_date, + reseller_mobile, + count(DISTINCT CASE WHEN order_recipient= 'Customer' THEN external_id END) AS Customer_order, + count(DISTINCT CASE WHEN order_recipient= 'Marketplace' THEN external_id END) AS Marketplace_order, + count(DISTINCT CASE WHEN order_recipient IN ('Customer', 'Marketplace') THEN external_id END) total_dropship_order, + count(DISTINCT external_id) total_order + FROM OM_GM_DB_Product_category + WHERE is_campaign='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2) A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; +*/ + +DROP TABLE IF EXISTS raena_analytics.post_discount_dsf_revenue; + + +CREATE TABLE raena_analytics.post_discount_dsf_revenue AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(payment_amount) discounted_price, + count(DISTINCT id) AS Number_of_orders, + count(DISTINCT reseller_id) AS total_users +FROM + (SELECT reseller_id , + external_id id , + discounted_price*quantity payment_amount , + reseller_mobile, + transaction_date + FROM raena_analytics.gm_dashboard + WHERE is_campaign='false' + AND lower(external_id) LIKE '%dsf') A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile =B.reseller_mobile +GROUP BY 1, + 2; + +----OM_Events_user_type;created in db_funnel report + + + +select max(transaction_date) uninstalled_td from raena_analytics.total_uninstalled; +select max(transaction_date) installed_td from raena_analytics.total_installed; +select max(transaction_date) launched_td from raena_analytics.Active_user; +select max(transaction_date) view_td from raena_analytics.total_view_item; +select max(transaction_date) gm_td from raena_analytics.lm_revenue_gm; + + + +DROP TABLE IF EXISTS raena_analytics.overall_conversion_trend; + + +CREATE TABLE raena_analytics.overall_conversion_trend AS +SELECT date_trunc('day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5; + + + +DROP TABLE IF EXISTS raena_analytics.average_dau_trend; + + +CREATE TABLE raena_analytics.average_dau_trend AS +SELECT A.transaction_date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('Day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('Day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('Day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('Day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS raena_analytics.overall_converting_user_trend; + + +CREATE TABLE raena_analytics.overall_converting_user_trend AS +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5; + + +DROP TABLE IF EXISTS raena_analytics.arpu_trend; + + +CREATE TABLE raena_analytics.arpu_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 ; + + + + +DROP TABLE IF EXISTS raena_analytics.pdp_trend; + + +CREATE TABLE raena_analytics.pdp_trend AS +SELECT A.transaction_date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5; + + + +DROP TABLE IF EXISTS raena_analytics.add_to_cart_trend; + + +CREATE TABLE raena_analytics.add_to_cart_trend AS +SELECT A.transaction_date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5; + + + +DROP TABLE IF EXISTS raena_analytics.checkout_trend; + + +CREATE TABLE raena_analytics.checkout_trend AS +SELECT A.transaction_date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5; +DROP TABLE IF EXISTS raena_analytics.install_uninstall_ratio_trend; + + +CREATE TABLE raena_analytics.install_uninstall_ratio_trend AS +SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_year, + 'Year'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Quarter'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Month'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Week'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Day'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5; + + +DROP TABLE IF EXISTS raena_analytics.mau_trend; + + +CREATE TABLE raena_analytics.mau_trend AS +SELECT date_trunc('Year',transaction_date)::date AS transaction_date, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Day'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5; + + +" > /home/ec2-user/cronjob/redshift/sql_code/product_matrics_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/product_matrics_etl.sql + + diff --git a/redshift/am_dashboard.sh b/redshift/am_dashboard.sh new file mode 100644 index 0000000..d7a121e --- /dev/null +++ b/redshift/am_dashboard.sh @@ -0,0 +1,222 @@ + #!/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/redshift/sql_code/am_dashboard.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/am_dashboard.sql + diff --git a/redshift/am_recommendation.sh b/redshift/am_recommendation.sh new file mode 100644 index 0000000..8a0e8bc --- /dev/null +++ b/redshift/am_recommendation.sh @@ -0,0 +1,2880 @@ + #!/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 raena_analytics.raw_ingredient_split_name; +CREATE TABLE raena_analytics.raw_ingredient_split_name AS +SELECT * , + split_part(concern,', ',1) concern1, + split_part(concern,', ',2) concern2, + split_part(concern,', ',3) concern3, + split_part(concern,', ',4) concern4, + split_part(concern,', ',5) concern5, + split_part(top_marketing_ingredients ,', ',1) ingredient1, + split_part(top_marketing_ingredients,', ',2) ingredient2, + split_part(top_marketing_ingredients,', ',3) ingredient3, + split_part(top_marketing_ingredients,', ',4) ingredient4, + split_part(top_marketing_ingredients,', ',5) ingredient5, + split_part(top_marketing_ingredients,', ',6) ingredient6, + split_part(top_marketing_ingredients,', ',7) ingredient7, + split_part(top_marketing_ingredients,', ',8) ingredient8, + split_part(top_marketing_ingredients,', ',9) ingredient9, + split_part(top_marketing_ingredients,', ',10) ingredient10, + split_part(top_marketing_ingredients,', ',11) ingredient11 +FROM + (SELECT DISTINCT group_base, + sku , + lower(concern) concern, + lower(top_marketing_ingredients) top_marketing_ingredients, + CASE + WHEN concern='' + AND top_marketing_ingredients='' THEN 1 + END flag + FROM raena_analytics.ingredients_base_table_final) A +WHERE flag IS NULL; + + DROP TABLE raena_analytics.raw_ingredient_mapping_table_stage; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage AS +SELECT A.* , + B.group_base mapping_group_base, + B.concern mapping_concerns, + B.top_marketing_ingredients mapping_ingredients +FROM raena_analytics.raw_ingredient_split_name A +LEFT JOIN + (SELECT * + FROM raena_analytics.raw_ingredient_split_name) B ON 1=1; + +DROP TABLE if exists raena_analytics.raw_ingredient_mapping_table_stage2; + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage2 AS +SELECT * , + CASE + WHEN raena_analytics.udf_string_campare(concern1,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern1,mapping_concerns) <> 'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern1, + CASE + WHEN raena_analytics.udf_string_campare(concern2,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern2,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern2, + CASE + WHEN raena_analytics.udf_string_campare(concern3,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern3,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern3, + CASE + WHEN raena_analytics.udf_string_campare(concern4,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern4,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern4, + CASE + WHEN raena_analytics.udf_string_campare(concern5,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern5,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern5, + REGEXP_COUNT(concern ,', ')+1 total_count_concern, + CASE + WHEN raena_analytics.udf_string_campare(ingredient1,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient1,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient1, + CASE + WHEN raena_analytics.udf_string_campare(ingredient2,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient2,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient2, + CASE + WHEN raena_analytics.udf_string_campare(ingredient3,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient3,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient3, + CASE + WHEN raena_analytics.udf_string_campare(ingredient4,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient4,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient4, + CASE + WHEN raena_analytics.udf_string_campare(ingredient5,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient5,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient5, + CASE + WHEN raena_analytics.udf_string_campare(ingredient6,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient6,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient6, + CASE + WHEN raena_analytics.udf_string_campare(ingredient7,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient7,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient7, + CASE + WHEN raena_analytics.udf_string_campare(ingredient8,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient8,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient8, + CASE + WHEN raena_analytics.udf_string_campare(ingredient9,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient9,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient9, + CASE + WHEN raena_analytics.udf_string_campare(ingredient10,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient10,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient10, + CASE + WHEN raena_analytics.udf_string_campare(ingredient11,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient11,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient11, + REGEXP_COUNT(top_marketing_ingredients ,', ')+1 total_count_ingredient +FROM raena_analytics.raw_ingredient_mapping_table_stage +WHERE group_base <> mapping_group_base; + + DROP TABLE if exists raena_analytics.raw_ingredient_mapping_table_stage3; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage3 AS +SELECT *, + flag_concern1+flag_concern2+flag_concern3+flag_concern3+flag_concern3 total_concern_match, + flag_ingredient1+flag_ingredient2+flag_ingredient3+flag_ingredient4+flag_ingredient5+flag_ingredient6+flag_ingredient7+flag_ingredient8+flag_ingredient9+flag_ingredient10+flag_ingredient11 total_ingredient_match +FROM raena_analytics.raw_ingredient_mapping_table_stage2 ; + + +DROP TABLE IF EXISTS raena_analytics.raw_ingredient_mapping_table_stage4; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage4 AS +SELECT *, (total_concern_match*100/total_count_concern) Percent_concern_match, (total_ingredient_match*100/total_count_ingredient) Percent_ingredient_match , + CASE WHEN concern='' AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND top_marketing_ingredients='' THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + END percent_flag +FROM raena_analytics.raw_ingredient_mapping_table_stage3; + + +Drop table if exists raena_analytics.raw_ingredient_mapping_table; + +Create table raena_analytics.raw_ingredient_mapping_table +as +SELECT group_base, + sku, + concern, + top_marketing_ingredients, + mapping_group_base, + mapping_concerns, + mapping_ingredients, + percent_concern_match, + Percent_ingredient_match +FROM raena_analytics.raw_ingredient_mapping_table_stage4 +WHERE percent_flag=1 +ORDER BY group_base , + percent_concern_match DESC , + Percent_ingredient_match DESC; + + +DROP TABLE IF EXISTS raena_analytics.sku_in_stock; + + +CREATE TABLE raena_analytics.sku_in_stock AS +SELECT sku , + sum(coalesce(F.stock_limit,0)) 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; + + --------------------------------------------------------------task 1 ---------------- + +drop table if exists raena_analytics.recommendation_power_seller_base_alt; + +create table raena_analytics.recommendation_power_seller_base_alt +as +select distinct email from raena_analytics.om_acquisition_mapping_list +union select distinct \"email id\" from raena_analytics.AM_REVENUE_LEAKAGE_LIST ; + + +INSERT INTO raena_analytics.recommendation_power_seller_base_alt + values ('cindyvioleta29@yahoo.com'), +('sherryfannydesita@gmail.com'), +('masintansilalahi@yahoo.co.id'), +('rumahskincarebpp@gmail.com'), +('puturisca@gmail.com'), +('sieny.go@gmail.com'), +('oki.zhang1988@gmail.com'), +('jeremytantono@yahoo.co.id'), +('yuke.cipta@gmail.com'), +('kwonvic@hotmail.com'), +('ervinafransiska95@gmail.com'), +('indahamaliasasmita@gmail.com'), +('mirosebeauty@gmail.com'), +('fourrshoptarakan@gmail.com'), +('stuffbyms@gmail.com'), +('cindyrissa14@gmail.com'), +('lieke1212@gmail.com'), +('jelitacosmetic.yk@gmail.com'), +('metafuji@gmail.com'), +('destiyapradhani@gmail.com'), +('liepinghau@gmail.com'), +('jennifer_kambuno@yahoo.com'), +('y35beauty@gmail.com'), +('nisamulya@gmail.com'), +('issadella@yahoo.co.id'), +('hartono.henny@yahoo.co.id'), +('a2335572@gmail.com'), +('navisgusmiati24@gmail.com'), +('djiwandimelany@gmail.com'), +('colinakoh178@gmail.com'), +('fairybeaute17@gmail.com'), +('fajri.vanbio95@gmail.com'), +('zidney_yao@yahoo.com'), +('chandraalex305@gmail.com'), +('elly.elisha@gmail.com'), +('erlinapr13@gmail.com'), +('mechan_06@hotmail.com'), +('limvincents@hotmail.com'), +('tjuganteng@gmail.com'), +('sralaydrus96@yahoo.com'), +('duwik_tonx@yahoo.co.id'), +('adelshopadelia@gmail.com'), +('shierlysusanto2405@gmail.com'), +('murni.cosmetics55@gmail.com'), +('stg_purchasing_melisa@yahoo.com'), +('asri.nilasari@watsons.co.id'), +('wayanndiana@gmail.com'), +('emailtedy@gmail.com'), +('Shantifm@hotmail.com'), +('shintachristiani2911@gmail.com'), +('dotienigloo@gmail.com'), +('mirandadessy95@gmail.com'), +('pangeranpetirr@gmail.com'), +('mariamellysacandra@ymail.com'), +('evitamalaa@ymail.com'), +('lindadarwinp@gmail.com'), +('dyfabandung@gmail.com'), +('yestastore@gmail.com'), +('outletmadanibeauty@gmail.com'), +('mrs.kumara@yahoo.com'), +('cindylum23@gmail.com'), +('fairy_love90@ymail.com'), +('fajarcosmetikpekanbaru@gmail.com'), +('alapstorekosmetik@gmail.com'), +('marlinabahnur@yahoo.co.id'), +('miswarderi61@gmail.com'), +('kristian_tjia@yahoo.co.id'), +('dessi.three@gmail.com'), +('rumahkurumahta@gmail.com'), +('nabilaalifia2000@gmail.com'), +('ullyfina@gmail.com'), +('tjindaidyandy@gmail.com'), +('shintayuu@gmail.com'), +('ratihpuji001@gmail.com'), +('shynobyshould@gmail.com'), +('rismawatisapu101@gmail.com'), +('caramelmadeby@gmail.com'), +('piyamapoppy@gmail.com'), +('scarlettwhitening.manado@gmail.com'), +('makeupbliss.id@gmail.com'), +('agnyjmpardosi@gmail.com'), +('sherbeautee@gmail.com'), +('yfbeauty99@gmail.com'), +('cmstadulako36@gmail.com'), +('lizalgs97@gmail.com'), +('emailnyawabisabi@gmail.com'), +('alvhiraalamrih@gmail.com'), +('megasyaf10@gmail.com'), +('ismisarisapitri6@gmail.com'), +('realrima91@gmail.com'), +('hestynorata74@gmail.com'), +('tokopinkbox@gmail.com'), +('melly.angelica@sils.co.id'), +('baguzokee@gmail.com'), +('vandy_hood@yahoo.com'), +('stevenwerencius15@gmail.com'), +('qurniaulan123@gmail.com'), +('ivanalystia21@gmail.com'), +('claralverinas@gmail.com'), +('riaarddd@gmail.com'), +('yayz_bubble@yahoo.com'), +('elsa.rivani@rocketmail.com'), +('feyrenpusura@gmail.com'), +('gunz_r@yahoo.com'), +('asrularman077@gmail.com'), +('hellojuwitaang@gmail.com'), +('bernike.daniel@gmail.com'), +('ayuerawati1993@gmail.com'), +('sudutcantik03@gmail.com'), +('norio.agnes@gmail.com'), +('smarch2u@gmail.com'), +('nunungokta8@gmail.com'), +('siaocie.tw@gmail.com'), +('sandrachen20@icloud.com'), +('siti.komariiah@gmail.com'), +('gustine.fu999@gmail.com'), +('ennysetiono@msn.com'), +('sartikanganro@gmail.com'), +('mirandahabibie@gmail.com'), +('tokodahliapadang@gmail.com'), +('natanaelsbrn@gmail.com'), +('jelitacosmetic@galeri.com'), +('rkkosmetikviral.anj@gmail.com'), +('sancaytulak26@gmail.com'), +('kelvinhosen@gmail.com'), +('elnazulma@gmail.com'), +('jevrihuang@gmail.com'), +('araitsoke@gmail.com'), +('haju.nutrifood@gmail.com'), +('harwindaharis99@gmail.com'), +('saherasukur92@gmail.com'), +('monamelosi05@gmail.com'), +('pramistiseptarinda@gmail.com'), +('rianadewi15746@gmail.com'), +('frivaldi08@gmail.com'), +('sausankho86@gmail.com'), +('3widjayanti@gmail.com'), +('prinzlina79@gmail.com'), +('restbeauty11@gmail.com'), +('kiarriyasatul@gmail.com'), +('surkiaa56@gmail.com'), +('dellikosmetik@gmail.com'), +('dinafebriana@gmail.com'), +('risnawatiyunus@yahoo.com'), +('pudoopy@gmail.com'), +('serbunganjuk@gmail.com'), +('linda_ho29@yahoo.com'), +('cassmgadm@gmail.com'), +('jennychateriner@gmail.com'), +('thasashop.id@yahoo.com'), +('diancagoodsbywitari@gmail.com'), +('chang3mu@icloud.com'), +('hana_kho@outlook.com'), +('jwl.skinofficial@gmail.com'), +('febri.ilham68@gmail.com'), +('gurl.shopaholic@gmail.com'), +('fenitan00@gmail.com'), +('nadrachanafie@gmail.com'), +('migibeauty1@gmail.com'), +('dewisusianti888@gmail.com'), +('nazwinadya@gmail.com'), +('andinimaylasari@gmail.com'), +('syahrani.cosmetic@gmail.com'), +('primasitaa@gmail.com'), +('spy_solution77@yahoo.com'), +('geeghina@gmail.com'), +('nida.syafa5@gmail.com'), +('financialelzamora@gmail.com'), +('irkosmetikdanskincare@gmail.com'), +('sherlinhosana@gmail.com'), +('sepdiarina@yahoo.com'), +('lumbangaol.ira@gmail.com'), +('aligunawan098@gmail.com'), +('ovarash@gmail.com'), +('rbeauty.storee@gmail.com'), +('cckosmetik77@gmail.com'), +('pinkcosmetich@gmail.com'), +('dhiniemsa@yahoo.com'), +('rulyindah1911@gmail.com'), +('elvina.6789@gmail.com'), +('moeyecosmetic99@gmail.com'), +('sana.19007@mhs.unesa.ac.id'), +('ikasusanti_baru@yahoo.com'), +('indaryani82@gmail.com'), +('hsyapon@gmail.com'), +('citra.ismaya@omniretail.co'), +('thevintagestrawberry@gmail.com'), +('ramadhani.kosmetik@gmail.com'), +('linawijaya2712@yahoo.com'), +('meilings0403@gmail.com'), +('nadyawkk@gmail.com'), +('prellasurabaya@gmail.com'), +('ziyyags@gmail.com'), +('stanley_oh@ymail.com'), +('andre1177az@gmail.com'), +('s.ekaputrirosalinda@gmail.com'), +('beautyland21@icloud.com'), +('javalotuscorp@gmail.com'), +('alxsuherman@gmail.com'), +('fenny3007.kwok@gmail.com'), +('leny_kurniawati@hotmail.com'), +('soputanlady10@gmail.com'), +('lampungbeauty2019@gmail.com'), +('thebeauteebox@gmail.com'), +('tfanixling00@gmail.com'), +('andilesmono06@gmail.com'), +('briggiteleorenza@gmail.com'), +('chanytatiara@gmail.com'), +('financeklinikpiramidajaya@gmail.com'), +('exacarolina@gmail.com'), +('wildhanhafid@gmail.com'), +('himakeup08@gmail.com'), +('riska.sofani@yahoo.com'), +('uciliana35@gmail.com'), +('agusjokin08@gmail.com'), +('madozai96@gmail.com'), +('flo_ver@yahoo.com'), +('dikinarasaki@gmail.com'), +('herul.batch3@gmail.com'), +('rudy.hartono2591@gmail.com'), +('vivi.plm187@gmail.com'), +('hallo@bencuan.id'), +('rahasiamama17@gmail.com'), +('stephzhengg@gmail.com'), +('shelinabeautycare@gmail.com'), +('joelchristianpamula@gmail.com'), +('rethamardjoen11@gmail.com'), +('pomadaivon@gmail.com'), +('divaparis28@gmail.com'), +('wulan.ranpatika@gmail.com'), +('mastoreyadi@gmail.com'), +('febi.muhi@gmail.com'), +('anggriawanferi1991@gmail.com'), +('feliciacolleen80@gmail.com'), +('istie.nodasekisan@gmail.com'), +('melliyniaanugrah@gmail.com'), +('dwiaziza98@gmail.com'), +('alfrianiyelsi@gmail.com'), +('adelia.vn@gmail.com'), +('alvina.iriady@gmail.com'), +('ratnaqisya03@gmail.com'), +('jelitacosmetic.galeri@gmail.com'), +('avex307@gmail.com'), +('rika.wulandaridwan@gmail.com'), +('srihartiniseptember1966@gmail.com'), +('theresia_margareths@yahoo.com'), +('kartikahs89@gmail.com'), +('yanuararifinnur@gmail.com'), +('luthfikhtr@gmail.com'), +('mshopper.id@gmail.com'), +('fifi.liu@yahoo.com'), +('christineeva07@gmail.com'), +('claralverina23@gmail.com'), +('12101na@gmail.com'), +('joanamarvelin.wongso@yahoo.com'), +('lylysiakwee@gmail.com'), +('andyirsaldi@gmail.com'), +('greceliautami@icloud.com'), +('rhayati48@gmail.com'), +('vidya@redroseindo.com'), +('ekanuraini131@gmail.com'), +('kimhiung168@gmail.com'), +('gitaporo@gmail.com'), +('aninditateresa@gmail.com'), +('lexonbeautyofficial@gmail.com'), +('novitaroshaputri@gmail.com'), +('theara.tiara@gmail.com'), +('selvi.chephy@gmail.com'), +('vhaans007@gmail.com'), +('tokosehat130@gmail.com'), +('mirayyacosmetics@gmail.com'), +('kusnen.sota@gmail.com'), +('hokimaxi2020@gmail.com'), +('keikolux@hotmail.com'), +('andreadewi19@gmail.com'), +('harlyskin@gmail.com'), +('viroe.nando@gmail.com'), +('aurelallshop@gmail.com'), +('kosmetikiis@gmail.com'), +('lifanny86@gmail.com'), +('nitamanda28@gmail.com'), +('cantiku8899@gmail.com'), +('sultanagengs3@gmail.com'), +('nurfiitriiyah@gmail.com'), +('nana01iphone@gmail.com'), +('mulyajaaya.motor96@gmail.com'), +('titarahmawati245@gmail.com'), +('marniatihinding@gmail.com'), +('erris.mahardika@gamal.men'), +('isti.qhatsah.nur@gmail.com'), +('dewilasolo@gmail.com'), +('aninditaputri30@gmail.com'), +('aryanto.firdaus@gmail.com'), +('novitaj68@gmail.com'), +('johan_breaker27@yahoo.com'), +('elsaberliya@gmail.com'), +('dewiyuant260693@gmail.com'), +('jeffrynoah@gmail.com'), +('rikasukmawar03@gmail.com'), +('yosua.sasauw1@gmail.com'), +('fitri.miauwh@gmail.com'), +('yuli120797@gmail.com'), +('kamala.tanray@gmail.com'), +('theresiad88@gmail.com'), +('isliana29@gmail.com'), +('chietra.anggraini93@gmail.com'), +('cesario2559@gmail.com'), +('the.secret.of.beauty.id@gmail.com'), +('elsacosmetics@yahoo.com'), +('evinrahmad2@gmail.com'), +('yuhyi.albadali@gmail.com'), +('darwin.saputra.ars@gmail.com'), +('akuntingtim2903@gmail.com'), +('wahyudinrazali4@gmail.com'), +('jwuysan@gmail.com'), +('ruus.silvana@gmail.com'), +('dianmarinaa@gmail.com'), +('dp.anggreani@gmail.com'), +('patricia.ang7@gmail.com'), +('medankosmetik89@gmail.com'), +('ayuputri.ap2201@gmail.com'), +('fikrizia13@gmail.com'), +('beautiva.skincare@gmail.com'), +('gjwproject@gmail.com'), +('pramudya.a.laksono@gmail.com'), +('ssshoptarakan@gmail.com'), +('bungalintong7@gmail.com'), +('ivanakaontole@gmail.com'), +('misel.delini@yahoo.com'), +('dwipujiati1995@gmail.com'), +('vdillahilda@gmail.com'), +('ellenanatasa@gmail.com'), +('bidarishabrina@gmail.com'), +('danimmm44@gmail.com'), +('evitsrikris@gmail.com'), +('gendisbeauty92@gmail.com'), +('eriana.dian0116@gmail.com'), +('eriskhfrd26@gmail.com'), +('jasmineniscalapandya@gmail.com'), +('eryryey03@gmail.com'), +('leessleepwear@gmail.com'), +('yoseptansil6@gmail.com'), +('natdailybeauty@gmail.com'), +('sidoarjosalonsupp@gmail.com'), +('auliarahmadanti77@gmail.com'), +('gustindw@gmail.com'), +('mamihcihuyyy@gmail.com'), +('narabeautytrk@gmail.com'), +('sandikaplg18@gmail.com'), +('20240010013@lspr.edu'), +('maynetav@gmail.com'), +('intandidi88@gmail.com'), +('jeniferpurnomo99@gmail.com'), +('dewisartika192@yahoo.co.id'), +('jokosus.susanto@gmail.com'), +('iswanto.1301@gmail.com'), +('kudo.palopo14@gmail.com'), +('micellangel@nuriglobal.com'), +('theeaanggraeny@gmail.com'), +('andi.or88@gmail.com'), +('rickyoktaviawan20@gmail.com'), +('yunibalisa661@gmail.com'), +('rikiscofieldsap@gmail.com'), +('feliciabeatrixtanner0407@gmail.com'), +('mon_taca@yahoo.com'), +('ardyanawidya@gmail.com'), +('d14na_nonic@yahoo.com'), +('kadirpnm@gmail.com'), +('mita.panji@gmail.com'), +('robbchriszzz13@gmail.com'), +('anicha8490@gmail.com'), +('galuh.acc.kediri@gmail.com'), +('tokosanjayakras.81@gmail.com'), +('vivian.effendi@gmail.com'), +('lisaderia@yahoo.com'), +('anggyfebryna17@gmail.com'), +('yuanita200989@gmail.com'), +('inggycitrasari@gmail.com'), +('musdalifa300396@gmail.com'), +('widdywedariasih3@gmail.com'), +('devinoariska06@gmail.com'), +('idealistina@yahoo.co.id'), +('kelsy18111@gmail.com'), +('elizchang88@gmail.com'), +('kavbeautyofficial@gmail.com'), +('archilagiska@gmail.com'), +('ginawati442@gmail.com'), +('revii.setiadi@gmail.com'), +('nisacuss@gmail.com'), +('eviana001075@gmail.com'), +('moanazone@gmail.com'), +('idabenangraja@gmail.com'), +('babybamscase@gmail.com'), +('bellasetiady@windowslive.com'), +('regina.amalia19@gmail.com'), +('hello.vebeauty@gmail.com'), +('ferinaindrayani@gmail.com'), +('yulia.cindy81@gmail.com'), +('ennakristiana@gmail.com'), +('nur.annisa0@gmail.com'), +('marindarara13@gmail.com'), +('sonyatiaratri2@gmail.com'), +('achmadfauzi1398@gmail.com'), +('fitrijean@gmail.com'), +('zul.hendra1297@gmail.com'), +('csbymagicskin@gmail.com'), +('ridadwi873@gmail.com'), +('grosirkosmetikpekanabaru@gmail.com'), +('gladys.agatha@gmail.com'), +('rahmizarti30@gmail.com'), +('mrs.sitiazizah@gmail.com'), +('paramitaqueena@yahoo.com'), +('uwiecute22@gmail.com'), +('slife557.cs@gmail.com'), +('adindazahra655@gmail.com'), +('lavibeautyandlifestyle@gmail.com'), +('ratna.pratiwi14.rp@gmail.com'), +('yunengazraapriani@gmail.com'), +('sheefaa84@gmail.com'), +('katherinelimanu21@gmail.com'), +('tutiyuniyanti@gmail.com'), +('kartinisarimah@gmail.com'), +('jonastampubolon123@gmail.com'), +('wuisancheni77@gmail.com'), +('shella.madirza888@gmail.com'), +('yulianisetiawan836@gmail.com'), +('janiceechenn@yahoo.com'), +('ferico.ridwan@gmail.com'), +('udjayamandiripgs@gmail.com'), +('budiwijaya1962@gmail.com'), +('rumahkosmetik001@gmail.com'), +('kinanbeautystore@gmail.com'), +('yuniraemah31@yahoo.co.id'), +('barokahbeaute@gmail.com'), +('shrlymarcella@gmail.com'), +('ikrt_2507@yahoo.com'), +('dikofebyan@gmail.com'), +('restyty.riezalina@yahoo.com'), +('ratukosmetik169@gmail.com'), +('faradillahif07@gmail.com'), +('sales@dewicosmetic.com'), +('kadek.arymas@gmail.com'), +('rahmaduwijayanti@gmail.com'), +('fanamaliaauthor@gmail.com'), +('vanessapratama24@gmail.com'), +('gangnamcosmetic109@gmail.com'), +('mayanglatuconsina@gmail.com'), +('lustremaiden@gmail.com'), +('ratihalexa@gmail.com'), +('anindyalarasp@gmail.com'), +('fourayoebeauty@gmail.com'), +('selenabeauty.banjarbaru@gmail.com'), +('destyisniaty@gmail.com'), +('rmdhniannisa15@gmail.com'), +('devimutiah18@gmail.com'), +('ainunmuthiaa@gmail.com'), +('nurhikmah.rasyid@gmail.com'), +('burizma.store@gmail.com'), +('ariefariq4@gmail.com'), +('haniaekap8814@gmail.com'), +('ricomerico240@gmail.com'), +('ekanvt07@gmail.com'), +('helenlim02@gmail.com'), +('lukm.hakm@gmail.com'), +('nur_aisyah1407@icloud.com'), +('serlina.mbc@gmail.com'), +('k.inayah23@gmail.com'), +('marisrisma@yahoo.com'), +('shela.annisa99@gmail.com'), +('angguntrivany@gmail.com'), +('nntheorange@gmail.com'), +('nathaliayaulin23@gmail.com'), +('mrs.gapura@gmail.com'), +('selviathalita92@gmail.com'), +('cynthia.de06@gmail.com'), +('beautemondee@gmail.com'), +('ninikrahmawati486@gmail.com'), +('anst.slvia@gmail.com'), +('beautycarepalu@gmail.com'), +('ririwiduri2002@gmail.com'), +('ilookdaily534@gmail.com'), +('apapunada.id@gmail.com'), +('dellasyaskia6@gmail.com'), +('nadiasalmau@gmail.com'), +('tikamaharaeni19@gmail.com'), +('aansabang@gmail.com'), +('devinatalithanabila98@gmail.com'), +('ennitrisha@gmail.com'), +('marialucyanasunjaya02762@gmail.com'), +('rtnohandayani@gmail.com'), +('nsbeauty06@gmail.com'), +('idapurnamasari0129@gmail.com'), +('msreysantana@gmail.com'), +('aldillaputri5@gmail.com'), +('maderusni25@gmail.com'), +('crystalglow.myshop@gmail.com'), +('dheamuliasari@gmail.com'), +('cindyfdle@gmail.com'), +('laninabeauty.official@gmail.com'), +('nimatussaadah29@gmail.com'), +('merrylaili@gmail.com'), +('wijaya.nanda1810@gmail.com'), +('kadavikadav@gmail.com'), +('anggikurniawati40@gmail.com'), +('mamikikeyu@gmail.com'), +('marlindaabdulrazak@gmail.com'), +('alizhaaa@gmail.com'), +('queenkosmetik20@gmail.com'), +('skincarekupang21@gmail.com'), +('feny.asnawi@gmail.com'), +('shanticahyadi82@gmail.com'), +('zilvanaputri@gmail.com'), +('malikaresmanti@gmail.com'), +('yunisianturi06@gmail.com'), +('beautydarlinggg@gmail.com'), +('lilybeauty.ofc@gmail.com'), +('racyasry@gmail.com'), +('terasbeauty.official@gmail.com'), +('mybeautystory.89@gmail.com'), +('novita.chyntia@yahoo.com'), +('tiffany.kimana@gmail.com'), +('rumahdandan.rd15@gmail.com'), +('desihartanti17@gmail.com'), +('forshare.id@gmail.com'), +('elmiacosmetics@gmail.com'), +('evikartika03@gmail.com'), +('allwomeneeds1403@yahoo.com'), +('nadyahudaya@yahoo.com'), +('drchrysant@gmail.com'), +('yurimeni16@gmail.com'), +('anientya@gmail.com'), +('martina.bernadeta@gmail.com'), +('wulan.dhizhi@gmail.com'), +('andrixue@gmail.com'), +('sitiqulsum308@gmail.com'), +('galaxystoreyogyakarta@gmail.com'), +('iskifitriani@yahoo.co.id'), +('arismunandar6496@gmail.com'), +('agenlingerie.id@gmail.com'), +('indahasmoro90@gmail.com'), +('ksbeautylover@gmail.com'), +('alheabeauty.id@gmail.com'), +('putriitunggal99@gmail.com'), +('ahmadmalangah@gmail.com'), +('perfectionten@gmail.com'), +('ivonsetiono26@gmail.com'), +('agencmb@gmail.com'), +('glowivier@gmail.com'), +('pinkyscosmetics1995@gmail.com'), +('evinakuesnoooo@gmail.com'), +('chennywi0126@icloud.com'), +('ekawahyuni1603@gmail.com'), +('auliaqoriardi21@gmail.com'), +('joyakhineigsoen@gmail.com'), +('susantifajarwati@yahoo.com'), +('ramadhana.selvia2408@gmail.com'), +('andarish.shop@gmail.com'), +('cindytsania@gmail.com'), +('joctora@gmail.com'), +('hannahimanuella@gmail.com'), +('dwiwulandari028@gmail.com'), +('merrychrystin06@gmail.com'), +('fibi.kdi891@gmail.com'), +('suwitosjahari@gmail.com'), +('likestoretlogosari@gmail.com'), +('care.meiyabeauty@gmail.com'), +('ajenganggitasari1@gmail.com'), +('rizkiyanitapinem@gmail.com'), +('sashop.nkc21@gmail.com'), +('lilistiyorini@gmail.com'), +('thebellas1212@gmail.com'), +('be.bellecosmetic@gmail.com'), +('nurlisaaulia@gmail.com'), +('hantybeauty19@gmail.com'), +('mputrisanti@gmail.com'), +('hellolightbit@gmail.com'), +('nadhirauw88@gmail.com'), +('asmiatiakib@gmail.com'), +('ranintya12@gmail.com'), +('afrijalrijal671@gmail.com'), +('ifayusana1993@gmail.com'), +('makeyouup.myu@gmail.com'), +('salbinus.innung@yahoo.com'), +('novia190@gmail.com'), +('evadewim.s@gmail.com'), +('itsmiracle2020@gmail.com'), +('neveltari2703@gmail.com'), +('lieusbudiman@gmail.com'), +('ferisya10@gmail.com'), +('echa.muaniez89@gmail.com'), +('trinarachmawati81@gmail.com'), +('erwinzeey123@gmail.com'), +('sahabat_debora@yahoo.com'), +('dhee2jopankar@gmail.com'), +('yunda.ade11@gmail.com'), +('cyril_steven_alexander_glory@yahoo.com'), +('devisiska32@gmail.com'), +('aureolasa.pratama@gmail.com'), +('ika.angky123@yahoo.com'), +('halimatussakdiyah1997@gmail.com'), +('rahmatjayaj2@gmail.com'), +('jesselynangella08@yahoo.com'), +('famela.ft07@gmail.com'), +('rotinsulufinny@gmail.com'), +('staryisnianti@gmail.com'), +('yulianazayan@gmail.com'), +('fandestian@gmail.com'), +('melindariaslamusnar88@gmail.com'), +('jnwkidswear@gmail.com'), +('wiwin.adriani@gmail.com'), +('febriantisiban25@gmail.com'), +('scoviabybelova@gmail.com'), +('wenny.than@gmail.com'), +('iranipontoh443@gmail.com'), +('hello.lazetta@gmail.com'), +('moullylaroid@gmail.com'), +('laurantjia@gmail.com'), +('dbeautyshop8@gmail.com'), +('godzillaphoneacc3@gmail.com'), +('wiwinstsajaah@gmail.com'), +('rizahani39@gmail.com'), +('dwihartanti_gizi@yahoo.com'), +('fangfangsaputra@gmail.com'), +('amoise.cosmetics@gmail.com'), +('dennyroseiana@gmail.com'), +('nelly.sulistiawati@gmail.com'), +('central.kosmetik1106@gmail.com'), +('marcellino.haryono@gmail.com'), +('veralia.saragih@gmail.com'), +('cefamanusia@gmail.com'), +('arindatrisna33@gmail.com'), +('oketomatala@gmail.com'), +('vilyapangajouw19@gmail.com'), +('faracintadia@gmail.com'), +('adm.elisabethbdg@yahoo.com'), +('cutmiqyal@hotmail.com'), +('harjono.honoris@gmail.com'), +('nandorian98@gmail.com'), +('milalimbong71@gmail.comcom'), +('viliansiregar24@gmail.com'), +('eldeekosmetik@gmail.com'), +('jeane.lelengboto@yahoo.com'), +('lydia.debby@gmail.com'), +('aboutmybusiness11@gmail.com'), +('triple501ara@gmail.com'), +('masfitroh@gmail.com'), +('meisalmis@gmail.com'), +('zackfair232@gmail.com'), +('nakamia_pie@yahoo.com'), +('abeauty.jogja@gmail.com'), +('lenaaska7@gmail.com'), +('zeebacosmetics31@gmail.com'), +('chiangkencana@gmail.com'), +('syaaudriana@gmail.com'), +('nadyaffu@gmail.com'), +('fauziahanisa509@gmail.com'), +('christyciasintaungu@gmail.com'), +('pradnyani.dwi@gmail.com'), +('zarazsby@gmail.com'), +('adewahyupauji@gmail.com'), +('firmantuo@gmail.com'), +('ramadhanilayla@gmail.com'), +('novascafe4@gmail.com'), +('mayaecc@yahoo.com'), +('adeyoan721@gmail.com'), +('stephaniefransss@gmail.com'), +('hermooi.official@gmail.com'), +('ratnaanita170294@gmail.com'), +('agatri.silitonga@gmail.com'), +('dewifitri2403@gmail.com'), +('moehamadsururi14@gmail.com'), +('ariestyayys@gmail.com'), +('heavenlybeauty.adm@gmail.com'), +('ikaabsarih@gmail.com'), +('dhee_amell@yahoo.co.id'), +('devianatanuwijaya@gmail.com'), +('rumah.karara@gmail.com'), +('sreejita.deb@gmail.com'), +('tagdirs.cosmetic@gmail.com'), +('ceviwilly@gmail.com'), +('daniel.khosuma@gmail.com'), +('muhammadyanis12091991@gmail.com'), +('liechintia07@gmail.com'), +('apt.deantigano@gmail.com'), +('ian.tampo22@gmail.com'), +('kunyil505@gmail.com'), +('hongjujen69@gmail.com'), +('wimma.shop@gmail.com'), +('mysamplemart@gmail.com'), +('safitriyuli3@gmail.com'), +('novanindra23@gmail.com'), +('hartantiarline@gmail.com'), +('dianyunus7@gmail.com'), +('aniyt161aan@gmail.com'), +('aestheticbeautycare23@gmail.com'), +('marissarahmaadhania9i30@gmail.com'), +('cecebeautybali@gmail.com'), +('devani.ersa@yahoo.com'), +('windiana10@gmail.com'), +('itaufik55@gmail.com'), +('zhisca_77@yahoo.co.id'), +('jillyanesasiage@gmail.com'), +('mybestiebeauty.bdg@gmail.com'), +('purchasing@bumame-farmasi.com'), +('denisa.denis89@yahoo.com'), +('ranggatedubara@gmail.com'), +('angelialim0311@gmail.com'), +('rindaagustiana@gmail.com'), +('aleshabeautycare57@gmail.com'), +('monpri28@gmail.com'), +('move.hamsari@gmail.com'), +('beautybelle2608@gmail.com'), +('priscillalastyfera@gmail.com'), +('vildarahmawati17@gmail.com'), +('nathaniaa099@gmail.com'), +('noyura@gmail.com'), +('juhatihajibe@gmail.com'), +('winda.christina@gmail.com'), +('mruslir21@gmail.com'), +('siskagsp6@gmail.com'), +('olyviadevita02@gmail.com'), +('ecgirl2023@gmail.com'), +('songlacosmetics@gmail.com'), +('jelsymakeup@gmail.com'), +('vanywidyawati86@gmail.com'), +('ongfreddykusuma@gmail.com'), +('charisthamaysadwisahara@gmail.com'), +('incjodu@gmail.com'), +('mjbeautyblitar@gmail.com'), +('ngeng.yuan27@gmail.com'), +('mybeautymart.id@gmail.com'), +('nabillafr22@gmail.com'), +('dianajennifer118@gmail.com'), +('wetirima9988@gmail.com'), +('shely.proborini@yahoo.com'), +('pak.serba@gmail.com'), +('lili_3038@yahoo.co.id'), +('tallamersiaa@gmail.com'), +('khalisanaura156@gmail.com'), +('milaatysakinah@gmail.com'), +('ucci.arfan@gmail.com'), +('lutfiah.nurhandari@gmail.com'), +('albinadhaja94@gmail.com'), +('dindaty@gmail.com'), +('shopkievanik@gmail.com'), +('zheng.jinde7692@gmail.com'), +('silvi@getvybes.co'), +('muhammadraydevan@gmail.com'), +('alstininoviyantitoar@gmail.com'), +('riahafarul778@gmail.com'), +('nrustam26@gmail.com'), +('gayabyys.wu@gmail.com'), +('lalootust@gmail.com'), +('rina2pan@yahoo.com'), +('disariska@gmail.com'), +('ninu.fira@gmail.com'), +('yuibabaretail@gmail.com'), +('makshifa93@gmail.com'), +('nadzimalaily@gmail.com'), +('nindysyarif@gmail.com'), +('mutiarapesonanganjuk@gmail.com'), +('beautybypelangi@gmail.com'), +('mariyani3412@gmail.com'), +('anitabudi1998@gmail.com'), +('farikhanum@ymail.com'), +('riscaamalia1989@gmail.com'), +('oyha.ciez01@gmail.com'), +('afriasafitri@ymail.com'), +('gitalaksmicosmetic@gmail.com'), +('oktavianimunir@gmail.com'), +('meithaekyh@gmail.com'), +('antouwkeren@gmail.com'), +('meeranova.id@gmail.com'), +('dmuhenti@gmail.com'), +('bima.ajun@gmail.com'), +('sitipluviana@gmail.com'), +('endahpratiwi522@gmail.com'), +('dindalabibah@gmail.com'), +('pusatkosmetikpinky@gmail.com'), +('anjalisibagariang2000@gmail.com'), +('krisnairma15@gmail.com'), +('ocha.rospa@gmail.com'), +('siscabudhyani86@gmail.com'), +('jhenisarykambuno@gmail.com'), +('mp-store4@raenabeauty.com'), +('luckyyosin0913@gmail.com'), +('monica1726@yahoo.co.id'), +('deasyrimanda@gmail.com'), +('pbudianoto@gmail.com'), +('hernispratiwi@gmail.com'), +('lelymelani@yahoo.com'), +('cantiksehat.sby@gmail.com'), +('gunk.fan29@gmail.com'), +('nurhayati01022021@gmail.com'), +('wardahafi@gmail.com'), +('nonikmery256@gmail.com'), +('lismiatilenta69@gmail.com'), +('dewinurhidayah5125@gmail.com'), +('nurhikmah.hasbi@gmail.com'), +('dordiaahss@gmail.com'), +('molly.fh2@gmail.com'), +('avostoresurabaya@gmail.com'), +('alhun.lun@gmail.com'), +('thebeautycare05@gmail.com'), +('mykarol88@gmail.com'), +('angela.sania@yahoo.com'), +('divaa01@ymail.com'), +('p.ariyanti2@gmail.com'), +('rizkycosmetics@gmail.com'), +('claudiayolandpurnomo@gmail.com'), +('ayudea1820@gmail.com'), +('allshopbisnis72@gmail.com'), +('yosephinetan@yahoo.co.id'), +('ayuri.minho@yahoo.com'), +('paolahermawan@yahoo.com'), +('mirna.mirnaaa02@gmail.com'), +('kumaskincare01@gmail.com'), +('officialrabeauty@gmail.com'), +('cacabeauty1234@gmail.com'), +('nadeakayuangellika@gmail.com'), +('linacaterina79@gmail.com'), +('rianishop210@gmail.com'), +('ilhamxmaulanaaa@gmail.com'), +('aratna643@gmail.com'), +('vifi2403@gmail.com'), +('paringdyahdinda@icloud.com'), +('suantysuanty10@gmail.com'), +('nandalisa045@gmail.com'), +('tinylilproject@yahoo.com'), +('arfan.arsya2022@gmail.com'), +('dahliaqueenshop@gmail.com'), +('triantiagus@gmail.com'), +('widyastevani17@gmail.com'), +('adityo_konandar@yahoo.com'), +('zoerosyidh89@gmail.com'), +('yusmane395@gmail.com'), +('tephtephyy@gmail.com'), +('seemser.id@gmail.com'), +('tantinaduu@gmail.com'), +('yannayeshika@gmail.com'), +('juliacsecogati@gmail.com'), +('pheroneid@gmail.com'), +('pauline222bn@gmail.com'), +('dpuspitaprameswari@gmail.com'), +('irnanda.vinka@yahoo.com'), +('agamwijaya89@gmail.com'), +('miniskinjars@gmail.com'), +('faizal@raenabeauty.com'), +('hanaamelinda95@gmail.com'), +('maharanimega39@gmail.com'), +('tia.aprita@gmail.com'), +('m.milanova.sh@gmail.com'), +('belscofabrics@gmail.com'), +('pratamanaufal98@gmail.com'), +('joshanshop@gmail.com'), +('ayuameliasaja@gmail.com'), +('alyadwi00@gmail.com'), +('helloyoumeindonesia@gmail.com'), +('desyrosiyana@gmail.com'), +('isvimega@gmail.com'), +('andrianiyana815@gmail.com'), +('andiakbar.ak@gmail.com'), +('isw11business@gmail.com'), +('guo_fengsheng@yahoo.com'), +('nirmaladewi03@gmail.com'), +('novitheng90@gmail.com'), +('poppymputranto@gmail.com'), +('shellahamayu1997@gmail.com'), +('suparjoariyanti@gmail.com'), +('pipinwahyu80@gmail.com'), +('chryseladarwin.olivia@gmail.com'), +('choisebest64@gmail.com'), +('rafin.trisna@gmail.com'), +('solehac098@gmail.com'), +('ayumirah12@yahoo.com'), +('fasahdi@gmail.com'), +('vindysabeauty@gmail.con'), +('dillalutchu08@gmail.com'), +('hi.dearbella@gmail.com'), +('awili.tri@gmail.com'), +('ardiradityananda@gmail.com'), +('dayanishop474@gmail.com'), +('apriliamega08@gmail.com'), +('vindysabeauty@gmail.com'), +('gilsneedit@gmail.com'), +('meiriannaafs@gmail.com'), +('mantepkebumen66@gmail.com'), +('lisnawatirohmah@gmail.com'), +('mhelydya@gmail.com'), +('apriliandesi@yahoo.co.id'), +('memonaofficial1007@gmail.com'), +('attraversiamobeauty@gmail.com'), +('triyatmoko3116@gmail.com'), +('toxena.shop@gmail.com'), +('fitridwifebriani59@gmail.com'), +('apadotid7@gmail.com'), +('misz_irma@yahoo.com'), +('riofabian112200@gmail.com'), +('indahyuliza7@gmail.com'), +('imasmaslihah2222@gmail.com'), +('letsglowid.jbi@gmail.com'), +('mahreenbeautyskin@gmail.com'), +('aldofiras1607@gmail.com'), +('firdamelia05@gmail.com'), +('hajernihajerni@gmail.com'), +('yunusdarmawan8@gmail.com'), +('fmaharani96@gmail.com'), +('tokoyangtepat@gmail.com'), +('endangdolphin2011@gmail.com'), +('pallawalipu21@gmail.com'), +('pie_buana@yahoo.com'), +('marina.diantika@gmail.com'), +('rihandika34@gmail.com'), +('ngm6756@gmail.com'), +('djonyali59@gmail.com'), +('rismagina02@gmail.com'), +('beautybyasame@gmail.com'), +('gracerushandi@gmail.com'), +('rinianggrayniinank@gmail.com'), +('lin_qiau_hua@yahoo.com'), +('buanainskonsi@gmail.com'), +('cosmetik.giant@gmail.com'), +('reynitaballack@gmail.com'), +('shopkichii88@gmail.com'), +('fadhilahafifah48@yahoo.com'), +('vivaforeva1@gmail.com'), +('sisilia.marshella@yahoo.com'), +('irmhanurjayanti@gmail.com'), +('galuhnur81@gmail.com'), +('gees.hijab@gmail.com'), +('putrimeyanti8@gmail.com'), +('rully0152@gmail.com'), +('buntukk166@gmail.com'), +('williantocindy@gmail.com'), +('nadilla@raenabeauty.com'), +('cindyfreda23@gmail.com'), +('liiyalimz@gmail.com'), +('kikaychan90@gmail.com'), +('tiffanyyaprilia@gmail.com'), +('2017120215@student.unpar.ac.id'), +('23jamesbond11@gmail.com'), +('36yonathan@gmail.com'), +('a.azzahrasyah@gmail.com'), +('a6ustina@gmail.com'), +('abyan.shop97@gmail.com'), +('acelinbeauty@gmail.com'), +('adeevaskincaresemarang@gmail.com'), +('adeyatoko@gmail.com'), +('adimahendraganteng@gmail.com'), +('agatha@raenabeauty.com'), +('agatyalaksmi@gmail.com'), +('agungfadilah810@gmail.com'), +('ahmadluckibawazier@gmail.com'), +('ai.x1.35300@sutomo-mdn.sch.id'), +('alfiatulkhubbi@gmail.com'), +('alfonsocosmetics@gmail.com'), +('alifiarizkiatania@gmail.com'), +('aline_fathin@yahoo.com'), +('alisheretotalk@gmail.com'), +('alithapradita@gmail.com'), +('allaboutskincare312@gmail.com'), +('almardhiyyahs@gmail.com'), +('almirabintangr@gmail.com'), +('alvissyahrin619@gmail.com'), +('alycia_monicaa@yahoo.co.id'), +('amaliakusumaniez@gmail.com'), +('ameicosmetic12@gmail.com'), +('amelianucifera@gmail.com'), +('ameliaresnaw@gmail.com'), +('analiani86@gmail.com'), +('andikharianto978@gmail.com'), +('angelinkayla118@gmail.com'), +('angellim0311@gmail.com'), +('angellina.lady96@gmail.com'), +('anggararnda972@gmail.com'), +('anggrini@raenabeauty.com'), +('anggriprad@gmail.com'), +('anisaauliaabas@gmail.com'), +('anisafarah24@gmail.com'), +('annachf.tan@gmail.com'), +('antodukos@gmail.com'), +('antonisang27@gmail.com'), +('appl3fuji@gmail.com'), +('arisrahmat333@gmail.com'), +('artiwayan14@gmail.com'), +('artrisnandari@gmail.com'), +('arumsekarsari573@gmail.com'), +('asepmirwan1@gmail.com'), +('assyafarrizki@gmail.com'), +('astrsari@gmail.com'), +('atikaputrims97@gmail.com'), +('audi.tabrany@gmail.com'), +('avoskinbeautybandung@gmail.com'), +('avuastore@gmail.com'), +('ayunamadewi@gmail.com'), +('babdungdyfa@gmail.com'), +('beatricetheresia1@gmail.com'), +('beautyshoujo@gmail.com'), +('bella.beautittaid@gmail.com'), +('bellaikne77@gmail.com'), +('bembie07@gmail.com'), +('beningkosmetik59@gmail.com'), +('biutidy@gmail.com'), +('brilianifridayasti@gmail.com'), +('budiondream@gmail.com'), +('business.qhansaanakkami@gmail.com'), +('cahaya.kosmetik.id@gmail.com'), +('cahyanidwisepti20@gmail.com'), +('cantikatya2@gmail.com'), +('catherinerambang620@gmail.com'), +('caty.adja@gmail.com'), +('cderz23@yahoo.com'), +('ceceliasansan8@gmail.com'), +('cerimartgo@gmail.com'), +('chandrahalim714@gmail.com'), +('chen.wong21@gmail.com'), +('cheryl.marsha.heryanto@gmail.com'), +('chococoip@gmail.com'), +('christianyendi@gmail.com'), +('cindyaharvardiana@gmail.com'), +('cinthyaruhantopp1@gmail.com'), +('cinthyaruhantopp3@gmail.com'), +('cisilia268@gmail.com'), +('clarysocimartinez@gmail.com'), +('cosellebeauty@gmail.com'), +('cvindokosmetikasejahtera@gmail.com'), +('d.lilacshop@gmail.com'), +('danielbudisantoso@gmail.com'), +('danielsimanjuntak231@gmail.com'), +('dedywijayamail@gmail.com'), +('definitionbeautyjogja@gmail.com'), +('dektasyaputra1301@gmail.com'), +('dellalisapaly@gmail.com'), +('despielia@gmail.com'), +('despielia92@gmail.com'), +('desyoong21@gmail.com'), +('deviannisme@gmail.com'), +('dewimellinda10@gmail.com'), +('dhevio1426@gmail.com'), +('dianakuliah@gmail.com'), +('dianps.haryanto@yahoo.com'), +('dillaparamita0@gmail.com'), +('dinaapril280@gmail.com'), +('dini.dwiarti3105@gmail.com'), +('dpsbykosme@gmail.com'), +('dwiagustin363@gmail.com'), +('dyas253@gmail.com'), +('dyfacafe@gmail.com'), +('eka.fokus@gmail.com'), +('eleonora@raenabeauty.com'), +('elfayunengsih20@gmail.com'), +('elisiabriana@gmail.com'), +('ellierahardja@gmail.com'), +('elsa.primasari.eps@gmail.com'), +('elsaokta26@gmail.com'), +('endahtriwedanti28@gmail.com'), +('endang.kosmetik1@gmail.com'), +('endangmarbunbbm678@gmail.com'), +('ennykoesrini676@gmail.com'), +('ennysetiono@gmail.com'), +('enokfatimah405@gmail.com'), +('entok345@gmail.com'), +('enungnurhalimah93@gmail.com'), +('envirolisasekar@gmail.com'), +('ernazulianita00@gmail.com'), +('estrellabeautee@gmail.com'), +('etienneskincare@gmail.com'), +('etisuningsih117@gmail.com'), +('evan.zelizta98@gmail.com'), +('evelyn.vio81@gmail.com'), +('evinrahmad4@gmail.com'), +('fajar950@gmail.com'), +('fajaroldman@gmail.com'), +('farah.fbindonesia@gmail.com'), +('faridahsalma@gmail.com'), +('febrina@raenabeauty.com'), +('feliceclarissa90@gmail.com'), +('fennyardisheila@gmail.com'), +('fera.julianti93@gmail.com'), +('feriscainez@gmail.com'), +('fffgh8@gmail.com'), +('fikrizia_insani@yahoo.com'), +('firabanun@gmail.com'), +('fitria_rida@yahoo.com'), +('fitriamalda123@gmail.com'), +('fitriand0192@gmail.com'), +('flavianusferdiaf@gmail.com'), +('flawless.officialshop@gmail.com'), +('flimtysurabayaofficial@gmail.com'), +('fmieayam@gmail.com'), +('freshnglow77@gmail.com'), +('friendhik25@gmail.com'), +('gabriastore@gmail.com'), +('gajahmadawaee@gmail.com'), +('galaticos02@hotmail.com'), +('gbjmarket@gmail.com'), +('givonsal@gmail.com'), +('glammarket64@gmail.com'), +('gloryharahap@gmail.com'), +('grosirmurahsukoharjo5@gmail.com'), +('guo_fengsheng@yahoo.co.id'), +('gustina.denaiyenti21@gmail.com'), +('hanselchan55@gmail.com'), +('hantarancilandak@gmail.com'), +('hardiantinila3@gmail.com'), +('hartono.henny@yahoo.com'), +('hasanyeong8@gmail.com'), +('helbert.simanjuntak@gmail.com'), +('hello.sheetmask@gmail.com'), +('hellopakbu@gmail.com'), +('helmikamanihuruk@gmail.com'), +('herdianametalic@gmail.com'), +('hestydharmayanti933@gmail.com'), +('heymarcell@gmail.com'), +('hijriahr08@gmail.com'), +('hulahulahali@gmail.com'), +('idcosmeticsidcosmetics@gmail.com'), +('ihc.korea@gmail.com'), +('ihccianjur@gmail.com'), +('iis.sugiati83@gmail.com'), +('ijallevine@gmail.com'), +('ikromyafik147@gmail.com'), +('imahgrosir@gmail.com'), +('indahpurwanti4410@gmail.com'), +('indrianimentaruk2@yahoo.com'), +('info.athirabeauty@gmail.com'), +('inggrid.kambuno@yahoo.com'), +('isbeyondstyle@gmail.com'), +('istrinyamalik@gmail.com'), +('iwanku203@gmail.com'), +('jacovcho@gmail.com'), +('jayaananda247@gmail.com'), +('jayde67@genesvjq.com'), +('jazzpro131@gmail.com'), +('jcrshop20@gmail.com'), +('jeliem96@gmail.com'), +('jelysoviana@gmail.com'), +('jesiskaruslan@gmail.com'), +('jessicaa2208@gmail.com'), +('jscang93@gmail.com'), +('jschrynt@gmail.com'), +('juliaviliajulia@gmail.com'), +('jullyangelina85@gmail.com'), +('jumrayana@gmail.com'), +('junidiarisna@gmail.com'), +('jwinarso889@gmail.com'), +('karinaagustin613@yahoo.co.id'), +('katalogskincarekorea@gmail.com'), +('keanoliem3@gmail.com'), +('kekensetiawan@gmail.com'), +('khairunniszahaifa@yahoo.com'), +('kikiseptiana92@gmail.com'), +('klikandshop18@gmail.com'), +('kntz199@gmail.com'), +('kosmetiklancarjaya@gmail.com'), +('kuboykukuh@gmail.com'), +('kunarifin@gmail.com'), +('kurniadi.phan@gmail.com'), +('kyuhaekoinline@gmail.com'), +('kyuhyunkoinline5@gmail.com'), +('lancarjayakosmetik@gmail.com'), +('lanisiska45@gmail.com'), +('laure.devienne@gmail.com'), +('lavoilla@gmail.com'), +('leejaeha31@gmail.com'), +('lenasinaga853@yahoo.com'), +('leo_azzuri@yahoo.com'), +('libbyclaudiar@gmail.com'), +('Liec534@gmail.com'), +('liedhawei@yahoo.com'), +('lina.marlinajkt228@gmail.com'), +('linamay2088@gmail.com'), +('lincebaiku34@gomail.com'), +('lindasekararum@gmail.com'), +('lindaten93@gmail.com'), +('lio.edhen@gmail.com'), +('lionachristina110887@gmail.com'), +('lolithaameliaa.la@gmail.com'), +('lolytha.arina@yahoo.co.id'), +('loverini1508@gmail.com'), +('lricosantoso@gmail.com'), +('luckyluna.id@gmail.com'), +('luckyme2611@gmail.com'), +('lusianaivang@yahoo.com'), +('luvpiggy2312@gmail.com'), +('lyannaming@gmail.com'), +('madya.wa@gmail.com'), +('mahesabeautyandstore@gmail.com'), +('maianton@yahoo.com'), +('mailto.wabisabi@gmail.com'), +('makeupfutura@gmail.com'), +('maramaratus@gmail.com'), +('marciaaact@gmail.com'), +('maria.ariesw87@gmail.com'), +('mariaboons0@gmail.com'), +('marianaulfa11@gmail.com'), +('mariiahulfah123@gmail.com'), +('marlinadiansafitri@gmail.com'), +('maroon16store@gmail.com'), +('marshandaditha@gmail.com'), +('martza.swastikasari@gmail.com'), +('maryani4398@gmail.com'), +('maryavictoryss3@gmail.com'), +('matrasunik@gmail.com'), +('maupesanposter@gmail.com'), +('maximlaundry@yahoo.com'), +('megashinta39@gmail.com'), +('mekarkosmetik.serang@gmail.com'), +('melaplestari2@gmail.com'), +('melindasuriyam01@gmail.com'), +('melisahariyanto14@gmail.com'), +('mentaripuspa59@gmail.com'), +('mh.lifestyles.in@gmail.com'), +('mikanmicheelen@gmail.com'), +('mirzaluzzia123@gmail.com'), +('misluciana@yahoo.com'), +('mitha2904aprilia@gmail.com'), +('mitufaya@gmail.com'), +('mitzukooutfit@gmail.com'), +('mizuharaaya@gmail.com'), +('mohd.nurfitriadin@gmail.com'), +('moildy.id@gmail.com'), +('monalisaflorist@gmail.com'), +('morashopmedan@gmail.com'), +('mr.yung30@gmail.com'), +('muhammad.hidayat.101@gmail.com'), +('mutiaazzahra177@gmail.com'), +('mutiarajesica0209@gmail.com'), +('mykagendis@gmail.com'), +('nadya.desnam@gmail.com'), +('nailavira102@gmail.com'), +('nanalistya22@gmail.com'), +('nandadevie1968@gmail.com'), +('nandipintaa@gmail.com'), +('nari38700@gmail.com'), +('natasyaputri4982@gmail.com'), +('naturrcosmetic@gmail.com'), +('niasepti28@gmail.com'), +('nicoags26@gmail.com'), +('nicolebeautyindonesia@gmail.com'), +('nicoyuganugraha@gmail.com'), +('ninadelinda26@gmail.com'), +('nirmala.sari709@gmail.com'), +('nonaewa@gmail.com'), +('nophaaelfrida@gmail.com'), +('nova.punya@gmail.com'), +('novandy.yohanes@gmail.com'), +('novi0128@gmail.com'), +('novitageraldinetgt@gmail.com'), +('nscollastica@gmail.com'), +('ntyaau26@gmail.com'), +('nuciferaamelia@gmail.com'), +('nugrahapiawai@gmail.com'), +('nur.wazilah@gmail.com'), +('nurazhwa170@gmail.com'), +('nurinasp@gmail.com'), +('nurlaelaramadanti@gmail.com'), +('officialbuyorbye@gmail.com'), +('oktinningrum@gmail.com'), +('opponingrum@gmail.com'), +('oyyisocial@gmail.com'), +('pajerokita@gmail.com'), +('pandridoge@gmail.com'), +('papamarvel_suhendy@yahoo.com'), +('pardosiares@gmail.com'), +('pastiibisaa@gmail.com'), +('pipitsanti999@gmail.com'), +('popoybunny@gmail.com'), +('prakososyam0403@gmail.com'), +('pramita2701@gmail.com'), +('prioritybekasi@gmail.com'), +('properbeauty.id@gmail.com'), +('psalimatulh@gmail.com'), +('puellabeautyshop@gmail.com'), +('puterifedina@gmail.com'), +('putriparapara@gmail.com'), +('putrisildosz@gmail.com'), +('putrivhidi@gmail.com'), +('putryalfionta@yahoo.com'), +('r.alika003@gmail.com'), +('r0n_ny@yahoo.com'), +('radhiah.hasan26@gmail.com'), +('rahayuwrdhn@gmail.com'), +('ranicantikhebat@gmail.com'), +('ratnashary4@gmail.com'), +('raynoldpanjaitan100@gmail.com'), +('regsmakeup@gmail.com'), +('resditamargaretha@gmail.com'), +('reviyuliswan86@gmail.com'), +('ribkanads@gmail.com'), +('richarddarmawanlaidji@gmail.com'), +('rickylie1989@gmail.com'), +('ries091@gmail.com'), +('rikiimpiandes@gmail.com'), +('rikomm822@gmail.com'), +('rinayunikartikaa@gmail.com'), +('rindaariani36@gmail.com'), +('riniismiyanti_94@yahoo.com'), +('ririalijofa@gmail.com'), +('riskalestari014@gmail.com'), +('rkcjawa@yahoo.com'), +('robeth.saputra01@gmail.com'), +('romlahabadsyam@gmail.com'), +('ronnywongsolim@gmail.com'), +('rovihshsh@gmail.com'), +('roviwardanap@gmail.com'), +('roviwardanaputra@gmail.com'), +('rperlita11@gmail.com'), +('rtjayaselalu@gmail.com'), +('rzkamll18@gmail.com'), +('sabithaanisa03@gmail.com'), +('saniaa3095@gmail.com'), +('saniyasch@gmail.com'), +('sansbeauty01@gmail.com'), +('santisujanto@gmail.com'), +('sarah@getvybes.co'), +('sari.astuty12@gmail.com'), +('sarihakim96@gmail.com'), +('sarmutias1@gmail.com'), +('sarmutias2@gmail.com'), +('sarmutias5@gmail.com'), +('seilareisya@gmail.com'), +('selfi.yonghwa08@gmail.com'), +('sendychristians@gmail.com'), +('sepriyanamelly@gmail.com'), +('serlinaepriyanti07@gmail.com'), +('setiawan.laia31@gmail.com'), +('setiawanandrew818@gmail.com'), +('sherlysherlyvi@gmail.com'), +('sholi.hijubpanda@gmail.com'), +('shopalover123@gmail.com'), +('shopbintar@gmail.com'), +('shoujoshopcosmetic@gmail.com'), +('siauwelizabeth@gmail.com'), +('sintafauziah41@gmail.com'), +('siti.lathifatul.k@gmail.com'), +('sitilathifatulkhofi@gmail.com'), +('siuni.reno@gmail.com'), +('siwiberlianti@gmail.com'), +('skincareindoid@gmail.com'), +('skinqueenproject@gmail.com'), +('smcosmeticsub@gmail.com'), +('snana602@gmail.com'), +('snrofi27@gmail.com'), +('soniadepi21@gmail.com'), +('sonie.usman@gmail.com'), +('sreejita@raenabeauty.com'), +('srialfia.san@gmail.com'), +('srihendrawati1@gmail.com'), +('sriyuliani9379@gmail.com'), +('stacia_andani91@yahoo.com'), +('staciaandani91@gmail.com'), +('stasyifa@gmail.com'), +('stefani_eli@ymail.com'), +('stepfanny91@gmail.com'), +('storemajujaya12@gmail.com'), +('sultonudin.madani21@gmail.com'), +('sun.edihtantra@gmail.com'), +('suryakosmetikjogja@gmail.com'), +('suryaniani1960@gmail.com'), +('susantiyap68@gmail.com'), +('suwandikarina61@gmail.com'), +('suwati169@gmail.com'), +('syahlaag@gmail.com'), +('syahliyaf@gmail.com'), +('syahna2128@gmail.com'), +('syahpudin09.183@gmail.com'), +('syifasahb@gmail.com'), +('tagustatang@gmail.com'), +('takiidonlineshopping@gmail.com'), +('talinazulfah@gmail.com'), +('tanmelky@yahoo.com'), +('taraaengg@gmail.com'), +('taspremium88@gmail.com'), +('taufikismail198720@gmail.com'), +('tcahyanti@gmail.com'), +('tfnhtp@gmail.com'), +('the_fortune_eo@yahoo.com'), +('theakirana133@gmail.com'), +('thebeausell@gmail.com'), +('thebeautyneed@gmail.com'), +('theidk.zone@gmail.com'), +('theskindarling@gmail.com'), +('tihanperkasadefitro@gmail.com'), +('tiifandi@gmail.com'), +('timyls@yahoo.com'), +('tina.metro@yahoo.com'), +('tioharianto.allianz@yahoo.com'), +('tjulovely2013@gmail.com'), +('toko.viragrosir88@gmail.com'), +('toko.winars.palopo@gmail.com'), +('tokounikita2@gmail.com'), +('tommy_priatama@ymail.com'), +('topupfu.sp@gmail.com'), +('tyabe888@yahoo.com'), +('ulibasagrc123@gmail.com'), +('umarsikumbang2020@icloud.com'), +('vannychristinna@yahoo.com'), +('varenunggulkarya@gmail.com'), +('veliakatarina@gmail.com'), +('velvetpinkred@gmail.com'), +('vhex307@gmail.com'), +('vidiachen@yahoo.com'), +('vilencyviranyy@gmail.com'), +('vina.astini@yahoo.com'), +('vincenttia23@gmail.com'), +('vinlie2020@gmail.com'), +('violyamerin@gmail.com'), +('viraltoday124@gmail.com'), +('virginiaca09@gmail.com'), +('viserysyvaine@gmail.com'), +('viviyuvita19@gmail.com'), +('wahyudiirwan35@gmail.com'), +('wahyuekajaya@gmail.com'), +('wahyuyana.prasetya@gmail.com'), +('wellington@nihonmart.id'), +('wellypandin76@gmail.com'), +('widoagusputra90@gmail.com'), +('wiwiekapsari@icloud.com'), +('wulan3804@gmail.com'), +('wulandary.sllh@gmail.com'), +('xenpang26@gmail.com'), +('xuanliuli0@gmail.com'), +('yayarafin@gmail.com'), +('yenimaryatin332@gmail.com'), +('yenny.irawati@yahoo.com'), +('yessicasarano16@gmail.com'), +('yogialqarni1807@gmail.com'), +('yogisugianto21@gmail.com'), +('yong2.chan@gmail.com'), +('yooabeaute@gmail.com'), +('ysurjadi@gmail.com'), +('yt40gt@gmail.com'), +('yukireiyuka@gmail.com'), +('yuliacandle@gmail.com'), +('yuliananina19@yahoo.com'), +('yunisri.hartiwi@yahoo.com'), +('yunitadewi23@gmail.com'), +('zabeth_yulie@yahoo.co.id'), +('zahraashop18@gmail.com'), +('zahrabeauty.share@gmail.com'), +('zelmi5009@gmail.com'), +('zharasonline@gmail.com'), +('zivenakbar08@gmail.com'), +('Fanamaliauthor@gmail.com'), +('tjuganteng@gmailcom'), +('ayusisicantik@gmail.com'), +('tsetiyaningsih71@gmail.com'), +('oyyiapple@gmail.com'), +('angellim0311@yahoo.com'), +('fericoridwan@gmail.com'), +('Ikasusanti_story@yahoo.com'), +('hamkyu359@gmail.com'); + + +DROP TABLE raena_analytics.SKU_base_recommendation_base; + +CREATE TABLE raena_analytics.SKU_base_recommendation_base AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum(total_payment_price) total_payment_price, + sum(total_cogs)total_cogs, + ((sum(retail_price)-sum(total_cogs))/sum(retail_price))*100 Pre_discount_gm +FROM + (SELECT sku, + product_type, + sum(quantity)quantity, + sum((retail_price-seller_margin)*quantity) total_payment_price, + sum(cogs*quantity)total_cogs, + sum((retail_price-seller_margin)*quantity)retail_price + FROM raena_analytics.gm_dashboard + WHERE transaction_date::date>cast('$reportDate' AS date)-interval'90 days' + AND cogs IS NOT NULL + AND cogs>0 + GROUP BY sku, + product_type)A +INNER JOIN + (SELECT sku , + category_id, + brand_id, + name + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B ON A.sku = B.sku +INNER JOIN + (SELECT id , + name + FROM raena_catalog_management.category) C ON B.category_id = C.id +INNER JOIN + (SELECT id , + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id +GROUP BY 1, + 2, + 3, + 4, + 5 ; + + +DROP TABLE raena_analytics.reseller_base_recommendation_base; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base AS +SELECT reseller_id , + A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum(total_payment_price) total_payment_price, + sum(total_cogs)total_cogs, + ((sum(retail_price)-sum(total_cogs))/sum(retail_price))*100 Pre_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM (SELECT sku,reseller_id , +reseller_email, + product_type, + sum(quantity)quantity, + sum((retail_price-seller_margin)*quantity) total_payment_price, + sum(cogs*quantity)total_cogs, + sum((retail_price-seller_margin)*quantity)retail_price + FROM raena_analytics.gm_dashboard + WHERE transaction_date::date>cast('$reportDate' AS date)-interval'90 days' + AND cogs IS NOT NULL + AND cogs>0 + AND reseller_id IS NOT NULL + GROUP BY sku, + product_type,reseller_id,reseller_email ) A +INNER JOIN (SELECT sku , + category_id, + brand_id, + name + FROM raena_catalog_management.product) B ON A.sku = B.sku +INNER JOIN (SELECT id , + name + FROM raena_catalog_management.category) C ON B.category_id = C.id +INNER JOIN (SELECT id , + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id +INNER JOIN raena_analytics.recommendation_power_seller_base BB ON A.reseller_email = BB.email +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_stage1; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_stage1 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>=10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM raena_analytics.reseller_base_recommendation_base; + +DROP TABLE IF EXISTS raena_analytics.raw_scraping_data; + +CREATE TABLE raena_analytics.raw_scraping_data AS +SELECT + E.raena_product_sku, + F.name AS SKU_name, + E.rce_product_id, + E.rce_variant_id, + A.category_name, + A.brand_name, + A.product_page_url, + A.rce_source_product_name, + A.product_variant_name, + A.product_variant_price, + A.product_variant_price_before_discount, + A.product_sold_total, + A.product_sold, + A.product_price_min, + A.product_price_min_before_discount, + A.product_price_max, + A.product_price_max_before_discount, + CAST(percentage_match AS DECIMAL(22, 2)) AS percentage_match, + A.ratings +FROM + raena_analytics.raena_shopee_product_mapping E +LEFT JOIN ( + SELECT + A.id AS rce_product_id, + D.id AS rce_product_varient_id, + B.category_name, + C.brand_name, + product_page_url, + rce_source_product_name, + product_variant_name, + case when product_variant_price is null then 0 WHEN product_variant_price = 'None' THEN 0 WHEN product_variant_price ~ '^[0-9]+$' THEN CAST(product_variant_price AS BIGINT) ELSE 0 end / 10000 AS product_variant_price, + case when product_variant_price_before_discount is null then 0 WHEN product_variant_price_before_discount = 'None' THEN 0 WHEN product_variant_price_before_discount ~ '^[0-9]+$' THEN CAST(product_variant_price_before_discount AS BIGINT) ELSE 0 end / 10000 AS product_variant_price_before_discount, + product_sold_total, + product_sold, + case when product_price_min is null then 0 WHEN product_price_min = 'None' THEN 0 WHEN product_price_min ~ '^[0-9]+$' THEN CAST(product_price_min AS BIGINT) ELSE 0 end / 10000 AS product_price_min, + case when product_price_min_before_discount is null then 0 WHEN product_price_min_before_discount = 'None' THEN 0 WHEN product_price_min_before_discount ~ '^[0-9]+$' THEN CAST(product_price_min_before_discount AS BIGINT) ELSE 0 end / 10000 AS product_price_min_before_discount, + case when product_price_max is null then 0 WHEN product_price_max = 'None' THEN 0 WHEN product_price_max ~ '^[0-9]+$' THEN CAST(product_price_max AS BIGINT) ELSE 0 end / 10000 AS product_price_max, + case when product_price_max_before_discount is null then 0 WHEN product_price_max_before_discount = 'None' THEN 0 WHEN product_price_max_before_discount ~ '^[0-9]+$' THEN CAST(product_price_max_before_discount AS BIGINT) ELSE 0 end / 10000 AS product_price_max_before_discount, + ratings + FROM + raena_spider_management.rce_product A + LEFT JOIN raena_spider_management.rce_category B ON A.rce_category_id = B.id + LEFT JOIN raena_spider_management.rce_brand C ON A.rce_brand_id = CAST(C.id AS VARCHAR) + LEFT JOIN raena_spider_management.rce_product_variant D ON A.id = D.rce_product_id +) A ON E.rce_product_id = CAST(A.rce_product_id AS VARCHAR) +AND REPLACE(E.rce_variant_id, '.0', '') = REPLACE(CAST(A.rce_product_varient_id AS VARCHAR), ',', '') +LEFT JOIN raena_catalog_management.product F ON E.raena_product_sku = F.sku; + + +DROP TABLE IF EXISTS raena_analytics.raw_scraping_data_stage ; + + +CREATE TABLE raena_analytics.raw_scraping_data_stage AS +SELECT A.* +FROM raena_analytics.raw_scraping_data A +INNER JOIN + (SELECT raena_product_sku, + max(percentage_match) percentage_match + FROM raena_analytics.raw_scraping_data + GROUP BY 1) B ON A.raena_product_sku = B.raena_product_sku +AND A.percentage_match =B.percentage_match; + + +DROP TABLE IF EXISTS raena_analytics.sku_sales_factor; + + +CREATE TABLE raena_analytics.sku_sales_factor AS +SELECT SKU, + cast(cast(sum(B.total_quantity*3) AS decimal(22,2))/cast(SUM(A.total_quantity) AS decimal(22,2)) AS decimal(22,2)) sale_factor +FROM raena_analytics.SKU_base_recommendation_base A +LEFT JOIN + (SELECT raena_product_sku, + sum(DISTINCT product_sold) total_quantity + FROM raena_analytics.raw_scraping_data_stage + GROUP BY 1) B ON A.sku = B.raena_product_sku +WHERE A.Pre_discount_gm>=10 +GROUP BY 1; + + +DROP TABLE IF EXISTS raena_analytics.recommendation_on_low_sku_task1; + + +CREATE TABLE raena_analytics.recommendation_on_low_sku_task1 AS +SELECT C.mobile Reseller_mobile , + C.email reseller_email, + A.sku, + A.sku_name, + A.brand_name, + A.category_name , + A.product_type , + Total_quantity, + total_payment_price, + total_cogs, + A.pre_discount_gm, + coalesce(B.sale_factor,0) sale_factor, + A.pre_discount_gm*coalesce(B.sale_factor,1) sorting +FROM + (SELECT * + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE Gm_type = 'DDB') A +INNER JOIN + (SELECT mobile, + email, + id + FROM raena_user_management.user) C ON A.reseller_id = cast(C.id AS varchar) +INNER JOIN + (SELECT sku , + pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base) D ON A.sku = D.sku +AND D.pre_discount_gm >=10 +INNER JOIN raena_analytics.sku_in_stock E ON A.sku = E.sku +AND E.in_stock >=10 +LEFT JOIN raena_analytics.sku_sales_factor B ON A.sku = B.sku; +---------------------------------------------task 2 --------------- + + + + + +DROP TABLE raena_analytics.SKU_base_recommendation_base_v2; + + +CREATE TABLE raena_analytics.SKU_base_recommendation_base_v2 AS +SELECT A.sku, + ((sum((A.retail_price-seller_margin) *quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM raena_analytics.gm_dashboard A +WHERE transaction_date::date>cast('$reportDate' as date)-interval'90 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND tier_name ='GOLD' +GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.recommendation_base_historical_data_offender; + + +CREATE TABLE raena_analytics.recommendation_base_historical_data_offender AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE gm_type ='Offender') A +LEFT JOIN raena_analytics.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN raena_analytics.sku_sales_factor C ON B.mapping_group_base = C.sku +LEFT JOIN raena_analytics.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND Pre_discount_gm>=10 +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM + (SELECT sku, + name, + product_type, + category_id, + brand_id + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.category) C ON B.category_id = C.id + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id ) E ON B.mapping_group_base= E.SKU; + + + DROP TABLE IF EXISTS raena_analytics.recommendation_base_historical_data_ddb; + + +CREATE TABLE raena_analytics.recommendation_base_historical_data_ddb AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + A.Pre_discount_gm reseller_pre_discount_gm, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + D.Pre_discount_gm, + in_stock +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type, + Pre_discount_gm + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE gm_type ='DDB') A +LEFT JOIN raena_analytics.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN raena_analytics.sku_sales_factor C ON A.SKU = C.sku +LEFT JOIN raena_analytics.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND D.Pre_discount_gm>=10 +LEFT JOIN raena_analytics.sku_in_stock F ON B.mapping_group_base = F.sku +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM + (SELECT sku, + name, + product_type, + category_id, + brand_id + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.category) C ON B.category_id = C.id + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id ) E ON B.mapping_group_base= E.SKU; + + DROP TABLE raena_analytics.recommentdation_offender_sku_concern; + + +CREATE TABLE raena_analytics.recommentdation_offender_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_offender B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_concern_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC; + + + DROP TABLE raena_analytics.recommentdation_offender_sku_ingredient; + + +CREATE TABLE raena_analytics.recommentdation_offender_sku_ingredient AS +SELECT reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_offender B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +AND recommended_ingredient_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC ; + + + DROP TABLE IF EXISTS raena_analytics.recommentdation_ddb_sku_concern ; + + +CREATE TABLE raena_analytics.recommentdation_ddb_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_concern_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE in_stock>50 and reseller_pre_discount_gm>10; + +DROP TABLE IF EXISTS raena_analytics.recommentdation_ddb_sku_ingredient ; + + +CREATE TABLE raena_analytics.recommentdation_ddb_sku_ingredient AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_ingredient_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE in_stock>50 +and reseller_pre_discount_gm >=10; + +DROP TABLE raena_analytics.recommentdation_sku_ingredient; + + +CREATE TABLE raena_analytics.recommentdation_sku_ingredient AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + Recommended_pre_discount_gm, + sale_factor , + (Ingredient_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm) sorting, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + cast(sale_factor AS decimal(22,2)) sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) tier_price from raena_catalog_management.product) BB on AA.Recommended_sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + + +DROP TABLE raena_analytics.recommentdation_sku_ingredient_purchase; + + +CREATE TABLE raena_analytics.recommentdation_sku_ingredient_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(tier_price AS decimal(22,2))gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku, + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) tier_price from raena_catalog_management.product) BB on AA.sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + + +DROP TABLE raena_analytics.recommentdation_sku_concern; + + +CREATE TABLE raena_analytics.recommentdation_sku_concern AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + Recommended_pre_discount_gm, + sale_factor , + concern_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm sorting, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) tier_price from raena_catalog_management.product) BB on AA.Recommended_sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + +DROP TABLE raena_analytics.recommentdation_sku_concern_purchase; + + +CREATE TABLE raena_analytics.recommentdation_sku_concern_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) tier_price from raena_catalog_management.product) BB on AA.sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + -----------------------------------------------Task 3 -------------------------------(completed) +--inner join (select distinct id as order_id , json_extract_path_text(reseller_info,'province',TRUE) reseller_province +-- from raena_order_management.order +-- where created_at::date>dateadd(DAY,-91,'$reportDate') and payment_status='Paid') AA on A.external_id = AA.order_id + + +DROP TABLE raena_analytics.SKU_base_recommendation_base_t3 ; + + +CREATE TABLE raena_analytics.SKU_base_recommendation_base_t3 AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + shipping_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM raena_analytics.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +WHERE transaction_date::date>cast('$reportDate' as date)-interval'90 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + and (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false' +GROUP BY 1, + 2, + 3, + 4, + 5, + 6 ; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_t3; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_t3 AS +SELECT reseller_id , + A.sku, + case when shipping_to ='Customer' then shipping_province end as dropshipping_province, + case when shipping_to ='Reseller' then shipping_province end as reseller_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin) *quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Post_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM raena_analytics.gm_dashboard A +INNER JOIN + (SELECT DISTINCT id AS order_id , + shipping_to + FROM raena_order_management.order + WHERE created_at::date>cast('$reportDate' as date)-interval'180 days' + AND payment_status='Paid') AA ON A.external_id = AA.order_id +INNER JOIN raena_analytics.recommendation_power_seller_base BB ON A.reseller_email = BB.email +WHERE transaction_date::date>cast('$reportDate' as date)-interval'180 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND reseller_id IS NOT NULL + AND order_placed_by ='Admin Panel' + AND shipping_to <> 'Marketplace' +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_stage1_t3; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_stage1_t3 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM raena_analytics.reseller_base_recommendation_base_t3; + + +drop table if exists raena_analytics.reseller_base_recommendation_base_stage1_t3_v1; + +create table raena_analytics.reseller_base_recommendation_base_stage1_t3_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm, + in_stock + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6,9; + + drop table if exists raena_analytics.reseller_base_recommendation_base_stage1_t3_v2; + +create table raena_analytics.reseller_base_recommendation_base_stage1_t3_v2 +as + SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku total_quantity_sku , + B.Pre_discount_gm + FROM (select distinct reseller_id , + sku, + dropshipping_province + from raena_analytics.reseller_base_recommendation_base_stage1_t3 + where dropshipping_province is not null ) A + LEFT JOIN raena_analytics.reseller_base_recommendation_base_stage1_t3_v1 B + ON A.dropshipping_province = B.shipping_province ; + + + +DROP TABLE raena_analytics.same_province_with_diff_sku_dropshipping; + + + +CREATE TABLE raena_analytics.same_province_with_diff_sku_dropshipping AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk +from raena_analytics.reseller_base_recommendation_base_stage1_t3_v2 A + WHERE A.sku <> A.recommended_sku and dropshipping_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + + --------------- + DROP TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM raena_analytics.same_province_with_diff_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + +drop table if exists raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1; + +create table raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +DROP TABLE IF EXISTS raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2 AS +SELECT A.reseller_id , + A.sku , + A.reseller_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm +FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A +LEFT JOIN raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1 B ON A.reseller_province = B.shipping_province +WHERE A.reseller_province IS NOT NULL; + + +DROP TABLE raena_analytics.same_province_with_diff_sku_reseller; + + +CREATE TABLE raena_analytics.same_province_with_diff_sku_reseller AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2 A +WHERE A.sku <> A.recommended_sku and A.reseller_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + +---- + + +DROP TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM raena_analytics.same_province_with_diff_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + +drop table if exists raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1; + +create table raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE IF EXISTS raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2 AS +SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm +FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A +LEFT JOIN raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1 B ON A.dropshipping_province = B.shipping_province +WHERE A.dropshipping_province IS NOT NULL; + +DROP TABLE raena_analytics.same_province_with_same_sku_dropshipping; + + +CREATE TABLE raena_analytics.same_province_with_same_sku_dropshipping AS +SELECT * +FROM + (SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition BY reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk + FROM raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2 A + WHERE sku = recommended_sku + AND A.dropshipping_province IS NOT NULL) AA +WHERE rnk<11 +ORDER BY sku , + rnk; + +DROP TABLE raena_analytics.recommendation_same_province_with_same_sku_dropshipping; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_same_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM raena_analytics.same_province_with_same_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + + +drop table if exists raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1; + +create table raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +drop table if exists raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2; + +create table raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2 +as +SELECT A.reseller_id , + A.sku , + A.reseller_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm + FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1 B + ON A.reseller_province = B.shipping_province where A.reseller_province is not null ; + + + DROP TABLE raena_analytics.same_province_with_same_sku_reseller; + + +CREATE TABLE raena_analytics.same_province_with_same_sku_reseller AS +select * from ( +SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2 A +WHERE sku = recommended_sku and A.reseller_province is not null )AA + where rnk<11 +ORDER BY sku , + rnk; + + DROP TABLE raena_analytics.recommendation_same_province_with_same_sku_reseller; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_same_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM raena_analytics.same_province_with_same_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + + +" > /home/ec2-user/cronjob/redshift/sql_code/am_recommendation_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/am_recommendation_etl.sql + + diff --git a/redshift/anchor_brand_notification_dashboard.sh b/redshift/anchor_brand_notification_dashboard.sh new file mode 100644 index 0000000..aeb8b33 --- /dev/null +++ b/redshift/anchor_brand_notification_dashboard.sh @@ -0,0 +1,624 @@ + #!/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 " + +------- Notification code + +drop table raena_analytics.notification_session_base_event; + +create table raena_analytics.notification_session_base_event +as +SELECT DISTINCT ts::date created_date, + events, + email, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid, + CASE + WHEN json_extract_path_text(event_props,'url',true) LIKE '%source=email%' THEN 'email' + WHEN json_extract_path_text(event_props,'url',true) LIKE '%source=push%' THEN 'push' + WHEN json_extract_path_text(event_props,'url',true) LIKE '%source=whatsapp%' THEN 'whatsapp' + END SOURCE, + split_part(json_extract_path_text(event_props,'url',true),'&requestId=', 2) requestid +FROM clevertap.clevertap_master_data cmd +WHERE events IN ('opened_using_link') + AND (json_extract_path_text(event_props,'url',true) LIKE '%source=email%' + OR json_extract_path_text(event_props,'url',true) LIKE '%source=push%' + OR json_extract_path_text(event_props,'url',true) LIKE '%source=whatsapp%') + AND ts::date >='2023-06-16'; + + +drop table raena_analytics.clevertab_session_id_base_data ; + +CREATE TABLE raena_analytics.clevertab_session_id_base_data AS +SELECT created_date, + email, + phone, + sessionid, + count(CASE WHEN events ='view_item' THEN email END) view_item, + count(CASE WHEN events ='view_cart' THEN email END) view_cart, + count(CASE WHEN events ='add_to_cart' THEN email END) add_to_cart, + count(CASE WHEN events ='begin_checkout' THEN email END) begin_checkout, + count(CASE WHEN events ='page_load_checkout_continue' THEN email END) page_load_checkout_continue, + count(CASE WHEN events ='page_load_finish_checkout' THEN email END) page_load_finish_checkout, + count(CASE WHEN events ='finish_checkout' THEN email END) finish_checkout, + count(CASE WHEN events ='Charged' THEN email END) Charged +FROM + (SELECT ts::date created_date, + events, + email, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid --identity + + FROM clevertap.clevertap_master_data cmd + WHERE events IN ('add_to_cart', + 'begin_checkout', + 'finish_checkout', + 'page_load_checkout_continue', + 'page_load_finish_checkout', + 'view_cart', + 'view_item', + 'Charged') + AND ts::date >='2023-06-16') A +GROUP BY 1, + 2, + 3, + 4; + + +drop table raena_analytics.clevertab_charged_base_data ; + +CREATE TABLE raena_analytics.clevertab_charged_base_data AS +SELECT created_date, + email, + phone, + count(CASE WHEN events ='Charged' THEN email END) Charged +FROM + (SELECT ts::date created_date, + events, + email, + phone + FROM clevertap.clevertap_master_data cmd + WHERE events IN ('Charged') + AND ts::date >='2023-06-16') A +GROUP BY 1, + 2, + 3; + + +drop table raena_analytics.notification_base_table ; + +create table raena_analytics.notification_base_table +as +select created_at::date created_at, + id notification_id, + user_id , + event_name, + notification_type, + request_id, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'sku',true) sku, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'price',true) price, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'price',true),'07030fbe-5801-4318-9e97-fe33fa169894',true) bronze_price, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'price',true),'8eb95d6e-915a-4a91-9c12-fa43db995e19',true) silver_price, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'price',true),'bf645e97-8a48-4977-8367-e987489760f9',true) gold_price, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'margin',true) margin, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'flashSaleId',true) flashSaleId, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'newQty',true) newQty, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'oldQty',true) oldQty, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'retailPrice',true) retailPrice, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'applicablePrice',true) applicablePrice, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'applicablePrice',true),'new',true) new_applicablePrice, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'applicablePrice',true),'old',true) old_applicablePrice, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'percentageChange',true) percentageChange, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'name',true) user_name, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'email',true) user_email, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'mobile',true) user_mobile, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'tierId',true) user_tierid, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'lastLoggedInAt',true) user_last_loggedin_at +from raena_notification_management.notifications_log A +left join (select Notification_type, id template_id from raena_notification_management.notification_template) B on A.template_id = B.template_id +where created_at ::date >='2023-06-01' +and event_name in ('flash-sale-start','back-in-stock','price-drop'); + + +delete from raena_analytics.notification_funnel_report +where created_at >='2023-06-30'; + + +INSERT INTO raena_analytics.notification_funnel_report +SELECT A.created_at, + A.event_name, + A.notification_type, + A.user_name, + A.User_mobile, + A.user_email, + count(DISTINCT CASE WHEN unique_user>0 THEN 1 END) unique_user, + count(DISTINCT CASE WHEN view_item>0 THEN 1 END) view_item, + count(DISTINCT CASE WHEN view_cart>0 THEN 1 END) view_cart, + count(DISTINCT CASE WHEN add_to_cart>0 THEN 1 END) add_to_cart, + count(DISTINCT CASE WHEN begin_checkout>0 THEN 1 END) begin_checkout, + count(DISTINCT CASE WHEN page_load_checkout_continue>0 THEN 1 END) page_load_checkout_continue, + count(DISTINCT CASE WHEN page_load_finish_checkout>0 THEN 1 END) page_load_finish_checkout, + count(DISTINCT CASE WHEN finish_checkout>0 THEN 1 END) finish_checkout, + count(DISTINCT CASE WHEN Charged>0 THEN 1 END) Charged +FROM + (SELECT DISTINCT created_at , + event_name, + notification_type, + user_name, + User_mobile, + user_email, + request_id + FROM raena_analytics.notification_base_table + WHERE created_at>='2023-06-30') A +LEFT JOIN + (SELECT A.created_date, + A.email, + A.phone, + A.source, + A.requestid, + count(DISTINCT A.email) unique_user, + count(DISTINCT CASE WHEN view_item >0 THEN A.email END) view_item, + count(DISTINCT CASE WHEN view_cart >0 THEN A.email END) view_cart, + count(DISTINCT CASE WHEN add_to_cart >0 THEN A.email END) add_to_cart, + count(DISTINCT CASE WHEN begin_checkout >0 THEN A.email END) begin_checkout, + count(DISTINCT CASE WHEN page_load_checkout_continue >0 THEN A.email END) page_load_checkout_continue, + count(DISTINCT CASE WHEN page_load_finish_checkout >0 THEN A.email END) page_load_finish_checkout, + count(DISTINCT CASE WHEN finish_checkout >0 THEN A.email END) finish_checkout, + count(DISTINCT CASE WHEN C.Charged >0 THEN A.email END) Charged + FROM raena_analytics.notification_session_base_event A + LEFT JOIN raena_analytics.clevertab_session_id_base_data B ON A.sessionid = B.sessionid + LEFT JOIN raena_analytics.clevertab_charged_base_data C ON A.email = C.email + AND A.phone = C.phone + AND A.created_date = C.created_date + WHERE A.created_date >='2023-06-30' + GROUP BY 1, + 2, + 3, + 4, + 5) B ON --(replace(A.User_mobile,'+','') = B.phone +A.user_email = B.email +AND A.request_id = B.requestid +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +DROP TABLE raena_analytics.notification_order_base ; + + +CREATE TABLE raena_analytics.notification_order_base AS +SELECT reseller_id, + reseller_mobile , + transaction_date::date transaction_date, + external_id , + sku , + brand_name , + category_name , + discounted_price*quantity payment_amount , + quantity , + sum(discounted_price*quantity) over(partition BY external_id) order_level_amount +FROM raena_analytics.gm_dashboard gd +WHERE transaction_date ::date >='2023-06-15' + AND order_placed_by <>'Admin Panel'; + + +DROP TABLE raena_analytics.segement_raw_data; +CREATE TABLE raena_analytics.segement_raw_data AS +SELECT id notification_id, + user_id , + event_name, + notification_type, + request_id, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'sku',true) sku, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'segment',true) segment +FROM raena_notification_management.notifications_log A +LEFT JOIN + (SELECT Notification_type, + template_id + FROM raena_notification_management.notification_event_template_mapping) B ON A.template_id = B.template_id +WHERE created_at ::date >='2023-06-30' + AND event_name IN ('flash-sale-start', + 'back-in-stock', + 'price-drop') + AND json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'segment',true) IS NOT NULL ; + + +DROP TABLE IF EXISTS raena_analytics.user_bucket; + +CREATE TABLE raena_analytics.user_bucket AS +SELECT reseller_id , + max(payment_amount) max_payment_amount +FROM + (SELECT reseller_id , + id , + sum(payment_amount)payment_amount + FROM raena_order_management.order + WHERE payment_status ='Paid' + GROUP BY 1, + 2) A +GROUP BY 1; + +delete from raena_analytics.transaction_notification_mapping +where created_at >='2023-06-30'; + +INSERT INTO raena_analytics.transaction_notification_mapping +SELECT D.name brand_name, + A.*, + B.external_id , + B.quantity, + CASE + WHEN A.sku = B.sku THEN payment_amount + END payment_amount, + CASE + WHEN A.sku IS NOT NULL + AND B.sku IS NOT NULL + AND A.sku = B.sku + AND payment_amount>0 THEN order_level_amount + END order_level_amount, + payment_amount indirect_payment_amount, + CASE + WHEN date_trunc('Month',A.created_at)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.created_at)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.created_at)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.created_at)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + DD.source, + XX.segment, + case when max_payment_amount between 0 and 2000000 then '0to2M' + when max_payment_amount between 2000000.00001 and 10000000 then '2Mto10M' + when max_payment_amount >= 10000000.000001 then 'greaterthan10M' end bucket +FROM raena_analytics.notification_base_table A +INNER JOIN raena_catalog_management.product C ON A.sku = C.sku +INNER JOIN raena_catalog_management.brand D ON C.brand_id = D.id +left join raena_analytics.segement_raw_data XX on A.notification_id = XX.notification_id +and A.user_id = XX.user_id +and A.event_name = XX.event_name +and A.notification_type = XX.notification_type +and A.request_id = XX.request_id +and A.sku = XX.sku +left join raena_analytics.user_bucket YY +on cast(A.user_id as varchar) = YY.reseller_id +LEFT JOIN raena_analytics.user_type_table flag ON replace(A.user_mobile,'+','')=flag.reseller_mobile +LEFT JOIN raena_analytics.notification_session_base_event dd ON replace(A.user_mobile,'+','')=DD.phone +AND --(date_part('day',A.created_at::date)-date_part('Day',created_date::date) = 1) and (date_part('Month',A.created_at::date)-date_part('Month',created_date::date) = 0 ) and + lower(A.notification_type) =lower("source") +AND A.request_id = dd.requestid +LEFT JOIN raena_analytics.notification_order_base B ON cast(A.user_id AS varchar) = B.reseller_id +and (transaction_date::date-A.created_at::date) between 0 and 3 +AND replace(B.reseller_mobile,'+','')=DD.phone +WHERE A.created_at >='2023-06-30'; + +drop table raena_analytics.anchor_brand_level_data ; + +CREATE TABLE raena_analytics.anchor_brand_level_data AS +SELECT * , + rank()over (partition BY external_id + ORDER BY brand_contribution DESC) rnk +FROM + (SELECT * , + CASE + WHEN order_level_payment_amount <2000000 THEN '0 to 2M' + WHEN order_level_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' + WHEN order_level_payment_amount >10000000 THEN '>10M' + END bucket, + (brand_payment_amount/nullif(order_level_payment_amount,0))*100 AS brand_contribution + FROM + (SELECT *, + sum(brand_payment_amount) over(partition BY external_id) order_level_payment_amount + FROM + (SELECT date_trunc('Year',transaction_date)::date transaction_date , + external_id , + brand_name, + sum(discounted_price*quantity) brand_payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2, + 3) A + WHERE brand_payment_amount>10) B) C +WHERE brand_contribution<100; + + drop table raena_analytics.anchor_brand_data; + +CREATE TABLE raena_analytics.anchor_brand_data AS +SELECT cast(date_part('Year',transaction_date) AS varchar) transaction_date2, + bucket, + brand_name , + avg(brand_contribution) avg_brand_contribution, + avg(brand_payment_amount) avg_payment_amount , + anchor_brand_name , + avg(anchor_brand_contribution) avg_anchor_brand_contribution , + avg(anchor_payment_amount) avg_anchor_payment_amount +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +GROUP BY 1, + 2, + 3, + 6 +ORDER BY transaction_date2 , + bucket, + brand_name , + avg_anchor_brand_contribution DESC; + + +DROP TABLE raena_analytics.reseller_level_anchor_brand_level_data ; + +CREATE TABLE raena_analytics.reseller_level_anchor_brand_level_data AS +SELECT * , + rank()over (partition BY external_id + ORDER BY brand_contribution DESC) rnk +FROM + (SELECT B.* , + CASE + WHEN bucket_level_payment_amount <2000000 THEN '0 to 2M' + WHEN bucket_level_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' + WHEN bucket_level_payment_amount >10000000 THEN '>10M' + END bucket, + (brand_payment_amount/nullif(order_level_payment_amount,0))*100 AS brand_contribution + FROM + (SELECT *, + sum(brand_payment_amount) over(partition BY external_id) order_level_payment_amount + FROM + (SELECT date_trunc('Year',transaction_date)::date transaction_date, + reseller_id, + reseller_mobile , + reseller_email , + external_id , + brand_name, + sum(discounted_price*quantity) brand_payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) A + WHERE brand_payment_amount>10) B + LEFT JOIN + (SELECT reseller_id, + max(payment_amount) bucket_level_payment_amount + FROM + (SELECT reseller_id , + external_id , + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2) A + GROUP BY 1) C ON B.reseller_id = C.reseller_id) D +WHERE brand_contribution<100; + +drop table raena_analytics.reseller_level_anchor_brand_data; + +CREATE TABLE raena_analytics.reseller_level_anchor_brand_data AS +SELECT reseller_id , + reseller_email , + reseller_mobile , + cast(date_part('Year',transaction_date) AS varchar) transaction_date2, + bucket, + brand_name , + avg(brand_contribution) avg_brand_contribution, + avg(brand_payment_amount) avg_payment_amount , + anchor_brand_name , + avg(anchor_brand_contribution) avg_anchor_brand_contribution , + avg(anchor_payment_amount) avg_anchor_payment_amount +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 9 +ORDER BY transaction_date2 , + bucket, + brand_name , + avg_anchor_brand_contribution DESC; + +DROP TABLE raena_analytics.anchor_analysis; + + +CREATE TABLE raena_analytics.anchor_analysis AS +SELECT BB.reseller_id , + brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_2023, + avg(CASE WHEN transaction_date <'2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_less_2023, + last_transaction_date +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +GROUP BY 1, + 2 , + 3, + 5, + 8 +ORDER BY 1, + 2, + 4; + +DROP TABLE raena_analytics.anchor_bucket_wise_for2023; + + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date BETWEEN '2023-01-01' AND '2023-05-31' + AND transaction_date ='2023-01-01' +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + +DROP TABLE raena_analytics.anchor_bucket_wise_for2023_churn_before2023; + + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023_churn_before2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date <'2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_before_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date <'2023-01-01' + AND transaction_date <'2023-01-01' +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + + +drop table raena_analytics.anchor_bucket_wise_for2023_churn_in2023; + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023_churn_in2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_in_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date ='2023-01-01' + AND brand_name IN + (SELECT DISTINCT brand_name + FROM raena_analytics.anchor_bucket_wise_for2023_churn_before2023) +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + + +" > /home/ec2-user/cronjob/redshift/sql_code/notification_doashboard_anchor_brand.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/notification_doashboard_anchor_brand.sql + + diff --git a/redshift/cart_abandment.sh b/redshift/cart_abandment.sh new file mode 100644 index 0000000..c44d06d --- /dev/null +++ b/redshift/cart_abandment.sh @@ -0,0 +1,426 @@ + #!/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 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 raena_analyticsseq_0_to_1000; + + +CREATE TABLE raena_analyticsseq_0_to_1000 AS + (SELECT row_number() over ( + ORDER BY TRUE)::integer - 1 AS i + FROM raena_analytics.final_clevertab_stage LIMIT 2000); + + drop table if exists raena_analytics.exploded_array; + +create table raena_analytics.exploded_array +as +SELECT *, + JSON_EXTRACT_ARRAY_ELEMENT_TEXT(products, seq.i) AS json + FROM raena_analytics.final_clevertab_stage, + raena_analytics.seq_0_to_1000 AS seq + WHERE seq.i < JSON_ARRAY_LENGTH(products) and products like '[%]'; + + +drop table 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 , + json_extract_path_text(json,'sku',TRUE) sku_name, + json_extract_path_text(json,'quantity',TRUE) quantity +FROM raena_analytics.exploded_array; + + +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/redshift/sql_code/cart_abandment_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/cart_abandment_etl.sql + + diff --git a/redshift/city_mapping_for_online_lead.sh b/redshift/city_mapping_for_online_lead.sh new file mode 100644 index 0000000..f3b6023 --- /dev/null +++ b/redshift/city_mapping_for_online_lead.sh @@ -0,0 +1,187 @@ + #!/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/redshift/sql_code/city_mapping_for_online_lead_db.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/city_mapping_for_online_lead_db.sql + + diff --git a/redshift/cogs_alert.sh b/redshift/cogs_alert.sh new file mode 100644 index 0000000..5c239a0 --- /dev/null +++ b/redshift/cogs_alert.sh @@ -0,0 +1,96 @@ + #!/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, + json_extract_path_text(raena_catalog_management.product.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) wholesale_gold_price, + json_extract_path_text(raena_catalog_management.product.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',true) wholesale_silver_price, + json_extract_path_text(raena_catalog_management.product.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',true) 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/redshift/sql_code/increase_cogs_alert.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/increase_cogs_alert.sql + diff --git a/redshift/consignment_sales_report.sh b/redshift/consignment_sales_report.sh new file mode 100644 index 0000000..7eb8ee6 --- /dev/null +++ b/redshift/consignment_sales_report.sh @@ -0,0 +1,228 @@ + #!/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, + DATEDIFF(day, gateway_expiry::date, CURRENT_DATE) AS number_of_day_pending, + payment_link, + CASE WHEN no_of_retries >= 2 THEN 'Yes' ELSE 'No' END AS offender_reseller +FROM ( + SELECT + A.id AS order_id, + A.created_at::date AS order_created_date, + A.status AS order_status, + payment_amount, + B.name AS Am_name, + CASE + WHEN B.email = C.\"emailid \" THEN 'Denny' + ELSE 'Manthan' + END AS Team, + json_extract_path_text(reseller_info, 'mobile', true) AS reseller_mobile, + json_extract_path_text(reseller_info, 'name', true) AS 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 AS number_of_day_pending, + CASE + WHEN payment_provider = 'DurianPay' THEN json_extract_path_text(payment_details,'invoice_url',true) + WHEN payment_provider = 'Xendit' THEN 'https://checkout.xendit.co/web/' || payment_provider_code + END AS payment_link, + no_of_retries + FROM ( + SELECT + REPLACE(CAST(json_extract_array_element_text(order_ids, 0) 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, +json_extract_path_text(event_props,'CT Session Id',true) sessionid , +json_extract_path_text(event_props,'item_id',true) item_id , +json_extract_path_text(event_props,'isGuestUser',true) isGuestUser , +json_extract_path_text(event_props,'CT Source',true) Source , +json_extract_path_text(event_props,'id',true) id , +json_extract_path_text(event_props,'brandName',true) brandName , +json_extract_path_text(event_props,'item_name',true) item_name , +json_extract_path_text(event_props,'offenderItems',true) offenderItems , +json_extract_path_text(event_props,'items',true) items , +json_extract_path_text(event_props,'search_term',true) search_term , +json_extract_path_text(event_props,'screen',true) 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/redshift/sql_code/consignment_sales_report.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/consignment_sales_report.sql + diff --git a/redshift/consignment_sales_report_v1.sh b/redshift/consignment_sales_report_v1.sh new file mode 100644 index 0000000..b377f18 --- /dev/null +++ b/redshift/consignment_sales_report_v1.sh @@ -0,0 +1,290 @@ + #!/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, + DATEDIFF(day, gateway_expiry::date, CURRENT_DATE) AS number_of_day_pending, + payment_link, + CASE WHEN no_of_retries >= 2 THEN 'Yes' ELSE 'No' END AS offender_reseller +FROM ( + SELECT + A.id AS order_id, + A.created_at::date AS order_created_date, + A.status AS order_status, + payment_amount, + B.name AS Am_name, + CASE + WHEN B.email = C.\"emailid \" THEN 'Denny' + ELSE 'Manthan' + END AS Team, + json_extract_path_text(reseller_info, 'mobile', true) AS reseller_mobile, + json_extract_path_text(reseller_info, 'name', true) AS 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 AS number_of_day_pending, + CASE + WHEN payment_provider = 'DurianPay' THEN json_extract_path_text(payment_details,'invoice_url',true) + WHEN payment_provider = 'Xendit' THEN 'https://checkout.xendit.co/web/' || payment_provider_code + END AS payment_link, + no_of_retries + FROM ( + SELECT + REPLACE(CAST(json_extract_array_element_text(order_ids, 0) 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; + + +update raena_analytics.pay_on_delivery_payment_dashboard +set team = 'Denny' +where am_name = 'Iskandar Sani. S Kom' or am_name = 'Rosidah'; + + + +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, + split_part(batch,'__',1) po_number, + 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, + A.expiry_date, + C.lot_no +FROM raena_order_management.fulfillment_detail A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.shipment_id = B.id +LEFT JOIN + (SELECT DISTINCT sku , + reference_no, + expiry_date, + lot_no + FROM raena_erp_management.inbound_grn_sku)C ON A.sku = C.sku +AND split_part(batch,'__',1) = C.reference_no +AND A.expiry_date = C.expiry_date +WHERE applied_cogs IS NOT NULL + AND applied_cogs <>0 +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 8, + 9, + 10; + + + +DROP TABLE IF EXISTS raena_analytics.cogs_base_data; + + +CREATE TABLE raena_analytics.cogs_base_data AS +SELECT sku, + cogs , + promo , + CASE + WHEN cogs <>0 THEN (cast(promo AS float)/cast(cogs AS float))*100 + ELSE 0 + END promo_percentage, + (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.order_sku_table; + + +CREATE TABLE raena_analytics.order_sku_table AS +SELECT A.order_id , + (A.created_at+interval'7 Hours')::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, + C.promo_percentage, + coalesce(B.payment_amount,A.payment_amount) payment_amount +FROM raena_order_management.sales_sub_order A +inner join (select id from raena_order_management.order + where (payment_status = 'Paid' or id like 'PL%') or (payment_status='Initiated' and is_cash_on_delivery='true')) BB on A.order_id = BB.id +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+interval'7 Hours')::date BETWEEN C.created_at AND C.last_date +WHERE (A.created_at+interval'7 Hours')::date >='2023-01-01' + AND A.status NOT IN ('Returned', + 'Cancelled') ; + + + +DROP TABLE raena_analytics.consignment_base_data ; + + +CREATE TABLE raena_analytics.consignment_base_data AS +SELECT transaction_date, + paid_status, + F.name Brand_name , + D.sku raena_code, + E.name sku_name, + B.Quantity consignment_quantity, + B.pkp_cogs, + A.po_number, + C.lot_no, + batch_no, + D.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.promo_percentage, + D.payment_amount + from raena_analytics.order_sku_table D +left join raena_analytics.fullfillment_base_data C on D.order_id = C.order_Id +and sales_sub_order_shipment_id= shipment_id +AND D.sku = C.sku +left join raena_erp_management.inbound_order_sku B +on C.batch=B.batch_no +AND C.sku=B.raena_code +left join raena_erp_management.inbound_order A +on B.reference_no=A.po_number +left join raena_catalog_management.product E on D.sku = E.sku +left join raena_catalog_management.brand F on E.brand_id = F.id +WHERE transaction_date>='2023-01-01'; + +--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 raena_analytics.order_sku_table 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, +json_extract_path_text(event_props,'CT Session Id',true) sessionid , +json_extract_path_text(event_props,'item_id',true) item_id , +json_extract_path_text(event_props,'isGuestUser',true) isGuestUser , +json_extract_path_text(event_props,'CT Source',true) Source , +json_extract_path_text(event_props,'id',true) id , +json_extract_path_text(event_props,'brandName',true) brandName , +json_extract_path_text(event_props,'item_name',true) item_name , +json_extract_path_text(event_props,'offenderItems',true) offenderItems , +json_extract_path_text(event_props,'items',true) items , +json_extract_path_text(event_props,'search_term',true) search_term , +json_extract_path_text(event_props,'screen',true) screen, +json_extract_path_text(event_props,'position',true) position_value , +json_extract_path_text(event_props,'story_name',true) story_name , +json_extract_path_text(event_props,'caraousel_title',true) caraousel_title , +json_extract_path_text(event_props,'title',true) title +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' +'home_product_Caraousel', +'bottom_bar_seller_center', +'bottom_bar_challenge' +) +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/redshift/sql_code/consignment_sales_report.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/consignment_sales_report.sql + diff --git a/redshift/demand_forcasting.sh b/redshift/demand_forcasting.sh new file mode 100644 index 0000000..b18391b --- /dev/null +++ b/redshift/demand_forcasting.sh @@ -0,0 +1,1007 @@ +#!/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"0 day" "+%Y-%m-%d") +echo 'reportDate'=$reportDate + +echo " +--------------------------Order Level Data -------------------------- +Drop table if exists raena_analytics.Gm_final_table; + +CREATE TABLE raena_analytics.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 raena_analytics.gm_dashboard A +LEFT JOIN raena_catalog_management.product B ON A.sku = B.sku; + +Drop table if exists raena_analytics.final_order_level_data; + +CREATE TABLE raena_analytics.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 raena_analytics.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,'$reportDate')) B ON external_id =id +WHERE discounted_price <>0 and cast(transaction_date AS date) between dateadd(d,-120,'$reportDate') and '$reportDate'; + + -------------------------------------------SKU Level Data --------------------------------- + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage1; + +CREATE TABLE raena_analytics.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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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 raena_analytics.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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') THEN 'L42D' + END Bucket, + sum(coalesce (revenue_on_after_discount,0)) Total_revenue + FROM raena_analytics.final_order_level_data + WHERE cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND '$reportDate' + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') 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,'$reportDate') AND '$reportDate' +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,'$reportDate') AND '$reportDate' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'$reportDate') AND dateadd(d,-7,'$reportDate') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'$reportDate') AND dateadd(d,-14,'$reportDate') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'$reportDate') AND dateadd(d,-21,'$reportDate') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'$reportDate') AND dateadd(d,-28,'$reportDate') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'$reportDate') AND dateadd(d,-35,'$reportDate') THEN 'L42D' + END , + Total_revenue; + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage2; + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage1 +WHERE warehouse IS NOT NULL +GROUP BY sku, + warehouse ; + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage3; + + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage2; + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_final; + + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage3; + +--------------------------------------Warehouse SKu Split---------------------------------- + +select count(1)sku_final_split_warehouse from raena_analytics.sku_warehouse_split_final; + +DROP TABLE IF EXISTS raena_analytics.sku_level_data ; + +CREATE TABLE raena_analytics.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.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 raena_analytics.storage_data B ON A.sku=B.sku +left join raena_analytics.demand_forecasting_configs G on A.sku= G.sku +LEFT JOIN raena_analytics.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 raena_analytics.sku_cogs_audit sca where sku_cogs_action='CREATED') F ON A.sku= F.sku_code ; +---------------------------_basic Calculation ------------------------- + +DROP TABLE IF EXISTS raena_analytics.basic_calculation; + + +CREATE TABLE raena_analytics.basic_calculation AS +SELECT sku , + CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') 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 raena_analytics.final_order_level_data A +LEFT JOIN + (SELECT CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END txn_period, + sum(coalesce (revenue_on_after_discount,0)) Total_revenue + FROM raena_analytics.final_order_level_data + WHERE cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND '$reportDate' + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END) B ON (CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END)=B.txn_period +WHERE cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND '$reportDate' +GROUP BY sku, + CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'$reportDate') AND '$reportDate' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'$reportDate') AND dateadd(d,-31,'$reportDate') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'$reportDate') AND dateadd(d,-61,'$reportDate') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'$reportDate') AND dateadd(d,-91,'$reportDate') THEN 'L120D' + END, + Total_revenue; + +select 1; + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage1; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.basic_calculation_stage1_v1; + +create table raena_analytics.basic_calculation_stage1_v1 as +select A.*, + abs(contribution-median_contribution) for_max_contribution +from raena_analytics.basic_calculation A +left join (select sku , + MEDIAN(contribution) median_contribution + from raena_analytics.basic_calculation group by sku ) B + on A.sku = B.sku ; + + + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage2; + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage1_v1 A +LEFT JOIN raena_analytics.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 raena_analytics.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 raena_analytics.basic_calculation_stage2 + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage3; + +/* +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation A +WHERE txn_period <> 'L120D' +GROUP BY A.SKu,txn_period; +*/ + + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation A +left join (select distinct sku , runrate from raena_analytics.basic_calculation where txn_period = 'L30D' ) B on A.sku = B.sku +WHERE txn_period <> 'L120D' +GROUP BY A.SKu; + + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage4; + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage2 A +LEFT JOIN raena_analytics.basic_calculation_stage3 B ON A.sku = B.sku; + + +--SELECT * FROM raena_analytics.basic_calculation_stage4; + +--select * from raena_analytics.basic_calculation_stage5 + +drop table if exists raena_analytics.basic_calculation_stage5; + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty2_avg_runrate +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty3_avg_quantity +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty4_expected_revenue_target +FROM raena_analytics.basic_calculation_stage4) A +group by sku; + +drop table if exists raena_analytics.basic_calculation_stage6; + +create table raena_analytics.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 raena_analytics.sku_warehouse_split_final B + group by sku; + +drop table if exists raena_analytics.final_basic_calucation_table; + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage4 A +LEFT JOIN raena_analytics.basic_calculation_stage6 B ON A.sku =B.sku +LEFT join raena_analytics.basic_calculation_stage5 C on A.sku=C.sku; + +drop table if exists raena_analytics.inbound_data; + +create table raena_analytics.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) >'$reportDate' 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 raena_analytics.warehouse_in_stock; + +create table raena_analytics.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 raena_analytics.demand_forecast_dashboard_base; + + +CREATE TABLE raena_analytics.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 /home/ec2-user/cronjob/redshift/sql_code/Demand_forecasting.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/Demand_forecasting.sql diff --git a/redshift/distributed_warehouse_dashboard.sh b/redshift/distributed_warehouse_dashboard.sh new file mode 100644 index 0000000..4b683c5 --- /dev/null +++ b/redshift/distributed_warehouse_dashboard.sh @@ -0,0 +1,1580 @@ +!/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.sku_type_warehouse; + +create table raena_analytics.sku_type_warehouse +as +select distinct sku , sku_type from raena_analytics.final_basic_calucation_table; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1 ; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) +SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price + --partner_name, + --Service_name + FROM basetable + where service_name = 'REG' + GROUP BY destination_area_id + --partner_name, + --Service_name + ) B ON A.destination_area_id=B.destination_area_id + --AND A.partner_name =B.partner_name + --AND A.Service_name= B.Service_name + ) AA +LEFT JOIN + (SELECT DISTINCT min_price, + --service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price + --,service_name + ,destination_area_id + ORDER BY min_price , + --service_name , + destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + -- A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price + --partner_name, + --Service_name + FROM basetable + where service_name = 'REG' + GROUP BY destination_area_id + --partner_name, + --Service_name + ) B ON A.destination_area_id=B.destination_area_id + --AND A.partner_name =B.partner_name + --AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON --AA.service_name = BB.service_name AND +AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; + +select 1; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + E.type service_type , + EE.name partner_name , + CASE + WHEN warehouse_code like 'WH-CGK%' THEN 'Cikarang' + WHEN warehouse_code like 'WH-SRG%' THEN 'Semarang' + WHEN warehouse_code like 'WH-SUB%' THEN 'Surabaya' + WHEN warehouse_code like 'WH-UPG%' THEN 'Makassar' + WHEN warehouse_code like 'WH-MES%' THEN 'Medan' + WHEN warehouse_code like 'WH-AAP%' THEN 'Samarinda' + WHEN warehouse_code like 'WH-BPN%' THEN 'Balikpapan' + WHEN warehouse_code like 'VEN%' THEN 'Supplier_warehouse' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN raena_transport_management.logistic_partner EE ON A.partner_id = EE.id +LEFT JOIN + (SELECT AA.order_id , + AA.sales_sub_order_shipment_id, + AA.sku, + CC.Quantity , + CC.discounted_price payment_amount + FROM + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + -- sum(coalesce(BB.quantity , AA.quantity)) Quantity + coalesce(BB.quantity , AA.quantity) Quantity + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + --GROUP BY 1,2,3 + ) AA + LEFT JOIN + (SELECT DISTINCT external_id , + product_sku , + quantity quantity, + discounted_price*quantity discounted_price + FROM raena_analytics.base_netsuite_final + ) CC ON AA.order_id = CC.external_id + AND AA.sku = CC.product_sku + AND AA.Quantity= CC.quantity ) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' + AND A.status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED'); + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3_final_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage3_final_v1 AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + service_type , + partner_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + product_sku sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM raena_analytics.warehouse_analysis_stage3 A +LEFT JOIN raena_analytics.base_netsuite_final B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM raena_analytics.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3_final; + +CREATE TABLE raena_analytics.warehouse_analysis_stage3_final AS +SELECT * +FROM raena_analytics.warehouse_analysis_stage3_final_v1 +WHERE order_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + AND payment_amount IS NOT NULL +UNION +SELECT A.Order_id, + reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + A.sku , + A.quantity, + shipping_amount, + B.payment_price +FROM raena_analytics.warehouse_analysis_stage3_final_v1 A +INNER JOIN + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity, + sum(coalesce(BB.payment_amount , AA.payment_amount)) payment_price + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + WHERE order_id IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + GROUP BY 1, + 2, + 3) B ON A.order_id = B.order_id +AND A.reference_id= B.sales_sub_order_shipment_id +AND A.sku = B.sku; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_1; + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN raena_analytics.warehouse_analysis_stage3_final A ON Z.id=A.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE ((Z.payment_status = 'Paid' or Z.id like 'PL%') or (Z.payment_status='Initiated' and Z.is_cash_on_delivery='true')) + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_2_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_2_v1 AS +SELECT BB.*, + CASE + WHEN BB.external_id = B.order_id + AND BB.sku = B.sku + AND (CASE + WHEN BB.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 +FROM + (SELECT external_id , + sku , + quantity, + item_type, + Brand_name, + discounted_price + FROM raena_analytics.gm_dashboard + LEFT JOIN + (SELECT order_id + FROM raena_analytics.warehouse_analysis_stage3_final) AA ON external_id = order_id + WHERE cast(transaction_date AS date)>='2022-04-01' + AND order_id IS NULL)BB +LEFT JOIN + (SELECT order_id, + sku, + product_class, + new_shipment_amount + FROM raena_analytics.sku_level_shipping_fee_final + WHERE new_shipment_amount<>0) B ON BB.external_id = B.order_id +AND BB.sku = B.sku +AND (CASE + WHEN BB.item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=B.product_class +LEFT JOIN + (SELECT external_id, + sku, + product_class, + new_shipment_amount + FROM raena_analytics.sku_level_shipping_fee_old_final + WHERE new_shipment_amount<>0) C ON BB.external_id = C.external_id +AND BB.sku = C.sku +AND (CASE + WHEN BB.item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=C.product_class; + + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_2; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + shipping_amount, + BB.Brand_name AS Brand_name , + shipping_to shipping_to, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + CASE + WHEN Z.provider='dsf' THEN 'DSF' + WHEN Z.provider='rdash360' THEN 'RDash' + WHEN Z.order_placed_by='merchant' THEN 'Merchant' + WHEN Z.order_placed_by='admin' THEN 'Admin Panel' + WHEN Z.order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN Z.is_campaign='true' THEN 'Campaign Order' + WHEN Z.is_consignment='true' THEN 'Consignment Order' + WHEN Z.provider='dsf' THEN 'DSF Order' + WHEN Z.order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM + (SELECT id , + status, + provider, + order_placed_by, + is_consignment, + payment_status, + is_campaign, + created_at, + shipping_to, + channel_id, + created_by + FROM raena_order_management.order + WHERE ((payment_status = 'Paid' or id like 'PL%') or (payment_status='Initiated' and is_cash_on_delivery='true')) + AND cast(created_at AS date)>='2022-04-01') Z +INNER JOIN raena_analytics.warehouse_analysis_stage1_2_v1 BB ON Z.id = BB.external_id +LEFT JOIN raena_order_management.channel D ON Z.channel_id = D.id ; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_3_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_3_v1 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.service_type , + A.partner_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to shipping_to, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + CASE + WHEN Z.provider='dsf' THEN 'DSF' + WHEN Z.provider='rdash360' THEN 'RDash' + WHEN Z.order_placed_by='merchant' THEN 'Merchant' + WHEN Z.order_placed_by='admin' THEN 'Admin Panel' + WHEN Z.order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN Z.is_campaign='true' THEN 'Campaign Order' + WHEN Z.is_consignment='true' THEN 'Consignment Order' + WHEN Z.provider='dsf' THEN 'DSF Order' + WHEN Z.order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN raena_analytics.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN raena_analytics.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +LEFT JOIN raena_order_management.channel D ON Z.channel_id = D.id +WHERE ((Z.payment_status = 'Paid' or Z.id like 'PL%') or (Z.payment_status='Initiated' and Z.is_cash_on_delivery='true')) + AND cast(Z.created_at AS date)>='2022-04-01'; + + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_3; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_3 AS +SELECT order_id, + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + sku, + quantity, + case when b.shipping_amount is not null then ((b.shipping_amount)/(b.order_weight))*(b.final_weight) else A.shipping_amount end shipping_amount, + shipping_to, + payment_amount, + A.payment_status, + order_status, + order_placed_by, + order_type, + created_at, + is_campaign, + created_by +FROM raena_analytics.warehouse_analysis_stage1_3_v1 A +LEFT JOIN raena_analytics.OM_Logistic_final_shipping B ON A.reference_id= B.reference_id; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2_v1_final_weight; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage2_v1_final_weight AS +SELECT order_id , reference_id , service_name , + service_type , + partner_name , + actual_warehouse, + CASE + WHEN (sum(B.Final_weight*A.Quantity))<1.3 THEN 1 + WHEN (sum(B.Final_weight*A.Quantity))>=1.3 + AND (ABS(sum(B.Final_weight*A.Quantity)) - FLOOR(ABS(sum(B.Final_weight*A.Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.Final_weight*A.Quantity))+1 + ELSE FLOOR(sum(B.Final_weight*A.Quantity)) + END AS sku_weight +FROM raena_analytics.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku + group by 1,2,3,4,5,6; + + + DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage2_v1 AS +SELECT A.*, + B.brand_name, + B.Final_weight*sku_weight/sum(B.Final_weight) over(partition by A.order_id , A.reference_id ) Final_weight +FROM raena_analytics.warehouse_analysis_stage1_3 A +left join raena_analytics.warehouse_analysis_stage2_v1_final_weight C on A.order_id = C.order_id and A.reference_id = C.reference_id +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + +drop table if exists raena_analytics.calculated_ninja_jne_oct_shipping_fee; + +CREATE TABLE raena_analytics.calculated_ninja_jne_oct_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(CASE WHEN C.partner_id = 5 THEN C.insurance_fee ELSE C.insurance_fee END) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM raena_analytics.warehouse_analysis_stage2_v1 + WHERE created_at::date BETWEEN '2022-10-01' AND '2022-10-14' + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + ) AA +GROUP BY 1, + 2, + 3, + 4; + +drop table if exists raena_analytics.calculated_feb_shipping_fee; + +CREATE TABLE raena_analytics.calculated_feb_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(C.insurance_fee ) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM raena_analytics.warehouse_analysis_stage2_v1 + WHERE created_at::date BETWEEN '2022-11-01' AND '2023-02-20' + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + ) A +GROUP BY 1, + 2, + 3, + 4; + + DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2; + +CREATE TABLE raena_analytics.warehouse_analysis_stage2 AS +SELECT A.*, + (final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(final_weight) over(partition BY A.reference_id) new_shipment_amount +FROM raena_analytics.warehouse_analysis_stage2_v1 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.reference_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.reference_id = C.reference_id ; + +select count(1) from raena_analytics.warehouse_analysis_stage2 ; + + +DROP TABLE IF EXISTS raena_analytics.shipping_fee_coupon_purpose; + + +CREATE TABLE raena_analytics.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to shipping_to, + order_placed_by, + order_type, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT A.id AS order_id , + reseller_id, + json_extract_path_text(A.reseller_info,'tierName',true) tierName, + json_extract_path_text(A.reseller_info,'email',true) email, + json_extract_path_text(A.reseller_info,'mobile',true) mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + A.created_at + FROM raena_order_management.order A + left join raena_order_management.channel D on A.channel_id = D.id + WHERE A.is_archived = 'false' + AND cast(A.created_at AS date)>='2022-04-01' + ORDER BY A.created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN +(SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN raena_analytics.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management.order + WHERE (payment_status= 'Paid' or id like 'PL%') + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + + +select count(1) from raena_analytics.shipping_fee_coupon_purpose; + +DROP TABLE IF EXISTS raena_analytics.rate_sheet_wise_expected_warehouse ; + + +CREATE TABLE raena_analytics.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + AA.sku , + cast(case when actual_warehouse is null then 'Cikarang' else actual_warehouse end as varchar(50)) actual_warehouse , + cast(case when expected_warehouse is null then 'Cikarang' else expected_warehouse end as varchar(50)) expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date, + DSF_flag, + brand_type, + cogs +FROM + ( + SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + A.service_type , + A.partner_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + coalesce(new_shipment_amount,shipping_amount)new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at, + case when A.order_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + CASE + WHEN coalesce(Y.Name,A.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 coalesce(Y.Name,A.Brand_name) IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC' + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type + FROM raena_analytics.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM raena_analytics.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN raena_analytics.warehouse_analysis_stage1 D ON A.rate_id = D.id + --AND A.service_name = D.service_name + )AA + LEFT JOIN + (SELECT distinct A.external_id , + A.sku , + A.cogs + FROM raena_analytics.business_report A ) CA ON AA.sku = CA.sku +AND AA.order_id = CA.external_id +WHERE AA.sku IS NOT NULL +GROUP BY order_id , + reference_id, + aa.sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date), + DSF_flag, + brand_type, + cogs; + +update raena_analytics.rate_sheet_wise_expected_warehouse + set payment_amount =0 + from raena_analytics.gm_dashboard + where discounted_price= 0 + and raena_analytics.rate_sheet_wise_expected_warehouse.order_id = raena_analytics.gm_dashboard.external_id + and raena_analytics.rate_sheet_wise_expected_warehouse.sku = raena_analytics.gm_dashboard.sku ; + + /* +update raena_analytics.rate_sheet_wise_expected_warehouse + set expected_warehouse = cast(CASE + WHEN expected_warehouse_code like 'WH-CGK%' THEN 'Cikarang' + WHEN expected_warehouse_code like 'WH-SRG%' THEN 'Semarang' + WHEN expected_warehouse_code like 'WH-SUB%' THEN 'Surabaya' + WHEN expected_warehouse_code like 'WH-UPG%' THEN 'Makassar' + WHEN expected_warehouse_code like 'WH-MES%' THEN 'Medan' + WHEN expected_warehouse_code like 'WH-AAP%' THEN 'Samarinda' + WHEN expected_warehouse_code like 'WH-BPN%' THEN 'Balikpapan' + WHEN expected_warehouse_code like 'VEN%' THEN 'Supplier_warehouse' + end as varchar) + from raena_order_management.sales_sub_order_shipment + where rate_sheet_wise_expected_warehouse.sub_order_shipping_id = id + and raena_order_management.sales_sub_order_shipment.expected_warehouse_code is not null; + */ + update raena_analytics.rate_sheet_wise_expected_warehouse + set expected_warehouse = 'Balikpapan' + where created_date>'2024-01-18' and expected_warehouse='Samarinda'; + + update raena_analytics.rate_sheet_wise_expected_warehouse + set missed_Inventory = CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END + where 1=1; + + + select count(1) from raena_analytics.rate_sheet_wise_expected_warehouse ; + +DROP TABLE IF EXISTS raena_analytics.quantity_trend_new; + + +CREATE TABLE raena_analytics.quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM raena_analytics.quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT cast(is_campaign as varchar) , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM raena_analytics.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS raena_analytics.quantity_trend; + + +CREATE TABLE raena_analytics.quantity_trend AS +SELECT * +FROM raena_analytics.quantity_trend_new; + + +DROP TABLE IF EXISTS raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + cast(case when actual_warehouse is null then 'Cikarang' else actual_warehouse end as varchar(50)) actual_warehouse , + cast(case when expected_warehouse is null then 'Cikarang' else expected_warehouse end as varchar(50)) expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM +(SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM raena_analytics.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM raena_analytics.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN raena_analytics.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + /* + update raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping + set expected_warehouse = cast(CASE + WHEN expected_warehouse_code like 'WH-CGK%' THEN 'Cikarang' + WHEN expected_warehouse_code like 'WH-SRG%' THEN 'Semarang' + WHEN expected_warehouse_code like 'WH-SUB%' THEN 'Surabaya' + WHEN expected_warehouse_code like 'WH-UPG%' THEN 'Makassar' + WHEN expected_warehouse_code like 'WH-MES%' THEN 'Medan' + WHEN expected_warehouse_code like 'WH-AAP%' THEN 'Samarinda' + WHEN expected_warehouse_code like 'WH-BPN%' THEN 'Balikpapan' + WHEN expected_warehouse_code like 'VEN%' THEN 'Supplier_warehouse' + end as varchar) + from raena_order_management.sales_sub_order_shipment + where rate_sheet_wise_expected_warehouse_expected_shipping.sub_order_shipping_id = id + and raena_order_management.sales_sub_order_shipment.expected_warehouse_code is not null; + */ + update raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping + set expected_warehouse = 'Balikpapan' + where created_date>'2024-01-18' and expected_warehouse='Samarinda'; + + update raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping + set missed_Inventory = CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END + where 1=1; + + select count(1) from raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping ; + +DROP TABLE IF EXISTS raena_analytics.om_distr_actaul_origin; + + +CREATE TABLE raena_analytics.om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + B.partner_id, + B.service_id, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse in ('Samarinda','Balikpapan') THEN 36700 + END AS expected_origin +FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS raena_analytics.om_distr_actaul_origin_2; + + +CREATE TABLE raena_analytics.om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM raena_analytics.om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +AND b.service_id = c.service_id +AND b.partner_id = c.partner_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN raena_analytics.om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id IS NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM raena_analytics.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM raena_analytics.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_4; + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM raena_analytics.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE raena_analytics.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM +(SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_6; + + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM raena_analytics.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM raena_analytics.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM raena_analytics.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + + --final dataset +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight=10; + + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 ; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN raena_analytics.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id and o.status <>'CANCELLED' +WHERE Sub_order_shipping_id IS NOT NULL; + +drop table if exists raena_analytics.om_shipment_jtr_shipping_fee_base_11; + +create table raena_analytics.om_shipment_jtr_shipping_fee_base_11 +as +select A.*,AA.new_sku sku,compared_weight sku_weight , +sum((compared_weight/order_weight)*expected_jtr_shipping_fee) sku_level_jtr_expected_shipping_fee, +sum((compared_weight/order_weight)*actual_jtr_shipping_fee) sku_level_jtr_actual_shipping_fee +from raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 A +left join +(SELECT order_id, reference_id ,new_sku, + sum(CASE + WHEN new_volume_weight>new_actual_weight THEN new_volume_weight + WHEN new_volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight /home/ec2-user/cronjob/redshift/sql_code/distributed_warehouse_etl_cron.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/distributed_warehouse_etl_cron.sql + diff --git a/redshift/gamification_etl.sh b/redshift/gamification_etl.sh new file mode 100644 index 0000000..bc41eff --- /dev/null +++ b/redshift/gamification_etl.sh @@ -0,0 +1,907 @@ + #!/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.challenge_user_wise_rewards; + + +CREATE TABLE raena_analytics.challenge_user_wise_rewards AS +SELECT challenge_id, + challenge_name, + challenge_type, + challenge_status, + CC.name , + CC.email, + CC.mobile, + rnk +FROM + (SELECT C.challenge_id, + challenge_name, + challenge_type, + challenge_status, + reseller_id , + current_progress, + rank()over(partition BY C.challenge_id + ORDER BY current_progress DESC ,gross_profit DESC,total_revenue DESC ,randomness DESC) rnk + FROM + (SELECT DISTINCT challengeid challenge_id , + name challenge_name, + TYPE challenge_type, + status challenge_status + FROM raena_analytics_challenge_management.challenge_management_main cm ) D + LEFT JOIN + (SELECT * + FROM + (SELECT reseller_id, + challenge_id , + current_progress, + gross_profit, + total_revenue, + randomness, + row_number() over (partition BY reseller_id,challenge_id + ORDER BY updated_at DESC) rn + FROM raena_challenge_management.rank_progress ) A + WHERE rn= 1) C ON C.challenge_id = D.challenge_id + ORDER BY C.challenge_id , + rnk, + reseller_id) AA +LEFT JOIN raena_user_management.user CC ON AA.reseller_id = cast(CC.id AS varchar) +ORDER BY challenge_id, + rnk; +--------- + +DROP TABLE raena_analytics.cohort_level_reseller; + + +CREATE TABLE raena_analytics.cohort_level_reseller AS +SELECT reseller_id , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '0to2M' + WHEN max_payment_amount BETWEEN 2000000 AND 10000000 THEN '2Mto10M' + 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',created_at)::date trnsaction_mount, + sum(payment_amount)payment_amount + FROM raena_order_management.order + GROUP BY 1, + 2) A + GROUP BY 1) B ; + + +drop table raena_analytics.challenge_brand_sku_mapping; + +create table raena_analytics.challenge_brand_sku_mapping +as +select distinct +A.challengeid challenge_id , + A.name challenge_name , + A.type challenge_type, + A.status challenge_status, + cast(A.startdate::TIMESTAMP +interval'7 hours' AS date) challenge_start_date, + cast(A.enddate::TIMESTAMP+interval'7 hours' AS date) challenge_end_date , + cast(A.createdat::TIMESTAMP+interval'7 hours'AS date) challenge_created_date, + brandid ,productsku, + reseller_id +from raena_analytics_challenge_management.challenge_management_main A +left join raena_analytics_challenge_management.participatingbrands AA on A.challengeid = AA.challengeid +LEFT JOIN + (SELECT DISTINCT challenge_id , + reseller_id + FROM raena_challenge_management.challenge_participant) b ON A.challengeid = B.challenge_id; + + +drop table raena_analytics.reseller_revenue_gamification; + +create table raena_analytics.reseller_revenue_gamification +as +SELECT order_date, + order_id , + reseller_id , + order_placed_by, + sku, + brand_id, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity, + sum(shipping_amount) shipping_amount + FROM + (SELECT order_date, + A.order_id , + reseller_id , + shipment_id, + case when A.order_id = C.order_id then C.new_shipment_amount else D.new_shipment_amount end shipping_amount, + A.sku, + A.brand_id, + CASE WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger A + LEFT JOIN + (SELECT DISTINCT id , + created_at, + order_placed_by + FROM raena_order_management.order + WHERE created_at::date >='2023-11-01') B ON A.order_id = B.id + LEFT JOIN raena_analytics.sku_level_shipping_fee_final C + on A.order_id = C.order_id + and A.sku = C.sku + left join raena_analytics.sku_level_shipping_fee_old_final D + on A.order_id = D.external_id + and A.sku = D.sku + WHERE status NOT IN ('CANCELLED', + 'RETURNED') and order_date >='2023-11-03' + GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8)c + GROUP BY 1, + 2, + 3, + 4,5,6; + + +DROP TABLE raena_analytics.challenge_rewards_data; + + +CREATE TABLE raena_analytics.challenge_rewards_data AS +SELECT distinct A.challenge_id, + B.name AS challenge_name , + B.type AS challenge_type, + A.type AS reward_type , + A.name AS reward_name, + A.reseller_id, + A.status rewards_status, + C.name reseller_name , + C.mobile , + C.email , + challenge_target, + value AS total_rewards, + json_extract_path_text(json_extract_path_text(claim_meta,'addressDetails',TRUE),'name',TRUE) address_reseller_name , + json_extract_path_text(json_extract_path_text(claim_meta,'addressDetails',TRUE),'address',TRUE) reseller_address , + json_extract_path_text(json_extract_path_text(claim_meta,'addressDetails',TRUE),'phoneNumber',TRUE) reseller_phonenumber , + json_extract_path_text(json_extract_path_text(claim_meta,'bankDetails',TRUE),'bankId',TRUE) reseller_bankid , + json_extract_path_text(json_extract_path_text(claim_meta,'bankDetails',TRUE),'bankCode',TRUE) reseller_code , + json_extract_path_text(json_extract_path_text(claim_meta,'bankDetails',TRUE),'bankAccount',TRUE) reseller_account +FROM raena_challenge_management.challenge_reward_status A +LEFT JOIN raena_analytics_challenge_management.challenge_management_main B ON A.challenge_id = B.challengeid +LEFT JOIN raena_user_management.user C ON A.reseller_id = C.id ; + + + + + + drop table raena_analytics.challenge_rewards_base_table; + + create table raena_analytics.challenge_rewards_base_table + as + SELECT challenge_id , + reseller_id , + sum(total_rewards) rewards + FROM + (select * + from raena_analytics.challenge_rewards_data + where reward_type in('CASHBACK' ,'EXTERNAL_PRODUCTS','SPARK_POINTS')) D + GROUP BY 1, + 2; + + +drop table raena_analytics.gamification_metrics_base_table_statge1_new; + +CREATE TABLE raena_analytics.gamification_metrics_base_table_statge1_new AS +SELECT A.challenge_id , + A.challenge_name , + A.challenge_type, + A.challenge_status, + A.challenge_start_date, + A.challenge_end_date , + A.challenge_created_date, + A.reseller_id, + coalesce(cast(C.order_date::TIMESTAMP+interval'7 hours'AS date),cast(CC.order_date::TIMESTAMP+interval'7 hours'AS date)) transaction_date, + coalesce(C.order_id,CC.order_id) order_id , + A.brandid , + A.productsku, + coalesce(C.amount,CC.amount) amount, + coalesce(C.cogs_amount,CC.cogs_amount) cogs_amount, + coalesce(C.quantity,CC.quantity) quantity, + D.rewards, + coalesce(C.shipping_amount,CC.shipping_amount) new_shipment_amount, + --case when C.order_id = E.order_id then E.new_shipment_amount else EE.new_shipment_amount end new_shipment_amount, + F.name AS reseller_name , + F.email AS reseller_email, + F.mobile AS reseller_mobile, + G.bucket, + coalesce(C.order_placed_by,CC.order_placed_by) order_placed_by +FROM raena_analytics.challenge_brand_sku_mapping A +LEFT JOIN raena_analytics.reseller_revenue_gamification C +ON C.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.challenge_start_date AND A.challenge_end_date +AND A.reseller_id = C.reseller_id +and A.brandid =C.brand_id and (A.productsku is not null or A.productsku ='') +LEFT JOIN raena_analytics.reseller_revenue_gamification CC +ON CC.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.challenge_start_date AND A.challenge_end_date +AND A.reseller_id = CC.reseller_id +and A.productsku = CC.sku and (A.productsku is not null or A.productsku ='') +LEFT JOIN raena_analytics.challenge_rewards_base_table D ON A.challenge_id = D.challenge_id +AND A.reseller_id = D.reseller_id +AND C.reseller_id = D.reseller_id +LEFT JOIN raena_user_management.user F ON A.reseller_id = cast(F.id AS varchar) +LEFT JOIN raena_analytics.cohort_level_reseller G ON A.reseller_id = G.reseller_id +WHERE challenge_type <> 'MILESTONE' +UNION +SELECT A.challengeid challenge_id , + A.name challenge_name , + A.type challenge_type, + A.status challenge_status, + cast(A.startdate::TIMESTAMP +interval'7 hours' AS date) challenge_start_date, + cast(A.enddate::TIMESTAMP+interval'7 hours' AS date) challenge_end_date , + cast(A.createdat::TIMESTAMP+interval'7 hours'AS date) challenge_created_date, + B.reseller_id, + cast(C.order_date::TIMESTAMP+interval'7 hours'AS date) transaction_date, + '', + '', + '', + 0 amount, + 0 Cogs_amount, + 0 quantity, + D.rewards, + 0 , + '' , + '', + '', + G.bucket, + '' +FROM raena_analytics_challenge_management.challenge_management_main A +LEFT JOIN + (SELECT DISTINCT reseller_id + FROM raena_challenge_management.challenge_participant + UNION SELECT DISTINCT reseller_id + FROM raena_challenge_management.order_ledger) b ON 1=1 +LEFT JOIN + (SELECT order_date, + order_id , + reseller_id , + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3)c ON b.reseller_id = c.reseller_id +INNER JOIN + (SELECT challenge_id , + reseller_id , + sum(total_rewards) rewards + FROM + (select * + from raena_analytics.challenge_rewards_data + where reward_type in('CASHBACK' ,'EXTERNAL_PRODUCTS','SPARK_POINTS')) D + GROUP BY 1, + 2) D ON A.challengeid = D.challenge_id +AND B.reseller_id = D.reseller_id +LEFT JOIN raena_user_management.user F ON B.reseller_id = cast(F.id AS varchar) +LEFT JOIN raena_analytics.cohort_level_reseller G ON B.reseller_id = G.reseller_id +WHERE A.type = 'MILESTONE' +ORDER BY 1, + challenge_start_date; + +DROP TABLE raena_analytics.gamification_metrics_base_table_statge1; + + +CREATE TABLE raena_analytics.gamification_metrics_base_table_statge1 AS +SELECT A.challengeid challenge_id , + A.name challenge_name , + A.type challenge_type, + A.status challenge_status, + cast(A.startdate::TIMESTAMP +interval'7 hours' AS date) challenge_start_date, + cast(A.enddate::TIMESTAMP+interval'7 hours' AS date) challenge_end_date , + cast(A.createdat::TIMESTAMP+interval'7 hours'AS date) challenge_created_date, + B.reseller_id, + cast(C.order_date::TIMESTAMP+interval'7 hours'AS date) transaction_date, + C.order_id , + C.amount, + C.cogs_amount, + C.quantity, + D.rewards, + shipping_amount new_shipment_amount, + --case when C.order_id = E.order_id then E.new_shipment_amount else EE.new_shipment_amount end new_shipment_amount, + + F.name AS reseller_name , + F.email AS reseller_email, + F.mobile AS reseller_mobile, + G.bucket, + order_placed_by +FROM raena_analytics_challenge_management.challenge_management_main A +LEFT JOIN + (SELECT DISTINCT challenge_id , + reseller_id + FROM raena_challenge_management.challenge_participant) b ON A.challengeid = B.challenge_id +LEFT JOIN + (SELECT order_date, + order_id , + reseller_id , + order_placed_by, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity, + sum(shipping_amount) shipping_amount + FROM + (SELECT order_date, + order_id , + reseller_id , + shipment_id, + shipping_amount, + CASE WHEN order_placed_by='merchant' THEN 'Merchant' WHEN order_placed_by='admin' THEN 'Admin Panel' WHEN order_placed_by='reseller' THEN 'App' END order_placed_by, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger A + LEFT JOIN + (SELECT DISTINCT id , + created_at, + order_placed_by + FROM raena_order_management.order + WHERE created_at::date >='2023-10-01') B ON A.order_id = B.id + LEFT JOIN + (SELECT DISTINCT id , + shipping_amount + FROM raena_order_management.sales_sub_order_shipment + WHERE status NOT IN ('CANCELLED', + 'RETURNED')) C ON A.shipment_id=C.id + WHERE status NOT IN ('CANCELLED', + 'RETURNED') + GROUP BY 1, + 2, + 3, + 4, + 5, + 6)c + GROUP BY 1, + 2, + 3, + 4) C ON C.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.startdate::TIMESTAMP+interval'7 hours' + AND (A.enddate::TIMESTAMP+interval'7 hours') +AND B.reseller_id = C.reseller_id +LEFT JOIN + (SELECT challenge_id , + reseller_id , + sum(total_rewards) rewards + FROM + (select * + from raena_analytics.challenge_rewards_data + where reward_type in('CASHBACK' ,'EXTERNAL_PRODUCTS','SPARK_POINTS')) D + GROUP BY 1, + 2) D ON A.challengeid = D.challenge_id +AND B.reseller_id = D.reseller_id +AND C.reseller_id = D.reseller_id +LEFT JOIN raena_user_management.user F ON B.reseller_id = cast(F.id AS varchar) +LEFT JOIN raena_analytics.cohort_level_reseller G ON B.reseller_id = G.reseller_id +WHERE A.type <> 'MILESTONE' +UNION +SELECT A.challengeid challenge_id , + A.name challenge_name , + A.type challenge_type, + A.status challenge_status, + cast(A.startdate::TIMESTAMP +interval'7 hours' AS date) challenge_start_date, + cast(A.enddate::TIMESTAMP+interval'7 hours' AS date) challenge_end_date , + cast(A.createdat::TIMESTAMP+interval'7 hours'AS date) challenge_created_date, + B.reseller_id, + cast(C.order_date::TIMESTAMP+interval'7 hours'AS date) transaction_date, + '' , + 0 amount, + 0 Cogs_amount, + 0 quantity, + D.rewards, + 0 , + '' , + '', + '', + G.bucket, + '' +FROM raena_analytics_challenge_management.challenge_management_main A +LEFT JOIN + (SELECT DISTINCT reseller_id + FROM raena_challenge_management.challenge_participant + UNION SELECT DISTINCT reseller_id + FROM raena_challenge_management.order_ledger) b ON 1=1 +LEFT JOIN + (SELECT order_date, + order_id , + reseller_id , + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3)c ON b.reseller_id = c.reseller_id +INNER JOIN + (SELECT challenge_id , + reseller_id , + sum(total_rewards) rewards + FROM + (select * + from raena_analytics.challenge_rewards_data + where reward_type in('CASHBACK' ,'EXTERNAL_PRODUCTS','SPARK_POINTS')) D + GROUP BY 1, + 2) D ON A.challengeid = D.challenge_id +AND B.reseller_id = D.reseller_id +LEFT JOIN raena_user_management.user F ON B.reseller_id = cast(F.id AS varchar) +LEFT JOIN raena_analytics.cohort_level_reseller G ON B.reseller_id = G.reseller_id +WHERE A.type = 'MILESTONE' +ORDER BY 1, + challenge_start_date; + +DROP TABLE raena_analytics.challenge_type_retention; + + +CREATE TABLE raena_analytics.challenge_type_retention AS +SELECT A.challenge_type, + min_order_date AS first_transaction_date, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=0 THEN A.reseller_id END) AS M0, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=1 THEN A.reseller_id END) AS M1, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=2 THEN A.reseller_id END) AS M2, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=3 THEN A.reseller_id END) AS M3, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=4 THEN A.reseller_id END) AS M4, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=5 THEN A.reseller_id END) AS M5, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=6 THEN A.reseller_id END) AS M6, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)>6 THEN A.reseller_id END) AS GT_M6 +FROM + (SELECT challenge_type, + reseller_id , + min(date_trunc('Month',transaction_date)::date) min_order_date + FROM raena_analytics.gamification_metrics_base_table_statge1_new + WHERE order_id IS NOT NULL + AND reseller_id IN + (SELECT DISTINCT C.id + FROM raena_analytics.charged_events_gamification_direct_revenue A + LEFT JOIN raena_user_management.user C ON A.phone = replace(C.mobile ,'+','') + WHERE challenge_detail_page>0 + AND view_item_session>0 + AND begin_checkout_session>0 + AND finish_checkout_session>0 + AND confirm_payment_session>0 + AND charged>0) + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT DISTINCT challenge_type, + reseller_id , + date_trunc('Month',transaction_date)::date order_date + FROM raena_analytics.gamification_metrics_base_table_statge1_new + WHERE order_id IS NOT NULL + AND reseller_id IN + (SELECT DISTINCT C.id + FROM raena_analytics.charged_events_gamification_direct_revenue A + LEFT JOIN raena_user_management.user C ON A.phone = replace(C.mobile ,'+','') + WHERE challenge_detail_page>0 + AND view_item_session>0 + AND begin_checkout_session>0 + AND finish_checkout_session>0 + AND confirm_payment_session>0 + AND charged>0)) B ON A.reseller_id = B.reseller_id +AND A.challenge_type = B.challenge_type +GROUP BY 1 , + 2; + + + +drop table raena_analytics.challenge_cohort_type_retention; + +CREATE TABLE raena_analytics.challenge_cohort_type_retention AS +SELECT A.challenge_type, + bucket, + min_order_date AS first_transaction_date, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=0 THEN A.reseller_id END) AS M0, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=1 THEN A.reseller_id END) AS M1, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=2 THEN A.reseller_id END) AS M2, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=3 THEN A.reseller_id END) AS M3, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=4 THEN A.reseller_id END) AS M4, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=5 THEN A.reseller_id END) AS M5, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=6 THEN A.reseller_id END) AS M6, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)>6 THEN A.reseller_id END) AS GT_M6 +FROM + (SELECT challenge_type, + reseller_id , + min(date_trunc('Month',transaction_date)::date) min_order_date + FROM raena_analytics.gamification_metrics_base_table_statge1_new + WHERE order_id IS NOT NULL + AND reseller_id IN + (SELECT DISTINCT C.id + FROM raena_analytics.charged_events_gamification_direct_revenue A + LEFT JOIN raena_user_management.user C ON A.phone = replace(C.mobile ,'+','') + WHERE challenge_detail_page>0 + AND view_item_session>0 + AND begin_checkout_session>0 + AND finish_checkout_session>0 + AND confirm_payment_session>0 + AND charged>0) + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT DISTINCT challenge_type, + reseller_id , + date_trunc('Month',transaction_date)::date order_date + FROM raena_analytics.gamification_metrics_base_table_statge1_new + WHERE order_id IS NOT NULL + AND reseller_id IN + (SELECT DISTINCT C.id + FROM raena_analytics.charged_events_gamification_direct_revenue A + LEFT JOIN raena_user_management.user C ON A.phone = replace(C.mobile ,'+','') + WHERE challenge_detail_page>0 + AND view_item_session>0 + AND begin_checkout_session>0 + AND finish_checkout_session>0 + AND confirm_payment_session>0 + AND charged>0)) B ON A.reseller_id = B.reseller_id +AND A.challenge_type = B.challenge_type +LEFT JOIN + (SELECT * + FROM raena_analytics.cohort_level_reseller) C ON A.reseller_id= C.reseller_id +GROUP BY 1, + 2, + 3; + + +/* +DROP TABLE raena_analytics.challenge_type_retention; + +CREATE TABLE raena_analytics.challenge_type_retention AS +SELECT challenge_type, + min_order_date AS first_transaction_date, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=0 THEN A.reseller_id END) AS M0, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=1 THEN A.reseller_id END) AS M1, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=2 THEN A.reseller_id END) AS M2, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=3 THEN A.reseller_id END) AS M3, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=4 THEN A.reseller_id END) AS M4, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=5 THEN A.reseller_id END) AS M5, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=6 THEN A.reseller_id END) AS M6, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)>6 THEN A.reseller_id END) AS GT_M6 +FROM + (SELECT TYPE challenge_type, + B.reseller_id , + min(date_trunc('Month',order_date)::date) min_order_date + FROM raena_analytics_challenge_management.challenge_management_main A + LEFT JOIN + (SELECT DISTINCT challenge_id , + reseller_id + FROM raena_challenge_management.challenge_participant) b ON A.challengeid = B.challenge_id + inner JOIN + (SELECT order_date, + order_id , + reseller_id , + status order_status, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3, + 4)c ON C.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.startdate::TIMESTAMP+interval'7 hours' AND (A.enddate::TIMESTAMP+interval'7 hours') + AND B.reseller_id = C.reseller_id + WHERE TYPE <> 'MILESTONE' + GROUP BY 1, + 2 + UNION SELECT TYPE challenge_type, + B.reseller_id , + min(date_trunc('Month',order_date)::date) min_order_date + FROM raena_analytics_challenge_management.challenge_management_main A + LEFT JOIN + (SELECT DISTINCT reseller_id + FROM raena_challenge_management.challenge_participant) b ON 1=1 + inner JOIN + (SELECT order_date, + order_id , + reseller_id , + status order_status, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3, + 4)c ON b.reseller_id = c.reseller_id + WHERE TYPE = 'MILESTONE' + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT DISTINCT date_trunc('Month',order_date)::date order_date, + reseller_id + FROM raena_challenge_management.order_ledger A ) B ON A.reseller_id = B.reseller_id +GROUP BY 1 , + 2; + + +drop table raena_analytics.challenge_cohort_type_retention; + +CREATE TABLE raena_analytics.challenge_cohort_type_retention AS +SELECT challenge_type, + bucket, + min_order_date AS first_transaction_date, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=0 THEN A.reseller_id END) AS M0, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=1 THEN A.reseller_id END) AS M1, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=2 THEN A.reseller_id END) AS M2, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=3 THEN A.reseller_id END) AS M3, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=4 THEN A.reseller_id END) AS M4, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=5 THEN A.reseller_id END) AS M5, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=6 THEN A.reseller_id END) AS M6, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)>6 THEN A.reseller_id END) AS GT_M6 +FROM + (SELECT TYPE challenge_type, + B.reseller_id , + min(date_trunc('Month',order_date)::date) min_order_date + FROM raena_analytics_challenge_management.challenge_management_main A + LEFT JOIN + (SELECT DISTINCT challenge_id , + reseller_id + FROM raena_challenge_management.challenge_participant) b ON A.challengeid = B.challenge_id + inner JOIN + (SELECT order_date, + order_id , + reseller_id , + status order_status, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3, + 4)c ON C.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.startdate::TIMESTAMP+interval'7 hours' AND (A.enddate::TIMESTAMP+interval'7 hours') + AND B.reseller_id = C.reseller_id + WHERE TYPE <> 'MILESTONE' + GROUP BY 1, + 2 + UNION SELECT TYPE challenge_type, + B.reseller_id , + min(date_trunc('Month',order_date)::date) min_order_date + FROM raena_analytics_challenge_management.challenge_management_main A + LEFT JOIN + (SELECT DISTINCT reseller_id + FROM raena_challenge_management.challenge_participant) b ON 1=1 + inner JOIN + (SELECT order_date, + order_id , + reseller_id , + status order_status, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3, + 4)c ON b.reseller_id = c.reseller_id + WHERE TYPE = 'MILESTONE' + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT DISTINCT date_trunc('Month',order_date)::date order_date, + reseller_id + FROM raena_challenge_management.order_ledger A ) B ON A.reseller_id = B.reseller_id +LEFT JOIN + (SELECT * + FROM raena_analytics.cohort_level_reseller) C ON A.reseller_id= C.reseller_id +GROUP BY 1, + 2, + 3; */ + + DROP TABLE IF EXISTS raena_analytics.gamification_funnel_base_stage1; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage1 AS +SELECT ts::date , + events, + name , + email , + phone, + json_extract_path_text(event_props,'CT Session Id',TRUE) sessionid , + json_extract_path_text(event_props,'page_name',TRUE) page_name , + json_extract_path_text(event_props,'ProductList',TRUE) brand_list +FROM clevertap.clevertap_master_data +WHERE events IN ('load_challenge_home_banner' , + 'click_on_challenge_home_banner', + 'bottom_bar_challenge', + 'click_on_challenge_home_banner', + 'page_load', + 'view_item', + 'begin_checkout', + 'Charged', + 'view_cart', + 'add_to_cart', + 'confirm_payment', + 'finish_checkout', + 'click_on_milestone') + AND ts::date >='2023-11-07'; + +DROP TABLE raena_analytics.gamification_funnel_base_stage2; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage2 AS +SELECT ts::date , + sessionid, + name , + email , + phone, + count(DISTINCT CASE WHEN events ='page_load' + AND page_name ='Home' THEN sessionid END) home_banner_session, + count(DISTINCT CASE WHEN events ='click_on_challenge_home_banner' THEN sessionid END) click_challenge_home_banner_session, + count(DISTINCT CASE WHEN events ='bottom_bar_challenge' THEN sessionid END) bottom_bar_challenge, + count(distinct case when events ='click_on_milestone' then sessionid end) load_challenge_milestone_page, + count(DISTINCT CASE WHEN events ='page_load' + AND page_name ='Challenges' THEN sessionid END) challenge_home_page, + count(DISTINCT CASE WHEN events ='page_load' + AND page_name ='challenge-details' THEN sessionid END) challenge_detail_page, + count(DISTINCT CASE WHEN events ='page_load' + AND page_name ='ProductList' THEN sessionid END) brand_page, + count(DISTINCT CASE WHEN events ='view_item' THEN sessionid END) view_item_session, + count(DISTINCT CASE WHEN events ='view_cart' THEN sessionid END)view_cart_session, + count(DISTINCT CASE WHEN events ='begin_checkout' THEN sessionid END) begin_checkout_session, + count(DISTINCT CASE WHEN events ='finish_checkout' THEN sessionid END) finish_checkout_session, + count(DISTINCT CASE WHEN events ='confirm_payment' THEN sessionid END) confirm_payment_session +FROM raena_analytics.gamification_funnel_base_stage1 +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE raena_analytics.gamification_funnel_base_stage3; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage3 AS +SELECT ts::date , + sessionid, + name , + email , + phone, + count(DISTINCT CASE WHEN events ='Charged' THEN sessionid END) Charged +FROM raena_analytics.gamification_funnel_base_stage1 +WHERE ts::date >='2023-11-07' +GROUP BY 1, + 2, + 3, + 4, + 5; + + + +DROP TABLE IF EXISTS raena_analytics.gamification_funnel_base_stage1_direct_revenue; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage1_direct_revenue AS +SELECT ts::date , + events, + name , + email , + phone, + json_extract_path_text(event_props,'CT Session Id',TRUE) sessionid , + json_extract_path_text(event_props,'challengeId',TRUE) challengeid +FROM clevertap.clevertap_master_data +WHERE events ='page_load' and json_extract_path_text(event_props,'page_name',TRUE) ='challenge-details' + AND ts::date >='2023-11-07'; + + +DROP TABLE IF EXISTS raena_analytics.gamification_funnel_base_stage2_direct_revenue; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage2_direct_revenue AS +SELECT ts::date , + events, + name , + email , + phone, + json_extract_path_text(event_props,'CT Session Id',TRUE) sessionid , + json_extract_path_text(event_props,'Payment ID',TRUE) payment_id +FROM clevertap.clevertap_master_data +WHERE events IN ('view_item', + 'begin_checkout', + 'Charged', + 'confirm_payment', + 'finish_checkout') + AND ts::date >='2023-11-07'; + + +drop table raena_analytics.gamification_funnel_base_stage3_direct_revenue; + +CREATE TABLE raena_analytics.gamification_funnel_base_stage3_direct_revenue AS +SELECT ts::date , + sessionid, + name , + email , + phone, + count(DISTINCT CASE WHEN events ='view_item' THEN sessionid END) view_item_session, + count(DISTINCT CASE WHEN events ='begin_checkout' THEN sessionid END) begin_checkout_session, + count(DISTINCT CASE WHEN events ='finish_checkout' THEN sessionid END) finish_checkout_session, + count(DISTINCT CASE WHEN events ='confirm_payment' THEN sessionid END) confirm_payment_session +FROM raena_analytics.gamification_funnel_base_stage2_direct_revenue +GROUP BY 1, + 2, + 3, + 4, + 5; + +DROP TABLE raena_analytics.gamification_funnel_base_stage4_direct_revenue; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage4_direct_revenue AS +SELECT ts::date , + sessionid, + name , + email , + phone, + payment_id, + count(DISTINCT CASE WHEN events ='Charged' THEN sessionid END) Charged, + order_id +FROM raena_analytics.gamification_funnel_base_stage2_direct_revenue A +left join raena_analytics.payment_order B on A.payment_id = B.id +WHERE events = 'Charged' +GROUP BY 1, + 2, + 3, + 4, + 5,6,8; + + +DROP TABLE IF EXISTS raena_analytics.charged_events_gamification_direct_revenue; + + +CREATE TABLE raena_analytics.charged_events_gamification_direct_revenue AS +SELECT A.ts, + A.phone, + A.challengeid, + count(DISTINCT A.sessionid) challenge_detail_page, + count(DISTINCT CASE WHEN view_item_session>0 THEN A.sessionid END) view_item_session, + count(DISTINCT CASE WHEN begin_checkout_session>0 THEN A.sessionid END) begin_checkout_session, + count(DISTINCT CASE WHEN finish_checkout_session>0 THEN A.sessionid END) finish_checkout_session , + count(DISTINCT CASE WHEN confirm_payment_session>0 THEN A.sessionid END) confirm_payment_session, + count(DISTINCT CASE WHEN Charged>0 THEN A.sessionid END) charged +FROM raena_analytics.gamification_funnel_base_stage1_direct_revenue A +LEFT JOIN raena_analytics.gamification_funnel_base_stage3_direct_revenue B ON A.sessionid = B.sessionid +LEFT JOIN + (SELECT DISTINCT A.order_id , + phone , + charged , + challenge_id + FROM raena_analytics.gamification_funnel_base_stage4_direct_revenue A + INNER JOIN raena_analytics.gamification_metrics_base_table_statge1_new B ON A.order_id = B.order_id) c ON B.phone = C.phone +AND A.challengeid = C.challenge_id +GROUP BY 1, + 2, + 3; + + + + + +" > /home/ec2-user/cronjob/redshift/sql_code/gamification_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/gamification_etl.sql + diff --git a/redshift/gm_dashboard.sh b/redshift/gm_dashboard.sh new file mode 100644 index 0000000..17f29a1 --- /dev/null +++ b/redshift/gm_dashboard.sh @@ -0,0 +1,2370 @@ + #!/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.manual_bundle_sku_data ; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.order_level_data; + + +CREATE TABLE raena_analytics.order_level_data AS +SELECT DISTINCT external_id, + transaction_date+interval'7 hours' AS transaction_date, + discount_amount, + shipping_cost, + A.coupon_code, + replace(cast(reseller_tier_name as varchar),'\"','')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, + is_campaign, + order_placed_by, + admin_fee +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, + cast(is_campaign as varchar) is_campaign, + order_placed_by, + admin_fee + FROM raena_order_management.order A + WHERE (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + AND cast(A.created_at+interval '7 hours' AS date) >='$reportDate' ) A +LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code; + + +SELECT max(created_at) +FROM raena_order_management.order ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage1_V1; + + +CREATE TABLE raena_analytics.base_netsuite_stage1_V1 AS +SELECT DISTINCT transaction_date 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_wholesale_price, + CASE + WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount + ELSE B.coupon_discount + END discount_price, + CASE + WHEN discount_amount > 0 + AND A.coupon_code IS NULL + AND A.order_placed_by = 'admin' + AND coupon_applied_on IS NULL THEN 'Cart' + ELSE coupon_applied_on + END 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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type, + a.is_campaign, + B.cogs_discount_type, + A.admin_fee +FROM raena_analytics.order_level_data A +LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id and status <> 'Cancelled' +LEFT JOIN raena_analytics.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, + actual_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+interval'7 hours' AS date)>='$reportDate') F ON B.id = F.sales_sub_order_id +ORDER BY 1, + 2; + + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1_base ; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1_base 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, + actual_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, + price_type, + is_campaign, + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND A.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + cogs_discount_type, + admin_fee +FROM raena_analytics.base_netsuite_stage1_V1 A +LEFT JOIN + (SELECT external_id , + sku , + final_loyalty_point + FROM raena_analytics.final_loyalty_point) B ON A.external_id = B.external_id +AND A.sku = B.sku +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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,4) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON A.sku = D.sku +AND A.transaction_date::date BETWEEN D.created_at AND last_date; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1 AS +SELECT transaction_date, + A.external_id, + A.sku, + parent_sku, + quantity quantity, + parent_quantity, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE retail_price + END retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_retail_price + END parent_retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE effective_wholesale_price + END effective_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_wholesale_price + END parent_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE actual_wholesale_price + END actual_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_price + END discount_price, + coupon_applied_on, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_amount + END discount_amount, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE dynamic_price + END dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_dynamic_price + END parent_dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_price + END payment_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_amount + END payment_amount, + product_class, + parent_product_class, + product_type_class, + sales_sub_order_id, + loyalty_discount, + price_type, + is_campaign, + CASE + WHEN A.sku = D.sku THEN 0 + ELSE cogs + END cogs, + cogs_flag, + cogs_discount_type, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE admin_fee + END admin_fee +FROM raena_analytics.base_netsuite_stage2_v1_base A +LEFT JOIN + (SELECT DISTINCT sku + FROM raena_catalog_management.product + WHERE lower(name) LIKE '%not for sale%' + OR lower(name) LIKE '%free item%' + OR lower(name) LIKE '%free gift%') D ON A.sku = D.sku ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage3_v1 ; + + +CREATE TABLE raena_analytics.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 + AND retail_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 discount_price + WHEN coupon_applied_on ='Cart' + AND discount_price=0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id) + END*/ + discount_price AS Final_discount , + retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + CASE + WHEN product_type_class IN ('Flash Bundle', + 'Flash') THEN 'Flash' + WHEN coupon_applied_on ='Cart' + AND price_type ='NEGOTIATED_PRICE' THEN 'Negotiated' + WHEN coupon_applied_on = 'Shipping Fee' THEN 'Shipping' + WHEN coupon_applied_on IS NOT NULL THEN 'Brand' + END discount_type +FROM raena_analytics.base_netsuite_stage2_v1 A ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage4_v1 ; + + +CREATE TABLE raena_analytics.base_netsuite_stage4_v1 AS +SELECT A.* , + CASE + WHEN (parent_product_class= 'Bundle' + OR product_type_class IN('Bundle', 'Flash Bundle')) + AND A.external_id = B.external_id + AND A.parent_sku = B.parent_sku and ttl_wholesale_price<>0 THEN (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/ttl_wholesale_price) + END AS additional_discount, + CASE + WHEN final_discount<>0 THEN final_discount-seller_margin + END AS effective_coupon_discount +FROM raena_analytics.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 + FROM raena_analytics.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 raena_analytics.base_netsuite_final +where transaction_date::date>='$reportDate'; + +--WHERE external_id IN + -- (SELECT DISTINCT external_id + -- FROM raena_analytics.order_level_data); + + +INSERT INTO raena_analytics.base_netsuite_final +SELECT A.external_id, + A.transaction_date 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) -coalesce((CASE WHEN final_discount >0 THEN 0 ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity END),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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, + 0 diff, + CASE + WHEN price_type IS NOT NULL THEN price_type + ELSE discount_type + END discount_type , + CASE + WHEN final_discount >0 THEN 0 + ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity + END promo_amount, + actual_wholesale_price, + effective_wholesale_price, + A.is_campaign, + '' sales_sub_order_id, + cogs_discount_type, + parent_sku, + null, + B.admin_fee sku_level_admin_fee +FROM raena_analytics.order_level_data A +LEFT JOIN raena_analytics.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + + + +UPDATE raena_analytics.base_netsuite_final AA +SET discounted_price = new_discounted_price +from (SELECT external_id , + product_sku , + cast(settlement_amount/quantity AS decimal(22,2)) new_discounted_price + FROM + (SELECT A.*, + discounted_price , + (effective_wholesale_price*B.\"Settlement amount\")/sum(effective_wholesale_price) over (partition BY external_id) settlement_amount , + B.\"Settlement amount\" + FROM raena_analytics.base_netsuite_final A + INNER JOIN (select distinct * from raena_analytics.mp_orders_hn_wd) B ON external_id= B.\"Order ID \" + WHERE A.transaction_date >'2022-12-31' and effective_wholesale_price<>0 ) A) BB + where AA.external_id = BB.external_id +AND AA.product_sku = BB.product_sku +and AA.transaction_date >'2022-12-31'; + + +update raena_analytics.base_netsuite_final A +set discounted_price = B.payment_price/quantity +from (select order_id,sku, max(payment_price) payment_price from raena_analytics.order_data_update group by 1,2) B +where A.external_id = B.order_id and A.parent_sku = B.sku and product_sku = parent_sku ; + + +update raena_analytics.base_netsuite_final A +set discounted_price = new_payment_price/A.quantity +from raena_analytics.import_data_for_sku_level_payment_amount B +where A.external_id = B.order_id and A.parent_sku = B.sku and A.product_sku <> A.parent_sku; + +with commen_data as +(select external_id order_id , product_sku sku, + sku_level_admin_fee*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition by external_id ),0) sku_level_admin_fee_final + from raena_analytics.base_netsuite_final + where transaction_date::date >='$reportDate' + ) +update raena_analytics.base_netsuite_final +set sku_level_admin_fee = sku_level_admin_fee_final +from commen_data where external_id = order_id and product_sku = sku +and transaction_date::date >='$reportDate' ; + + +SELECT max(transaction_date) Netsuite_base_date +FROM raena_analytics.base_netsuite_final; + + +DROP TABLE IF EXISTS raena_analytics.gm_dimensions_stage1; + + +CREATE TABLE raena_analytics.gm_dimensions_stage1 AS +SELECT distinct 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, + shipping_to, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + cast(is_campaign AS varchar) is_campaign +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 +LEFT JOIN raena_order_management.channel D ON A.channel_id = D.id +WHERE (( payment_status='Paid' or A.id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + AND cast(A.created_at+interval'7 hours' AS date) >='$reportDate'; + +drop table if exists raena_analytics.sku_gm_mapping; + +create table raena_analytics.sku_gm_mapping +as +select A.sku ,A.name sku_name,B.name brand_name,C.name as category_name,product_type + 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 ; + + +DELETE +FROM raena_analytics.GM_dashboard +where transaction_date::date >='$reportDate'; +--WHERE external_id IN +-- (SELECT DISTINCT external_id +-- FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.GM_dashboard +SELECT A.external_id , + B.transaction_date AS transaction_date, + B.order_discount_amount , + B.shipping_cost , + A.shipping_province, + B.coupon_code , + A.discount_type , + B.coupon_applied_on , + brand_name, + category_name , + product_type , + product_sku sku, + sku_name , + A.reseller_name , + A.reseller_email , + A.reseller_mobile , + reseller_tier_name tier_name, + A.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 , + coalesce(effective_cogs,coalesce(C.applied_cogs,D.cogs-coalesce(promo,0))) 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type , + Customer_type, + BB.gm_target, + B.discount_type discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + coalesce(case when cogs_type ='REGULAR' then 'Promo' else cogs_type end ,CASE WHEN B.product_sku = D.sku + AND B.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + A.is_campaign, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END AS DSF_Flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender, + cohort, + cast(sku_level_admin_fee as decimal(22,4)) +FROM raena_analytics.gm_dimensions_stage1 A +INNER JOIN raena_analytics.base_netsuite_final B ON A.external_id =B.external_id +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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,4) C ON B.product_sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON B.product_sku = D.sku +AND B.transaction_date::date BETWEEN D.created_at AND last_date +left join (select distinct order_id , + created_at+interval'7 hours' AS created_date, + parent_sku sku, + effective_cogs + from raena_order_management.sales_sub_order + where price_type is not null and parent_sku not like 'BAZ%')XX + on A.external_Id=XX.order_id and B.product_sku= XX.sku +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 B.product_sku = BB.sku +AND A.reseller_tier_name =BB.tierName +LEFT JOIN raena_analytics.sku_gm_mapping AAA ON B.product_sku = AAA.sku +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) BBB on B.product_sku=BBB.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid') ; + + + + +SELECT max(transaction_date) gm_dashboard_date +FROM raena_analytics.GM_dashboard; + +DROP TABLE IF EXISTS raena_analytics.GM_dashboard_v1; + + +CREATE TABLE raena_analytics.GM_dashboard_v1 AS +SELECT * +FROM raena_analytics.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +DELETE +FROM raena_analytics.GM_dashboard +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.GM_dashboard +SELECT external_id, + transaction_date, + order_discount_amount, + shipping_cost, + shipping_province, + coupon_code, + discount_type, + coupon_applied_on, + brand_name, + category_name, + product_type, + sku, + sku_name, + reseller_name, + reseller_email, + reseller_mobile, + tier_name, + reseller_id, + quantity, + 0 retail_price, + 0 seller_margin, + 0 , + 0 , + 0, + 0, + item_type, + order_placed_by, + 0 cogs, + order_recipient, + brand_type, + customer_type, + gm_target, + discount_promo_type, + 0 promo_amount, + 0, + 0, + cogs_flag, + is_campaign, + dsf_flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender, + '<=2M', + sku_level_admin_fee +FROM raena_analytics.GM_dashboard_v1 +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + + +UPDATE raena_analytics.gm_dashboard AA +SET cogs = 0 +from (SELECT distinct order_id, sku + from raena_analytics.beau_cogs_update) BB + where AA.external_id = BB.order_id +AND AA.sku = BB.sku; + + +DROP TABLE IF EXISTS raena_analytics.business_dimensions_stage1; + + +CREATE TABLE raena_analytics.business_dimensions_stage1 AS +SELECT distinct A.id AS external_id , + reseller_id, + customer_id, + cart_id, + A.flash_sale_id, + shipping_to, + payment_status, + A.status, + A.stock_type, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN E.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + 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,'province',TRUE) reseller_province, + 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 , + cast(is_bank_transfer AS varchar) is_bank_transfer , + cast(is_campaign AS varchar)is_campaign , + discount_type, + shipping_coupon_discount +FROM raena_order_management.order A +LEFT JOIN raena_analytics.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 +LEFT JOIN raena_order_management.channel E ON A.channel_id = E.id +WHERE cast(A.created_at+interval'7 hours' AS date) >='$reportDate' ; + +DELETE +FROM raena_analytics.business_report +where created_date::date >='$reportDate'; +--WHERE external_id IN +-- (SELECT DISTINCT external_id +-- FROM raena_analytics.business_dimensions_stage1); + + +INSERT INTO raena_analytics.business_report +SELECT DISTINCT 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, + A.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 , + a.is_campaign , + C.Country, + B.quantity , + B.retail_price, + seller_margin, + discounted_price, + loyalty_discount, + effective_coupon_discount , + additional_discount, + item_type, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END DSF_flag, + CASE + WHEN D.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 D.name IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type, + A.order_type, + F.is_offender, + cohort, + coalesce(effective_cogs,coalesce(CA.applied_cogs,CD.cogs-coalesce(promo,0))) cogs, + cast(sku_level_admin_fee as decimal(22,4)) +FROM raena_analytics.business_dimensions_stage1 A +LEFT JOIN raena_analytics.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 +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) F on B.product_sku= F.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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, + 4) CA ON B.product_sku = CA.sku +AND A.external_id = CA.order_id +LEFT JOIN + (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) CD ON B.product_sku = CD.sku +AND B.transaction_date::date BETWEEN CD.created_at AND last_date +LEFT JOIN + (SELECT DISTINCT order_id , + created_at+interval'7 hours' AS created_date, + parent_sku sku, + effective_cogs + FROM raena_order_management.sales_sub_order + WHERE price_type IS NOT NULL + AND parent_sku NOT LIKE 'BAZ%')XX ON A.external_Id=XX.order_id +AND B.product_sku= XX.sku +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND (( payment_status='Paid' or id like 'PL%') and (payment_status = 'Initiated' and is_cash_on_delivery = 'true' ))); + +DROP TABLE IF EXISTS raena_analytics.business_report_v1; + + +CREATE TABLE raena_analytics.business_report_v1 AS +SELECT * +FROM raena_analytics.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + +DELETE +FROM raena_analytics.business_report USING + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B +WHERE raena_analytics.business_report.external_id = B.external_id + AND raena_analytics.business_report.sku=B.sku + AND raena_analytics.business_report.external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + +INSERT INTO raena_analytics.business_report +SELECT external_id, + created_date, + order_discount_amount, + shipping_cost, + coupon_code, + brand_name, + product_type, + category_name, + sku_name, + sku, + reseller_id, + customer_id, + cart_id, + flash_sale_id, + shipping_to, + payment_status, + status, + stock_type, + order_placed_by, + order_warehouse, + channel_id, + medium, + marketplace, + provider, + 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, + country, + quantity, + 0 retail_price, + 0 seller_margin, + 0 discount_price, + 0 loyalty_discount, + 0 effective_coupon_discount, + 0 additional_discount, + item_type, + discount_promo_type, + 0 promo_amount, + 0 actual_wholesale_price, + 0 effective_wholesale_price, + reseller_province, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + 0 cogs, + sku_level_admin_fee +FROM + (SELECT raena_analytics.business_report_v1.* + FROM raena_analytics.business_report_v1 + INNER JOIN + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B ON raena_analytics.business_report_v1.external_id = B.external_id + AND raena_analytics.business_report_v1.sku=B.sku) A +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + +SELECT max(created_date) business_date +FROM raena_analytics.business_report; + + +DELETE +FROM raena_analytics.base_netsuite_final +WHERE external_id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + +DELETE +FROM raena_analytics.business_report +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + DELETE +FROM raena_analytics.gm_dashboard +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + +insert into raena_analytics.base_netsuite_final +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +''coupon_applied_on, +''tier, +sku product_sku, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +(pre_discount_rev-post_discount_revenue)/quantity coupon_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 loyalty_discount, +0 additional_discount, +'' item_type, +0 diff, +'' discount_type, +0 promo_amount, +pre_discount_rev/quantity actual_wholesale_price, +pre_discount_rev/quantity effective_wholesale_price, +'false' is_campaign, +'' sales_sub_order_id, +'' cogs_discount_type, +'' parent_sku, +0 +from raena_analytics.sk_product; + + +update raena_analytics.gm_dashboard +set cogs=0 +where discounted_price=0 and cogs <>0 +and transaction_date::date >='2024-01-01'; + +update raena_analytics.gm_dashboard +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.gm_dashboard +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + + +update raena_analytics.gm_dashboard +set effective_coupon_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and effective_coupon_discount >0; + + +update raena_analytics.OM_GM_DB_Product_category +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.OM_GM_DB_Product_category +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + + +insert into raena_analytics.gm_dashboard +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +B.province shipping_province, +'SK Product'coupon_code, +'' discount_type, +'' coupon_applied_on, +brand_name, +category_name, +product_type, +A.sku, +sku_name, +B.name reseller_name, +reseller reseller_email, +B.mobile reseller_mobile, +D.name tier_name, +B.id reseller_id, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 additional_discount, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +'' item_type, +'Admin Panel'order_placed_by, +cogs/quantity cogs, +'' order_recipient, +'' brand_type, +'SK product' customer_type, +gm_target, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +'' cogs_flag, +'false' is_campaign, +'false' dsf_flag, +'' cogs_discount_type, +'SK product' order_type, +'' parent_sku, +'' is_offender, +'' cohort, +0 +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.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 D.name =BB.tierName ; + + +insert into raena_analytics.business_report +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +brand_name, +product_type, +category_name, +sku_name, +A.sku, +B.id reseller_id, +null customer_id, +null cart_id, +null flash_sale_id, +null shipping_to, +'Paid' payment_status, +'' status, +'' stock_type, +'Admin Panel' order_placed_by, +'' order_warehouse, +'' channel_id, +'' medium, +'' marketplace, +'' provider, +D.name tier, +city reseller_city, +B.name reseller_name, +reseller reseller_email, +mobile reseller_mobile, +store reseller_store, +address_line1 shipping_address_line1, +address_line2 shipping_address_line2, +pincode shipping_pincode, +district shipping_district, +city shipping_city, +province shipping_province, +'false' is_bank_transfer, +'false' is_campaign, +'' country, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 additional_discount, +'' item_type, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +province reseller_province, +'false' dsf_flag, +case when \"el/pl_flag\"= 'Yes' then 'EL/PL' end brand_type, +'SK Product 'order_type, +'' cohort, +0 cogs, +0 +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.id ; + + +select 'raena_analytics.OM_GM_DB_Product_category'; + + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE transaction_date::date >='$reportDate' ; + +--external_Id IN +-- (SELECT DISTINCT external_id +-- FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.OM_GM_DB_Product_category +SELECT external_id, transaction_date, + \"Time\", + order_discount_amount, + shipping_cost, + coupon_code , + discount_type , + coupon_applied_on , + brand_name, + category_name , + product_type , + sku, + sku_name , + reseller_name , + reseller_email , + base.reseller_mobile , + tier_name , + reseller_id , + quantity , + retail_price , + seller_margin , + discounted_price , + additional_discount, + item_type , + order_placed_by , + cogs, + shipping_province , + Flag , + wholesale_price, + order_recipient, + brand_type, + Customer_type, + gm_target, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + CASE WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 28 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=28 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=28 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + is_campaign, + dsf_flag, + cogs_flag, + cogs_discount_type, + order_type, + parent_sku, + First_transaction_date, + is_offender, + cohort, + sku_level_admin_fee +FROM + (SELECT A.external_id , + cast(transaction_date AS date) 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + is_campaign, + A.dsf_flag, + A.cogs_flag, + A.cogs_discount_type, + A.order_type, + parent_sku, + is_offender, + cohort, + sku_level_admin_fee + FROM raena_analytics.GM_dashboard A + LEFT JOIN + (SELECT DISTINCT external_id + FROM + (SELECT external_id, + discounted_price , + sku , + (((retail_price-seller_margin)*quantity) -(cogs*quantity))/nullif((CASE WHEN retail_price<>0 THEN (retail_price-seller_margin)*quantity END),0) Pre_discount, + CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/nullif((discounted_price*quantity),0) ELSE 0 END Post_discount + FROM raena_analytics.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 raena_analytics.pricing_sheet P ON A.sku=p.skucode + WHERE cast(transaction_date AS date) >='$reportDate' + AND A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )))) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(transaction_date) om_gm_db_product_date +FROM raena_analytics.OM_GM_DB_Product_category; + +update raena_analytics.gm_dashboard + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.gm_dashboard where sku like '______X'); + + update raena_analytics.om_gm_db_product_category + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.om_gm_db_product_category where sku like '______X'); + + + +update raena_analytics.OM_GM_DB_Product_category +set retail_price=0, + seller_margin=0, + additional_discount=0, + wholesale_price=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 + where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + +update raena_analytics.gm_dashboard +set retail_price=0, + seller_margin=0, + loyalty_discount=0, + effective_coupon_discount=0, + additional_discount=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 +where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + + DELETE +FROM raena_analytics.GM_GROWTH_TAB1 +WHERE created_date >='$reportDate'; + +INSERT INTO raena_analytics.GM_GROWTH_TAB1 +SELECT tier, + 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, + base.reseller_mobile, + Number_of_Orders , + RSP , + wholesale_price , + Payment_Price , + Effective_Coupon_Discount, + Total_Loyalty_point, + additional_discount, + seller_margin, + AOV, + quantity, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + CASE + WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + external_id, + order_type, + First_transaction_date, + is_offender, + cohort, + cogs, + item_type +FROM + (SELECT tier, + 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, + reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + external_id, + order_type, + is_offender, + cohort, + cogs, + item_type + FROM + (SELECT DISTINCT coalesce(a.tier,'NA') AS tier, + 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, + reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + cogs, + item_type, + row_number() over (partition BY external_id + ORDER BY external_id) AS R + FROM raena_analytics.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 + left join (select distinct id, is_cash_on_delivery + from raena_order_management.order + where cast(created_at AS date) >= '$reportDate' and is_cash_on_delivery='true') D + on A.external_id = D.id + WHERE (( payment_status='Paid' or a.external_id like 'PL%') or (payment_status='Initiated' and is_cash_on_delivery='true')) + AND created_date >= '$reportDate') A + GROUP BY tier, + 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, + reseller_mobile, + external_id, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + cogs, + item_type) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(created_date) gm_growth_date +FROM raena_analytics.GM_GROWTH_TAB1; + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB2; + + +CREATE TABLE raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'quarter' AS frequency, + To_char(created_date,'quarter') AS month_name, + date_part('quarter',created_date) AS sort , + * +FROM raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'month' AS frequency, + To_char(created_date,'month') AS month_name, + date_part('month',created_date) AS sort , + * +FROM raena_analytics.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 raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +ORDER BY 3; + + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB3; +CREATE TABLE raena_analytics.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +ORDER BY 3; + +DROP TABLE raena_analytics.shipping_fee_base_table; + + +CREATE TABLE raena_analytics.shipping_fee_base_table AS +SELECT A.order_id , + sum(CASE WHEN A.sales_sub_order_shipment_id = C.reference_id THEN ((C.shipping_amount)/(C.order_weight))*(C.final_weight) ELSE B.shipping_amount END) shipping_amount +FROM + (SELECT DISTINCT order_id , + sales_sub_order_shipment_id + FROM raena_order_management.sales_sub_order) A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.sales_sub_order_shipment_id = B.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping C ON A.sales_sub_order_shipment_id = C.reference_id +GROUP BY A.order_id; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage1 ; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + + +DROP TABLE raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.shipping_fee_sku_base_stage2_v1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2_v1 AS +SELECT order_id , + sales_sub_order_shipment_id, + CASE + WHEN (sum(B.weight*Quantity))<1.3 THEN 1 + WHEN (sum(B.weight*Quantity))>=1.3 + AND (ABS(sum(B.weight*Quantity)) - FLOOR(ABS(sum(B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.weight*Quantity))+1 + ELSE FLOOR(sum(B.weight*Quantity)) + END AS sku_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +GROUP BY 1, + 2; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2 AS +SELECT A.*, + B.weight*sku_weight/sum(B.weight) over(partition BY A.order_id , A.sales_sub_order_shipment_id) Final_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.shipping_fee_sku_base_stage2_v1 C ON A.order_id = C.order_id +AND A.sales_sub_order_shipment_id = C.sales_sub_order_shipment_id +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage3; + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage3 AS +SELECT A.*, + (Final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(Final_weight) over(partition BY A.sales_sub_order_shipment_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage2 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.sales_sub_order_shipment_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.sales_sub_order_shipment_id = C.reference_id ; + +DROP TABLE raena_analytics.sku_level_shipping_fee_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM raena_analytics.gm_dashboard A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +where A.order_type <> 'SK product'; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage1 ; + + +DROP TABLE raena_analytics.sku_level_shipping_fee_old_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3 +union + select distinct order_id , sku , 'Product' , + sum(shipping_fee_sku_level) + from raena_analytics.sk_product +group by 1,2 ; + + + + +DROP TABLE IF EXISTS raena_analytics.payment_order_stage; + + +CREATE TABLE raena_analytics.payment_order_stage 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, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM NS +INNER JOIN raena_order_management.payment B ON NS.n <= REGEXP_COUNT(B.order_ids , ',') + 1 +GROUP BY 1, + 3,4,5,6,7; + + +DROP TABLE IF EXISTS raena_analytics.payment_order; + + +CREATE TABLE raena_analytics.payment_order AS +SELECT A.id , + A.created_at, + A.order_id, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_analytics.payment_order_stage A +INNER JOIN + (SELECT order_id , + max(created_at) max_created_at + FROM raena_analytics.payment_order_stage + GROUP BY order_id) B ON A.created_at= B.max_created_at +AND A.order_id = B.order_id; + +select count(1) from raena_analytics.payment_order; + +drop table raena_analytics.om_app_admin_dump_table; + + +CREATE TABLE raena_analytics.om_app_admin_dump_table AS +SELECT A.id AS order_id, + (A.created_at+interval '7 hours') AS order_date, + (A.created_at+interval '7 hours') AS id_order_date, + A.status AS order_status, + A.Payment_status, + D.name AS channel_name, + cast(A.is_bank_transfer AS varchar) AS bank_transfer, + cast(A.is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + GB.payment_amount, + coalesce(GB.new_shipping_amount_final,shipping_amount) AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + A.Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + A.shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN A.order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + A.stock_type AS product_stock_type, + coalesce(GB.sku,oi.sku) AS product_sku, + coalesce(gb.sku_name,oi.name) AS product_name, + product_code, + date_key, + coalesce(GB.product_type,oi.product_type) product_type, + batch, + country, + coalesce(GB.quantity,oi.quantity) quantity, + coalesce(GB.actual_wholesale_price,oi.wholesale_price) wholesale_price, + coalesce(GB.retail_price,oi.retail_price) retail_price, + ((GB.retail_price*GB.quantity)-(GB.seller_margin*GB.quantity)) total_price, + Total_earnings, + A.is_slash_price_enabled , + coalesce(gb.brand_name,oi.Brand_name) brand_name, + coalesce(gb.category_name,oi.category_name) category_name, + oi.product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + (GB.cogs) as total_cogs, + case when oi.parent_sku is null then 'NA' else oi.parent_sku end bundle_sku, + new_shipping_amount sku_level_shipping_amount, + GB.discounted_price*GB.quantity sku_level_payment_amount, + is_cash_on_delivery, + payment_provider, + payment_type, + payment_category, + payment_channel, + admin_fee, + service_fee +FROM raena_order_management.order A +inner join (select AD.* , + sum(new_shipping_amount) over(partition by AD.external_id) new_shipping_amount_final , + sum(discounted_price*quantity) over(partition by AD.external_id) payment_amount + from (SELECT A.*, + discounted_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_discounted_price, + actual_wholesale_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_actual_wholesale_price , + retail_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_retail_price, + seller_margin/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_seller_margin , + cogs/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_cogs, + (case when (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)/count(1) over(partition BY A.external_id,A.sku,A.item_type) new_shipping_amount + 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) AD + )GB + ON A.id = GB.external_id AND GB.sku not like 'BAZ%' + left JOIN (select A.*,B.sku parent_sku + from raena_order_management.order_item A + left join raena_order_management.order_item B on A.parent_item_id = B.id ) oi + ON GB.external_id=oi.order_id + AND OI.SKU not like 'BAZ%' + AND GB.sku=oi.sku + and GB.parent_sku= oi.parent_sku +left join raena_analytics.payment_order C on A.id=C.order_id +left join raena_order_management.channel D on A.channel_id = D.id +WHERE cast(A.created_at+interval'7 hours' as date)>='2021-10-28'; + +DROP TABLE IF EXISTS raena_analytics.remove_duplicate_from_app_admin; + + +CREATE TABLE raena_analytics.remove_duplicate_from_app_admin AS +SELECT A.external_id, + wholesale_price_gm, + wholesale_price_app_admin, + wholesale_price_gm-wholesale_price_app_admin diff +FROM + (SELECT external_id , + sum(discounted_price*quantity) P1, + sum((retail_price*quantity)-(seller_margin*quantity)) wholesale_price_gm + FROM raena_analytics.gm_dashboard gd + GROUP BY external_id) A +FULL OUTER JOIN + (SELECT order_id , + payment_amount p2, + sum(total_price) wholesale_price_app_admin + FROM raena_analytics.om_app_admin_dump_table oaadt + GROUP BY order_id, + payment_amount) B ON A.external_id= B.order_id +WHERE wholesale_price_gm-wholesale_price_app_admin<>0; + +DROP TABLE IF EXISTS raena_analytics.dubplicate_record_app_admin; + +CREATE TABLE raena_analytics.dubplicate_record_app_admin AS +SELECT DISTINCT * +FROM raena_analytics.om_app_admin_dump_table oaadt +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + +DELETE +FROM raena_analytics.om_app_admin_dump_table +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + +INSERT INTO raena_analytics.om_app_admin_dump_table +SELECT * +FROM raena_analytics.dubplicate_record_app_admin; + +" > /home/ec2-user/cronjob/redshift/sql_code/gm_etl_cron.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/gm_etl_cron.sql + + + diff --git a/redshift/gm_dashboard_new.sh b/redshift/gm_dashboard_new.sh new file mode 100644 index 0000000..143ffef --- /dev/null +++ b/redshift/gm_dashboard_new.sh @@ -0,0 +1,2481 @@ + #!/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.manual_bundle_sku_data ; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.order_level_data; + + +CREATE TABLE raena_analytics.order_level_data AS +SELECT DISTINCT external_id, + transaction_date+interval'7 hours' AS transaction_date, + discount_amount, + shipping_cost, + A.coupon_code, + replace(cast(reseller_tier_name AS varchar),'\"','')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, + is_campaign, + order_placed_by, + admin_fee, + service_fee +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, + cast(is_campaign AS varchar) is_campaign, + order_placed_by, + admin_fee, + service_fee + FROM raena_order_management.order A + WHERE ((payment_status='Paid' + OR id LIKE 'PL%') + OR (payment_status = 'Initiated' + AND is_cash_on_delivery = 'true')) + AND cast(A.created_at+interval '7 hours' AS date) >='$reportDate') A +LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage1_V1; + + +CREATE TABLE raena_analytics.base_netsuite_stage1_V1 AS +SELECT DISTINCT transaction_date 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_wholesale_price, + CASE + WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount + ELSE B.coupon_discount + END discount_price, + CASE + WHEN discount_amount > 0 + AND A.coupon_code IS NULL + AND A.order_placed_by = 'admin' + AND coupon_applied_on IS NULL THEN 'Cart' + ELSE coupon_applied_on + END 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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type, + a.is_campaign, + B.cogs_discount_type, + A.admin_fee, + A.service_fee +FROM raena_analytics.order_level_data A +LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id +AND status <> 'Cancelled' +LEFT JOIN raena_analytics.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, + actual_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+interval'7 hours' AS date)>='$reportDate') F ON B.id = F.sales_sub_order_id +ORDER BY 1, + 2; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1_base ; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1_base 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, + actual_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, + price_type, + is_campaign, + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND A.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + cogs_discount_type, + admin_fee, + service_fee +FROM raena_analytics.base_netsuite_stage1_V1 A +LEFT JOIN + (SELECT external_id , + sku , + final_loyalty_point + FROM raena_analytics.final_loyalty_point) B ON A.external_id = B.external_id +AND A.sku = B.sku +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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, + 4) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON A.sku = D.sku +AND A.transaction_date::date BETWEEN D.created_at AND last_date; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1 AS +SELECT transaction_date, + A.external_id, + A.sku, + parent_sku, + quantity quantity, + parent_quantity, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE retail_price + END retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_retail_price + END parent_retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE effective_wholesale_price + END effective_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_wholesale_price + END parent_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE actual_wholesale_price + END actual_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_price + END discount_price, + coupon_applied_on, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_amount + END discount_amount, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE dynamic_price + END dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_dynamic_price + END parent_dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_price + END payment_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_amount + END payment_amount, + product_class, + parent_product_class, + product_type_class, + sales_sub_order_id, + loyalty_discount, + price_type, + is_campaign, + CASE + WHEN A.sku = D.sku THEN 0 + ELSE cogs + END cogs, + cogs_flag, + cogs_discount_type, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE admin_fee + END admin_fee, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE service_fee + END service_fee +FROM raena_analytics.base_netsuite_stage2_v1_base A +LEFT JOIN + (SELECT DISTINCT sku + FROM raena_catalog_management.product + WHERE lower(name) LIKE '%not for sale%' + OR lower(name) LIKE '%free item%' + OR lower(name) LIKE '%free gift%') D ON A.sku = D.sku ; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage3_v1 ; + + +CREATE TABLE raena_analytics.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 + AND retail_price<>0 THEN retail_price*quantity END)over(partitionBY A.external_id) + WHEN coupon_applied_on <> 'Cart' + AND discount_price=0 THEN 0 + WHEN coupon_applied_on ='Cart' + AND discount_price>=0 THEN discount_price + WHEN coupon_applied_on ='Cart' + AND discount_price=0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id) + END*/ + discount_price AS Final_discount , + retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + CASE + WHEN product_type_class IN ('Flash Bundle', + 'Flash') THEN 'Flash' + WHEN coupon_applied_on ='Cart' + AND price_type ='NEGOTIATED_PRICE' THEN 'Negotiated' + WHEN coupon_applied_on = 'Shipping Fee' THEN 'Shipping' + WHEN coupon_applied_on IS NOT NULL THEN 'Brand' + END discount_type +FROM raena_analytics.base_netsuite_stage2_v1 A ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage4_v1 ; + + +CREATE TABLE raena_analytics.base_netsuite_stage4_v1 AS +SELECT A.* , + CASE + WHEN (parent_product_class= 'Bundle' + OR product_type_class IN('Bundle', 'Flash Bundle')) + AND A.external_id = B.external_id + AND A.parent_sku = B.parent_sku and ttl_wholesale_price<>0 THEN (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/ttl_wholesale_price) + END AS additional_discount, + CASE + WHEN final_discount<>0 THEN final_discount-seller_margin + END AS effective_coupon_discount +FROM raena_analytics.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 + FROM raena_analytics.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 raena_analytics.base_netsuite_final +where transaction_date::date>='$reportDate'; + + +INSERT INTO raena_analytics.base_netsuite_final +SELECT A.external_id, + A.transaction_date 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) -coalesce((CASE WHEN final_discount >0 THEN 0 ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity END),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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, + 0 diff, + CASE + WHEN price_type IS NOT NULL THEN price_type + ELSE discount_type + END discount_type , + CASE + WHEN final_discount >0 THEN 0 + ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity + END promo_amount, + actual_wholesale_price, + effective_wholesale_price, + A.is_campaign, + '' sales_sub_order_id, + cogs_discount_type, + parent_sku, + null, + B.admin_fee sku_level_admin_fee, + B.service_fee sku_leel_service_fee +FROM raena_analytics.order_level_data A +LEFT JOIN raena_analytics.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + + + +UPDATE raena_analytics.base_netsuite_final AA +SET discounted_price = new_discounted_price +from (SELECT external_id , + product_sku , + cast(settlement_amount/quantity AS decimal(22,2)) new_discounted_price + FROM + (SELECT A.*, + discounted_price , + (effective_wholesale_price*B.\"Settlement amount\")/sum(effective_wholesale_price) over (partition BY external_id) settlement_amount , + B.\"Settlement amount\" + FROM raena_analytics.base_netsuite_final A + INNER JOIN (select distinct * from raena_analytics.mp_orders_hn_wd) B ON external_id= B.\"Order ID \" + WHERE A.transaction_date >'2022-12-31' and effective_wholesale_price<>0 ) A) BB + where AA.external_id = BB.external_id +AND AA.product_sku = BB.product_sku +and AA.transaction_date >'2022-12-31'; + + + +update raena_analytics.base_netsuite_final A +set discounted_price = B.payment_price/quantity +from (select order_id,sku, max(payment_price) payment_price from raena_analytics.order_data_update group by 1,2) B +where A.external_id = B.order_id and A.parent_sku = B.sku and product_sku = parent_sku ; + + +update raena_analytics.base_netsuite_final A +set discounted_price = new_payment_price/A.quantity +from raena_analytics.import_data_for_sku_level_payment_amount B +where A.external_id = B.order_id and A.parent_sku = B.sku and A.product_sku <> A.parent_sku; + +with commen_data as +(select external_id order_id , product_sku sku,quantity sku_quantity, + sku_level_admin_fee*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition by external_id ),0) sku_level_admin_fee_final + from raena_analytics.base_netsuite_final + where transaction_date::date >='$reportDate' + ) +update raena_analytics.base_netsuite_final +set sku_level_admin_fee = sku_level_admin_fee_final +from commen_data where external_id = order_id and product_sku = sku and sku_quantity = quantity +and transaction_date::date >='$reportDate' ; + + + +with commen_data as +(select external_id order_id , product_sku sku, quantity sku_quantity, + service_fee_at_sku_level*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition by external_id ),0) sku_level_service_fee_final + from raena_analytics.base_netsuite_final + where transaction_date::date >='$reportDate' + ) +update raena_analytics.base_netsuite_final +set service_fee_at_sku_level = sku_level_service_fee_final +from commen_data where external_id = order_id and product_sku = sku and sku_quantity = quantity +and transaction_date::date >='$reportDate' ; + +SELECT max(transaction_date) Netsuite_base_date +FROM raena_analytics.base_netsuite_final; + +DROP TABLE IF EXISTS raena_analytics.gm_dimensions_stage1; + + +CREATE TABLE raena_analytics.gm_dimensions_stage1 AS +SELECT distinct 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, + shipping_to, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + cast(is_campaign AS varchar) is_campaign +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 +LEFT JOIN raena_order_management.channel D ON A.channel_id = D.id +WHERE (( payment_status='Paid' or A.id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + AND cast(A.created_at+interval'7 hours' AS date) >='$reportDate'; + + + drop table if exists raena_analytics.sku_gm_mapping; + +create table raena_analytics.sku_gm_mapping +as +select A.sku ,A.name sku_name,B.name brand_name,C.name as category_name,product_type + 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 ; + + +DELETE +FROM raena_analytics.GM_dashboard +where transaction_date::date >='$reportDate'; + +INSERT INTO raena_analytics.GM_dashboard +SELECT A.external_id , + B.transaction_date AS transaction_date, + B.order_discount_amount , + B.shipping_cost , + A.shipping_province, + B.coupon_code , + A.discount_type , + B.coupon_applied_on , + brand_name, + category_name , + product_type , + product_sku sku, + sku_name , + A.reseller_name , + A.reseller_email , + A.reseller_mobile , + reseller_tier_name tier_name, + A.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 , + coalesce(effective_cogs,coalesce(C.applied_cogs,D.cogs-coalesce(promo,0))) 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type , + Customer_type, + BB.gm_target, + B.discount_type discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + coalesce(case when cogs_type ='REGULAR' then 'Promo' else cogs_type end ,CASE WHEN B.product_sku = D.sku + AND B.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + A.is_campaign, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END AS DSF_Flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender, + cohort, + cast(sku_level_admin_fee as decimal(22,4)), + cast(service_fee_at_sku_level as decimal(22,4)) +FROM raena_analytics.gm_dimensions_stage1 A +INNER JOIN raena_analytics.base_netsuite_final B ON A.external_id =B.external_id +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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,4) C ON B.product_sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON B.product_sku = D.sku +AND B.transaction_date::date BETWEEN D.created_at AND last_date +left join (select distinct order_id , + created_at+interval'7 hours' AS created_date, + parent_sku sku, + effective_cogs + from raena_order_management.sales_sub_order + where price_type is not null and parent_sku not like 'BAZ%')XX + on A.external_Id=XX.order_id and B.product_sku= XX.sku +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 B.product_sku = BB.sku +AND A.reseller_tier_name =BB.tierName +LEFT JOIN raena_analytics.sku_gm_mapping AAA ON B.product_sku = AAA.sku +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) BBB on B.product_sku=BBB.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid') ; + + +SELECT max(transaction_date) gm_dashboard_date +FROM raena_analytics.GM_dashboard; + +DROP TABLE IF EXISTS raena_analytics.GM_dashboard_v1; + + +CREATE TABLE raena_analytics.GM_dashboard_v1 AS +SELECT * +FROM raena_analytics.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +DELETE +FROM raena_analytics.GM_dashboard +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.GM_dashboard +SELECT external_id, + transaction_date, + order_discount_amount, + shipping_cost, + shipping_province, + coupon_code, + discount_type, + coupon_applied_on, + brand_name, + category_name, + product_type, + sku, + sku_name, + reseller_name, + reseller_email, + reseller_mobile, + tier_name, + reseller_id, + quantity, + 0 retail_price, + 0 seller_margin, + 0 , + 0 , + 0, + 0, + item_type, + order_placed_by, + 0 cogs, + order_recipient, + brand_type, + customer_type, + gm_target, + discount_promo_type, + 0 promo_amount, + 0, + 0, + cogs_flag, + is_campaign, + dsf_flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender, + '<=2M', + sku_level_admin_fee, + service_fee_at_sku_level +FROM raena_analytics.GM_dashboard_v1 +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + + +UPDATE raena_analytics.gm_dashboard AA +SET cogs = 0 +from (SELECT distinct order_id, sku + from raena_analytics.beau_cogs_update) BB + where AA.external_id = BB.order_id +AND AA.sku = BB.sku; + +update raena_analytics.gm_dashboard +set retail_price = 0 , +seller_margin = 0 , +discounted_price = 0 , +additional_discount = 0 , +cogs = 0 , +actual_wholesale_price = 0 , +effective_wholesale_price = 0 , +sku_level_admin_fee = 0 +where external_id = 'OD1716365410302838REG' +and sku = 'MAX005'; + +update raena_analytics.gm_dashboard +set retail_price = 0 , +seller_margin = 0 , +discounted_price = 0 , +additional_discount = 0 , +cogs = 0 , +actual_wholesale_price = 0 , +effective_wholesale_price = 0 , +sku_level_admin_fee = 0 +where parent_sku like 'BAZ%' +and sku = 'SBM018X'; + + + + +DROP TABLE IF EXISTS raena_analytics.business_dimensions_stage1; + + +CREATE TABLE raena_analytics.business_dimensions_stage1 AS +SELECT distinct A.id AS external_id , + reseller_id, + customer_id, + cart_id, + A.flash_sale_id, + shipping_to, + payment_status, + A.status, + A.stock_type, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN E.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + 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,'province',TRUE) reseller_province, + 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 , + cast(is_bank_transfer AS varchar) is_bank_transfer , + cast(is_campaign AS varchar)is_campaign , + discount_type, + shipping_coupon_discount +FROM raena_order_management.order A +LEFT JOIN raena_analytics.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 +LEFT JOIN raena_order_management.channel E ON A.channel_id = E.id +WHERE cast(A.created_at+interval'7 hours' AS date) >='$reportDate' ; + +DELETE +FROM raena_analytics.business_report +where created_date::date >='$reportDate'; + +INSERT INTO raena_analytics.business_report +SELECT DISTINCT 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, + A.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 , + a.is_campaign , + C.Country, + B.quantity , + B.retail_price, + seller_margin, + discounted_price, + loyalty_discount, + effective_coupon_discount , + additional_discount, + item_type, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END DSF_flag, + CASE + WHEN D.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 D.name IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type, + A.order_type, + F.is_offender, + cohort, + coalesce(effective_cogs,coalesce(CA.applied_cogs,CD.cogs-coalesce(promo,0))) cogs, + cast(sku_level_admin_fee as decimal(22,4)), + cast(service_fee_at_sku_level as decimal(22,4)) + FROM raena_analytics.business_dimensions_stage1 A +LEFT JOIN raena_analytics.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 +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) F on B.product_sku= F.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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, + 4) CA ON B.product_sku = CA.sku +AND A.external_id = CA.order_id +LEFT JOIN + (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) CD ON B.product_sku = CD.sku +AND B.transaction_date::date BETWEEN CD.created_at AND last_date +LEFT JOIN + (SELECT DISTINCT order_id , + created_at+interval'7 hours' AS created_date, + parent_sku sku, + effective_cogs + FROM raena_order_management.sales_sub_order + WHERE price_type IS NOT NULL + AND parent_sku NOT LIKE 'BAZ%')XX ON A.external_Id=XX.order_id +AND B.product_sku= XX.sku +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND (( payment_status='Paid' or id like 'PL%') and (payment_status = 'Initiated' and is_cash_on_delivery = 'true' ))); + + DROP TABLE IF EXISTS raena_analytics.business_report_v1; + + +CREATE TABLE raena_analytics.business_report_v1 AS +SELECT * +FROM raena_analytics.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + + +DELETE +FROM raena_analytics.business_report USING + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B +WHERE raena_analytics.business_report.external_id = B.external_id + AND raena_analytics.business_report.sku=B.sku + AND raena_analytics.business_report.external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + + +INSERT INTO raena_analytics.business_report +SELECT external_id, + created_date, + order_discount_amount, + shipping_cost, + coupon_code, + brand_name, + product_type, + category_name, + sku_name, + sku, + reseller_id, + customer_id, + cart_id, + flash_sale_id, + shipping_to, + payment_status, + status, + stock_type, + order_placed_by, + order_warehouse, + channel_id, + medium, + marketplace, + provider, + 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, + country, + quantity, + 0 retail_price, + 0 seller_margin, + 0 discount_price, + 0 loyalty_discount, + 0 effective_coupon_discount, + 0 additional_discount, + item_type, + discount_promo_type, + 0 promo_amount, + 0 actual_wholesale_price, + 0 effective_wholesale_price, + reseller_province, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + 0 cogs, + sku_level_admin_fee, + service_fee_at_sku_level +FROM + (SELECT raena_analytics.business_report_v1.* + FROM raena_analytics.business_report_v1 + INNER JOIN + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B ON raena_analytics.business_report_v1.external_id = B.external_id + AND raena_analytics.business_report_v1.sku=B.sku) A +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + +SELECT max(created_date) business_date +FROM raena_analytics.business_report; + + +DELETE +FROM raena_analytics.base_netsuite_final +WHERE external_id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + +DELETE +FROM raena_analytics.business_report +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + DELETE +FROM raena_analytics.gm_dashboard +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + +insert into raena_analytics.base_netsuite_final +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +''coupon_applied_on, +''tier, +sku product_sku, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +(pre_discount_rev-post_discount_revenue)/quantity coupon_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 loyalty_discount, +0 additional_discount, +'' item_type, +0 diff, +'' discount_type, +0 promo_amount, +pre_discount_rev/quantity actual_wholesale_price, +pre_discount_rev/quantity effective_wholesale_price, +'false' is_campaign, +'' sales_sub_order_id, +'' cogs_discount_type, +'' parent_sku, +0 +from raena_analytics.sk_product; + + +update raena_analytics.gm_dashboard +set cogs=0 +where discounted_price=0 and cogs <>0 +and transaction_date::date >='2024-01-01'; + +update raena_analytics.gm_dashboard +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.gm_dashboard +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + +update raena_analytics.gm_dashboard +set effective_coupon_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and effective_coupon_discount >0; + + +update raena_analytics.OM_GM_DB_Product_category +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.OM_GM_DB_Product_category +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + + +insert into raena_analytics.gm_dashboard +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +B.province shipping_province, +'SK Product'coupon_code, +'' discount_type, +'' coupon_applied_on, +brand_name, +category_name, +product_type, +A.sku, +sku_name, +B.name reseller_name, +reseller reseller_email, +B.mobile reseller_mobile, +D.name tier_name, +B.id reseller_id, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 additional_discount, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +'' item_type, +'Admin Panel'order_placed_by, +cogs/quantity cogs, +'' order_recipient, +'' brand_type, +'SK product' customer_type, +gm_target, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +'' cogs_flag, +'false' is_campaign, +'false' dsf_flag, +'' cogs_discount_type, +'SK product' order_type, +'' parent_sku, +'' is_offender, +'' cohort, +0 +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.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 D.name =BB.tierName ; + + +insert into raena_analytics.business_report +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +brand_name, +product_type, +category_name, +sku_name, +A.sku, +B.id reseller_id, +null customer_id, +null cart_id, +null flash_sale_id, +null shipping_to, +'Paid' payment_status, +'' status, +'' stock_type, +'Admin Panel' order_placed_by, +'' order_warehouse, +'' channel_id, +'' medium, +'' marketplace, +'' provider, +D.name tier, +city reseller_city, +B.name reseller_name, +reseller reseller_email, +mobile reseller_mobile, +store reseller_store, +address_line1 shipping_address_line1, +address_line2 shipping_address_line2, +pincode shipping_pincode, +district shipping_district, +city shipping_city, +province shipping_province, +'false' is_bank_transfer, +'false' is_campaign, +'' country, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 additional_discount, +'' item_type, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +province reseller_province, +'false' dsf_flag, +case when \"el/pl_flag\"= 'Yes' then 'EL/PL' end brand_type, +'SK Product 'order_type, +'' cohort, +0 cogs, +0 +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.id ; + + + +select 'raena_analytics.OM_GM_DB_Product_category'; + + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE transaction_date::date >='$reportDate' ; + +--external_Id IN +-- (SELECT DISTINCT external_id +-- FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.OM_GM_DB_Product_category +SELECT external_id, transaction_date, + \"Time\", + order_discount_amount, + shipping_cost, + coupon_code , + discount_type , + coupon_applied_on , + brand_name, + category_name , + product_type , + sku, + sku_name , + reseller_name , + reseller_email , + base.reseller_mobile , + tier_name , + reseller_id , + quantity , + retail_price , + seller_margin , + discounted_price , + additional_discount, + item_type , + order_placed_by , + cogs, + shipping_province , + Flag , + wholesale_price, + order_recipient, + brand_type, + Customer_type, + gm_target, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + CASE WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 28 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=28 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=28 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + is_campaign, + dsf_flag, + cogs_flag, + cogs_discount_type, + order_type, + parent_sku, + First_transaction_date, + is_offender, + cohort, + sku_level_admin_fee, + service_fee_at_sku_level +FROM + (SELECT A.external_id , + cast(transaction_date AS date) 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + is_campaign, + A.dsf_flag, + A.cogs_flag, + A.cogs_discount_type, + A.order_type, + parent_sku, + is_offender, + cohort, + sku_level_admin_fee, + service_fee_at_sku_level + FROM raena_analytics.GM_dashboard A + LEFT JOIN + (SELECT DISTINCT external_id + FROM + (SELECT external_id, + discounted_price , + sku , + (((retail_price-seller_margin)*quantity) -(cogs*quantity))/nullif((CASE WHEN retail_price<>0 THEN (retail_price-seller_margin)*quantity END),0) Pre_discount, + CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/nullif((discounted_price*quantity),0) ELSE 0 END Post_discount + FROM raena_analytics.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 raena_analytics.pricing_sheet P ON A.sku=p.skucode + WHERE cast(transaction_date AS date) >='$reportDate' + AND A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )))) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(transaction_date) om_gm_db_product_date +FROM raena_analytics.OM_GM_DB_Product_category; + +update raena_analytics.gm_dashboard + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.gm_dashboard where sku like '______X'); + + update raena_analytics.om_gm_db_product_category + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.om_gm_db_product_category where sku like '______X'); + +update raena_analytics.OM_GM_DB_Product_category +set retail_price=0, + seller_margin=0, + additional_discount=0, + wholesale_price=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 + where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + +update raena_analytics.gm_dashboard +set retail_price=0, + seller_margin=0, + loyalty_discount=0, + effective_coupon_discount=0, + additional_discount=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 +where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + + DELETE +FROM raena_analytics.GM_GROWTH_TAB1 +WHERE created_date >='$reportDate'; + +INSERT INTO raena_analytics.GM_GROWTH_TAB1 +SELECT tier, + 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, + base.reseller_mobile, + Number_of_Orders , + RSP , + wholesale_price , + Payment_Price , + Effective_Coupon_Discount, + Total_Loyalty_point, + additional_discount, + seller_margin, + AOV, + quantity, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + CASE + WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + external_id, + order_type, + First_transaction_date, + is_offender, + cohort, + cogs, + item_type +FROM + (SELECT tier, + 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, + reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + external_id, + order_type, + is_offender, + cohort, + cogs, + item_type + FROM + (SELECT DISTINCT coalesce(a.tier,'NA') AS tier, + 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, + reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + cogs, + item_type, + row_number() over (partition BY external_id + ORDER BY external_id) AS R + FROM raena_analytics.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 + left join (select distinct id, is_cash_on_delivery + from raena_order_management.order + where cast(created_at AS date) >= '$reportDate' and is_cash_on_delivery='true') D + on A.external_id = D.id + WHERE (( payment_status='Paid' or a.external_id like 'PL%') or (payment_status='Initiated' and is_cash_on_delivery='true')) + AND created_date >= '$reportDate') A + GROUP BY tier, + 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, + reseller_mobile, + external_id, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + cogs, + item_type) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + + +SELECT max(created_date) gm_growth_date +FROM raena_analytics.GM_GROWTH_TAB1; + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB2; + + +CREATE TABLE raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'quarter' AS frequency, + To_char(created_date,'quarter') AS month_name, + date_part('quarter',created_date) AS sort , + * +FROM raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'month' AS frequency, + To_char(created_date,'month') AS month_name, + date_part('month',created_date) AS sort , + * +FROM raena_analytics.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 raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +ORDER BY 3; + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB3; +CREATE TABLE raena_analytics.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +ORDER BY 3; + +DROP TABLE raena_analytics.shipping_fee_base_table; + + +CREATE TABLE raena_analytics.shipping_fee_base_table AS +SELECT A.order_id , + sum(CASE WHEN A.sales_sub_order_shipment_id = C.reference_id THEN ((C.shipping_amount)/(C.order_weight))*(C.final_weight) ELSE B.shipping_amount END) shipping_amount +FROM + (SELECT DISTINCT order_id , + sales_sub_order_shipment_id + FROM raena_order_management.sales_sub_order) A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.sales_sub_order_shipment_id = B.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping C ON A.sales_sub_order_shipment_id = C.reference_id +GROUP BY A.order_id; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage1 ; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + + + + +DROP TABLE raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.shipping_fee_sku_base_stage2_v1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2_v1 AS +SELECT order_id , + sales_sub_order_shipment_id, + CASE + WHEN (sum(B.weight*Quantity))<1.3 THEN 1 + WHEN (sum(B.weight*Quantity))>=1.3 + AND (ABS(sum(B.weight*Quantity)) - FLOOR(ABS(sum(B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.weight*Quantity))+1 + ELSE FLOOR(sum(B.weight*Quantity)) + END AS sku_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +GROUP BY 1, + 2; + + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2 AS +SELECT A.*, + B.weight*sku_weight/sum(B.weight) over(partition BY A.order_id , A.sales_sub_order_shipment_id) Final_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.shipping_fee_sku_base_stage2_v1 C ON A.order_id = C.order_id +AND A.sales_sub_order_shipment_id = C.sales_sub_order_shipment_id +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage3; + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage3 AS +SELECT A.*, + (Final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(Final_weight) over(partition BY A.sales_sub_order_shipment_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage2 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.sales_sub_order_shipment_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.sales_sub_order_shipment_id = C.reference_id ; + +DROP TABLE raena_analytics.sku_level_shipping_fee_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM raena_analytics.gm_dashboard A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +where A.order_type <> 'SK product'; + + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage1 ; + + +DROP TABLE raena_analytics.sku_level_shipping_fee_old_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3 +union + select distinct order_id , sku , 'Product' , + sum(shipping_fee_sku_level) + from raena_analytics.sk_product +group by 1,2 ; + + +WITH commen_data AS + (SELECT external_id order_id , + sku product_sku , + service_fee, + quantity sku_quantity, + round(service_fee*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition BY external_id),0)) sku_level_service_fee_final + FROM raena_analytics.gm_dashboard A + INNER JOIN + (SELECT id , + service_fee + FROM raena_order_management.order + WHERE service_fee>0) B ON A.external_id = B.id + ORDER BY 1) +UPDATE raena_analytics.gm_dashboard +SET service_fee_at_sku_level = sku_level_service_fee_final +FROM commen_data +WHERE external_id = order_id + AND sku = product_sku + AND quantity=sku_quantity ; + + + + WITH commen_data AS + (SELECT external_id order_id , + sku product_sku , + service_fee, + quantity sku_quantity, + round(service_fee*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition BY external_id),0)) sku_level_service_fee_final + FROM raena_analytics.gm_dashboard A + INNER JOIN + (SELECT id , + service_fee + FROM raena_order_management. + ORDER + WHERE service_fee>0) B ON A.external_id = B.id + ORDER BY 1) + UPDATE raena_analytics.om_gm_db_product_category + SET service_fee_at_sku_level = sku_level_service_fee_final + FROM commen_data WHERE external_id = order_id + AND sku = product_sku + AND quantity=sku_quantity; + + +UPDATE raena_analytics.om_gm_db_product_category + SET service_fee_at_sku_level = 0 + where service_fee_at_sku_level is null ; + +UPDATE raena_analytics.gm_dashboard + SET service_fee_at_sku_level = 0 + where service_fee_at_sku_level is null ; + + +UPDATE raena_analytics.om_gm_db_product_category + SET sku_level_admin_fee = 0 + where sku_level_admin_fee is null ; + +UPDATE raena_analytics.gm_dashboard + SET sku_level_admin_fee = 0 + where sku_level_admin_fee is null ; + + + + +DROP TABLE IF EXISTS raena_analytics.payment_order_stage; + + +CREATE TABLE raena_analytics.payment_order_stage 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, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM NS +INNER JOIN raena_order_management.payment B ON NS.n <= REGEXP_COUNT(B.order_ids , ',') + 1 +GROUP BY 1, 3,4,5,6,7; + + +DROP TABLE IF EXISTS raena_analytics.payment_order; + + +CREATE TABLE raena_analytics.payment_order AS +SELECT A.id , + A.created_at, + A.order_id, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_analytics.payment_order_stage A +INNER JOIN + (SELECT order_id , + max(created_at) max_created_at + FROM raena_analytics.payment_order_stage + GROUP BY order_id) B ON A.created_at= B.max_created_at +AND A.order_id = B.order_id; + +select count(1) from raena_analytics.payment_order; + +drop table raena_analytics.om_app_admin_dump_table; + + +CREATE TABLE raena_analytics.om_app_admin_dump_table AS +SELECT A.id AS order_id, + (A.created_at+interval '7 hours') AS order_date, + (A.created_at+interval '7 hours') AS id_order_date, + A.status AS order_status, + A.Payment_status, + D.name AS channel_name, + cast(A.is_bank_transfer AS varchar) AS bank_transfer, + cast(A.is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + GB.payment_amount, + coalesce(GB.new_shipping_amount_final,shipping_amount) AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + A.Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + A.shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN A.order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + A.stock_type AS product_stock_type, + coalesce(GB.sku,oi.sku) AS product_sku, + coalesce(gb.sku_name,oi.name) AS product_name, + product_code, + date_key, + coalesce(GB.product_type,oi.product_type) product_type, + batch, + country, + coalesce(GB.quantity,oi.quantity) quantity, + coalesce(GB.actual_wholesale_price,oi.wholesale_price) wholesale_price, + coalesce(GB.retail_price,oi.retail_price) retail_price, + ((GB.retail_price*GB.quantity)-(GB.seller_margin*GB.quantity)) total_price, + Total_earnings, + A.is_slash_price_enabled , + coalesce(gb.brand_name,oi.Brand_name) brand_name, + coalesce(gb.category_name,oi.category_name) category_name, + oi.product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + (GB.cogs) as total_cogs, + case when oi.parent_sku is null then 'NA' else oi.parent_sku end bundle_sku, + new_shipping_amount sku_level_shipping_amount, + GB.discounted_price*GB.quantity sku_level_payment_amount, + is_cash_on_delivery, + payment_provider, + payment_type, + payment_category, + payment_channel, + admin_fee, + service_fee +FROM raena_order_management.order A +inner join (select AD.* , + sum(new_shipping_amount) over(partition by AD.external_id) new_shipping_amount_final , + sum(discounted_price*quantity) over(partition by AD.external_id) payment_amount + from (SELECT A.*, + discounted_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_discounted_price, + actual_wholesale_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_actual_wholesale_price , + retail_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_retail_price, + seller_margin/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_seller_margin , + cogs/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_cogs, + (case when (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)/count(1) over(partition BY A.external_id,A.sku,A.item_type) new_shipping_amount + 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) AD + )GB + ON A.id = GB.external_id AND GB.sku not like 'BAZ%' + left JOIN (select A.*,B.sku parent_sku + from raena_order_management.order_item A + left join raena_order_management.order_item B on A.parent_item_id = B.id ) oi + ON GB.external_id=oi.order_id + AND OI.SKU not like 'BAZ%' + AND GB.sku=oi.sku + and GB.parent_sku= oi.parent_sku +left join raena_analytics.payment_order C on A.id=C.order_id +left join raena_order_management.channel D on A.channel_id = D.id +WHERE cast(A.created_at+interval'7 hours' as date)>='2021-10-28'; + +DROP TABLE IF EXISTS raena_analytics.remove_duplicate_from_app_admin; + + + +CREATE TABLE raena_analytics.remove_duplicate_from_app_admin AS +SELECT A.external_id, + wholesale_price_gm, + wholesale_price_app_admin, + wholesale_price_gm-wholesale_price_app_admin diff +FROM + (SELECT external_id , + sum(discounted_price*quantity) P1, + sum((retail_price*quantity)-(seller_margin*quantity)) wholesale_price_gm + FROM raena_analytics.gm_dashboard gd + GROUP BY external_id) A +FULL OUTER JOIN + (SELECT order_id , + payment_amount p2, + sum(total_price) wholesale_price_app_admin + FROM raena_analytics.om_app_admin_dump_table oaadt + GROUP BY order_id, + payment_amount) B ON A.external_id= B.order_id +WHERE wholesale_price_gm-wholesale_price_app_admin<>0; + +DROP TABLE IF EXISTS raena_analytics.dubplicate_record_app_admin; + +CREATE TABLE raena_analytics.dubplicate_record_app_admin AS +SELECT DISTINCT * +FROM raena_analytics.om_app_admin_dump_table oaadt +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + + +DELETE +FROM raena_analytics.om_app_admin_dump_table +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + +INSERT INTO raena_analytics.om_app_admin_dump_table +SELECT * +FROM raena_analytics.dubplicate_record_app_admin; + +" > /home/ec2-user/cronjob/redshift/sql_code/gm_etl_cron.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/gm_etl_cron.sql + +: + diff --git a/redshift/home_page_pdp_flash.sh b/redshift/home_page_pdp_flash.sh new file mode 100644 index 0000000..f52d1a7 --- /dev/null +++ b/redshift/home_page_pdp_flash.sh @@ -0,0 +1,749 @@ +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.homepage_base_stage1_v1; + +create table raena_analytics.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 raena_analytics.homepage_base_stage1 +where ts::date >= '$reportDate'; + +insert into raena_analytics.homepage_base_stage1 +SELECT ts, + events, + name , + email, + phone, + objectid, + json_extract_path_text(profiledata,'tier',true) tier, + json_extract_path_text(profiledata,'isguestuser',true) isguestuser, + split_part(json_extract_path_text(replace(event_props,'\\\xa0',' '),'url' ,true),'/',2) collection, + split_part(json_extract_path_text(replace(event_props,'\\\xa0',' '),'url',true) ,'/',3) collection_id, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'userId',true) userId, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'sectionName',true) sectionName, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'guest_user',true) guest_user, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'tier',true) tier_id, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'categoryName',true) categoryName, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'brand_code',true) brand_code, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'brand_name',true) brand_name, + case when events ='home_product_infinite' then json_extract_path_text(replace(event_props,'\\\xa0',' '),'brand',true) end brand_id, + case when events ='home_product_Caraousel' then json_extract_path_text(replace(event_props,'\\\xa0',' '),'brand',true) end brand, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'productId',true) productId, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'videoId',true) videoId, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'pageName',true) pageName, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'SKU_code',true) SKU_code +FROM raena_analytics.homepage_base_stage1_v1; + +drop table if exists raena_analytics.homepage_base; + +create table raena_analytics.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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11; + + + drop table if exists raena_analytics.pdp_base_stage1_v1; + +create table raena_analytics.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 raena_analytics.pdp_base_stage1 +where ts::date >= '$reportDate' ; + +insert into raena_analytics.pdp_base_stage1 +SELECT ts, + events, + name , + email, + phone, + objectid, + json_extract_path_text(profiledata,'tier',true) tier, + json_extract_path_text(profiledata,'isguestuser',true) isguestuser, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'item_brand',true) brand_name, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'item_id',true) SKU_code, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'categoryPageId',true) categoryPageId, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'screen',true) screen +FROM raena_analytics.pdp_base_stage1_v1; + + + +drop table if exists raena_analytics.pdp_base; + +create table raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9; + + +drop table if exists raena_analytics.flash_sale_order_stage_v1; + +create table raena_analytics.flash_sale_order_stage_v1 +as +SELECT BB.*,AA.flash_sale_id + FROM (select external_id , + sku, + brand_name, + tier_name, + sum(quantity)quantity, + sum((discounted_price*quantity)) discounted_price, + raena_analytics.gm_dashboard.reseller_id, + transaction_date::date transaction_date + from raena_analytics.gm_dashboard + where item_type like 'Flash%' + group by 1,2,3,4,7,8) BB + INNER JOIN (select id,flash_sale_id from raena_order_management.order where flash_sale_id IS NOT NULL)AA ON external_id = id ; + +DROP TABLE IF EXISTS raena_analytics.flash_sale_order; + + +CREATE TABLE raena_analytics.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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +delete from raena_analytics.flash_base_stage1 +where ts::date >= '$reportDate'; + +insert into raena_analytics.flash_base_stage1 +SELECT ts, + name , + email, + events, + phone, + objectid, + json_extract_path_text(profiledata,'tier',true) tier, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'item_brand',true) brand_name, + json_extract_path_text(replace(event_props,'\\\xa0',' '),'item_id',true) 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 raena_analytics.flash_sale_event_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.flash_base_stage1 +GROUP BY 1, + 2, + 3, + 4, + 5; + + +" > /home/ec2-user/cronjob/redshift/sql_code/home_page_pdp_flash.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/home_page_pdp_flash.sql + + diff --git a/redshift/inapp_appflayer_etl.sh b/redshift/inapp_appflayer_etl.sh new file mode 100644 index 0000000..c4b9c28 --- /dev/null +++ b/redshift/inapp_appflayer_etl.sh @@ -0,0 +1,269 @@ + #!/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/redshift/sql_code/etl_inappevent_appsflyer.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/etl_inappevent_appsflyer.sql + + diff --git a/redshift/inapp_appflayer_etl2.sh b/redshift/inapp_appflayer_etl2.sh new file mode 100644 index 0000000..bacd998 --- /dev/null +++ b/redshift/inapp_appflayer_etl2.sh @@ -0,0 +1,314 @@ + #!/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, + event_time::date event_time, + attributed_touch_time::date attributed_touch_time, + count( case when lower(attributed_touch_type)='impression' then 1 end )total_impression +FROM raena_appsflyer.dw_marketing_inappevent_stats A +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 12, + 13,14, + 15, + 16,17,18; + + +DROP TABLE raena_analytics.campaign_inappEvent_base_data; + + + +CREATE TABLE raena_analytics.campaign_inappEvent_base_data AS +SELECT A.* , + D.min_created_date install_date, + coalesce(reseller_mobile,C.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 + left join (SELECT profile_phone, + min(created_at) min_created_date + FROM + (SELECT profile_phone, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) created_at + FROM clevertap.app_installed + GROUP BY profile_phone + UNION + SELECT replace(mobile,'+','') profile_phone, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1) A + GROUP BY 1) D ON B.reseller_mobile=D.profile_phone; + + + 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, + sum(retail_price*quantity)total_retail_price, + tier_name, + cast(transaction_date AS date)transaction_date, + reseller_id +FROM raena_analytics.gm_dashboard +WHERE is_campaign= 'false' +GROUP BY + 4, + 5,6; + +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 , + total_retail_price, + 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, + last_transaction +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, + sum(total_retail_price) total_retail_price, + 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, + max(B.transaction_date) last_transaction, + 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, 5, + 6, + 7, + 13,14) 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, + sum(total_retail_amount)total_retail_amount +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, + sum(quantity*retail_price) total_retail_amount + 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, + B.total_retail_amount, + 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, + total_impression, + os_version , + app_version , + city, + A.reseller_mobile , + province , + ttl_order , + ttl_amount total_amount, + total_retail_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, + attributed_touch_time, + event_time , + bucket, + CASE WHEN date_trunc('Month',A.event_time)::date = date_trunc('Month',install_date)::date + AND date_trunc('Month',A.event_time)::date = date_trunc('Month',conversion_date)::date THEN 'New' + WHEN date_trunc('Month',A.event_time)::date-date_trunc('Month',install_date)::date>= 30 + AND date_trunc('Month',A.event_time)::date = date_trunc('Month',conversion_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.event_time)::date-date_trunc('Month',install_date)::date>=30 + AND date_trunc('Month',A.event_time)::date - date_trunc('Month',conversion_date)::date>=30 THEN 'Existing' + WHEN (date_part('Month',install_date) =2 or date_part('Month',conversion_date)=2 ) and + date_trunc('Month',A.event_time)::date-date_trunc('Month',install_date)::date>= 28 + AND date_trunc('Month',A.event_time)::date = date_trunc('Month',conversion_date)::date THEN 'Activation' + WHEN (date_part('Month',install_date) =2 or date_part('Month',conversion_date)=2 ) and + date_trunc('Month',A.event_time)::date-date_trunc('Month',install_date)::date>=28 + AND date_trunc('Month',A.event_time)::date - date_trunc('Month',conversion_date)::date>=28 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag +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 and mobile <> '') B ON A.reseller_mobile= B.mobile + left join (select A.*, replace(B.mobile,'+','') mobile from raena_analytics.cohort_level_reseller A + left join raena_user_management.user B on A.reseller_id = B.id) C on A.reseller_mobile = C.mobile + --LEFT JOIN raena_analytics.user_type_table flag ON replace(A.reseller_mobile,'+','')=flag.reseller_mobile + ; + + " > /home/ec2-user/cronjob/redshift/sql_code/etl_inappevent_appsflyer.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/etl_inappevent_appsflyer.sql + + diff --git a/redshift/loyalty_point.sh b/redshift/loyalty_point.sh new file mode 100644 index 0000000..dc8bbd1 --- /dev/null +++ b/redshift/loyalty_point.sh @@ -0,0 +1,87 @@ + #!/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.loyalty_reseller_stage1; + +CREATE TABLE raena_analytics.loyalty_reseller_stage1 AS +SELECT 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 (select distinct * from raena_analytics.lms_transactions) A +LEFT JOIN raena_user_management.user B ON A.resellerid = cast(B.id as varchar) +LEFT JOIN raena_user_management.tier C ON A.tierid = cast(C.id as varchar) ; + + +DROP TABLE IF EXISTS raena_analytics.loyalty_total_orders; + +CREATE TABLE raena_analytics.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 >='2021-01-01'; + + +drop table if exists raena_analytics.loyalty_base_1; + + +create table raena_analytics.loyalty_base_1 +as +select B.created_date, + coalesce(B.resellerid,A.reseller_id) reseller_id, + A.reseller_name, + coalesce(B.email,A.reseller_email) reseller_email, + coalesce(B.mobile,A.reseller_mobile) reseller_mobile, + coalesce(B.tier_name,A.reseller_tier_name) reseller_tier_name, + A.payment_status, + A.order_id, + A.payment_price, + A.order_status, + B.transactionid, + 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 raena_analytics.loyalty_reseller_stage1 B left join raena_analytics.loyalty_total_orders A + on A.order_id = B.order_id; + +" > /home/ec2-user/cronjob/redshift/sql_code/loyalty_point_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/loyalty_point_etl.sql + diff --git a/redshift/new_user_funnel_report.sh b/redshift/new_user_funnel_report.sh new file mode 100644 index 0000000..ac7ccfd --- /dev/null +++ b/redshift/new_user_funnel_report.sh @@ -0,0 +1,303 @@ + #!/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.om_clevertap_install_jan_may; + + +CREATE TABLE raena_analytics.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 cast('2022-04-01' as date) AS App_install_date, + email, + phone, + email AS user_id + FROM raena_analytics.clevertap_april_csv + UNION SELECT '2022-03-30' AS App_install_date, + email, + phone, + email AS user_id + FROM raena_analytics.clevertap_march_csv + WHERE phone NOT IN + (SELECT DISTINCT profile_phone + FROM raena_analytics.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)A)B +WHERE R=1 ; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_2; + +CREATE TABLE raena_analytics.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 raena_analytics.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)A + WHERE R=1 + AND email is NOT NULL) 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)A; + + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_3; + + +CREATE TABLE raena_analytics.om_clevertap_install_jan_may_3 AS +SELECT a.*, + cast(b.created_at AS date) AS transaction_date, + transacted_email +FROM raena_analytics.om_clevertap_install_jan_may_2 a +LEFT JOIN + (SELECT reseller_email AS transacted_email , + min(transaction_date) AS created_at + FROM raena_analytics.gm_dashboard + GROUP BY 1) b ON a.email=transacted_email; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_final; + + +CREATE TABLE raena_analytics.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<=app_install_date+interval'30 day' THEN 'Yes' + ELSE 'No' + END AS transacted_flag +FROM raena_analytics.om_clevertap_install_jan_may_3 ; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_order; + + +CREATE TABLE raena_analytics.om_clevertap_install_jan_may_order AS +SELECT * +FROM raena_analytics.om_clevertap_install_jan_may_final +WHERE transacted_email NOTNULL + AND rtrim(ltrim(transacted_email)) != ''; ; + +INSERT INTO raena_analytics.Test_1_week_om_clevertap_install_jan_may_final +SELECT *, + CURRENT_DATE-1 AS Report_date +FROM raena_analytics.om_clevertap_install_jan_may_final; + + +DROP TABLE IF EXISTS raena_analytics.om_reseller_info; + + +CREATE TABLE raena_analytics.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 raena_analytics.OM_Order_id_payment_id; + + +CREATE TABLE raena_analytics.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; + +delete from raena_analytics.OM_Events_user_type +where cast(ts as date)>='$reportDate'; + +insert into raena_analytics.OM_Events_user_type +SELECT A.*, + CASE + WHEN date_trunc('Month',A.ts)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.ts)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.ts)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.ts)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag +FROM (select * from clevertap.clevertap_master_data 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') and ts::date >= '$reportDate') A +LEFT JOIN raena_analytics.user_type_table flag ON replace(A.phone,'+','')=flag.reseller_mobile; + + +DROP TABLE IF EXISTS raena_analytics.om_order_brand_type; + + +DROP TABLE IF EXISTS raena_analytics.om_order_brand_type; + + +CREATE TABLE raena_analytics.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 (select distinct id,order_id,parent_sku from raena_order_management.sales_sub_order) sso +LEFT JOIN (select distinct sales_sub_order_id,sku from 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/redshift/sql_code/new_user_funnel_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/new_user_funnel_etl.sql + diff --git a/redshift/offender_dashboard.sh b/redshift/offender_dashboard.sh new file mode 100644 index 0000000..e862efa --- /dev/null +++ b/redshift/offender_dashboard.sh @@ -0,0 +1,962 @@ + #!/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.base_analytics_dump; + + +CREATE TABLE raena_analytics.base_analytics_dump AS +SELECT ts, + events, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid , + json_extract_path_text(event_props,'page_name',true) pagename +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events IN ('load_home_page', + 'page_load', + 'add_to_cart', + 'begin_checkout', + 'finish_checkout', + 'Charged', + 'page_load_checkout_continue', + 'page_load_finish_checkout', + 'confirm_payment', + 'view_cart', + 'view_item' ) + AND event_props NOT LIKE '%Focallure%'; + +DROP TABLE raena_analytics.add_to_cart_base_1; + + +CREATE TABLE raena_analytics.add_to_cart_base_1 AS +SELECT DISTINCT ts::date, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid , + json_extract_path_text(event_props,'screen',true) pagename, + json_extract_path_text(event_props,'item_id',true) sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='add_to_cart' + AND json_extract_path_text(event_props,'item_id',true) IS NOT NULL ; + + +DROP TABLE raena_analytics.view_item_base_1; + + +CREATE TABLE raena_analytics.view_item_base_1 AS +SELECT DISTINCT ts::date, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid , + json_extract_path_text(event_props,'screen',true) pagename, + json_extract_path_text(event_props,'item_id',true) sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='view_item' + AND json_extract_path_text(event_props,'item_id',true) IS NOT NULL ; + + +DROP TABLE raena_analytics.begin_checkout_base_1; + + +CREATE TABLE raena_analytics.begin_checkout_base_1 AS +SELECT DISTINCT + ts::date, + phone, + json_extract_path_text(event_props, 'CT Session Id', true) AS sessionid, + REPLACE(REPLACE(SPLIT_PART(json_extract_path_text(event_props, 'items', true), ',', 1), ']', ''), '[', '') AS sku +FROM + clevertap.clevertap_master_data cmd +WHERE + ts::date >= '2023-01-01' + AND events ='begin_checkout'; + + + +drop table raena_analytics.finish_checkout_base_1; + +create table raena_analytics.finish_checkout_base_1 +as +select A.*,P.sku +from +( SELECT DISTINCT + ts::date, + phone, + json_extract_path_text(event_props, 'CT Session Id', true) AS sessionid, + REPLACE(REPLACE(SPLIT_PART(json_extract_path_text(event_props, 'items', true), ',', 1), ']', ''), '[', '') AS pagename +FROM + clevertap.clevertap_master_data cmd +WHERE + ts::date >= '2023-01-01' +and events ='finish_checkout') A +left join raena_catalog_management.product P on A.pagename = cast(P.id as varchar) ; + +drop table raena_analytics.pagename_homepage_data; + +create table raena_analytics.pagename_homepage_data +as +select distinct sessionid ,phone,pagename,cast(ts as date) ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Home'; + + +drop table if exists raena_analytics.pagename_homepage_data_remaining_funnel_stage1; + +CREATE TABLE raena_analytics.pagename_homepage_data_remaining_funnel_stage1 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_homepage_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Home' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + +drop table raena_analytics.offender_funnel_report; + +CREATE TABLE raena_analytics.offender_funnel_report AS +SELECT ts, + pagename, + C.is_offender , + D.name, + count(phone) count_view_item, + count(add_to_cart_phone) count_add_to_cart, + count(begin_checkout_cart) begin_checkout_cart, + count(finish_checkout_cart) finish_checkout_cart +FROM + (SELECT DISTINCT A.ts, + A.sessionid, + A.phone, + A.pagename , + A.sku , + B.phone add_to_cart_phone , + C.phone begin_checkout_cart, + D.phone finish_checkout_cart + FROM raena_analytics.view_item_base_1 A + LEFT JOIN raena_analytics.add_to_cart_base_1 B ON A.sessionid = B.sessionid + AND A.sku = B.sku + LEFT JOIN raena_analytics.begin_checkout_base_1 C ON A.sessionid = C.sessionid + AND A.sku = C.sku + LEFT JOIN raena_analytics.finish_checkout_base_1 D ON A.sessionid = D.sessionid + AND A.sku = C.sku) AA +LEFT JOIN raena_catalog_management.product C ON AA.sku = C.sku +LEFT JOIN raena_catalog_management.brand D ON C.brand_id = D.id +GROUP BY 1, + 2, + 3, + 4; + +drop table raena_analytics.pagename_Brands_data; + +create table raena_analytics.pagename_Brands_data +as +select distinct sessionid ,phone,pagename,ts::date ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Brands'; + + +drop table if exists raena_analytics.pagename_brand_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_brand_data_remaining_funnel_stage2 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_brands_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Brands' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + + +drop table raena_analytics.pagename_search_data; + +create table raena_analytics.pagename_search_data +as +select distinct sessionid ,phone,'Search' pagename,ts::date ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename like '%Search%'; + + +drop table if exists raena_analytics.pagename_search_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_search_data_remaining_funnel_stage2 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_search_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename LIKE '%Search%' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + +-----CategoryList---- + + + +drop table raena_analytics.pagename_CategoryList_data; + +create table raena_analytics.pagename_CategoryList_data +as +select distinct sessionid ,phone,pagename,ts::date ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'CategoryList'; + + +drop table if exists raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_CategoryList_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'CategoryList' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + + +-----Cart----- + + + +drop table raena_analytics.pagename_Cart_data; + +create table raena_analytics.pagename_Cart_data +as +select distinct sessionid ,phone,pagename,ts::date ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Cart'; + + +drop table if exists raena_analytics.pagename_Cart_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_Cart_data_remaining_funnel_stage2 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_Cart_data A + INNER JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Cart' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + +drop table if exists raena_analytics.offender_dashboard_base_table; + +CREATE TABLE raena_analytics.offender_dashboard_base_table AS +SELECT gd.transaction_date , + category_name , + product_type , + brand_name , + gd.sku , + gd.shipping_province , + order_recipient , + gd.order_placed_by , + order_type , + tier_name , + gd.external_id , + sum(quantity) quantity , + sum(discounted_price*quantity) payment_price, + sum(cogs*quantity) total_cogs, + sum((retail_price*quantity)-(seller_margin*quantity)) total_wholesale_price , + C.name channel_name , + O.status, + payment_amount order_level_payment_amount, + reseller_mobile , + reseller_email , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END bucket, + is_offender, + sum(CASE WHEN gd.external_id = BB.order_id + AND gd.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount +FROM raena_analytics.gm_dashboard gd +LEFT JOIN raena_order_management.order O ON gd.external_id = O.id +LEFT JOIN raena_order_management.channel C ON O.channel_id = C.id +LEFT JOIN + (SELECT reseller_id , + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1, + 2) A + GROUP BY 1) D ON gd.reseller_id = D.reseller_id +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON gd.external_id = BB.order_id +AND gd.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON gd.external_id = CC.external_id +AND gd.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +GROUP BY gd.transaction_date , + category_name , + product_type , + brand_name , + gd.sku , + gd.shipping_province , + order_recipient , + gd.order_placed_by , + order_type , + tier_name , + gd.external_id , + C.name , + O.status, + payment_amount , + reseller_mobile , + reseller_email , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END , + is_offender; + + +drop table if exists raena_analytics.rentention_bucket_wise ; + +create table raena_analytics.rentention_bucket_wise +as +select month_sort , bucket, + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user, + count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user, + count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user, + count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user, + count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user, + count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user, + count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user, + count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user, + count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user +from ( +SELECT mobile , month_sort , bucket,is_offender,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end bucket, + is_offender, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT * + FROM + (SELECT DISTINCT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + is_offender, + discounted_price*quantity payment_amount + FROM raena_analytics.gm_dashboard gd + ) A + GROUP BY 1, + 2,3) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') AS mobile, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3,4) BB + group by 1,2 + order by 1,2; + + + +drop table if exists raena_analytics.rentention_overall ; + +create table raena_analytics.rentention_overall +as +select month_sort , + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user, + count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user, + count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user, + count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user, + count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user, + count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user, + count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user, + count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user, + count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user +from ( +SELECT mobile , month_sort,is_offender,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + is_offender, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT * + FROM + (SELECT DISTINCT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + is_offender, + discounted_price*quantity payment_amount + FROM raena_analytics.gm_dashboard gd + ) A + GROUP BY 1, + 2,3) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') AS mobile, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3) BB + group by 1 + order by 1; + + + + +drop table if exists raena_analytics.rentention_brand_wise ; + +create table raena_analytics.rentention_brand_wise +as +select month_sort ,bucket,brand_name,is_offender, + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user +from ( +SELECT mobile , month_sort ,bucket,brand_name,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end bucket, + base_1.brand_name, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT distinct * + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date)::date created_date, + brand_name, + discounted_price*quantity payment_amount, + row_number() over(partition BY reseller_mobile + ORDER BY cast(transaction_date AS Date)) created_rnk + FROM raena_analytics.gm_dashboard o + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + discounted_price*quantity payment_amount, + row_number() over(partition BY replace(cast(reseller_mobile AS varchar),'\"',''), date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date)) + ORDER BY date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date))) created_rnk + FROM raena_analytics.gm_dashboard o + ) A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_mobile AS mobile, + date_trunc('month',transaction_date)::date AS created_date, + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard o + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3,4) BB + left join (select distinct name, is_offender from raena_catalog_management.brand ) CC on BB.brand_name = CC.name + group by 1,2,3,4 + order by 1,2,3; + + + drop table raena_analytics.churn_reseller_data; + + CREATE TABLE raena_analytics.churn_reseller_data AS +SELECT date_trunc('Month',A.first_transaction_date)::date , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END bucket, + count(DISTINCT A.mobile) M0_user, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' THEN B.mobile END) churn, + count(DISTINCT CASE WHEN created_date ='2023-05-01' THEN base_2.mobile END) may_retention, + count(DISTINCT CASE WHEN created_date ='2023-06-01' THEN base_2.mobile END) jun_retention, + count(DISTINCT CASE WHEN created_date ='2023-07-01' THEN base_2.mobile END) july_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-05-01' THEN base_2.mobile END) from_churn_may_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-06-01' THEN base_2.mobile END) from_churn_jun_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-07-01' THEN base_2.mobile END) from_churn_july_retention, + count(DISTINCT CASE WHEN created_date ='2023-05-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_may_retention, + count(DISTINCT CASE WHEN created_date ='2023-06-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_jun_retention, + count(DISTINCT CASE WHEN created_date ='2023-07-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_july_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-05-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_may_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-06-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_jun_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-07-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_july_retention +FROM + (SELECT DISTINCT mobile, + created_date first_transaction_date, + payment_amount + FROM + (SELECT DISTINCT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') + ORDER BY cast(created_at AS Date)::date) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' ) a + WHERE a.created_rnk=1) A +LEFT JOIN + (SELECT DISTINCT mobile, + created_date last_transaction_date, + payment_amount + FROM + (SELECT DISTINCT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'\"','') + ORDER BY cast(created_at AS Date)::date DESC) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + AND cast(created_at AS Date)::date <='2023-07-17' ) a + WHERE a.created_rnk=1) B ON A.mobile = B.mobile +LEFT JOIN + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + discounted_price*quantity payment_amount, + p.is_offender + FROM raena_analytics.gm_dashboard o + LEFT JOIN raena_catalog_management.product P ON o.sku = P.sku + WHERE cast(transaction_date AS Date) >= date_trunc('Month',CURRENT_DATE)::date +interval'-4 Month' ) A + GROUP BY 1, + 2, + 3) base_2 ON A.mobile=base_2.mobile +LEFT JOIN + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_mobile AS mobile, + date_trunc('month',transaction_date)::date AS created_date, + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard o + GROUP BY 1, + 2) A + GROUP BY 1) base_3 ON A.mobile=base_3.mobile +GROUP BY 1, + 2; +" > /home/ec2-user/cronjob/redshift/sql_code/offender_dashboard.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/offender_dashboard.sql + + + diff --git a/redshift/oos_stock_restock.sh b/redshift/oos_stock_restock.sh new file mode 100644 index 0000000..56990e5 --- /dev/null +++ b/redshift/oos_stock_restock.sh @@ -0,0 +1,650 @@ + #!/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 " + +--------------------------OOS ------------------------- +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_1; + + +CREATE TABLE raena_analytics.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, + to_timestamp(snp_date || ' ' || snp_time,'YY-MM-DD HH24:MI:SS') +interval'7 hours' AS snp_date_time, + sku_code, + inventory + FROM raena_analytics.inventory_snapshot + where snp_time+interval'7 hours'>='23:30' + )A ) B +WHERE R=1; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_2; + +CREATE TABLE raena_analytics.om_oos_soldout_2 AS +select *, datediff(day,cast(snp_date_time as date),current_date) as age +FROM raena_analytics.om_oos_soldout_1; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_2_med; + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_soldout_2 +WHERE age>0; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_3; + + +CREATE TABLE raena_analytics.om_oos_soldout_3 AS +SELECT a.*, + b.inventory AS Maximum_occurred_inventory +FROM raena_analytics.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 raena_analytics.om_oos_soldout_2_med + GROUP BY inventory, + age_group, + sku_code + ORDER BY 4 DESC,1) A) AA + 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 raena_analytics.om_oos_soldout_3_med; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_soldout_3 + GROUP BY 1 + ORDER BY sku_code) b ON a.sku_code=b.sku_code ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_3_med_2; + + +CREATE TABLE raena_analytics.om_oos_soldout_3_med_2 AS +SELECT a.*, + b.sold_out_fl +FROM raena_analytics.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 + FROM raena_analytics.om_oos_soldout_3_med + )A) B + WHERE R=1) b ON a.sku_code=b.sku_code ; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_4; + + +CREATE TABLE raena_analytics.om_oos_soldout_4 AS +SELECT a.*, + b.brand_name, + b.\"EL/PL Flag\", b.give_away, + b.active_status +FROM raena_analytics.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 b.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 raena_analytics.demand_forecasting_configs c ON a.sku=c.sku) b ON a.sku_code=b.sku ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_5; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_soldout_4 a +LEFT JOIN raena_analytics.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' or o.id like 'PL%') + AND cast(o.is_archived AS varchar)='false' + GROUP BY sku) d ON a.sku_code=d.sku ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_6; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 IS NULL + AND payment_amount is NOT NULL + 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 raena_analytics.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 raena_analytics.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')AAA) AA + 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 raena_analytics.om_oos_restock_1; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_restock_1_med; + +CREATE TABLE raena_analytics.om_oos_restock_1_med AS +SELECT * +FROM raena_analytics.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 raena_analytics.om_oos_restock_2; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_restock_1_med +WHERE age>0; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_3; + + +CREATE TABLE raena_analytics.om_oos_restock_3 AS +SELECT a.*, + b.restock AS Maximum_occurred_inventory +FROM raena_analytics.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 raena_analytics.om_oos_restock_2 + GROUP BY cnt, + age_group, + warehouse, + sku + ORDER BY 5 DESC,1) A) B + 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 raena_analytics.om_oos_restock_4; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_restock_4_stage1; + +create table raena_analytics.om_oos_restock_4_stage1 +as + 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 s OLD 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 + FROM raena_analytics.om_oos_restock_4 + )A )B + WHERE R=1 ; + +drop table if exists raena_analytics.om_oos_restock_4_stage2; +create table raena_analytics.om_oos_restock_4_stage2 +as + SELECT sku, + sum(quantity)/count(DISTINCT cast(transaction_date AS date)) AS runrate + FROM raena_analytics.gm_dashboard + WHERE cast(transaction_date AS date)>=cast((CURRENT_DATE-90) AS date) + GROUP BY sku; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_5; + +CREATE TABLE raena_analytics.om_oos_restock_5 AS +SELECT a.*, + b.sold_out_fl, + c.runrate AS \"90_days_runrate\" +FROM raena_analytics.om_oos_restock_4 a +INNER JOIN raena_analytics.om_oos_restock_4_stage1 b ON a.sku=b.sku +AND a.warehouse=b.warehouse +LEFT JOIN raena_analytics.om_oos_restock_4_stage2 c ON a.sku=c.sku; + + +DROP TABLE IF EXISTS raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_restock_5_stage1; + +CREATE TABLE raena_analytics.om_oos_restock_5_stage1 AS + SELECT sku, + sum(payment_amount)payment_amount, + sum(quantity)quantity, + sub_order_shipping_id, + sum(shipping_amount)shipping_amount + FROM raena_analytics.rate_sheet_wise_expected_warehouse + WHERE created_date>=(CURRENT_DATE-90) + AND is_campaign='false' + group by 1,4; + + + + +drop table if exists raena_analytics.om_oos_restock_5_stage2; + +CREATE TABLE raena_analytics.om_oos_restock_5_stage2 AS +SELECT DISTINCT A.sku, + A.payment_amount, + A.quantity, + A.sub_order_shipping_id, + A.shipping_amount \"Actual Shipping Fee\", + DD.Expected_shipping_fee, + jsf.expected_JTR_shipping_fee +FROM + raena_analytics.om_oos_restock_5_stage1 A + LEFT JOIN + (SELECT DISTINCT sub_order_shipping_id, + Expected_shipping_fee + FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping_final + WHERE Expected_shipping_fee IS NOT NULL ) DD ON A.sub_order_shipping_id=DD.sub_order_shipping_id + LEFT join + (SELECT sub_order_shipping_id , + expected_JTR_shipping_fee + FROM raena_analytics.om_shipment_jtr_shipping_fee_base_11 + WHERE expected_JTR_shipping_fee IS NOT NULL) jsf ON A.sub_order_shipping_id=jsf.sub_order_shipping_id; + + DROP TABLE IF EXISTS raena_analytics.om_shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.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, + payment_amount, + quantity, + sub_order_shipping_id, + coalesce(expected_JTR_shipping_fee,Expected_shipping_fee,\"Actual Shipping Fee\") AS Expected_shipping_fee, + \"Actual Shipping Fee\" + from raena_analytics.om_oos_restock_5_stage2) A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + +DROP TABLE IF EXISTS raena_analytics.om_shipping_fee_sku_base_stage3; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_shipping_fee_sku_base_stage2; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_6; + +CREATE TABLE raena_analytics.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, + \"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 raena_analytics.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 raena_analytics.om_oos_restock_5 + WHERE sold_out_fl NOT LIKE '%stocked' + GROUP BY 1, + 2) b ON a.sku=b.sku +LEFT JOIN raena_analytics.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, + (payment_amount/quantity) AS payment_amount, + (new_shipment_amount/quantity) AS \"Actual Shipping Fee\", (new_expected_shipment_amount/quantity) AS new_expected_shipment_amount + FROM raena_analytics.om_shipping_fee_sku_base_stage3 + )A) B + WHERE payment_amount!=0 + GROUP BY 1) d ON a.sku=d.sku; + + + " > /home/ec2-user/cronjob/redshift/sql_code/oos_restock_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/oos_restock_etl.sql + diff --git a/redshift/payment_dashboard.sh b/redshift/payment_dashboard.sh new file mode 100644 index 0000000..c31c590 --- /dev/null +++ b/redshift/payment_dashboard.sh @@ -0,0 +1,153 @@ +#!/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.google_map_search; + + +CREATE TABLE raena_analytics.google_map_search AS +SELECT * +FROM raena_crawler_management.google_map_search_results +WHERE created_at > '2024-04-19 00:00:00.000' + AND rating_category IN ('Cosmetics wholesaler', + 'Beauty school', + 'Cosmetics industry', + 'Beauty supply store', + 'Beauty product supplier', + 'Dermatologist', + 'Make-up artist', + 'Health and beauty shop', + 'Nail salon', + 'Beauty salon', + 'beautification saloon', + 'Hair salon', + 'Skin care clinic', + 'General store', + 'Beauty supplies store', + 'Beauty products wholesaler', + 'Beautician', + 'Cosmetics and perfumes supplier', + 'Facial spa', + 'Perfume store', + 'Cosmetic products manufacturer', + 'Cosmetics store', + 'Spa and health club', + 'Shop', + 'Wellness center'); + + + + +DROP TABLE IF EXISTS raena_analytics.user_type_table_payment ; + + +CREATE TABLE raena_analytics.user_type_table_payment AS +SELECT DISTINCT reseller_mobile, + reseller_id, + First_transaction_date, + created_at first_install_date +FROM + (SELECT replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') reseller_mobile, + reseller_id, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' + GROUP BY 1, + 2) A +INNER JOIN + (SELECT replace(mobile,'+','') profile_phone, + id, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1, + 2) B ON A.reseller_mobile=B.profile_phone; + + +DROP TABLE raena_analytics.Payment_category_filter_payment; + + +CREATE TABLE raena_analytics.Payment_category_filter_payment AS +SELECT PAYMENT_ID, + order_placed_by, + New_existing_flag, + CASE + WHEN payment_channel='INVOICE' + AND payment_provider='DurianPay' + AND PORDER_id LIKE '%DSF' THEN 'Invoice_DP_DSF' + WHEN payment_channel='INVOICE' + AND payment_provider='DurianPay' + AND order_placed_by='admin' THEN 'Invoice_DP_Admin' + WHEN payment_channel='INVOICE' + AND payment_provider='Xendit' THEN 'Inovice - Xendit' + WHEN payment_channel='INVOICE' + AND payment_provider='PrepaidPayment' THEN 'Inovice - PrepaidPayment' + ELSE payment_channel + END AS payment_category_filter +FROM + (SELECT a.*, + b.order_placed_by, + b.New_existing_flag + FROM (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 PAYMENT_ID, + created_at::date 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 porder_id, + payment_provider, + payment_category, + payment_channel + FROM NS + INNER JOIN raena_order_management.payment B ON NS.n <= REGEXP_COUNT(B.order_ids , ',') + 1) a + LEFT JOIN + (SELECT DISTINCT id, + order_placed_by, + New_existing_flag + FROM + (SELECT id, + order_placed_by, + CASE WHEN date_trunc('Month',o.created_at)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',o.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' WHEN date_trunc('Month',o.created_at)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',o.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' WHEN date_trunc('Month',o.created_at)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',o.created_at)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' ELSE 'Na' END AS New_existing_flag + FROM RAENA_ORDER_MANAGEMENT.order o + LEFT JOIN raena_analytics.user_type_table_payment utt ON o.reseller_id = utt.reseller_id) c) b ON a.porder_id=b.id) C; + +" > /home/ec2-user/cronjob/redshift/sql_code/payment_dashboard.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/payment_dashboard.sql + diff --git a/redshift/rdash_dashboard.sh b/redshift/rdash_dashboard.sh new file mode 100644 index 0000000..0c439b8 --- /dev/null +++ b/redshift/rdash_dashboard.sh @@ -0,0 +1,149 @@ + #!/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, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'name') as actual_category_name +from raena_analytics.mongo_baseproducts +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'name') as actual_category_name +from raena_analytics.mongo_baseproducts mo +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'name') as actual_category_name +from raena_analytics.mongo_baseproducts mo +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'name') as actual_category_name +from raena_analytics.mongo_baseproducts mo; + +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, +json_extract_path_text(storeinfo, 'name') as store, +json_extract_path_text(storeinfo, 'storeOwner') as sellername, +json_extract_path_text(storeinfo, '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, + json_extract_path_text(storeinfo, 'storeOwner') as sellername, + json_extract_path_text(storeinfo, 'storeOwnerPhone') as Sellerphone +FROM raena_analytics.mongo_orders; + + + + " > /home/ec2-user/cronjob/redshift/sql_code/360_rdash_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/360_rdash_etl.sql + diff --git a/redshift/retention_dashboard.sh b/redshift/retention_dashboard.sh new file mode 100644 index 0000000..6d30f88 --- /dev/null +++ b/redshift/retention_dashboard.sh @@ -0,0 +1,459 @@ +!/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.OM_Reseller_Wise_latest_tier; + + +CREATE TABLE raena_analytics.OM_Reseller_Wise_latest_tier AS +SELECT mobile, + tier from + ( SELECT mobile, + tier, + row_number() over (partition BY mobile ORDER BY created_date DESC) AS R + FROM + ( SELECT DISTINCT replace(json_extract_path_text(o.reseller_info,'mobile',true),'+','') AS mobile, + json_extract_path_text(o.reseller_info,'tierName',true) AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o) A ) B +WHERE R=1 + AND mobile is NOT NULL; + +DROP TABLE IF EXISTS raena_analytics.Retention_new_filters_install; + +CREATE TABLE raena_analytics.Retention_new_filters_install AS +SELECT DISTINCT TO_CHAR(o.created_at, 'Month') AS MONTH, + cast(date_part('year',o.created_at) AS varchar) AS YEAR, + CASE + WHEN shipping_to='Reseller' THEN 'Reseller' + ELSE 'Dropshipper' + END AS customer_type, + order_placed_by, + CASE + WHEN order_placed_by='admin' THEN 'admin' + ELSE marketplace + END AS Sales_channel, + provider, + c.channel_name AS channel_name, + replace(json_extract_path_text(o.reseller_info,'mobile',true),'+','') mobile, + verification_status, + tier +FROM raena_user_management.user u +LEFT JOIN raena_order_management.order o ON replace(json_extract_path_text(o.reseller_info,'mobile',true),'+','')=replace(u.mobile,'+','') +LEFT JOIN raena_analytics.OM_channel c ON cast(c.id AS text)=cast(o.channel_id AS text) +LEFT JOIN + (SELECT mobile, + tier + FROM + (SELECT mobile, + tier, + row_number() over (partition BY mobile + ORDER BY created_date DESC) AS R + FROM + (SELECT DISTINCT replace(json_extract_path_text(o.reseller_info,'mobile',true),'+','') AS mobile, + json_extract_path_text(o.reseller_info,'tierName',true) AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o)A) B + WHERE R=1 + AND mobile IS NOT NULL) t ON u.mobile=t.mobile; + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION; + + + +CREATE TABLE raena_analytics.DAU_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS conversion_year, + TO_CHAR(base_1.created_date, 'Month') AS conversion_month, + date_part('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(o.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(o.reseller_info,'mobile',true),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') 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(o.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(o.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 o + WHERE payment_status = 'Paid' or id like 'PL%') b + 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 <> '' ) c ) base_3 ON base_1.mobile=base_3.active_user) bucket_final; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_REVENUE_RETENTION; + + +CREATE TABLE raena_analytics.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, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(o.reseller_info,'mobile',true),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') A + GROUP BY 1, + 2)B) C) 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(o.reseller_info,'mobile',true),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.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, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(o.reseller_info,'mobile',true),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%')A + GROUP BY 1, + 2)B)C) 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(o.reseller_info,'mobile',true),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT; + + +CREATE TABLE raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT AS +SELECT * +FROM + (SELECT install_base.*, + bucket.bucket + FROM + (SELECT DISTINCT a.Active_user, + date_part('YEAR',a.created_date) AS acquisition_year, + TO_CHAR(a.created_date, 'Month') AS acquisition_month, + date_part('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') 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') 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(o.reseller_info,'mobile',true),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') 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(o.reseller_info,'mobile',true),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1) A ) 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 raena_analytics.OMREVENUE_RETENTION; + + +CREATE TABLE raena_analytics.OMREVENUE_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('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(o.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(o.reseller_info,'mobile',true),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') 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(o.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(o.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 o + WHERE payment_status = 'Paid' or id like 'PL%') A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + +DROP TABLE IF EXISTS raena_analytics.RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.RESELLER_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('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(o.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(o.reseller_info,'mobile',true),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') 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(o.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(o.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 o + WHERE payment_status = 'Paid' or id like 'PL%')A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + +" > /home/ec2-user/cronjob/redshift/sql_code/retention_code_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/retention_code_etl.sql + + diff --git a/redshift/revenue_leakage.sh b/redshift/revenue_leakage.sh new file mode 100644 index 0000000..7f3ce9e --- /dev/null +++ b/redshift/revenue_leakage.sh @@ -0,0 +1,640 @@ + #!/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.revenue_leakage_new_stage1; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage1 AS +SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at, + payment_amount, + payment_status_sort, + quantity +FROM + (SELECT *, + row_number() over (partition BY reseller_id,sku,payment_status,cast(created_at AS date) + ORDER BY created_at,payment_status_sort,payment_amount DESC) AS R + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.payment_price AS payment_amount, + CASE WHEN payment_status='Paid' THEN 1 WHEN payment_status='Expired' THEN 2 ELSE 3 END AS payment_status_sort, + oi.quantity + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status IN ('Expired', + 'Failed') + AND o.status='Order_Placed' + AND cast(o.created_at AS date) >='2022-01-01') inn) m +WHERE R=1 ; + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage2; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage2 AS +SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.quantity, + oi.payment_price AS payment_amount +FROM + (SELECT id, + order_placed_by , + reseller_id, + customer_id, + payment_status, + created_at + FROM RAENA_ORDER_MANAGEMENT.order + WHERE payment_status='Paid' + AND cast(created_at AS date) >='2022-01-01') o +INNER JOIN + (SELECT order_id, + sku, + quantity, + payment_price + FROM RAENA_ORDER_MANAGEMENT.order_item + WHERE cast(created_at AS date) >='2022-01-01') oi ON o.id=oi.order_id; + + +DROP TABLE IF EXISTS raena_analytics.om_revenue_leakage_qty_match; + + +CREATE TABLE raena_analytics.om_revenue_leakage_qty_match AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at AS date) AS concat1 +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_amount AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + LEFT JOIN raena_analytics.revenue_leakage_new_stage2 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND a.quantity=b.quantity + AND a.customer_id IS NULL + AND b.customer_id IS NULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS' + WHERE a.customer_id IS NULL) c +WHERE b_reseller_id IS NULL +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount) AS payment_amount, + a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at AS date) +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_amount AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + LEFT JOIN raena_analytics.revenue_leakage_new_stage2 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND a.quantity=b.quantity + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS' + WHERE a.customer_id IS NOT NULL) c +WHERE b_reseller_id IS NULL; + + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage3; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage3 AS +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price +FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >='2022-01-01' + AND customer_id IS NOT NULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist +INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >='2022-01-01' + AND customer_id IS NOT NULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by +AND ist.reseller_id=nxt.reseller_id +AND ist.customer_id=nxt.customer_id +AND ist.sku=nxt.sku +AND ist.created_at=cast(nxt.created_at AS date); + + +DROP TABLE IF EXISTS public.om_revenue_leakage_qty_not_match; + + +CREATE TABLE public.om_revenue_leakage_qty_not_match AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount)-(b_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at AS date) AS concat2 +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + INNER JOIN raena_analytics.revenue_leakage_new_stage3 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_amount>b.payment_price + AND a.customer_id ISNULL + AND b.customer_id ISNULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount)-(b_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at AS date) +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + INNER JOIN raena_analytics.revenue_leakage_new_stage3 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_amount>b.payment_price + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +ORDER BY 2, + 4; + + + +drop table if exists raena_analytics.revenue_leakage_new_stage4; + +create table raena_analytics.revenue_leakage_new_stage4 +as +SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at, + payment_price, + payment_status_sort, + quantity + FROM + (SELECT *, + row_number() over (partition BY reseller_id,sku,payment_status,cast(created_at AS date) + ORDER BY created_at,payment_status_sort,payment_price DESC) AS R + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.payment_price, + CASE WHEN payment_status='Paid' THEN 1 WHEN payment_status='Expired' THEN 2 ELSE 3 END AS payment_status_sort, + oi.quantity + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status!='Paid' + AND payment_status!='Pending' + AND o.status='Order_Placed' + AND cast(o.created_at AS date) >= '2022-01-01') inn) m + WHERE R=1; + +drop table if exists raena_analytics.revenue_leakage_new_stage5; + +create table raena_analytics.revenue_leakage_new_stage5 +as +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id ISNULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist + INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id ISNULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by + AND ist.reseller_id=nxt.reseller_id + AND ist.sku=nxt.sku + AND ist.created_at=cast(nxt.created_at AS date); + + +drop table raena_analytics.revenue_leakage_new_stage6; + +create table raena_analytics.revenue_leakage_new_stage6 +as +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id NOTNULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist + INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id NOTNULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by + AND ist.reseller_id=nxt.reseller_id + AND ist.customer_id=nxt.customer_id + AND ist.sku=nxt.sku + AND ist.created_at=cast(nxt.created_at AS date); + + +DROP TABLE IF EXISTS raena_analytics.OM_Revenue_leakage; + + +CREATE TABLE raena_analytics.OM_Revenue_leakage AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(revenue_leakage) AS revenue_leakage +FROM + (SELECT * + FROM + (SELECT qm.*, + qnm.concat2 + FROM raena_analytics.om_revenue_leakage_qty_match qm + LEFT JOIN raena_analytics.om_revenue_leakage_qty_not_match qnm ON qm.concat1=qnm.concat2) n + WHERE concat2 ISNULL) s +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(a_payment_amount)-sum(b_payment_amount) AS revenue_leakage +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_price AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + from raena_analytics.revenue_leakage_new_stage4 a + INNER join raena_analytics.revenue_leakage_new_stage5 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_price>b.payment_price + AND a.customer_id ISNULL + AND b.customer_id ISNULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(a_payment_amount)-sum(b_payment_amount) AS revenue_leakage +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_price AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + from raena_analytics.revenue_leakage_new_stage4 a + INNER JOIN raena_analytics.revenue_leakage_new_stage6 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_price>b.payment_price + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +GROUP BY 1, + 2, + 3, + 4, + 5 +ORDER BY 2, + 4; + + DROP TABLE IF EXISTS raena_analytics.OM_Revenue_leakage_dump; + + +CREATE TABLE raena_analytics.OM_Revenue_leakage_dump AS +SELECT * +FROM raena_analytics.om_revenue_leakage_qty_match +UNION +SELECT * +FROM raena_analytics.om_revenue_leakage_qty_not_match; + +DROP TABLE IF EXISTS raena_analytics.om_conversion_order_data_1; + + +CREATE TABLE raena_analytics.om_conversion_order_data_1 AS +SELECT DISTINCT cast(created_at + interval '7 Hours' AS date) AS created_date, + date_part('year',created_at + interval '7 Hours') AS order_year, + date_part('month',created_at + interval '7 Hours') AS order_month, + To_char(created_at + interval '7 Hours','month') Month_name, + 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 + interval '7 Hours'),date_part('month',created_at + interval '7 Hours')) AS R +FROM raena_order_management.ORDER a +LEFT 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 + interval '7 Hours') AS order_year, + date_part('month',created_at + interval '7 Hours') AS order_month, + reseller_id , + sum(payment_amount) AS revenue + FROM raena_order_management.ORDER + WHERE (payment_status='Paid' or id like 'PL%') + AND cast(is_archived AS varchar)='false' + GROUP BY date_part('year',created_at + interval '7 Hours'), + date_part('month',created_at + interval '7 Hours'), + reseller_id ) AA + WHERE reseller_id is not NULL) b ON a.reseller_id=b.reseller_id +AND date_part('year',a.created_at + interval '7 Hours') =b.order_year +AND date_part('month',a.created_at + interval '7 Hours')=b.order_month +LEFT 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' or id like 'PL%') + AND cast(is_archived AS varchar)='false' + GROUP BY reseller_id ) BB + WHERE reseller_id is not NULL) c ON a.reseller_id=c.reseller_id +WHERE (payment_status='Paid' or id like 'PL%') + AND cast(is_archived AS varchar)='false' + AND cast(is_campaign AS varchar)='false' +ORDER BY 1, + 3; + + +" > /home/ec2-user/cronjob/redshift/sql_code/revenue_leakage_etl.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/revenue_leakage_etl.sql + + diff --git a/redshift/sold_product.sh b/redshift/sold_product.sh new file mode 100644 index 0000000..0ae1942 --- /dev/null +++ b/redshift/sold_product.sh @@ -0,0 +1,122 @@ + #!/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.monthly_sold_product; + +create table raena_analytics.monthly_sold_product +as +select brand_name , brand_page_url , + sum(product_sold) product_sold +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id +where product_sold is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url +order by sum(product_sold) desc; + + +drop table if exists raena_analytics.avg_sold_product; + +create table raena_analytics.avg_sold_product +as +select distinct brand_name , brand_page_url , + avg(ratings) avg_ratings +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id +where ratings is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url +order by avg(ratings) desc; + +drop table if exists raena_analytics.total_sold_product; + +create table raena_analytics.total_sold_product +as +select brand_name , brand_page_url , + rceb.rce_source_id, + sum(product_sold_total) product_sold_total, + sum(product_sold) product_sold, + avg(ratings) avg_ratings, + count(distinct rcers.id) number_of_reseller_store_selling, + CASE + WHEN LOWER(brand_name) = LOWER(b.name) then true + ELSE false + END ss_present_in_Raena, + b.name raena_brand_name, + number_of_products_in_raena +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id and rceb.rce_source_id= rcep.rce_source_id +left join +( + select A.name , count(distinct B.sku) number_of_products_in_raena + from raena_catalog_management.brand A + left join raena_catalog_management.product B on A.id = B.brand_id + group by 1 +) b on LOWER(b.name) = LOWER(rceb.brand_name) +left join +( + select * + from raena_spider_management.rce_reseller_store +) rcers on rcers.id = rcep.rce_store_id +where product_sold_total is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url,rceb.rce_source_id,CASE + WHEN LOWER(brand_name) = LOWER(b.name) then true + ELSE false + end,b.name,number_of_products_in_raena +order by sum(product_sold_total) desc; + +drop table if exists raena_analytics.inventory_snapshot_dashboard_table ; + +create table raena_analytics.inventory_snapshot_dashboard_table +as +SELECT snp_date, sku_code,/*case WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' end*/ + warehouse_code warehouse , min(inventory) inventory_remaining +FROM raena_analytics.inventory_snapshot +GROUP BY 1,2,3 +ORDER BY 1,2,3; + + +" > /home/ec2-user/cronjob/redshift/sql_code/sold_product.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/sold_product.sql diff --git a/redshift/sql_code/360_rdash_etl.sql b/redshift/sql_code/360_rdash_etl.sql new file mode 100644 index 0000000..215986e --- /dev/null +++ b/redshift/sql_code/360_rdash_etl.sql @@ -0,0 +1,131 @@ + +--------------------------Rdash ------------------------- + +DROP TABLE IF EXISTS raena_analytics.om_mongo_brand_category; + + +CREATE TABLE raena_analytics.om_mongo_brand_category AS +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'name') as actual_category_name +from raena_analytics.mongo_baseproducts +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'name') as actual_category_name +from raena_analytics.mongo_baseproducts mo +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'name') as actual_category_name +from raena_analytics.mongo_baseproducts mo +union +select rsellviewid, +json_extract_path_text(brand, 'name') as Brand_name, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'hasChild') as has_child, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'id') as actual_categoryid, +json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'name') as actual_category_name +from raena_analytics.mongo_baseproducts mo; + +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, +json_extract_path_text(storeinfo, 'name') as store, +json_extract_path_text(storeinfo, 'storeOwner') as sellername, +json_extract_path_text(storeinfo, '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, + json_extract_path_text(storeinfo, 'storeOwner') as sellername, + json_extract_path_text(storeinfo, 'storeOwnerPhone') as Sellerphone +FROM raena_analytics.mongo_orders; + + + + diff --git a/redshift/sql_code/Demand_forecasting.sql b/redshift/sql_code/Demand_forecasting.sql new file mode 100644 index 0000000..0e0d2aa --- /dev/null +++ b/redshift/sql_code/Demand_forecasting.sql @@ -0,0 +1,992 @@ + +--------------------------Order Level Data -------------------------- +Drop table if exists raena_analytics.Gm_final_table; + +CREATE TABLE raena_analytics.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 raena_analytics.gm_dashboard A +LEFT JOIN raena_catalog_management.product B ON A.sku = B.sku; + +Drop table if exists raena_analytics.final_order_level_data; + +CREATE TABLE raena_analytics.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 raena_analytics.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,'2024-06-23')) B ON external_id =id +WHERE discounted_price <>0 and cast(transaction_date AS date) between dateadd(d,-120,'2024-06-23') and '2024-06-23'; + + -------------------------------------------SKU Level Data --------------------------------- + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage1; + +CREATE TABLE raena_analytics.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,'2024-06-23') AND '2024-06-23' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2024-06-23') AND dateadd(d,-7,'2024-06-23') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2024-06-23') AND dateadd(d,-14,'2024-06-23') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2024-06-23') AND dateadd(d,-21,'2024-06-23') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2024-06-23') AND dateadd(d,-28,'2024-06-23') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2024-06-23') AND dateadd(d,-35,'2024-06-23') 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 raena_analytics.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,'2024-06-23') AND '2024-06-23' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2024-06-23') AND dateadd(d,-7,'2024-06-23') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2024-06-23') AND dateadd(d,-14,'2024-06-23') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2024-06-23') AND dateadd(d,-21,'2024-06-23') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2024-06-23') AND dateadd(d,-28,'2024-06-23') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2024-06-23') AND dateadd(d,-35,'2024-06-23') THEN 'L42D' + END Bucket, + sum(coalesce (revenue_on_after_discount,0)) Total_revenue + FROM raena_analytics.final_order_level_data + WHERE cast(transaction_date as date) BETWEEN dateadd(d,-41,'2024-06-23') AND '2024-06-23' + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-6,'2024-06-23') AND '2024-06-23' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2024-06-23') AND dateadd(d,-7,'2024-06-23') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2024-06-23') AND dateadd(d,-14,'2024-06-23') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2024-06-23') AND dateadd(d,-21,'2024-06-23') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2024-06-23') AND dateadd(d,-28,'2024-06-23') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2024-06-23') AND dateadd(d,-35,'2024-06-23') 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,'2024-06-23') AND '2024-06-23' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2024-06-23') AND dateadd(d,-7,'2024-06-23') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2024-06-23') AND dateadd(d,-14,'2024-06-23') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2024-06-23') AND dateadd(d,-21,'2024-06-23') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2024-06-23') AND dateadd(d,-28,'2024-06-23') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2024-06-23') AND dateadd(d,-35,'2024-06-23') 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,'2024-06-23') AND '2024-06-23' +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,'2024-06-23') AND '2024-06-23' THEN 'L7D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-13,'2024-06-23') AND dateadd(d,-7,'2024-06-23') THEN 'L14D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-20,'2024-06-23') AND dateadd(d,-14,'2024-06-23') THEN 'L21D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-27,'2024-06-23') AND dateadd(d,-21,'2024-06-23') THEN 'L28D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-34,'2024-06-23') AND dateadd(d,-28,'2024-06-23') THEN 'L35D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-41,'2024-06-23') AND dateadd(d,-35,'2024-06-23') THEN 'L42D' + END , + Total_revenue; + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage2; + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage1 +WHERE warehouse IS NOT NULL +GROUP BY sku, + warehouse ; + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_stage3; + + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage2; + + +DROP TABLE IF EXISTS raena_analytics.sku_warehouse_split_final; + + +CREATE TABLE raena_analytics.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 raena_analytics.sku_warehouse_split_stage3; + +--------------------------------------Warehouse SKu Split---------------------------------- + +select count(1)sku_final_split_warehouse from raena_analytics.sku_warehouse_split_final; + +DROP TABLE IF EXISTS raena_analytics.sku_level_data ; + +CREATE TABLE raena_analytics.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.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 raena_analytics.storage_data B ON A.sku=B.sku +left join raena_analytics.demand_forecasting_configs G on A.sku= G.sku +LEFT JOIN raena_analytics.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 raena_analytics.sku_cogs_audit sca where sku_cogs_action='CREATED') F ON A.sku= F.sku_code ; +---------------------------_basic Calculation ------------------------- + +DROP TABLE IF EXISTS raena_analytics.basic_calculation; + + +CREATE TABLE raena_analytics.basic_calculation AS +SELECT sku , + CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2024-06-23') AND '2024-06-23' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2024-06-23') AND dateadd(d,-31,'2024-06-23') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2024-06-23') AND dateadd(d,-61,'2024-06-23') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2024-06-23') AND dateadd(d,-91,'2024-06-23') 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 raena_analytics.final_order_level_data A +LEFT JOIN + (SELECT CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2024-06-23') AND '2024-06-23' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2024-06-23') AND dateadd(d,-31,'2024-06-23') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2024-06-23') AND dateadd(d,-61,'2024-06-23') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2024-06-23') AND dateadd(d,-91,'2024-06-23') THEN 'L120D' + END txn_period, + sum(coalesce (revenue_on_after_discount,0)) Total_revenue + FROM raena_analytics.final_order_level_data + WHERE cast(transaction_date as date) BETWEEN dateadd(d,-120,'2024-06-23') AND '2024-06-23' + GROUP BY CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2024-06-23') AND '2024-06-23' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2024-06-23') AND dateadd(d,-31,'2024-06-23') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2024-06-23') AND dateadd(d,-61,'2024-06-23') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2024-06-23') AND dateadd(d,-91,'2024-06-23') THEN 'L120D' + END) B ON (CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2024-06-23') AND '2024-06-23' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2024-06-23') AND dateadd(d,-31,'2024-06-23') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2024-06-23') AND dateadd(d,-61,'2024-06-23') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2024-06-23') AND dateadd(d,-91,'2024-06-23') THEN 'L120D' + END)=B.txn_period +WHERE cast(transaction_date as date) BETWEEN dateadd(d,-120,'2024-06-23') AND '2024-06-23' +GROUP BY sku, + CASE + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-30,'2024-06-23') AND '2024-06-23' THEN 'L30D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-60,'2024-06-23') AND dateadd(d,-31,'2024-06-23') THEN 'L60D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-90,'2024-06-23') AND dateadd(d,-61,'2024-06-23') THEN 'L90D' + WHEN cast(transaction_date as date) BETWEEN dateadd(d,-120,'2024-06-23') AND dateadd(d,-91,'2024-06-23') THEN 'L120D' + END, + Total_revenue; + +select 1; + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage1; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.basic_calculation_stage1_v1; + +create table raena_analytics.basic_calculation_stage1_v1 as +select A.*, + abs(contribution-median_contribution) for_max_contribution +from raena_analytics.basic_calculation A +left join (select sku , + MEDIAN(contribution) median_contribution + from raena_analytics.basic_calculation group by sku ) B + on A.sku = B.sku ; + + + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage2; + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage1_v1 A +LEFT JOIN raena_analytics.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 raena_analytics.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 raena_analytics.basic_calculation_stage2 + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage3; + +/* +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation A +WHERE txn_period <> 'L120D' +GROUP BY A.SKu,txn_period; +*/ + + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation A +left join (select distinct sku , runrate from raena_analytics.basic_calculation where txn_period = 'L30D' ) B on A.sku = B.sku +WHERE txn_period <> 'L120D' +GROUP BY A.SKu; + + +DROP TABLE IF EXISTS raena_analytics.basic_calculation_stage4; + + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage2 A +LEFT JOIN raena_analytics.basic_calculation_stage3 B ON A.sku = B.sku; + + +--SELECT * FROM raena_analytics.basic_calculation_stage4; + +--select * from raena_analytics.basic_calculation_stage5 + +drop table if exists raena_analytics.basic_calculation_stage5; + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty2_avg_runrate +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty3_avg_quantity +FROM raena_analytics.basic_calculation_stage4 +UNION ALL +SELECT sku , + qty4_expected_revenue_target +FROM raena_analytics.basic_calculation_stage4) A +group by sku; + +drop table if exists raena_analytics.basic_calculation_stage6; + +create table raena_analytics.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 raena_analytics.sku_warehouse_split_final B + group by sku; + +drop table if exists raena_analytics.final_basic_calucation_table; + +CREATE TABLE raena_analytics.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 raena_analytics.basic_calculation_stage4 A +LEFT JOIN raena_analytics.basic_calculation_stage6 B ON A.sku =B.sku +LEFT join raena_analytics.basic_calculation_stage5 C on A.sku=C.sku; + +drop table if exists raena_analytics.inbound_data; + +create table raena_analytics.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) >'2024-06-23' 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 raena_analytics.warehouse_in_stock; + +create table raena_analytics.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 raena_analytics.demand_forecast_dashboard_base; + + +CREATE TABLE raena_analytics.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) + END30 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; + + + diff --git a/redshift/sql_code/am_recommendation_etl.sql b/redshift/sql_code/am_recommendation_etl.sql new file mode 100644 index 0000000..2592a14 --- /dev/null +++ b/redshift/sql_code/am_recommendation_etl.sql @@ -0,0 +1,2863 @@ + + + + +DROP TABLE raena_analytics.raw_ingredient_split_name; +CREATE TABLE raena_analytics.raw_ingredient_split_name AS +SELECT * , + split_part(concern,', ',1) concern1, + split_part(concern,', ',2) concern2, + split_part(concern,', ',3) concern3, + split_part(concern,', ',4) concern4, + split_part(concern,', ',5) concern5, + split_part(top_marketing_ingredients ,', ',1) ingredient1, + split_part(top_marketing_ingredients,', ',2) ingredient2, + split_part(top_marketing_ingredients,', ',3) ingredient3, + split_part(top_marketing_ingredients,', ',4) ingredient4, + split_part(top_marketing_ingredients,', ',5) ingredient5, + split_part(top_marketing_ingredients,', ',6) ingredient6, + split_part(top_marketing_ingredients,', ',7) ingredient7, + split_part(top_marketing_ingredients,', ',8) ingredient8, + split_part(top_marketing_ingredients,', ',9) ingredient9, + split_part(top_marketing_ingredients,', ',10) ingredient10, + split_part(top_marketing_ingredients,', ',11) ingredient11 +FROM + (SELECT DISTINCT group_base, + sku , + lower(concern) concern, + lower(top_marketing_ingredients) top_marketing_ingredients, + CASE + WHEN concern='' + AND top_marketing_ingredients='' THEN 1 + END flag + FROM raena_analytics.ingredients_base_table_final) A +WHERE flag IS NULL; + + DROP TABLE raena_analytics.raw_ingredient_mapping_table_stage; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage AS +SELECT A.* , + B.group_base mapping_group_base, + B.concern mapping_concerns, + B.top_marketing_ingredients mapping_ingredients +FROM raena_analytics.raw_ingredient_split_name A +LEFT JOIN + (SELECT * + FROM raena_analytics.raw_ingredient_split_name) B ON 1=1; + +DROP TABLE if exists raena_analytics.raw_ingredient_mapping_table_stage2; + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage2 AS +SELECT * , + CASE + WHEN raena_analytics.udf_string_campare(concern1,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern1,mapping_concerns) <> 'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern1, + CASE + WHEN raena_analytics.udf_string_campare(concern2,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern2,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern2, + CASE + WHEN raena_analytics.udf_string_campare(concern3,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern3,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern3, + CASE + WHEN raena_analytics.udf_string_campare(concern4,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern4,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern4, + CASE + WHEN raena_analytics.udf_string_campare(concern5,mapping_concerns) <>'' + AND raena_analytics.udf_string_campare(concern5,mapping_concerns) <>'NA' + AND mapping_concerns IS NOT NULL THEN 1 + ELSE 0 + END flag_concern5, + REGEXP_COUNT(concern ,', ')+1 total_count_concern, + CASE + WHEN raena_analytics.udf_string_campare(ingredient1,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient1,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient1, + CASE + WHEN raena_analytics.udf_string_campare(ingredient2,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient2,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient2, + CASE + WHEN raena_analytics.udf_string_campare(ingredient3,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient3,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient3, + CASE + WHEN raena_analytics.udf_string_campare(ingredient4,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient4,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient4, + CASE + WHEN raena_analytics.udf_string_campare(ingredient5,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient5,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient5, + CASE + WHEN raena_analytics.udf_string_campare(ingredient6,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient6,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient6, + CASE + WHEN raena_analytics.udf_string_campare(ingredient7,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient7,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient7, + CASE + WHEN raena_analytics.udf_string_campare(ingredient8,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient8,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient8, + CASE + WHEN raena_analytics.udf_string_campare(ingredient9,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient9,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient9, + CASE + WHEN raena_analytics.udf_string_campare(ingredient10,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient10,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient10, + CASE + WHEN raena_analytics.udf_string_campare(ingredient11,mapping_ingredients) <>'' + AND raena_analytics.udf_string_campare(ingredient11,mapping_ingredients) <>'NA' + AND mapping_ingredients IS NOT NULL THEN 1 + ELSE 0 + END flag_ingredient11, + REGEXP_COUNT(top_marketing_ingredients ,', ')+1 total_count_ingredient +FROM raena_analytics.raw_ingredient_mapping_table_stage +WHERE group_base <> mapping_group_base; + + DROP TABLE if exists raena_analytics.raw_ingredient_mapping_table_stage3; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage3 AS +SELECT *, + flag_concern1+flag_concern2+flag_concern3+flag_concern3+flag_concern3 total_concern_match, + flag_ingredient1+flag_ingredient2+flag_ingredient3+flag_ingredient4+flag_ingredient5+flag_ingredient6+flag_ingredient7+flag_ingredient8+flag_ingredient9+flag_ingredient10+flag_ingredient11 total_ingredient_match +FROM raena_analytics.raw_ingredient_mapping_table_stage2 ; + + +DROP TABLE IF EXISTS raena_analytics.raw_ingredient_mapping_table_stage4; + + +CREATE TABLE raena_analytics.raw_ingredient_mapping_table_stage4 AS +SELECT *, (total_concern_match*100/total_count_concern) Percent_concern_match, (total_ingredient_match*100/total_count_ingredient) Percent_ingredient_match , + CASE WHEN concern='' AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND top_marketing_ingredients='' THEN 1 + WHEN (total_concern_match*100/total_count_concern)>0 AND (total_ingredient_match*100/total_count_ingredient)>0 THEN 1 + END percent_flag +FROM raena_analytics.raw_ingredient_mapping_table_stage3; + + +Drop table if exists raena_analytics.raw_ingredient_mapping_table; + +Create table raena_analytics.raw_ingredient_mapping_table +as +SELECT group_base, + sku, + concern, + top_marketing_ingredients, + mapping_group_base, + mapping_concerns, + mapping_ingredients, + percent_concern_match, + Percent_ingredient_match +FROM raena_analytics.raw_ingredient_mapping_table_stage4 +WHERE percent_flag=1 +ORDER BY group_base , + percent_concern_match DESC , + Percent_ingredient_match DESC; + + +DROP TABLE IF EXISTS raena_analytics.sku_in_stock; + + +CREATE TABLE raena_analytics.sku_in_stock AS +SELECT sku , + sum(coalesce(F.stock_limit,0)) 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; + + --------------------------------------------------------------task 1 ---------------- + +drop table if exists raena_analytics.recommendation_power_seller_base_alt; + +create table raena_analytics.recommendation_power_seller_base_alt +as +select distinct email from raena_analytics.om_acquisition_mapping_list +union select distinct "email id" from raena_analytics.AM_REVENUE_LEAKAGE_LIST ; + + +INSERT INTO raena_analytics.recommendation_power_seller_base_alt + values ('cindyvioleta29@yahoo.com'), +('sherryfannydesita@gmail.com'), +('masintansilalahi@yahoo.co.id'), +('rumahskincarebpp@gmail.com'), +('puturisca@gmail.com'), +('sieny.go@gmail.com'), +('oki.zhang1988@gmail.com'), +('jeremytantono@yahoo.co.id'), +('yuke.cipta@gmail.com'), +('kwonvic@hotmail.com'), +('ervinafransiska95@gmail.com'), +('indahamaliasasmita@gmail.com'), +('mirosebeauty@gmail.com'), +('fourrshoptarakan@gmail.com'), +('stuffbyms@gmail.com'), +('cindyrissa14@gmail.com'), +('lieke1212@gmail.com'), +('jelitacosmetic.yk@gmail.com'), +('metafuji@gmail.com'), +('destiyapradhani@gmail.com'), +('liepinghau@gmail.com'), +('jennifer_kambuno@yahoo.com'), +('y35beauty@gmail.com'), +('nisamulya@gmail.com'), +('issadella@yahoo.co.id'), +('hartono.henny@yahoo.co.id'), +('a2335572@gmail.com'), +('navisgusmiati24@gmail.com'), +('djiwandimelany@gmail.com'), +('colinakoh178@gmail.com'), +('fairybeaute17@gmail.com'), +('fajri.vanbio95@gmail.com'), +('zidney_yao@yahoo.com'), +('chandraalex305@gmail.com'), +('elly.elisha@gmail.com'), +('erlinapr13@gmail.com'), +('mechan_06@hotmail.com'), +('limvincents@hotmail.com'), +('tjuganteng@gmail.com'), +('sralaydrus96@yahoo.com'), +('duwik_tonx@yahoo.co.id'), +('adelshopadelia@gmail.com'), +('shierlysusanto2405@gmail.com'), +('murni.cosmetics55@gmail.com'), +('stg_purchasing_melisa@yahoo.com'), +('asri.nilasari@watsons.co.id'), +('wayanndiana@gmail.com'), +('emailtedy@gmail.com'), +('Shantifm@hotmail.com'), +('shintachristiani2911@gmail.com'), +('dotienigloo@gmail.com'), +('mirandadessy95@gmail.com'), +('pangeranpetirr@gmail.com'), +('mariamellysacandra@ymail.com'), +('evitamalaa@ymail.com'), +('lindadarwinp@gmail.com'), +('dyfabandung@gmail.com'), +('yestastore@gmail.com'), +('outletmadanibeauty@gmail.com'), +('mrs.kumara@yahoo.com'), +('cindylum23@gmail.com'), +('fairy_love90@ymail.com'), +('fajarcosmetikpekanbaru@gmail.com'), +('alapstorekosmetik@gmail.com'), +('marlinabahnur@yahoo.co.id'), +('miswarderi61@gmail.com'), +('kristian_tjia@yahoo.co.id'), +('dessi.three@gmail.com'), +('rumahkurumahta@gmail.com'), +('nabilaalifia2000@gmail.com'), +('ullyfina@gmail.com'), +('tjindaidyandy@gmail.com'), +('shintayuu@gmail.com'), +('ratihpuji001@gmail.com'), +('shynobyshould@gmail.com'), +('rismawatisapu101@gmail.com'), +('caramelmadeby@gmail.com'), +('piyamapoppy@gmail.com'), +('scarlettwhitening.manado@gmail.com'), +('makeupbliss.id@gmail.com'), +('agnyjmpardosi@gmail.com'), +('sherbeautee@gmail.com'), +('yfbeauty99@gmail.com'), +('cmstadulako36@gmail.com'), +('lizalgs97@gmail.com'), +('emailnyawabisabi@gmail.com'), +('alvhiraalamrih@gmail.com'), +('megasyaf10@gmail.com'), +('ismisarisapitri6@gmail.com'), +('realrima91@gmail.com'), +('hestynorata74@gmail.com'), +('tokopinkbox@gmail.com'), +('melly.angelica@sils.co.id'), +('baguzokee@gmail.com'), +('vandy_hood@yahoo.com'), +('stevenwerencius15@gmail.com'), +('qurniaulan123@gmail.com'), +('ivanalystia21@gmail.com'), +('claralverinas@gmail.com'), +('riaarddd@gmail.com'), +('yayz_bubble@yahoo.com'), +('elsa.rivani@rocketmail.com'), +('feyrenpusura@gmail.com'), +('gunz_r@yahoo.com'), +('asrularman077@gmail.com'), +('hellojuwitaang@gmail.com'), +('bernike.daniel@gmail.com'), +('ayuerawati1993@gmail.com'), +('sudutcantik03@gmail.com'), +('norio.agnes@gmail.com'), +('smarch2u@gmail.com'), +('nunungokta8@gmail.com'), +('siaocie.tw@gmail.com'), +('sandrachen20@icloud.com'), +('siti.komariiah@gmail.com'), +('gustine.fu999@gmail.com'), +('ennysetiono@msn.com'), +('sartikanganro@gmail.com'), +('mirandahabibie@gmail.com'), +('tokodahliapadang@gmail.com'), +('natanaelsbrn@gmail.com'), +('jelitacosmetic@galeri.com'), +('rkkosmetikviral.anj@gmail.com'), +('sancaytulak26@gmail.com'), +('kelvinhosen@gmail.com'), +('elnazulma@gmail.com'), +('jevrihuang@gmail.com'), +('araitsoke@gmail.com'), +('haju.nutrifood@gmail.com'), +('harwindaharis99@gmail.com'), +('saherasukur92@gmail.com'), +('monamelosi05@gmail.com'), +('pramistiseptarinda@gmail.com'), +('rianadewi15746@gmail.com'), +('frivaldi08@gmail.com'), +('sausankho86@gmail.com'), +('3widjayanti@gmail.com'), +('prinzlina79@gmail.com'), +('restbeauty11@gmail.com'), +('kiarriyasatul@gmail.com'), +('surkiaa56@gmail.com'), +('dellikosmetik@gmail.com'), +('dinafebriana@gmail.com'), +('risnawatiyunus@yahoo.com'), +('pudoopy@gmail.com'), +('serbunganjuk@gmail.com'), +('linda_ho29@yahoo.com'), +('cassmgadm@gmail.com'), +('jennychateriner@gmail.com'), +('thasashop.id@yahoo.com'), +('diancagoodsbywitari@gmail.com'), +('chang3mu@icloud.com'), +('hana_kho@outlook.com'), +('jwl.skinofficial@gmail.com'), +('febri.ilham68@gmail.com'), +('gurl.shopaholic@gmail.com'), +('fenitan00@gmail.com'), +('nadrachanafie@gmail.com'), +('migibeauty1@gmail.com'), +('dewisusianti888@gmail.com'), +('nazwinadya@gmail.com'), +('andinimaylasari@gmail.com'), +('syahrani.cosmetic@gmail.com'), +('primasitaa@gmail.com'), +('spy_solution77@yahoo.com'), +('geeghina@gmail.com'), +('nida.syafa5@gmail.com'), +('financialelzamora@gmail.com'), +('irkosmetikdanskincare@gmail.com'), +('sherlinhosana@gmail.com'), +('sepdiarina@yahoo.com'), +('lumbangaol.ira@gmail.com'), +('aligunawan098@gmail.com'), +('ovarash@gmail.com'), +('rbeauty.storee@gmail.com'), +('cckosmetik77@gmail.com'), +('pinkcosmetich@gmail.com'), +('dhiniemsa@yahoo.com'), +('rulyindah1911@gmail.com'), +('elvina.6789@gmail.com'), +('moeyecosmetic99@gmail.com'), +('sana.19007@mhs.unesa.ac.id'), +('ikasusanti_baru@yahoo.com'), +('indaryani82@gmail.com'), +('hsyapon@gmail.com'), +('citra.ismaya@omniretail.co'), +('thevintagestrawberry@gmail.com'), +('ramadhani.kosmetik@gmail.com'), +('linawijaya2712@yahoo.com'), +('meilings0403@gmail.com'), +('nadyawkk@gmail.com'), +('prellasurabaya@gmail.com'), +('ziyyags@gmail.com'), +('stanley_oh@ymail.com'), +('andre1177az@gmail.com'), +('s.ekaputrirosalinda@gmail.com'), +('beautyland21@icloud.com'), +('javalotuscorp@gmail.com'), +('alxsuherman@gmail.com'), +('fenny3007.kwok@gmail.com'), +('leny_kurniawati@hotmail.com'), +('soputanlady10@gmail.com'), +('lampungbeauty2019@gmail.com'), +('thebeauteebox@gmail.com'), +('tfanixling00@gmail.com'), +('andilesmono06@gmail.com'), +('briggiteleorenza@gmail.com'), +('chanytatiara@gmail.com'), +('financeklinikpiramidajaya@gmail.com'), +('exacarolina@gmail.com'), +('wildhanhafid@gmail.com'), +('himakeup08@gmail.com'), +('riska.sofani@yahoo.com'), +('uciliana35@gmail.com'), +('agusjokin08@gmail.com'), +('madozai96@gmail.com'), +('flo_ver@yahoo.com'), +('dikinarasaki@gmail.com'), +('herul.batch3@gmail.com'), +('rudy.hartono2591@gmail.com'), +('vivi.plm187@gmail.com'), +('hallo@bencuan.id'), +('rahasiamama17@gmail.com'), +('stephzhengg@gmail.com'), +('shelinabeautycare@gmail.com'), +('joelchristianpamula@gmail.com'), +('rethamardjoen11@gmail.com'), +('pomadaivon@gmail.com'), +('divaparis28@gmail.com'), +('wulan.ranpatika@gmail.com'), +('mastoreyadi@gmail.com'), +('febi.muhi@gmail.com'), +('anggriawanferi1991@gmail.com'), +('feliciacolleen80@gmail.com'), +('istie.nodasekisan@gmail.com'), +('melliyniaanugrah@gmail.com'), +('dwiaziza98@gmail.com'), +('alfrianiyelsi@gmail.com'), +('adelia.vn@gmail.com'), +('alvina.iriady@gmail.com'), +('ratnaqisya03@gmail.com'), +('jelitacosmetic.galeri@gmail.com'), +('avex307@gmail.com'), +('rika.wulandaridwan@gmail.com'), +('srihartiniseptember1966@gmail.com'), +('theresia_margareths@yahoo.com'), +('kartikahs89@gmail.com'), +('yanuararifinnur@gmail.com'), +('luthfikhtr@gmail.com'), +('mshopper.id@gmail.com'), +('fifi.liu@yahoo.com'), +('christineeva07@gmail.com'), +('claralverina23@gmail.com'), +('12101na@gmail.com'), +('joanamarvelin.wongso@yahoo.com'), +('lylysiakwee@gmail.com'), +('andyirsaldi@gmail.com'), +('greceliautami@icloud.com'), +('rhayati48@gmail.com'), +('vidya@redroseindo.com'), +('ekanuraini131@gmail.com'), +('kimhiung168@gmail.com'), +('gitaporo@gmail.com'), +('aninditateresa@gmail.com'), +('lexonbeautyofficial@gmail.com'), +('novitaroshaputri@gmail.com'), +('theara.tiara@gmail.com'), +('selvi.chephy@gmail.com'), +('vhaans007@gmail.com'), +('tokosehat130@gmail.com'), +('mirayyacosmetics@gmail.com'), +('kusnen.sota@gmail.com'), +('hokimaxi2020@gmail.com'), +('keikolux@hotmail.com'), +('andreadewi19@gmail.com'), +('harlyskin@gmail.com'), +('viroe.nando@gmail.com'), +('aurelallshop@gmail.com'), +('kosmetikiis@gmail.com'), +('lifanny86@gmail.com'), +('nitamanda28@gmail.com'), +('cantiku8899@gmail.com'), +('sultanagengs3@gmail.com'), +('nurfiitriiyah@gmail.com'), +('nana01iphone@gmail.com'), +('mulyajaaya.motor96@gmail.com'), +('titarahmawati245@gmail.com'), +('marniatihinding@gmail.com'), +('erris.mahardika@gamal.men'), +('isti.qhatsah.nur@gmail.com'), +('dewilasolo@gmail.com'), +('aninditaputri30@gmail.com'), +('aryanto.firdaus@gmail.com'), +('novitaj68@gmail.com'), +('johan_breaker27@yahoo.com'), +('elsaberliya@gmail.com'), +('dewiyuant260693@gmail.com'), +('jeffrynoah@gmail.com'), +('rikasukmawar03@gmail.com'), +('yosua.sasauw1@gmail.com'), +('fitri.miauwh@gmail.com'), +('yuli120797@gmail.com'), +('kamala.tanray@gmail.com'), +('theresiad88@gmail.com'), +('isliana29@gmail.com'), +('chietra.anggraini93@gmail.com'), +('cesario2559@gmail.com'), +('the.secret.of.beauty.id@gmail.com'), +('elsacosmetics@yahoo.com'), +('evinrahmad2@gmail.com'), +('yuhyi.albadali@gmail.com'), +('darwin.saputra.ars@gmail.com'), +('akuntingtim2903@gmail.com'), +('wahyudinrazali4@gmail.com'), +('jwuysan@gmail.com'), +('ruus.silvana@gmail.com'), +('dianmarinaa@gmail.com'), +('dp.anggreani@gmail.com'), +('patricia.ang7@gmail.com'), +('medankosmetik89@gmail.com'), +('ayuputri.ap2201@gmail.com'), +('fikrizia13@gmail.com'), +('beautiva.skincare@gmail.com'), +('gjwproject@gmail.com'), +('pramudya.a.laksono@gmail.com'), +('ssshoptarakan@gmail.com'), +('bungalintong7@gmail.com'), +('ivanakaontole@gmail.com'), +('misel.delini@yahoo.com'), +('dwipujiati1995@gmail.com'), +('vdillahilda@gmail.com'), +('ellenanatasa@gmail.com'), +('bidarishabrina@gmail.com'), +('danimmm44@gmail.com'), +('evitsrikris@gmail.com'), +('gendisbeauty92@gmail.com'), +('eriana.dian0116@gmail.com'), +('eriskhfrd26@gmail.com'), +('jasmineniscalapandya@gmail.com'), +('eryryey03@gmail.com'), +('leessleepwear@gmail.com'), +('yoseptansil6@gmail.com'), +('natdailybeauty@gmail.com'), +('sidoarjosalonsupp@gmail.com'), +('auliarahmadanti77@gmail.com'), +('gustindw@gmail.com'), +('mamihcihuyyy@gmail.com'), +('narabeautytrk@gmail.com'), +('sandikaplg18@gmail.com'), +('20240010013@lspr.edu'), +('maynetav@gmail.com'), +('intandidi88@gmail.com'), +('jeniferpurnomo99@gmail.com'), +('dewisartika192@yahoo.co.id'), +('jokosus.susanto@gmail.com'), +('iswanto.1301@gmail.com'), +('kudo.palopo14@gmail.com'), +('micellangel@nuriglobal.com'), +('theeaanggraeny@gmail.com'), +('andi.or88@gmail.com'), +('rickyoktaviawan20@gmail.com'), +('yunibalisa661@gmail.com'), +('rikiscofieldsap@gmail.com'), +('feliciabeatrixtanner0407@gmail.com'), +('mon_taca@yahoo.com'), +('ardyanawidya@gmail.com'), +('d14na_nonic@yahoo.com'), +('kadirpnm@gmail.com'), +('mita.panji@gmail.com'), +('robbchriszzz13@gmail.com'), +('anicha8490@gmail.com'), +('galuh.acc.kediri@gmail.com'), +('tokosanjayakras.81@gmail.com'), +('vivian.effendi@gmail.com'), +('lisaderia@yahoo.com'), +('anggyfebryna17@gmail.com'), +('yuanita200989@gmail.com'), +('inggycitrasari@gmail.com'), +('musdalifa300396@gmail.com'), +('widdywedariasih3@gmail.com'), +('devinoariska06@gmail.com'), +('idealistina@yahoo.co.id'), +('kelsy18111@gmail.com'), +('elizchang88@gmail.com'), +('kavbeautyofficial@gmail.com'), +('archilagiska@gmail.com'), +('ginawati442@gmail.com'), +('revii.setiadi@gmail.com'), +('nisacuss@gmail.com'), +('eviana001075@gmail.com'), +('moanazone@gmail.com'), +('idabenangraja@gmail.com'), +('babybamscase@gmail.com'), +('bellasetiady@windowslive.com'), +('regina.amalia19@gmail.com'), +('hello.vebeauty@gmail.com'), +('ferinaindrayani@gmail.com'), +('yulia.cindy81@gmail.com'), +('ennakristiana@gmail.com'), +('nur.annisa0@gmail.com'), +('marindarara13@gmail.com'), +('sonyatiaratri2@gmail.com'), +('achmadfauzi1398@gmail.com'), +('fitrijean@gmail.com'), +('zul.hendra1297@gmail.com'), +('csbymagicskin@gmail.com'), +('ridadwi873@gmail.com'), +('grosirkosmetikpekanabaru@gmail.com'), +('gladys.agatha@gmail.com'), +('rahmizarti30@gmail.com'), +('mrs.sitiazizah@gmail.com'), +('paramitaqueena@yahoo.com'), +('uwiecute22@gmail.com'), +('slife557.cs@gmail.com'), +('adindazahra655@gmail.com'), +('lavibeautyandlifestyle@gmail.com'), +('ratna.pratiwi14.rp@gmail.com'), +('yunengazraapriani@gmail.com'), +('sheefaa84@gmail.com'), +('katherinelimanu21@gmail.com'), +('tutiyuniyanti@gmail.com'), +('kartinisarimah@gmail.com'), +('jonastampubolon123@gmail.com'), +('wuisancheni77@gmail.com'), +('shella.madirza888@gmail.com'), +('yulianisetiawan836@gmail.com'), +('janiceechenn@yahoo.com'), +('ferico.ridwan@gmail.com'), +('udjayamandiripgs@gmail.com'), +('budiwijaya1962@gmail.com'), +('rumahkosmetik001@gmail.com'), +('kinanbeautystore@gmail.com'), +('yuniraemah31@yahoo.co.id'), +('barokahbeaute@gmail.com'), +('shrlymarcella@gmail.com'), +('ikrt_2507@yahoo.com'), +('dikofebyan@gmail.com'), +('restyty.riezalina@yahoo.com'), +('ratukosmetik169@gmail.com'), +('faradillahif07@gmail.com'), +('sales@dewicosmetic.com'), +('kadek.arymas@gmail.com'), +('rahmaduwijayanti@gmail.com'), +('fanamaliaauthor@gmail.com'), +('vanessapratama24@gmail.com'), +('gangnamcosmetic109@gmail.com'), +('mayanglatuconsina@gmail.com'), +('lustremaiden@gmail.com'), +('ratihalexa@gmail.com'), +('anindyalarasp@gmail.com'), +('fourayoebeauty@gmail.com'), +('selenabeauty.banjarbaru@gmail.com'), +('destyisniaty@gmail.com'), +('rmdhniannisa15@gmail.com'), +('devimutiah18@gmail.com'), +('ainunmuthiaa@gmail.com'), +('nurhikmah.rasyid@gmail.com'), +('burizma.store@gmail.com'), +('ariefariq4@gmail.com'), +('haniaekap8814@gmail.com'), +('ricomerico240@gmail.com'), +('ekanvt07@gmail.com'), +('helenlim02@gmail.com'), +('lukm.hakm@gmail.com'), +('nur_aisyah1407@icloud.com'), +('serlina.mbc@gmail.com'), +('k.inayah23@gmail.com'), +('marisrisma@yahoo.com'), +('shela.annisa99@gmail.com'), +('angguntrivany@gmail.com'), +('nntheorange@gmail.com'), +('nathaliayaulin23@gmail.com'), +('mrs.gapura@gmail.com'), +('selviathalita92@gmail.com'), +('cynthia.de06@gmail.com'), +('beautemondee@gmail.com'), +('ninikrahmawati486@gmail.com'), +('anst.slvia@gmail.com'), +('beautycarepalu@gmail.com'), +('ririwiduri2002@gmail.com'), +('ilookdaily534@gmail.com'), +('apapunada.id@gmail.com'), +('dellasyaskia6@gmail.com'), +('nadiasalmau@gmail.com'), +('tikamaharaeni19@gmail.com'), +('aansabang@gmail.com'), +('devinatalithanabila98@gmail.com'), +('ennitrisha@gmail.com'), +('marialucyanasunjaya02762@gmail.com'), +('rtnohandayani@gmail.com'), +('nsbeauty06@gmail.com'), +('idapurnamasari0129@gmail.com'), +('msreysantana@gmail.com'), +('aldillaputri5@gmail.com'), +('maderusni25@gmail.com'), +('crystalglow.myshop@gmail.com'), +('dheamuliasari@gmail.com'), +('cindyfdle@gmail.com'), +('laninabeauty.official@gmail.com'), +('nimatussaadah29@gmail.com'), +('merrylaili@gmail.com'), +('wijaya.nanda1810@gmail.com'), +('kadavikadav@gmail.com'), +('anggikurniawati40@gmail.com'), +('mamikikeyu@gmail.com'), +('marlindaabdulrazak@gmail.com'), +('alizhaaa@gmail.com'), +('queenkosmetik20@gmail.com'), +('skincarekupang21@gmail.com'), +('feny.asnawi@gmail.com'), +('shanticahyadi82@gmail.com'), +('zilvanaputri@gmail.com'), +('malikaresmanti@gmail.com'), +('yunisianturi06@gmail.com'), +('beautydarlinggg@gmail.com'), +('lilybeauty.ofc@gmail.com'), +('racyasry@gmail.com'), +('terasbeauty.official@gmail.com'), +('mybeautystory.89@gmail.com'), +('novita.chyntia@yahoo.com'), +('tiffany.kimana@gmail.com'), +('rumahdandan.rd15@gmail.com'), +('desihartanti17@gmail.com'), +('forshare.id@gmail.com'), +('elmiacosmetics@gmail.com'), +('evikartika03@gmail.com'), +('allwomeneeds1403@yahoo.com'), +('nadyahudaya@yahoo.com'), +('drchrysant@gmail.com'), +('yurimeni16@gmail.com'), +('anientya@gmail.com'), +('martina.bernadeta@gmail.com'), +('wulan.dhizhi@gmail.com'), +('andrixue@gmail.com'), +('sitiqulsum308@gmail.com'), +('galaxystoreyogyakarta@gmail.com'), +('iskifitriani@yahoo.co.id'), +('arismunandar6496@gmail.com'), +('agenlingerie.id@gmail.com'), +('indahasmoro90@gmail.com'), +('ksbeautylover@gmail.com'), +('alheabeauty.id@gmail.com'), +('putriitunggal99@gmail.com'), +('ahmadmalangah@gmail.com'), +('perfectionten@gmail.com'), +('ivonsetiono26@gmail.com'), +('agencmb@gmail.com'), +('glowivier@gmail.com'), +('pinkyscosmetics1995@gmail.com'), +('evinakuesnoooo@gmail.com'), +('chennywi0126@icloud.com'), +('ekawahyuni1603@gmail.com'), +('auliaqoriardi21@gmail.com'), +('joyakhineigsoen@gmail.com'), +('susantifajarwati@yahoo.com'), +('ramadhana.selvia2408@gmail.com'), +('andarish.shop@gmail.com'), +('cindytsania@gmail.com'), +('joctora@gmail.com'), +('hannahimanuella@gmail.com'), +('dwiwulandari028@gmail.com'), +('merrychrystin06@gmail.com'), +('fibi.kdi891@gmail.com'), +('suwitosjahari@gmail.com'), +('likestoretlogosari@gmail.com'), +('care.meiyabeauty@gmail.com'), +('ajenganggitasari1@gmail.com'), +('rizkiyanitapinem@gmail.com'), +('sashop.nkc21@gmail.com'), +('lilistiyorini@gmail.com'), +('thebellas1212@gmail.com'), +('be.bellecosmetic@gmail.com'), +('nurlisaaulia@gmail.com'), +('hantybeauty19@gmail.com'), +('mputrisanti@gmail.com'), +('hellolightbit@gmail.com'), +('nadhirauw88@gmail.com'), +('asmiatiakib@gmail.com'), +('ranintya12@gmail.com'), +('afrijalrijal671@gmail.com'), +('ifayusana1993@gmail.com'), +('makeyouup.myu@gmail.com'), +('salbinus.innung@yahoo.com'), +('novia190@gmail.com'), +('evadewim.s@gmail.com'), +('itsmiracle2020@gmail.com'), +('neveltari2703@gmail.com'), +('lieusbudiman@gmail.com'), +('ferisya10@gmail.com'), +('echa.muaniez89@gmail.com'), +('trinarachmawati81@gmail.com'), +('erwinzeey123@gmail.com'), +('sahabat_debora@yahoo.com'), +('dhee2jopankar@gmail.com'), +('yunda.ade11@gmail.com'), +('cyril_steven_alexander_glory@yahoo.com'), +('devisiska32@gmail.com'), +('aureolasa.pratama@gmail.com'), +('ika.angky123@yahoo.com'), +('halimatussakdiyah1997@gmail.com'), +('rahmatjayaj2@gmail.com'), +('jesselynangella08@yahoo.com'), +('famela.ft07@gmail.com'), +('rotinsulufinny@gmail.com'), +('staryisnianti@gmail.com'), +('yulianazayan@gmail.com'), +('fandestian@gmail.com'), +('melindariaslamusnar88@gmail.com'), +('jnwkidswear@gmail.com'), +('wiwin.adriani@gmail.com'), +('febriantisiban25@gmail.com'), +('scoviabybelova@gmail.com'), +('wenny.than@gmail.com'), +('iranipontoh443@gmail.com'), +('hello.lazetta@gmail.com'), +('moullylaroid@gmail.com'), +('laurantjia@gmail.com'), +('dbeautyshop8@gmail.com'), +('godzillaphoneacc3@gmail.com'), +('wiwinstsajaah@gmail.com'), +('rizahani39@gmail.com'), +('dwihartanti_gizi@yahoo.com'), +('fangfangsaputra@gmail.com'), +('amoise.cosmetics@gmail.com'), +('dennyroseiana@gmail.com'), +('nelly.sulistiawati@gmail.com'), +('central.kosmetik1106@gmail.com'), +('marcellino.haryono@gmail.com'), +('veralia.saragih@gmail.com'), +('cefamanusia@gmail.com'), +('arindatrisna33@gmail.com'), +('oketomatala@gmail.com'), +('vilyapangajouw19@gmail.com'), +('faracintadia@gmail.com'), +('adm.elisabethbdg@yahoo.com'), +('cutmiqyal@hotmail.com'), +('harjono.honoris@gmail.com'), +('nandorian98@gmail.com'), +('milalimbong71@gmail.comcom'), +('viliansiregar24@gmail.com'), +('eldeekosmetik@gmail.com'), +('jeane.lelengboto@yahoo.com'), +('lydia.debby@gmail.com'), +('aboutmybusiness11@gmail.com'), +('triple501ara@gmail.com'), +('masfitroh@gmail.com'), +('meisalmis@gmail.com'), +('zackfair232@gmail.com'), +('nakamia_pie@yahoo.com'), +('abeauty.jogja@gmail.com'), +('lenaaska7@gmail.com'), +('zeebacosmetics31@gmail.com'), +('chiangkencana@gmail.com'), +('syaaudriana@gmail.com'), +('nadyaffu@gmail.com'), +('fauziahanisa509@gmail.com'), +('christyciasintaungu@gmail.com'), +('pradnyani.dwi@gmail.com'), +('zarazsby@gmail.com'), +('adewahyupauji@gmail.com'), +('firmantuo@gmail.com'), +('ramadhanilayla@gmail.com'), +('novascafe4@gmail.com'), +('mayaecc@yahoo.com'), +('adeyoan721@gmail.com'), +('stephaniefransss@gmail.com'), +('hermooi.official@gmail.com'), +('ratnaanita170294@gmail.com'), +('agatri.silitonga@gmail.com'), +('dewifitri2403@gmail.com'), +('moehamadsururi14@gmail.com'), +('ariestyayys@gmail.com'), +('heavenlybeauty.adm@gmail.com'), +('ikaabsarih@gmail.com'), +('dhee_amell@yahoo.co.id'), +('devianatanuwijaya@gmail.com'), +('rumah.karara@gmail.com'), +('sreejita.deb@gmail.com'), +('tagdirs.cosmetic@gmail.com'), +('ceviwilly@gmail.com'), +('daniel.khosuma@gmail.com'), +('muhammadyanis12091991@gmail.com'), +('liechintia07@gmail.com'), +('apt.deantigano@gmail.com'), +('ian.tampo22@gmail.com'), +('kunyil505@gmail.com'), +('hongjujen69@gmail.com'), +('wimma.shop@gmail.com'), +('mysamplemart@gmail.com'), +('safitriyuli3@gmail.com'), +('novanindra23@gmail.com'), +('hartantiarline@gmail.com'), +('dianyunus7@gmail.com'), +('aniyt161aan@gmail.com'), +('aestheticbeautycare23@gmail.com'), +('marissarahmaadhania9i30@gmail.com'), +('cecebeautybali@gmail.com'), +('devani.ersa@yahoo.com'), +('windiana10@gmail.com'), +('itaufik55@gmail.com'), +('zhisca_77@yahoo.co.id'), +('jillyanesasiage@gmail.com'), +('mybestiebeauty.bdg@gmail.com'), +('purchasing@bumame-farmasi.com'), +('denisa.denis89@yahoo.com'), +('ranggatedubara@gmail.com'), +('angelialim0311@gmail.com'), +('rindaagustiana@gmail.com'), +('aleshabeautycare57@gmail.com'), +('monpri28@gmail.com'), +('move.hamsari@gmail.com'), +('beautybelle2608@gmail.com'), +('priscillalastyfera@gmail.com'), +('vildarahmawati17@gmail.com'), +('nathaniaa099@gmail.com'), +('noyura@gmail.com'), +('juhatihajibe@gmail.com'), +('winda.christina@gmail.com'), +('mruslir21@gmail.com'), +('siskagsp6@gmail.com'), +('olyviadevita02@gmail.com'), +('ecgirl2023@gmail.com'), +('songlacosmetics@gmail.com'), +('jelsymakeup@gmail.com'), +('vanywidyawati86@gmail.com'), +('ongfreddykusuma@gmail.com'), +('charisthamaysadwisahara@gmail.com'), +('incjodu@gmail.com'), +('mjbeautyblitar@gmail.com'), +('ngeng.yuan27@gmail.com'), +('mybeautymart.id@gmail.com'), +('nabillafr22@gmail.com'), +('dianajennifer118@gmail.com'), +('wetirima9988@gmail.com'), +('shely.proborini@yahoo.com'), +('pak.serba@gmail.com'), +('lili_3038@yahoo.co.id'), +('tallamersiaa@gmail.com'), +('khalisanaura156@gmail.com'), +('milaatysakinah@gmail.com'), +('ucci.arfan@gmail.com'), +('lutfiah.nurhandari@gmail.com'), +('albinadhaja94@gmail.com'), +('dindaty@gmail.com'), +('shopkievanik@gmail.com'), +('zheng.jinde7692@gmail.com'), +('silvi@getvybes.co'), +('muhammadraydevan@gmail.com'), +('alstininoviyantitoar@gmail.com'), +('riahafarul778@gmail.com'), +('nrustam26@gmail.com'), +('gayabyys.wu@gmail.com'), +('lalootust@gmail.com'), +('rina2pan@yahoo.com'), +('disariska@gmail.com'), +('ninu.fira@gmail.com'), +('yuibabaretail@gmail.com'), +('makshifa93@gmail.com'), +('nadzimalaily@gmail.com'), +('nindysyarif@gmail.com'), +('mutiarapesonanganjuk@gmail.com'), +('beautybypelangi@gmail.com'), +('mariyani3412@gmail.com'), +('anitabudi1998@gmail.com'), +('farikhanum@ymail.com'), +('riscaamalia1989@gmail.com'), +('oyha.ciez01@gmail.com'), +('afriasafitri@ymail.com'), +('gitalaksmicosmetic@gmail.com'), +('oktavianimunir@gmail.com'), +('meithaekyh@gmail.com'), +('antouwkeren@gmail.com'), +('meeranova.id@gmail.com'), +('dmuhenti@gmail.com'), +('bima.ajun@gmail.com'), +('sitipluviana@gmail.com'), +('endahpratiwi522@gmail.com'), +('dindalabibah@gmail.com'), +('pusatkosmetikpinky@gmail.com'), +('anjalisibagariang2000@gmail.com'), +('krisnairma15@gmail.com'), +('ocha.rospa@gmail.com'), +('siscabudhyani86@gmail.com'), +('jhenisarykambuno@gmail.com'), +('mp-store4@raenabeauty.com'), +('luckyyosin0913@gmail.com'), +('monica1726@yahoo.co.id'), +('deasyrimanda@gmail.com'), +('pbudianoto@gmail.com'), +('hernispratiwi@gmail.com'), +('lelymelani@yahoo.com'), +('cantiksehat.sby@gmail.com'), +('gunk.fan29@gmail.com'), +('nurhayati01022021@gmail.com'), +('wardahafi@gmail.com'), +('nonikmery256@gmail.com'), +('lismiatilenta69@gmail.com'), +('dewinurhidayah5125@gmail.com'), +('nurhikmah.hasbi@gmail.com'), +('dordiaahss@gmail.com'), +('molly.fh2@gmail.com'), +('avostoresurabaya@gmail.com'), +('alhun.lun@gmail.com'), +('thebeautycare05@gmail.com'), +('mykarol88@gmail.com'), +('angela.sania@yahoo.com'), +('divaa01@ymail.com'), +('p.ariyanti2@gmail.com'), +('rizkycosmetics@gmail.com'), +('claudiayolandpurnomo@gmail.com'), +('ayudea1820@gmail.com'), +('allshopbisnis72@gmail.com'), +('yosephinetan@yahoo.co.id'), +('ayuri.minho@yahoo.com'), +('paolahermawan@yahoo.com'), +('mirna.mirnaaa02@gmail.com'), +('kumaskincare01@gmail.com'), +('officialrabeauty@gmail.com'), +('cacabeauty1234@gmail.com'), +('nadeakayuangellika@gmail.com'), +('linacaterina79@gmail.com'), +('rianishop210@gmail.com'), +('ilhamxmaulanaaa@gmail.com'), +('aratna643@gmail.com'), +('vifi2403@gmail.com'), +('paringdyahdinda@icloud.com'), +('suantysuanty10@gmail.com'), +('nandalisa045@gmail.com'), +('tinylilproject@yahoo.com'), +('arfan.arsya2022@gmail.com'), +('dahliaqueenshop@gmail.com'), +('triantiagus@gmail.com'), +('widyastevani17@gmail.com'), +('adityo_konandar@yahoo.com'), +('zoerosyidh89@gmail.com'), +('yusmane395@gmail.com'), +('tephtephyy@gmail.com'), +('seemser.id@gmail.com'), +('tantinaduu@gmail.com'), +('yannayeshika@gmail.com'), +('juliacsecogati@gmail.com'), +('pheroneid@gmail.com'), +('pauline222bn@gmail.com'), +('dpuspitaprameswari@gmail.com'), +('irnanda.vinka@yahoo.com'), +('agamwijaya89@gmail.com'), +('miniskinjars@gmail.com'), +('faizal@raenabeauty.com'), +('hanaamelinda95@gmail.com'), +('maharanimega39@gmail.com'), +('tia.aprita@gmail.com'), +('m.milanova.sh@gmail.com'), +('belscofabrics@gmail.com'), +('pratamanaufal98@gmail.com'), +('joshanshop@gmail.com'), +('ayuameliasaja@gmail.com'), +('alyadwi00@gmail.com'), +('helloyoumeindonesia@gmail.com'), +('desyrosiyana@gmail.com'), +('isvimega@gmail.com'), +('andrianiyana815@gmail.com'), +('andiakbar.ak@gmail.com'), +('isw11business@gmail.com'), +('guo_fengsheng@yahoo.com'), +('nirmaladewi03@gmail.com'), +('novitheng90@gmail.com'), +('poppymputranto@gmail.com'), +('shellahamayu1997@gmail.com'), +('suparjoariyanti@gmail.com'), +('pipinwahyu80@gmail.com'), +('chryseladarwin.olivia@gmail.com'), +('choisebest64@gmail.com'), +('rafin.trisna@gmail.com'), +('solehac098@gmail.com'), +('ayumirah12@yahoo.com'), +('fasahdi@gmail.com'), +('vindysabeauty@gmail.con'), +('dillalutchu08@gmail.com'), +('hi.dearbella@gmail.com'), +('awili.tri@gmail.com'), +('ardiradityananda@gmail.com'), +('dayanishop474@gmail.com'), +('apriliamega08@gmail.com'), +('vindysabeauty@gmail.com'), +('gilsneedit@gmail.com'), +('meiriannaafs@gmail.com'), +('mantepkebumen66@gmail.com'), +('lisnawatirohmah@gmail.com'), +('mhelydya@gmail.com'), +('apriliandesi@yahoo.co.id'), +('memonaofficial1007@gmail.com'), +('attraversiamobeauty@gmail.com'), +('triyatmoko3116@gmail.com'), +('toxena.shop@gmail.com'), +('fitridwifebriani59@gmail.com'), +('apadotid7@gmail.com'), +('misz_irma@yahoo.com'), +('riofabian112200@gmail.com'), +('indahyuliza7@gmail.com'), +('imasmaslihah2222@gmail.com'), +('letsglowid.jbi@gmail.com'), +('mahreenbeautyskin@gmail.com'), +('aldofiras1607@gmail.com'), +('firdamelia05@gmail.com'), +('hajernihajerni@gmail.com'), +('yunusdarmawan8@gmail.com'), +('fmaharani96@gmail.com'), +('tokoyangtepat@gmail.com'), +('endangdolphin2011@gmail.com'), +('pallawalipu21@gmail.com'), +('pie_buana@yahoo.com'), +('marina.diantika@gmail.com'), +('rihandika34@gmail.com'), +('ngm6756@gmail.com'), +('djonyali59@gmail.com'), +('rismagina02@gmail.com'), +('beautybyasame@gmail.com'), +('gracerushandi@gmail.com'), +('rinianggrayniinank@gmail.com'), +('lin_qiau_hua@yahoo.com'), +('buanainskonsi@gmail.com'), +('cosmetik.giant@gmail.com'), +('reynitaballack@gmail.com'), +('shopkichii88@gmail.com'), +('fadhilahafifah48@yahoo.com'), +('vivaforeva1@gmail.com'), +('sisilia.marshella@yahoo.com'), +('irmhanurjayanti@gmail.com'), +('galuhnur81@gmail.com'), +('gees.hijab@gmail.com'), +('putrimeyanti8@gmail.com'), +('rully0152@gmail.com'), +('buntukk166@gmail.com'), +('williantocindy@gmail.com'), +('nadilla@raenabeauty.com'), +('cindyfreda23@gmail.com'), +('liiyalimz@gmail.com'), +('kikaychan90@gmail.com'), +('tiffanyyaprilia@gmail.com'), +('2017120215@student.unpar.ac.id'), +('23jamesbond11@gmail.com'), +('36yonathan@gmail.com'), +('a.azzahrasyah@gmail.com'), +('a6ustina@gmail.com'), +('abyan.shop97@gmail.com'), +('acelinbeauty@gmail.com'), +('adeevaskincaresemarang@gmail.com'), +('adeyatoko@gmail.com'), +('adimahendraganteng@gmail.com'), +('agatha@raenabeauty.com'), +('agatyalaksmi@gmail.com'), +('agungfadilah810@gmail.com'), +('ahmadluckibawazier@gmail.com'), +('ai.x1.35300@sutomo-mdn.sch.id'), +('alfiatulkhubbi@gmail.com'), +('alfonsocosmetics@gmail.com'), +('alifiarizkiatania@gmail.com'), +('aline_fathin@yahoo.com'), +('alisheretotalk@gmail.com'), +('alithapradita@gmail.com'), +('allaboutskincare312@gmail.com'), +('almardhiyyahs@gmail.com'), +('almirabintangr@gmail.com'), +('alvissyahrin619@gmail.com'), +('alycia_monicaa@yahoo.co.id'), +('amaliakusumaniez@gmail.com'), +('ameicosmetic12@gmail.com'), +('amelianucifera@gmail.com'), +('ameliaresnaw@gmail.com'), +('analiani86@gmail.com'), +('andikharianto978@gmail.com'), +('angelinkayla118@gmail.com'), +('angellim0311@gmail.com'), +('angellina.lady96@gmail.com'), +('anggararnda972@gmail.com'), +('anggrini@raenabeauty.com'), +('anggriprad@gmail.com'), +('anisaauliaabas@gmail.com'), +('anisafarah24@gmail.com'), +('annachf.tan@gmail.com'), +('antodukos@gmail.com'), +('antonisang27@gmail.com'), +('appl3fuji@gmail.com'), +('arisrahmat333@gmail.com'), +('artiwayan14@gmail.com'), +('artrisnandari@gmail.com'), +('arumsekarsari573@gmail.com'), +('asepmirwan1@gmail.com'), +('assyafarrizki@gmail.com'), +('astrsari@gmail.com'), +('atikaputrims97@gmail.com'), +('audi.tabrany@gmail.com'), +('avoskinbeautybandung@gmail.com'), +('avuastore@gmail.com'), +('ayunamadewi@gmail.com'), +('babdungdyfa@gmail.com'), +('beatricetheresia1@gmail.com'), +('beautyshoujo@gmail.com'), +('bella.beautittaid@gmail.com'), +('bellaikne77@gmail.com'), +('bembie07@gmail.com'), +('beningkosmetik59@gmail.com'), +('biutidy@gmail.com'), +('brilianifridayasti@gmail.com'), +('budiondream@gmail.com'), +('business.qhansaanakkami@gmail.com'), +('cahaya.kosmetik.id@gmail.com'), +('cahyanidwisepti20@gmail.com'), +('cantikatya2@gmail.com'), +('catherinerambang620@gmail.com'), +('caty.adja@gmail.com'), +('cderz23@yahoo.com'), +('ceceliasansan8@gmail.com'), +('cerimartgo@gmail.com'), +('chandrahalim714@gmail.com'), +('chen.wong21@gmail.com'), +('cheryl.marsha.heryanto@gmail.com'), +('chococoip@gmail.com'), +('christianyendi@gmail.com'), +('cindyaharvardiana@gmail.com'), +('cinthyaruhantopp1@gmail.com'), +('cinthyaruhantopp3@gmail.com'), +('cisilia268@gmail.com'), +('clarysocimartinez@gmail.com'), +('cosellebeauty@gmail.com'), +('cvindokosmetikasejahtera@gmail.com'), +('d.lilacshop@gmail.com'), +('danielbudisantoso@gmail.com'), +('danielsimanjuntak231@gmail.com'), +('dedywijayamail@gmail.com'), +('definitionbeautyjogja@gmail.com'), +('dektasyaputra1301@gmail.com'), +('dellalisapaly@gmail.com'), +('despielia@gmail.com'), +('despielia92@gmail.com'), +('desyoong21@gmail.com'), +('deviannisme@gmail.com'), +('dewimellinda10@gmail.com'), +('dhevio1426@gmail.com'), +('dianakuliah@gmail.com'), +('dianps.haryanto@yahoo.com'), +('dillaparamita0@gmail.com'), +('dinaapril280@gmail.com'), +('dini.dwiarti3105@gmail.com'), +('dpsbykosme@gmail.com'), +('dwiagustin363@gmail.com'), +('dyas253@gmail.com'), +('dyfacafe@gmail.com'), +('eka.fokus@gmail.com'), +('eleonora@raenabeauty.com'), +('elfayunengsih20@gmail.com'), +('elisiabriana@gmail.com'), +('ellierahardja@gmail.com'), +('elsa.primasari.eps@gmail.com'), +('elsaokta26@gmail.com'), +('endahtriwedanti28@gmail.com'), +('endang.kosmetik1@gmail.com'), +('endangmarbunbbm678@gmail.com'), +('ennykoesrini676@gmail.com'), +('ennysetiono@gmail.com'), +('enokfatimah405@gmail.com'), +('entok345@gmail.com'), +('enungnurhalimah93@gmail.com'), +('envirolisasekar@gmail.com'), +('ernazulianita00@gmail.com'), +('estrellabeautee@gmail.com'), +('etienneskincare@gmail.com'), +('etisuningsih117@gmail.com'), +('evan.zelizta98@gmail.com'), +('evelyn.vio81@gmail.com'), +('evinrahmad4@gmail.com'), +('fajar950@gmail.com'), +('fajaroldman@gmail.com'), +('farah.fbindonesia@gmail.com'), +('faridahsalma@gmail.com'), +('febrina@raenabeauty.com'), +('feliceclarissa90@gmail.com'), +('fennyardisheila@gmail.com'), +('fera.julianti93@gmail.com'), +('feriscainez@gmail.com'), +('fffgh8@gmail.com'), +('fikrizia_insani@yahoo.com'), +('firabanun@gmail.com'), +('fitria_rida@yahoo.com'), +('fitriamalda123@gmail.com'), +('fitriand0192@gmail.com'), +('flavianusferdiaf@gmail.com'), +('flawless.officialshop@gmail.com'), +('flimtysurabayaofficial@gmail.com'), +('fmieayam@gmail.com'), +('freshnglow77@gmail.com'), +('friendhik25@gmail.com'), +('gabriastore@gmail.com'), +('gajahmadawaee@gmail.com'), +('galaticos02@hotmail.com'), +('gbjmarket@gmail.com'), +('givonsal@gmail.com'), +('glammarket64@gmail.com'), +('gloryharahap@gmail.com'), +('grosirmurahsukoharjo5@gmail.com'), +('guo_fengsheng@yahoo.co.id'), +('gustina.denaiyenti21@gmail.com'), +('hanselchan55@gmail.com'), +('hantarancilandak@gmail.com'), +('hardiantinila3@gmail.com'), +('hartono.henny@yahoo.com'), +('hasanyeong8@gmail.com'), +('helbert.simanjuntak@gmail.com'), +('hello.sheetmask@gmail.com'), +('hellopakbu@gmail.com'), +('helmikamanihuruk@gmail.com'), +('herdianametalic@gmail.com'), +('hestydharmayanti933@gmail.com'), +('heymarcell@gmail.com'), +('hijriahr08@gmail.com'), +('hulahulahali@gmail.com'), +('idcosmeticsidcosmetics@gmail.com'), +('ihc.korea@gmail.com'), +('ihccianjur@gmail.com'), +('iis.sugiati83@gmail.com'), +('ijallevine@gmail.com'), +('ikromyafik147@gmail.com'), +('imahgrosir@gmail.com'), +('indahpurwanti4410@gmail.com'), +('indrianimentaruk2@yahoo.com'), +('info.athirabeauty@gmail.com'), +('inggrid.kambuno@yahoo.com'), +('isbeyondstyle@gmail.com'), +('istrinyamalik@gmail.com'), +('iwanku203@gmail.com'), +('jacovcho@gmail.com'), +('jayaananda247@gmail.com'), +('jayde67@genesvjq.com'), +('jazzpro131@gmail.com'), +('jcrshop20@gmail.com'), +('jeliem96@gmail.com'), +('jelysoviana@gmail.com'), +('jesiskaruslan@gmail.com'), +('jessicaa2208@gmail.com'), +('jscang93@gmail.com'), +('jschrynt@gmail.com'), +('juliaviliajulia@gmail.com'), +('jullyangelina85@gmail.com'), +('jumrayana@gmail.com'), +('junidiarisna@gmail.com'), +('jwinarso889@gmail.com'), +('karinaagustin613@yahoo.co.id'), +('katalogskincarekorea@gmail.com'), +('keanoliem3@gmail.com'), +('kekensetiawan@gmail.com'), +('khairunniszahaifa@yahoo.com'), +('kikiseptiana92@gmail.com'), +('klikandshop18@gmail.com'), +('kntz199@gmail.com'), +('kosmetiklancarjaya@gmail.com'), +('kuboykukuh@gmail.com'), +('kunarifin@gmail.com'), +('kurniadi.phan@gmail.com'), +('kyuhaekoinline@gmail.com'), +('kyuhyunkoinline5@gmail.com'), +('lancarjayakosmetik@gmail.com'), +('lanisiska45@gmail.com'), +('laure.devienne@gmail.com'), +('lavoilla@gmail.com'), +('leejaeha31@gmail.com'), +('lenasinaga853@yahoo.com'), +('leo_azzuri@yahoo.com'), +('libbyclaudiar@gmail.com'), +('Liec534@gmail.com'), +('liedhawei@yahoo.com'), +('lina.marlinajkt228@gmail.com'), +('linamay2088@gmail.com'), +('lincebaiku34@gomail.com'), +('lindasekararum@gmail.com'), +('lindaten93@gmail.com'), +('lio.edhen@gmail.com'), +('lionachristina110887@gmail.com'), +('lolithaameliaa.la@gmail.com'), +('lolytha.arina@yahoo.co.id'), +('loverini1508@gmail.com'), +('lricosantoso@gmail.com'), +('luckyluna.id@gmail.com'), +('luckyme2611@gmail.com'), +('lusianaivang@yahoo.com'), +('luvpiggy2312@gmail.com'), +('lyannaming@gmail.com'), +('madya.wa@gmail.com'), +('mahesabeautyandstore@gmail.com'), +('maianton@yahoo.com'), +('mailto.wabisabi@gmail.com'), +('makeupfutura@gmail.com'), +('maramaratus@gmail.com'), +('marciaaact@gmail.com'), +('maria.ariesw87@gmail.com'), +('mariaboons0@gmail.com'), +('marianaulfa11@gmail.com'), +('mariiahulfah123@gmail.com'), +('marlinadiansafitri@gmail.com'), +('maroon16store@gmail.com'), +('marshandaditha@gmail.com'), +('martza.swastikasari@gmail.com'), +('maryani4398@gmail.com'), +('maryavictoryss3@gmail.com'), +('matrasunik@gmail.com'), +('maupesanposter@gmail.com'), +('maximlaundry@yahoo.com'), +('megashinta39@gmail.com'), +('mekarkosmetik.serang@gmail.com'), +('melaplestari2@gmail.com'), +('melindasuriyam01@gmail.com'), +('melisahariyanto14@gmail.com'), +('mentaripuspa59@gmail.com'), +('mh.lifestyles.in@gmail.com'), +('mikanmicheelen@gmail.com'), +('mirzaluzzia123@gmail.com'), +('misluciana@yahoo.com'), +('mitha2904aprilia@gmail.com'), +('mitufaya@gmail.com'), +('mitzukooutfit@gmail.com'), +('mizuharaaya@gmail.com'), +('mohd.nurfitriadin@gmail.com'), +('moildy.id@gmail.com'), +('monalisaflorist@gmail.com'), +('morashopmedan@gmail.com'), +('mr.yung30@gmail.com'), +('muhammad.hidayat.101@gmail.com'), +('mutiaazzahra177@gmail.com'), +('mutiarajesica0209@gmail.com'), +('mykagendis@gmail.com'), +('nadya.desnam@gmail.com'), +('nailavira102@gmail.com'), +('nanalistya22@gmail.com'), +('nandadevie1968@gmail.com'), +('nandipintaa@gmail.com'), +('nari38700@gmail.com'), +('natasyaputri4982@gmail.com'), +('naturrcosmetic@gmail.com'), +('niasepti28@gmail.com'), +('nicoags26@gmail.com'), +('nicolebeautyindonesia@gmail.com'), +('nicoyuganugraha@gmail.com'), +('ninadelinda26@gmail.com'), +('nirmala.sari709@gmail.com'), +('nonaewa@gmail.com'), +('nophaaelfrida@gmail.com'), +('nova.punya@gmail.com'), +('novandy.yohanes@gmail.com'), +('novi0128@gmail.com'), +('novitageraldinetgt@gmail.com'), +('nscollastica@gmail.com'), +('ntyaau26@gmail.com'), +('nuciferaamelia@gmail.com'), +('nugrahapiawai@gmail.com'), +('nur.wazilah@gmail.com'), +('nurazhwa170@gmail.com'), +('nurinasp@gmail.com'), +('nurlaelaramadanti@gmail.com'), +('officialbuyorbye@gmail.com'), +('oktinningrum@gmail.com'), +('opponingrum@gmail.com'), +('oyyisocial@gmail.com'), +('pajerokita@gmail.com'), +('pandridoge@gmail.com'), +('papamarvel_suhendy@yahoo.com'), +('pardosiares@gmail.com'), +('pastiibisaa@gmail.com'), +('pipitsanti999@gmail.com'), +('popoybunny@gmail.com'), +('prakososyam0403@gmail.com'), +('pramita2701@gmail.com'), +('prioritybekasi@gmail.com'), +('properbeauty.id@gmail.com'), +('psalimatulh@gmail.com'), +('puellabeautyshop@gmail.com'), +('puterifedina@gmail.com'), +('putriparapara@gmail.com'), +('putrisildosz@gmail.com'), +('putrivhidi@gmail.com'), +('putryalfionta@yahoo.com'), +('r.alika003@gmail.com'), +('r0n_ny@yahoo.com'), +('radhiah.hasan26@gmail.com'), +('rahayuwrdhn@gmail.com'), +('ranicantikhebat@gmail.com'), +('ratnashary4@gmail.com'), +('raynoldpanjaitan100@gmail.com'), +('regsmakeup@gmail.com'), +('resditamargaretha@gmail.com'), +('reviyuliswan86@gmail.com'), +('ribkanads@gmail.com'), +('richarddarmawanlaidji@gmail.com'), +('rickylie1989@gmail.com'), +('ries091@gmail.com'), +('rikiimpiandes@gmail.com'), +('rikomm822@gmail.com'), +('rinayunikartikaa@gmail.com'), +('rindaariani36@gmail.com'), +('riniismiyanti_94@yahoo.com'), +('ririalijofa@gmail.com'), +('riskalestari014@gmail.com'), +('rkcjawa@yahoo.com'), +('robeth.saputra01@gmail.com'), +('romlahabadsyam@gmail.com'), +('ronnywongsolim@gmail.com'), +('rovihshsh@gmail.com'), +('roviwardanap@gmail.com'), +('roviwardanaputra@gmail.com'), +('rperlita11@gmail.com'), +('rtjayaselalu@gmail.com'), +('rzkamll18@gmail.com'), +('sabithaanisa03@gmail.com'), +('saniaa3095@gmail.com'), +('saniyasch@gmail.com'), +('sansbeauty01@gmail.com'), +('santisujanto@gmail.com'), +('sarah@getvybes.co'), +('sari.astuty12@gmail.com'), +('sarihakim96@gmail.com'), +('sarmutias1@gmail.com'), +('sarmutias2@gmail.com'), +('sarmutias5@gmail.com'), +('seilareisya@gmail.com'), +('selfi.yonghwa08@gmail.com'), +('sendychristians@gmail.com'), +('sepriyanamelly@gmail.com'), +('serlinaepriyanti07@gmail.com'), +('setiawan.laia31@gmail.com'), +('setiawanandrew818@gmail.com'), +('sherlysherlyvi@gmail.com'), +('sholi.hijubpanda@gmail.com'), +('shopalover123@gmail.com'), +('shopbintar@gmail.com'), +('shoujoshopcosmetic@gmail.com'), +('siauwelizabeth@gmail.com'), +('sintafauziah41@gmail.com'), +('siti.lathifatul.k@gmail.com'), +('sitilathifatulkhofi@gmail.com'), +('siuni.reno@gmail.com'), +('siwiberlianti@gmail.com'), +('skincareindoid@gmail.com'), +('skinqueenproject@gmail.com'), +('smcosmeticsub@gmail.com'), +('snana602@gmail.com'), +('snrofi27@gmail.com'), +('soniadepi21@gmail.com'), +('sonie.usman@gmail.com'), +('sreejita@raenabeauty.com'), +('srialfia.san@gmail.com'), +('srihendrawati1@gmail.com'), +('sriyuliani9379@gmail.com'), +('stacia_andani91@yahoo.com'), +('staciaandani91@gmail.com'), +('stasyifa@gmail.com'), +('stefani_eli@ymail.com'), +('stepfanny91@gmail.com'), +('storemajujaya12@gmail.com'), +('sultonudin.madani21@gmail.com'), +('sun.edihtantra@gmail.com'), +('suryakosmetikjogja@gmail.com'), +('suryaniani1960@gmail.com'), +('susantiyap68@gmail.com'), +('suwandikarina61@gmail.com'), +('suwati169@gmail.com'), +('syahlaag@gmail.com'), +('syahliyaf@gmail.com'), +('syahna2128@gmail.com'), +('syahpudin09.183@gmail.com'), +('syifasahb@gmail.com'), +('tagustatang@gmail.com'), +('takiidonlineshopping@gmail.com'), +('talinazulfah@gmail.com'), +('tanmelky@yahoo.com'), +('taraaengg@gmail.com'), +('taspremium88@gmail.com'), +('taufikismail198720@gmail.com'), +('tcahyanti@gmail.com'), +('tfnhtp@gmail.com'), +('the_fortune_eo@yahoo.com'), +('theakirana133@gmail.com'), +('thebeausell@gmail.com'), +('thebeautyneed@gmail.com'), +('theidk.zone@gmail.com'), +('theskindarling@gmail.com'), +('tihanperkasadefitro@gmail.com'), +('tiifandi@gmail.com'), +('timyls@yahoo.com'), +('tina.metro@yahoo.com'), +('tioharianto.allianz@yahoo.com'), +('tjulovely2013@gmail.com'), +('toko.viragrosir88@gmail.com'), +('toko.winars.palopo@gmail.com'), +('tokounikita2@gmail.com'), +('tommy_priatama@ymail.com'), +('topupfu.sp@gmail.com'), +('tyabe888@yahoo.com'), +('ulibasagrc123@gmail.com'), +('umarsikumbang2020@icloud.com'), +('vannychristinna@yahoo.com'), +('varenunggulkarya@gmail.com'), +('veliakatarina@gmail.com'), +('velvetpinkred@gmail.com'), +('vhex307@gmail.com'), +('vidiachen@yahoo.com'), +('vilencyviranyy@gmail.com'), +('vina.astini@yahoo.com'), +('vincenttia23@gmail.com'), +('vinlie2020@gmail.com'), +('violyamerin@gmail.com'), +('viraltoday124@gmail.com'), +('virginiaca09@gmail.com'), +('viserysyvaine@gmail.com'), +('viviyuvita19@gmail.com'), +('wahyudiirwan35@gmail.com'), +('wahyuekajaya@gmail.com'), +('wahyuyana.prasetya@gmail.com'), +('wellington@nihonmart.id'), +('wellypandin76@gmail.com'), +('widoagusputra90@gmail.com'), +('wiwiekapsari@icloud.com'), +('wulan3804@gmail.com'), +('wulandary.sllh@gmail.com'), +('xenpang26@gmail.com'), +('xuanliuli0@gmail.com'), +('yayarafin@gmail.com'), +('yenimaryatin332@gmail.com'), +('yenny.irawati@yahoo.com'), +('yessicasarano16@gmail.com'), +('yogialqarni1807@gmail.com'), +('yogisugianto21@gmail.com'), +('yong2.chan@gmail.com'), +('yooabeaute@gmail.com'), +('ysurjadi@gmail.com'), +('yt40gt@gmail.com'), +('yukireiyuka@gmail.com'), +('yuliacandle@gmail.com'), +('yuliananina19@yahoo.com'), +('yunisri.hartiwi@yahoo.com'), +('yunitadewi23@gmail.com'), +('zabeth_yulie@yahoo.co.id'), +('zahraashop18@gmail.com'), +('zahrabeauty.share@gmail.com'), +('zelmi5009@gmail.com'), +('zharasonline@gmail.com'), +('zivenakbar08@gmail.com'), +('Fanamaliauthor@gmail.com'), +('tjuganteng@gmailcom'), +('ayusisicantik@gmail.com'), +('tsetiyaningsih71@gmail.com'), +('oyyiapple@gmail.com'), +('angellim0311@yahoo.com'), +('fericoridwan@gmail.com'), +('Ikasusanti_story@yahoo.com'), +('hamkyu359@gmail.com'); + + +DROP TABLE raena_analytics.SKU_base_recommendation_base; + +CREATE TABLE raena_analytics.SKU_base_recommendation_base AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum(total_payment_price) total_payment_price, + sum(total_cogs)total_cogs, + ((sum(retail_price)-sum(total_cogs))/sum(retail_price))*100 Pre_discount_gm +FROM + (SELECT sku, + product_type, + sum(quantity)quantity, + sum((retail_price-seller_margin)*quantity) total_payment_price, + sum(cogs*quantity)total_cogs, + sum((retail_price-seller_margin)*quantity)retail_price + FROM raena_analytics.gm_dashboard + WHERE transaction_date::date>cast('2024-06-23' AS date)-interval'90 days' + AND cogs IS NOT NULL + AND cogs>0 + GROUP BY sku, + product_type)A +INNER JOIN + (SELECT sku , + category_id, + brand_id, + name + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B ON A.sku = B.sku +INNER JOIN + (SELECT id , + name + FROM raena_catalog_management.category) C ON B.category_id = C.id +INNER JOIN + (SELECT id , + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id +GROUP BY 1, + 2, + 3, + 4, + 5 ; + + +DROP TABLE raena_analytics.reseller_base_recommendation_base; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base AS +SELECT reseller_id , + A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + sum(quantity)total_quantity, + sum(total_payment_price) total_payment_price, + sum(total_cogs)total_cogs, + ((sum(retail_price)-sum(total_cogs))/sum(retail_price))*100 Pre_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM (SELECT sku,reseller_id , +reseller_email, + product_type, + sum(quantity)quantity, + sum((retail_price-seller_margin)*quantity) total_payment_price, + sum(cogs*quantity)total_cogs, + sum((retail_price-seller_margin)*quantity)retail_price + FROM raena_analytics.gm_dashboard + WHERE transaction_date::date>cast('2024-06-23' AS date)-interval'90 days' + AND cogs IS NOT NULL + AND cogs>0 + AND reseller_id IS NOT NULL + GROUP BY sku, + product_type,reseller_id,reseller_email ) A +INNER JOIN (SELECT sku , + category_id, + brand_id, + name + FROM raena_catalog_management.product) B ON A.sku = B.sku +INNER JOIN (SELECT id , + name + FROM raena_catalog_management.category) C ON B.category_id = C.id +INNER JOIN (SELECT id , + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id +INNER JOIN raena_analytics.recommendation_power_seller_base BB ON A.reseller_email = BB.email +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_stage1; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_stage1 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>=10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM raena_analytics.reseller_base_recommendation_base; + +DROP TABLE IF EXISTS raena_analytics.raw_scraping_data; + +CREATE TABLE raena_analytics.raw_scraping_data AS +SELECT + E.raena_product_sku, + F.name AS SKU_name, + E.rce_product_id, + E.rce_variant_id, + A.category_name, + A.brand_name, + A.product_page_url, + A.rce_source_product_name, + A.product_variant_name, + A.product_variant_price, + A.product_variant_price_before_discount, + A.product_sold_total, + A.product_sold, + A.product_price_min, + A.product_price_min_before_discount, + A.product_price_max, + A.product_price_max_before_discount, + CAST(percentage_match AS DECIMAL(22, 2)) AS percentage_match, + A.ratings +FROM + raena_analytics.raena_shopee_product_mapping E +LEFT JOIN ( + SELECT + A.id AS rce_product_id, + D.id AS rce_product_varient_id, + B.category_name, + C.brand_name, + product_page_url, + rce_source_product_name, + product_variant_name, + case when product_variant_price is null then 0 WHEN product_variant_price = 'None' THEN 0 WHEN product_variant_price ~ '^[0-9]+$' THEN CAST(product_variant_price AS BIGINT) ELSE 0 end / 10000 AS product_variant_price, + case when product_variant_price_before_discount is null then 0 WHEN product_variant_price_before_discount = 'None' THEN 0 WHEN product_variant_price_before_discount ~ '^[0-9]+$' THEN CAST(product_variant_price_before_discount AS BIGINT) ELSE 0 end / 10000 AS product_variant_price_before_discount, + product_sold_total, + product_sold, + case when product_price_min is null then 0 WHEN product_price_min = 'None' THEN 0 WHEN product_price_min ~ '^[0-9]+$' THEN CAST(product_price_min AS BIGINT) ELSE 0 end / 10000 AS product_price_min, + case when product_price_min_before_discount is null then 0 WHEN product_price_min_before_discount = 'None' THEN 0 WHEN product_price_min_before_discount ~ '^[0-9]+$' THEN CAST(product_price_min_before_discount AS BIGINT) ELSE 0 end / 10000 AS product_price_min_before_discount, + case when product_price_max is null then 0 WHEN product_price_max = 'None' THEN 0 WHEN product_price_max ~ '^[0-9]+$' THEN CAST(product_price_max AS BIGINT) ELSE 0 end / 10000 AS product_price_max, + case when product_price_max_before_discount is null then 0 WHEN product_price_max_before_discount = 'None' THEN 0 WHEN product_price_max_before_discount ~ '^[0-9]+$' THEN CAST(product_price_max_before_discount AS BIGINT) ELSE 0 end / 10000 AS product_price_max_before_discount, + ratings + FROM + raena_spider_management.rce_product A + LEFT JOIN raena_spider_management.rce_category B ON A.rce_category_id = B.id + LEFT JOIN raena_spider_management.rce_brand C ON A.rce_brand_id = CAST(C.id AS VARCHAR) + LEFT JOIN raena_spider_management.rce_product_variant D ON A.id = D.rce_product_id +) A ON E.rce_product_id = CAST(A.rce_product_id AS VARCHAR) +AND REPLACE(E.rce_variant_id, '.0', '') = REPLACE(CAST(A.rce_product_varient_id AS VARCHAR), ',', '') +LEFT JOIN raena_catalog_management.product F ON E.raena_product_sku = F.sku; + + +DROP TABLE IF EXISTS raena_analytics.raw_scraping_data_stage ; + + +CREATE TABLE raena_analytics.raw_scraping_data_stage AS +SELECT A.* +FROM raena_analytics.raw_scraping_data A +INNER JOIN + (SELECT raena_product_sku, + max(percentage_match) percentage_match + FROM raena_analytics.raw_scraping_data + GROUP BY 1) B ON A.raena_product_sku = B.raena_product_sku +AND A.percentage_match =B.percentage_match; + + +DROP TABLE IF EXISTS raena_analytics.sku_sales_factor; + + +CREATE TABLE raena_analytics.sku_sales_factor AS +SELECT SKU, + cast(cast(sum(B.total_quantity*3) AS decimal(22,2))/cast(SUM(A.total_quantity) AS decimal(22,2)) AS decimal(22,2)) sale_factor +FROM raena_analytics.SKU_base_recommendation_base A +LEFT JOIN + (SELECT raena_product_sku, + sum(DISTINCT product_sold) total_quantity + FROM raena_analytics.raw_scraping_data_stage + GROUP BY 1) B ON A.sku = B.raena_product_sku +WHERE A.Pre_discount_gm>=10 +GROUP BY 1; + + +DROP TABLE IF EXISTS raena_analytics.recommendation_on_low_sku_task1; + + +CREATE TABLE raena_analytics.recommendation_on_low_sku_task1 AS +SELECT C.mobile Reseller_mobile , + C.email reseller_email, + A.sku, + A.sku_name, + A.brand_name, + A.category_name , + A.product_type , + Total_quantity, + total_payment_price, + total_cogs, + A.pre_discount_gm, + coalesce(B.sale_factor,0) sale_factor, + A.pre_discount_gm*coalesce(B.sale_factor,1) sorting +FROM + (SELECT * + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE Gm_type = 'DDB') A +INNER JOIN + (SELECT mobile, + email, + id + FROM raena_user_management.user) C ON A.reseller_id = cast(C.id AS varchar) +INNER JOIN + (SELECT sku , + pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base) D ON A.sku = D.sku +AND D.pre_discount_gm >=10 +INNER JOIN raena_analytics.sku_in_stock E ON A.sku = E.sku +AND E.in_stock >=10 +LEFT JOIN raena_analytics.sku_sales_factor B ON A.sku = B.sku; +---------------------------------------------task 2 --------------- + + + + + +DROP TABLE raena_analytics.SKU_base_recommendation_base_v2; + + +CREATE TABLE raena_analytics.SKU_base_recommendation_base_v2 AS +SELECT A.sku, + ((sum((A.retail_price-seller_margin) *quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM raena_analytics.gm_dashboard A +WHERE transaction_date::date>cast('2024-06-23' as date)-interval'90 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND tier_name ='GOLD' +GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.recommendation_base_historical_data_offender; + + +CREATE TABLE raena_analytics.recommendation_base_historical_data_offender AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE gm_type ='Offender') A +LEFT JOIN raena_analytics.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN raena_analytics.sku_sales_factor C ON B.mapping_group_base = C.sku +LEFT JOIN raena_analytics.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND Pre_discount_gm>=10 +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM + (SELECT sku, + name, + product_type, + category_id, + brand_id + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.category) C ON B.category_id = C.id + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id ) E ON B.mapping_group_base= E.SKU; + + + DROP TABLE IF EXISTS raena_analytics.recommendation_base_historical_data_ddb; + + +CREATE TABLE raena_analytics.recommendation_base_historical_data_ddb AS +SELECT reseller_id , + A.sku , + A.sku_name , + A.brand_name , + A.category_name, + A.product_type, + A.Pre_discount_gm reseller_pre_discount_gm, + mapping_group_base recommended_sku, + E.sku_name recommended_sku_name, + E.brand_name recommended_brand_name, + E.category_name recommended_category_name, + E.product_type recommended_product_type, + percent_concern_match recommended_concern_match_percent, + percent_ingredient_match recommended_ingredient_match_percent, + sale_factor , + D.Pre_discount_gm, + in_stock +FROM + (SELECT reseller_id , + sku , + sku_name , + brand_name, + category_name, + product_type, + Pre_discount_gm + FROM raena_analytics.reseller_base_recommendation_base_stage1 + WHERE gm_type ='DDB') A +LEFT JOIN raena_analytics.raw_ingredient_mapping_table B ON A.sku = B.group_base +LEFT JOIN raena_analytics.sku_sales_factor C ON A.SKU = C.sku +LEFT JOIN raena_analytics.SKU_base_recommendation_base_v2 D ON B.mapping_group_base = D.sku +AND D.Pre_discount_gm>=10 +LEFT JOIN raena_analytics.sku_in_stock F ON B.mapping_group_base = F.sku +LEFT JOIN + (SELECT DISTINCT B.sku , + B.name sku_name , + D.name brand_name, + C.name category_name, + B.product_type + FROM + (SELECT sku, + name, + product_type, + category_id, + brand_id + FROM raena_catalog_management.product + WHERE (lower(name) NOT LIKE '%gift %' + AND lower(name) NOT LIKE '% free %' + AND lower(name) NOT LIKE '%not for %') + AND is_archived ='false' + AND is_delisted ='false') B + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.category) C ON B.category_id = C.id + INNER JOIN + (SELECT id, + name + FROM raena_catalog_management.brand) D ON B.brand_id = D.id ) E ON B.mapping_group_base= E.SKU; + + DROP TABLE raena_analytics.recommentdation_offender_sku_concern; + + +CREATE TABLE raena_analytics.recommentdation_offender_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_offender B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_concern_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC; + + + DROP TABLE raena_analytics.recommentdation_offender_sku_ingredient; + + +CREATE TABLE raena_analytics.recommentdation_offender_sku_ingredient AS +SELECT reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + sku , + sku_name , + brand_name , + category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_offender B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +AND recommended_ingredient_match_percent>0 and Pre_discount_gm is not null +ORDER BY 1, + 16 DESC ; + + + DROP TABLE IF EXISTS raena_analytics.recommentdation_ddb_sku_concern ; + + +CREATE TABLE raena_analytics.recommentdation_ddb_sku_concern AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_concern_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_concern_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE in_stock>50 and reseller_pre_discount_gm>10; + +DROP TABLE IF EXISTS raena_analytics.recommentdation_ddb_sku_ingredient ; + + +CREATE TABLE raena_analytics.recommentdation_ddb_sku_ingredient AS +SELECT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + recommended_sku, + recommended_sku_name, + recommended_brand_name, + recommended_category_name, + recommended_product_type, + recommended_ingredient_match_percent, + sale_factor , + Pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE recommended_ingredient_match_percent>0 + AND in_stock<50 + and Pre_discount_gm is not null +UNION +SELECT DISTINCT B.reseller_id , + C.email reseller_email, + C.mobile reseller_mobile , + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + B.sku , + B.sku_name , + B.brand_name , + B.category_name, + product_type, + 100 , + 1 sale_factor, + reseller_pre_discount_gm +FROM raena_analytics.recommendation_base_historical_data_ddb B +INNER JOIN (select id , email,mobile from raena_user_management.user) C ON B.reseller_id = cast(C.id as varchar) +WHERE in_stock>50 +and reseller_pre_discount_gm >=10; + +DROP TABLE raena_analytics.recommentdation_sku_ingredient; + + +CREATE TABLE raena_analytics.recommentdation_sku_ingredient AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + Recommended_pre_discount_gm, + sale_factor , + (Ingredient_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm) sorting, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + cast(sale_factor AS decimal(22,2)) sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + Ingredient_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_ingredient_match_percent Ingredient_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) tier_price from raena_catalog_management.product) BB on AA.Recommended_sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + + +DROP TABLE raena_analytics.recommentdation_sku_ingredient_purchase; + + +CREATE TABLE raena_analytics.recommentdation_sku_ingredient_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(tier_price AS decimal(22,2))gold_price +FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_ddb_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku, + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT distinct Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_offender_sku_ingredient + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) tier_price from raena_catalog_management.product) BB on AA.sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + + +DROP TABLE raena_analytics.recommentdation_sku_concern; + + +CREATE TABLE raena_analytics.recommentdation_sku_concern AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + Recommended_pre_discount_gm, + sale_factor , + concern_match*coalesce(sale_factor,1)*Recommended_pre_discount_gm sorting, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + concern_match, + sale_factor, + Recommended_pre_discount_gm + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + Recommended_sku_name, + recommended_category_name , + recommended_brand_name , + recommended_product_type, + recommended_concern_match_percent concern_match, + sale_factor, + pre_discount_gm Recommended_pre_discount_gm + FROM raena_analytics.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) tier_price from raena_catalog_management.product) BB on AA.Recommended_sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email, + sorting DESC; + +DROP TABLE raena_analytics.recommentdation_sku_concern_purchase; + + +CREATE TABLE raena_analytics.recommentdation_sku_concern_purchase AS +SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + AA.sku, + AA.sku_name, + AA.category_name , + AA.brand_name , + AA.product_type, + cast(tier_price AS decimal(22,2)) gold_price +FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_ddb_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A + UNION SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM + (SELECT Reseller_email , + Reseller_mobile , + Recommended_sku , + sku, + sku_name, + category_name , + brand_name , + product_type + FROM raena_analytics.recommentdation_offender_sku_concern + WHERE pre_discount_gm IS NOT NULL + and category_name = recommended_category_name + and product_type=recommended_product_type) A)AA + inner join (select sku, json_extract_path_text(tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) tier_price from raena_catalog_management.product) BB on AA.sku= BB.sku + inner JOIN raena_analytics.sku_in_stock F ON AA.Recommended_sku = F.sku and F.in_Stock>=10 +ORDER BY reseller_email; + + -----------------------------------------------Task 3 -------------------------------(completed) +--inner join (select distinct id as order_id , json_extract_path_text(reseller_info,'province',TRUE) reseller_province +-- from raena_order_management.order +-- where created_at::date>dateadd(DAY,-91,'2024-06-23') and payment_status='Paid') AA on A.external_id = AA.order_id + + +DROP TABLE raena_analytics.SKU_base_recommendation_base_t3 ; + + +CREATE TABLE raena_analytics.SKU_base_recommendation_base_t3 AS +SELECT A.sku, + B.name sku_name , + D.name brand_name, + C.name category_name, + A.product_type , + shipping_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin)*quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Pre_discount_gm +FROM raena_analytics.gm_dashboard A +INNER JOIN raena_catalog_management.product B ON A.sku = B.sku +INNER JOIN raena_catalog_management.category C ON B.category_id = C.id +INNER JOIN raena_catalog_management.brand D ON B.brand_id = D.id +WHERE transaction_date::date>cast('2024-06-23' as date)-interval'90 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + and (lower(B.name) not like '%gift %' +and lower(B.name) not like '% free %' +and lower(B.name) not like '%not for %') +and B.is_archived ='false' +and B.is_delisted ='false' +GROUP BY 1, + 2, + 3, + 4, + 5, + 6 ; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_t3; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_t3 AS +SELECT reseller_id , + A.sku, + case when shipping_to ='Customer' then shipping_province end as dropshipping_province, + case when shipping_to ='Reseller' then shipping_province end as reseller_province, + sum(quantity)total_quantity, + sum((A.retail_price-seller_margin) *quantity) total_payment_price, + sum(A.cogs*quantity)total_cogs, + ((sum((A.retail_price-seller_margin)*quantity)-sum(A.cogs*quantity))/sum((A.retail_price-seller_margin)*quantity))*100 Post_discount_gm --case when ((sum(discounted_price*quantity)-sum(cogs*quantity))/sum(discounted_price*quantity))*100>10 then 'DDB' else 'Offender' end GM_type +FROM raena_analytics.gm_dashboard A +INNER JOIN + (SELECT DISTINCT id AS order_id , + shipping_to + FROM raena_order_management.order + WHERE created_at::date>cast('2024-06-23' as date)-interval'180 days' + AND payment_status='Paid') AA ON A.external_id = AA.order_id +INNER JOIN raena_analytics.recommendation_power_seller_base BB ON A.reseller_email = BB.email +WHERE transaction_date::date>cast('2024-06-23' as date)-interval'180 days' + AND A.cogs IS NOT NULL + AND A.cogs>0 + AND reseller_id IS NOT NULL + AND order_placed_by ='Admin Panel' + AND shipping_to <> 'Marketplace' +GROUP BY 1, + 2, + 3, + 4; + +DROP TABLE raena_analytics.reseller_base_recommendation_base_stage1_t3; + + +CREATE TABLE raena_analytics.reseller_base_recommendation_base_stage1_t3 AS +SELECT *, + CASE + WHEN ((total_payment_price-total_cogs)/total_payment_price)*100>10 THEN 'DDB' + ELSE 'Offender' + END GM_type +FROM raena_analytics.reseller_base_recommendation_base_t3; + + +drop table if exists raena_analytics.reseller_base_recommendation_base_stage1_t3_v1; + +create table raena_analytics.reseller_base_recommendation_base_stage1_t3_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm, + in_stock + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6,9; + + drop table if exists raena_analytics.reseller_base_recommendation_base_stage1_t3_v2; + +create table raena_analytics.reseller_base_recommendation_base_stage1_t3_v2 +as + SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku total_quantity_sku , + B.Pre_discount_gm + FROM (select distinct reseller_id , + sku, + dropshipping_province + from raena_analytics.reseller_base_recommendation_base_stage1_t3 + where dropshipping_province is not null ) A + LEFT JOIN raena_analytics.reseller_base_recommendation_base_stage1_t3_v1 B + ON A.dropshipping_province = B.shipping_province ; + + + +DROP TABLE raena_analytics.same_province_with_diff_sku_dropshipping; + + + +CREATE TABLE raena_analytics.same_province_with_diff_sku_dropshipping AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk +from raena_analytics.reseller_base_recommendation_base_stage1_t3_v2 A + WHERE A.sku <> A.recommended_sku and dropshipping_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + + --------------- + DROP TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM raena_analytics.same_province_with_diff_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + +drop table if exists raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1; + +create table raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +DROP TABLE IF EXISTS raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2 AS +SELECT A.reseller_id , + A.sku , + A.reseller_province, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm +FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A +LEFT JOIN raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v1 B ON A.reseller_province = B.shipping_province +WHERE A.reseller_province IS NOT NULL; + + +DROP TABLE raena_analytics.same_province_with_diff_sku_reseller; + + +CREATE TABLE raena_analytics.same_province_with_diff_sku_reseller AS +select * from +( +SELECT *,row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM raena_analytics.recommendation_same_province_with_diff_sku_dropshipping_v2 A +WHERE A.sku <> A.recommended_sku and A.reseller_province is not null ) AA + where rnk<11 +ORDER BY sku , + rnk; + +---- + + +DROP TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + total_quantity_sku sales_factor, + Pre_discount_gm*total_quantity_sku sorting +FROM raena_analytics.same_province_with_diff_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + +drop table if exists raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1; + +create table raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +DROP TABLE IF EXISTS raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2 AS +SELECT A.reseller_id , + A.sku , + A.dropshipping_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm +FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A +LEFT JOIN raena_analytics.recommendation_same_province_with_diff_sku_reseller_v1 B ON A.dropshipping_province = B.shipping_province +WHERE A.dropshipping_province IS NOT NULL; + +DROP TABLE raena_analytics.same_province_with_same_sku_dropshipping; + + +CREATE TABLE raena_analytics.same_province_with_same_sku_dropshipping AS +SELECT * +FROM + (SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition BY reseller_id ,sku,dropshipping_province + ORDER BY Pre_discount_gm DESC) rnk + FROM raena_analytics.recommendation_same_province_with_diff_sku_reseller_v2 A + WHERE sku = recommended_sku + AND A.dropshipping_province IS NOT NULL) AA +WHERE rnk<11 +ORDER BY sku , + rnk; + +DROP TABLE raena_analytics.recommendation_same_province_with_same_sku_dropshipping; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_same_sku_dropshipping AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + dropshipping_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM raena_analytics.same_province_with_same_sku_dropshipping A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + + +drop table if exists raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1; + +create table raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1 +as +SELECT shipping_province, + A.sku , + A.sku_name , + A.brand_name, + A.category_name, + A.product_type , + sum(total_quantity) total_quantity_sku, + sum(DISTINCT B.Pre_discount_gm) Pre_discount_gm + FROM raena_analytics.SKU_base_recommendation_base_t3 A + INNER JOIN raena_analytics.SKU_base_recommendation_base_v2 B ON A.sku= B.sku + inner join raena_analytics.sku_in_stock C on A.sku=C.sku + WHERE B.Pre_discount_gm>=10 and C.in_stock>10 + GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + +drop table if exists raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2; + +create table raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2 +as +SELECT A.reseller_id , + A.sku , + A.reseller_province, + A.total_quantity, + B.sku recommended_sku, + B.sku_name AS recommended_sku_name, + B.category_name AS recommended_category, + B.product_type AS recommended_product, + B.brand_name AS recommended_brand, + B.shipping_province recommended_province, + total_quantity_sku , + B.Pre_discount_gm + FROM raena_analytics.reseller_base_recommendation_base_stage1_t3 A + LEFT JOIN raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v1 B + ON A.reseller_province = B.shipping_province where A.reseller_province is not null ; + + + DROP TABLE raena_analytics.same_province_with_same_sku_reseller; + + +CREATE TABLE raena_analytics.same_province_with_same_sku_reseller AS +select * from ( +SELECT *, + total_quantity_sku/total_quantity sales_factor, + row_number() over(partition by reseller_id ,sku,reseller_province + ORDER BY Pre_discount_gm DESC) rnk +FROM raena_analytics.recommendation_same_province_with_same_sku_dropshipping_v2 A +WHERE sku = recommended_sku and A.reseller_province is not null )AA + where rnk<11 +ORDER BY sku , + rnk; + + DROP TABLE raena_analytics.recommendation_same_province_with_same_sku_reseller; + + +CREATE TABLE raena_analytics.recommendation_same_province_with_same_sku_reseller AS +SELECT DISTINCT B.email reseller_email, + B.mobile reseller_mobile, + sku , + reseller_province, + recommended_sku, + recommended_sku_name, + recommended_category, + recommended_product, + recommended_brand, + Pre_discount_gm, + sales_factor, + Pre_discount_gm*sales_factor sorting +FROM raena_analytics.same_province_with_same_sku_reseller A +INNER JOIN raena_user_management.user B ON A.reseller_id = cast(B.id as varchar) +WHERE Pre_discount_gm >=10; + + + diff --git a/redshift/sql_code/appflyer_etl.sql b/redshift/sql_code/appflyer_etl.sql new file mode 100644 index 0000000..c16a8c8 --- /dev/null +++ b/redshift/sql_code/appflyer_etl.sql @@ -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; + + diff --git a/redshift/sql_code/cart_abandment_etl.sql b/redshift/sql_code/cart_abandment_etl.sql new file mode 100644 index 0000000..c1f3fd4 --- /dev/null +++ b/redshift/sql_code/cart_abandment_etl.sql @@ -0,0 +1,409 @@ + + +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 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 raena_analyticsseq_0_to_1000; + + +CREATE TABLE raena_analyticsseq_0_to_1000 AS + (SELECT row_number() over ( + ORDER BY TRUE)::integer - 1 AS i + FROM raena_analytics.final_clevertab_stage LIMIT 2000); + + drop table if exists raena_analytics.exploded_array; + +create table raena_analytics.exploded_array +as +SELECT *, + JSON_EXTRACT_ARRAY_ELEMENT_TEXT(products, seq.i) AS json + FROM raena_analytics.final_clevertab_stage, + raena_analytics.seq_0_to_1000 AS seq + WHERE seq.i < JSON_ARRAY_LENGTH(products) and products like '[%]'; + + +drop table 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 , + json_extract_path_text(json,'sku',TRUE) sku_name, + json_extract_path_text(json,'quantity',TRUE) quantity +FROM raena_analytics.exploded_array; + + +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; + + diff --git a/redshift/sql_code/consignment_sales_report.sql b/redshift/sql_code/consignment_sales_report.sql new file mode 100644 index 0000000..581f2ae --- /dev/null +++ b/redshift/sql_code/consignment_sales_report.sql @@ -0,0 +1,272 @@ + + +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, + DATEDIFF(day, gateway_expiry::date, CURRENT_DATE) AS number_of_day_pending, + payment_link, + CASE WHEN no_of_retries >= 2 THEN 'Yes' ELSE 'No' END AS offender_reseller +FROM ( + SELECT + A.id AS order_id, + A.created_at::date AS order_created_date, + A.status AS order_status, + payment_amount, + B.name AS Am_name, + CASE + WHEN B.email = C."emailid " THEN 'Denny' + ELSE 'Manthan' + END AS Team, + json_extract_path_text(reseller_info, 'mobile', true) AS reseller_mobile, + json_extract_path_text(reseller_info, 'name', true) AS 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 AS number_of_day_pending, + CASE + WHEN payment_provider = 'DurianPay' THEN json_extract_path_text(payment_details,'invoice_url',true) + WHEN payment_provider = 'Xendit' THEN 'https://checkout.xendit.co/web/' || payment_provider_code + END AS payment_link, + no_of_retries + FROM ( + SELECT + REPLACE(CAST(json_extract_array_element_text(order_ids, 0) 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; + + +update raena_analytics.pay_on_delivery_payment_dashboard +set team = 'Denny' +where am_name = 'Iskandar Sani. S Kom' or am_name = 'Rosidah'; + + + +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, + split_part(batch,'__',1) po_number, + 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, + A.expiry_date, + C.lot_no +FROM raena_order_management.fulfillment_detail A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.shipment_id = B.id +LEFT JOIN + (SELECT DISTINCT sku , + reference_no, + expiry_date, + lot_no + FROM raena_erp_management.inbound_grn_sku)C ON A.sku = C.sku +AND split_part(batch,'__',1) = C.reference_no +AND A.expiry_date = C.expiry_date +WHERE applied_cogs IS NOT NULL + AND applied_cogs <>0 +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 8, + 9, + 10; + + + +DROP TABLE IF EXISTS raena_analytics.cogs_base_data; + + +CREATE TABLE raena_analytics.cogs_base_data AS +SELECT sku, + cogs , + promo , + CASE + WHEN cogs <>0 THEN (cast(promo AS float)/cast(cogs AS float))*100 + ELSE 0 + END promo_percentage, + (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.order_sku_table; + + +CREATE TABLE raena_analytics.order_sku_table AS +SELECT A.order_id , + (A.created_at+interval'7 Hours')::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, + C.promo_percentage, + coalesce(B.payment_amount,A.payment_amount) payment_amount +FROM raena_order_management.sales_sub_order A +inner join (select id from raena_order_management.order + where (payment_status = 'Paid' or id like 'PL%') or (payment_status='Initiated' and is_cash_on_delivery='true')) BB on A.order_id = BB.id +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+interval'7 Hours')::date BETWEEN C.created_at AND C.last_date +WHERE (A.created_at+interval'7 Hours')::date >='2023-01-01' + AND A.status NOT IN ('Returned', + 'Cancelled') ; + + + +DROP TABLE raena_analytics.consignment_base_data ; + + +CREATE TABLE raena_analytics.consignment_base_data AS +SELECT transaction_date, + paid_status, + F.name Brand_name , + D.sku raena_code, + E.name sku_name, + B.Quantity consignment_quantity, + B.pkp_cogs, + A.po_number, + C.lot_no, + batch_no, + D.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.promo_percentage, + D.payment_amount + from raena_analytics.order_sku_table D +left join raena_analytics.fullfillment_base_data C on D.order_id = C.order_Id +and sales_sub_order_shipment_id= shipment_id +AND D.sku = C.sku +left join raena_erp_management.inbound_order_sku B +on C.batch=B.batch_no +AND C.sku=B.raena_code +left join raena_erp_management.inbound_order A +on B.reference_no=A.po_number +left join raena_catalog_management.product E on D.sku = E.sku +left join raena_catalog_management.brand F on E.brand_id = F.id +WHERE transaction_date>='2023-01-01'; + +--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 raena_analytics.order_sku_table 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, +json_extract_path_text(event_props,'CT Session Id',true) sessionid , +json_extract_path_text(event_props,'item_id',true) item_id , +json_extract_path_text(event_props,'isGuestUser',true) isGuestUser , +json_extract_path_text(event_props,'CT Source',true) Source , +json_extract_path_text(event_props,'id',true) id , +json_extract_path_text(event_props,'brandName',true) brandName , +json_extract_path_text(event_props,'item_name',true) item_name , +json_extract_path_text(event_props,'offenderItems',true) offenderItems , +json_extract_path_text(event_props,'items',true) items , +json_extract_path_text(event_props,'search_term',true) search_term , +json_extract_path_text(event_props,'screen',true) screen, +json_extract_path_text(event_props,'position',true) position_value , +json_extract_path_text(event_props,'story_name',true) story_name , +json_extract_path_text(event_props,'caraousel_title',true) caraousel_title , +json_extract_path_text(event_props,'title',true) title +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' +'home_product_Caraousel', +'bottom_bar_seller_center', +'bottom_bar_challenge' +) +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 ; + + + diff --git a/redshift/sql_code/distributed_warehouse_etl_cron.sql b/redshift/sql_code/distributed_warehouse_etl_cron.sql new file mode 100644 index 0000000..96fc88b --- /dev/null +++ b/redshift/sql_code/distributed_warehouse_etl_cron.sql @@ -0,0 +1,1564 @@ + + +drop table if exists raena_analytics.sku_type_warehouse; + +create table raena_analytics.sku_type_warehouse +as +select distinct sku , sku_type from raena_analytics.final_basic_calucation_table; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1 ; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) +SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price + --partner_name, + --Service_name + FROM basetable + where service_name = 'REG' + GROUP BY destination_area_id + --partner_name, + --Service_name + ) B ON A.destination_area_id=B.destination_area_id + --AND A.partner_name =B.partner_name + --AND A.Service_name= B.Service_name + ) AA +LEFT JOIN + (SELECT DISTINCT min_price, + --service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price + --,service_name + ,destination_area_id + ORDER BY min_price , + --service_name , + destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + -- A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price + --partner_name, + --Service_name + FROM basetable + where service_name = 'REG' + GROUP BY destination_area_id + --partner_name, + --Service_name + ) B ON A.destination_area_id=B.destination_area_id + --AND A.partner_name =B.partner_name + --AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON --AA.service_name = BB.service_name AND +AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; + +select 1; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + E.type service_type , + EE.name partner_name , + CASE + WHEN warehouse_code like 'WH-CGK%' THEN 'Cikarang' + WHEN warehouse_code like 'WH-SRG%' THEN 'Semarang' + WHEN warehouse_code like 'WH-SUB%' THEN 'Surabaya' + WHEN warehouse_code like 'WH-UPG%' THEN 'Makassar' + WHEN warehouse_code like 'WH-MES%' THEN 'Medan' + WHEN warehouse_code like 'WH-AAP%' THEN 'Samarinda' + WHEN warehouse_code like 'WH-BPN%' THEN 'Balikpapan' + WHEN warehouse_code like 'VEN%' THEN 'Supplier_warehouse' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN raena_transport_management.logistic_partner EE ON A.partner_id = EE.id +LEFT JOIN + (SELECT AA.order_id , + AA.sales_sub_order_shipment_id, + AA.sku, + CC.Quantity , + CC.discounted_price payment_amount + FROM + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + -- sum(coalesce(BB.quantity , AA.quantity)) Quantity + coalesce(BB.quantity , AA.quantity) Quantity + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + --GROUP BY 1,2,3 + ) AA + LEFT JOIN + (SELECT DISTINCT external_id , + product_sku , + quantity quantity, + discounted_price*quantity discounted_price + FROM raena_analytics.base_netsuite_final + ) CC ON AA.order_id = CC.external_id + AND AA.sku = CC.product_sku + AND AA.Quantity= CC.quantity ) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' + AND A.status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED'); + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3_final_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage3_final_v1 AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + service_type , + partner_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + product_sku sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM raena_analytics.warehouse_analysis_stage3 A +LEFT JOIN raena_analytics.base_netsuite_final B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM raena_analytics.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage3_final; + +CREATE TABLE raena_analytics.warehouse_analysis_stage3_final AS +SELECT * +FROM raena_analytics.warehouse_analysis_stage3_final_v1 +WHERE order_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + AND payment_amount IS NOT NULL +UNION +SELECT A.Order_id, + reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + A.sku , + A.quantity, + shipping_amount, + B.payment_price +FROM raena_analytics.warehouse_analysis_stage3_final_v1 A +INNER JOIN + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity, + sum(coalesce(BB.payment_amount , AA.payment_amount)) payment_price + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + WHERE order_id IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + GROUP BY 1, + 2, + 3) B ON A.order_id = B.order_id +AND A.reference_id= B.sales_sub_order_shipment_id +AND A.sku = B.sku; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_1; + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN raena_analytics.warehouse_analysis_stage3_final A ON Z.id=A.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE ((Z.payment_status = 'Paid' or Z.id like 'PL%') or (Z.payment_status='Initiated' and Z.is_cash_on_delivery='true')) + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_2_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_2_v1 AS +SELECT BB.*, + CASE + WHEN BB.external_id = B.order_id + AND BB.sku = B.sku + AND (CASE + WHEN BB.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 +FROM + (SELECT external_id , + sku , + quantity, + item_type, + Brand_name, + discounted_price + FROM raena_analytics.gm_dashboard + LEFT JOIN + (SELECT order_id + FROM raena_analytics.warehouse_analysis_stage3_final) AA ON external_id = order_id + WHERE cast(transaction_date AS date)>='2022-04-01' + AND order_id IS NULL)BB +LEFT JOIN + (SELECT order_id, + sku, + product_class, + new_shipment_amount + FROM raena_analytics.sku_level_shipping_fee_final + WHERE new_shipment_amount<>0) B ON BB.external_id = B.order_id +AND BB.sku = B.sku +AND (CASE + WHEN BB.item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=B.product_class +LEFT JOIN + (SELECT external_id, + sku, + product_class, + new_shipment_amount + FROM raena_analytics.sku_level_shipping_fee_old_final + WHERE new_shipment_amount<>0) C ON BB.external_id = C.external_id +AND BB.sku = C.sku +AND (CASE + WHEN BB.item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=C.product_class; + + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_2; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + shipping_amount, + BB.Brand_name AS Brand_name , + shipping_to shipping_to, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + CASE + WHEN Z.provider='dsf' THEN 'DSF' + WHEN Z.provider='rdash360' THEN 'RDash' + WHEN Z.order_placed_by='merchant' THEN 'Merchant' + WHEN Z.order_placed_by='admin' THEN 'Admin Panel' + WHEN Z.order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN Z.is_campaign='true' THEN 'Campaign Order' + WHEN Z.is_consignment='true' THEN 'Consignment Order' + WHEN Z.provider='dsf' THEN 'DSF Order' + WHEN Z.order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM + (SELECT id , + status, + provider, + order_placed_by, + is_consignment, + payment_status, + is_campaign, + created_at, + shipping_to, + channel_id, + created_by + FROM raena_order_management.order + WHERE ((payment_status = 'Paid' or id like 'PL%') or (payment_status='Initiated' and is_cash_on_delivery='true')) + AND cast(created_at AS date)>='2022-04-01') Z +INNER JOIN raena_analytics.warehouse_analysis_stage1_2_v1 BB ON Z.id = BB.external_id +LEFT JOIN raena_order_management.channel D ON Z.channel_id = D.id ; + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_3_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_3_v1 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.service_type , + A.partner_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to shipping_to, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + CASE + WHEN Z.provider='dsf' THEN 'DSF' + WHEN Z.provider='rdash360' THEN 'RDash' + WHEN Z.order_placed_by='merchant' THEN 'Merchant' + WHEN Z.order_placed_by='admin' THEN 'Admin Panel' + WHEN Z.order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN Z.is_campaign='true' THEN 'Campaign Order' + WHEN Z.is_consignment='true' THEN 'Consignment Order' + WHEN Z.provider='dsf' THEN 'DSF Order' + WHEN Z.order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN raena_analytics.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN raena_analytics.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +LEFT JOIN raena_order_management.channel D ON Z.channel_id = D.id +WHERE ((Z.payment_status = 'Paid' or Z.id like 'PL%') or (Z.payment_status='Initiated' and Z.is_cash_on_delivery='true')) + AND cast(Z.created_at AS date)>='2022-04-01'; + + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage1_3; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage1_3 AS +SELECT order_id, + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + sku, + quantity, + case when b.shipping_amount is not null then ((b.shipping_amount)/(b.order_weight))*(b.final_weight) else A.shipping_amount end shipping_amount, + shipping_to, + payment_amount, + A.payment_status, + order_status, + order_placed_by, + order_type, + created_at, + is_campaign, + created_by +FROM raena_analytics.warehouse_analysis_stage1_3_v1 A +LEFT JOIN raena_analytics.OM_Logistic_final_shipping B ON A.reference_id= B.reference_id; + + +DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2_v1_final_weight; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage2_v1_final_weight AS +SELECT order_id , reference_id , service_name , + service_type , + partner_name , + actual_warehouse, + CASE + WHEN (sum(B.Final_weight*A.Quantity))<1.3 THEN 1 + WHEN (sum(B.Final_weight*A.Quantity))>=1.3 + AND (ABS(sum(B.Final_weight*A.Quantity)) - FLOOR(ABS(sum(B.Final_weight*A.Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.Final_weight*A.Quantity))+1 + ELSE FLOOR(sum(B.Final_weight*A.Quantity)) + END AS sku_weight +FROM raena_analytics.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku + group by 1,2,3,4,5,6; + + + DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2_v1; + + +CREATE TABLE raena_analytics.warehouse_analysis_stage2_v1 AS +SELECT A.*, + B.brand_name, + B.Final_weight*sku_weight/sum(B.Final_weight) over(partition by A.order_id , A.reference_id ) Final_weight +FROM raena_analytics.warehouse_analysis_stage1_3 A +left join raena_analytics.warehouse_analysis_stage2_v1_final_weight C on A.order_id = C.order_id and A.reference_id = C.reference_id +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + +drop table if exists raena_analytics.calculated_ninja_jne_oct_shipping_fee; + +CREATE TABLE raena_analytics.calculated_ninja_jne_oct_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(CASE WHEN C.partner_id = 5 THEN C.insurance_fee ELSE C.insurance_fee END) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM raena_analytics.warehouse_analysis_stage2_v1 + WHERE created_at::date BETWEEN '2022-10-01' AND '2022-10-14' + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + ) AA +GROUP BY 1, + 2, + 3, + 4; + +drop table if exists raena_analytics.calculated_feb_shipping_fee; + +CREATE TABLE raena_analytics.calculated_feb_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(C.insurance_fee ) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM raena_analytics.warehouse_analysis_stage2_v1 + WHERE created_at::date BETWEEN '2022-11-01' AND '2023-02-20' + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + ) A +GROUP BY 1, + 2, + 3, + 4; + + DROP TABLE IF EXISTS raena_analytics.warehouse_analysis_stage2; + +CREATE TABLE raena_analytics.warehouse_analysis_stage2 AS +SELECT A.*, + (final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(final_weight) over(partition BY A.reference_id) new_shipment_amount +FROM raena_analytics.warehouse_analysis_stage2_v1 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.reference_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.reference_id = C.reference_id ; + +select count(1) from raena_analytics.warehouse_analysis_stage2 ; + + +DROP TABLE IF EXISTS raena_analytics.shipping_fee_coupon_purpose; + + +CREATE TABLE raena_analytics.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to shipping_to, + order_placed_by, + order_type, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT A.id AS order_id , + reseller_id, + json_extract_path_text(A.reseller_info,'tierName',true) tierName, + json_extract_path_text(A.reseller_info,'email',true) email, + json_extract_path_text(A.reseller_info,'mobile',true) mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + A.created_at + FROM raena_order_management.order A + left join raena_order_management.channel D on A.channel_id = D.id + WHERE A.is_archived = 'false' + AND cast(A.created_at AS date)>='2022-04-01' + ORDER BY A.created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN +(SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN raena_analytics.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management.order + WHERE (payment_status= 'Paid' or id like 'PL%') + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + + +select count(1) from raena_analytics.shipping_fee_coupon_purpose; + +DROP TABLE IF EXISTS raena_analytics.rate_sheet_wise_expected_warehouse ; + + +CREATE TABLE raena_analytics.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + AA.sku , + cast(case when actual_warehouse is null then 'Cikarang' else actual_warehouse end as varchar(50)) actual_warehouse , + cast(case when expected_warehouse is null then 'Cikarang' else expected_warehouse end as varchar(50)) expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date, + DSF_flag, + brand_type, + cogs +FROM + ( + SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + A.service_type , + A.partner_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + coalesce(new_shipment_amount,shipping_amount)new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at, + case when A.order_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + CASE + WHEN coalesce(Y.Name,A.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 coalesce(Y.Name,A.Brand_name) IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC' + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type + FROM raena_analytics.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM raena_analytics.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN raena_analytics.warehouse_analysis_stage1 D ON A.rate_id = D.id + --AND A.service_name = D.service_name + )AA + LEFT JOIN + (SELECT distinct A.external_id , + A.sku , + A.cogs + FROM raena_analytics.business_report A ) CA ON AA.sku = CA.sku +AND AA.order_id = CA.external_id +WHERE AA.sku IS NOT NULL +GROUP BY order_id , + reference_id, + aa.sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date), + DSF_flag, + brand_type, + cogs; + +update raena_analytics.rate_sheet_wise_expected_warehouse + set payment_amount =0 + from raena_analytics.gm_dashboard + where discounted_price= 0 + and raena_analytics.rate_sheet_wise_expected_warehouse.order_id = raena_analytics.gm_dashboard.external_id + and raena_analytics.rate_sheet_wise_expected_warehouse.sku = raena_analytics.gm_dashboard.sku ; + + /* +update raena_analytics.rate_sheet_wise_expected_warehouse + set expected_warehouse = cast(CASE + WHEN expected_warehouse_code like 'WH-CGK%' THEN 'Cikarang' + WHEN expected_warehouse_code like 'WH-SRG%' THEN 'Semarang' + WHEN expected_warehouse_code like 'WH-SUB%' THEN 'Surabaya' + WHEN expected_warehouse_code like 'WH-UPG%' THEN 'Makassar' + WHEN expected_warehouse_code like 'WH-MES%' THEN 'Medan' + WHEN expected_warehouse_code like 'WH-AAP%' THEN 'Samarinda' + WHEN expected_warehouse_code like 'WH-BPN%' THEN 'Balikpapan' + WHEN expected_warehouse_code like 'VEN%' THEN 'Supplier_warehouse' + end as varchar) + from raena_order_management.sales_sub_order_shipment + where rate_sheet_wise_expected_warehouse.sub_order_shipping_id = id + and raena_order_management.sales_sub_order_shipment.expected_warehouse_code is not null; + */ + update raena_analytics.rate_sheet_wise_expected_warehouse + set expected_warehouse = 'Balikpapan' + where created_date>'2024-01-18' and expected_warehouse='Samarinda'; + + update raena_analytics.rate_sheet_wise_expected_warehouse + set missed_Inventory = CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END + where 1=1; + + + select count(1) from raena_analytics.rate_sheet_wise_expected_warehouse ; + +DROP TABLE IF EXISTS raena_analytics.quantity_trend_new; + + +CREATE TABLE raena_analytics.quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM raena_analytics.quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT cast(is_campaign as varchar) , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM raena_analytics.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS raena_analytics.quantity_trend; + + +CREATE TABLE raena_analytics.quantity_trend AS +SELECT * +FROM raena_analytics.quantity_trend_new; + + +DROP TABLE IF EXISTS raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + cast(case when actual_warehouse is null then 'Cikarang' else actual_warehouse end as varchar(50)) actual_warehouse , + cast(case when expected_warehouse is null then 'Cikarang' else expected_warehouse end as varchar(50)) expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM +(SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM raena_analytics.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM raena_analytics.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN raena_analytics.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + /* + update raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping + set expected_warehouse = cast(CASE + WHEN expected_warehouse_code like 'WH-CGK%' THEN 'Cikarang' + WHEN expected_warehouse_code like 'WH-SRG%' THEN 'Semarang' + WHEN expected_warehouse_code like 'WH-SUB%' THEN 'Surabaya' + WHEN expected_warehouse_code like 'WH-UPG%' THEN 'Makassar' + WHEN expected_warehouse_code like 'WH-MES%' THEN 'Medan' + WHEN expected_warehouse_code like 'WH-AAP%' THEN 'Samarinda' + WHEN expected_warehouse_code like 'WH-BPN%' THEN 'Balikpapan' + WHEN expected_warehouse_code like 'VEN%' THEN 'Supplier_warehouse' + end as varchar) + from raena_order_management.sales_sub_order_shipment + where rate_sheet_wise_expected_warehouse_expected_shipping.sub_order_shipping_id = id + and raena_order_management.sales_sub_order_shipment.expected_warehouse_code is not null; + */ + update raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping + set expected_warehouse = 'Balikpapan' + where created_date>'2024-01-18' and expected_warehouse='Samarinda'; + + update raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping + set missed_Inventory = CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END + where 1=1; + + select count(1) from raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping ; + +DROP TABLE IF EXISTS raena_analytics.om_distr_actaul_origin; + + +CREATE TABLE raena_analytics.om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + B.partner_id, + B.service_id, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse in ('Samarinda','Balikpapan') THEN 36700 + END AS expected_origin +FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS raena_analytics.om_distr_actaul_origin_2; + + +CREATE TABLE raena_analytics.om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM raena_analytics.om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +AND b.service_id = c.service_id +AND b.partner_id = c.partner_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN raena_analytics.om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id IS NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM raena_analytics.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM raena_analytics.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_base_exepected_warehouse_4; + +CREATE TABLE raena_analytics.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM raena_analytics.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS raena_analytics.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE raena_analytics.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM +(SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_6; + + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM raena_analytics.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM raena_analytics.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM raena_analytics.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + + --final dataset +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight=10; + + + +DROP TABLE IF EXISTS raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 ; + + +CREATE TABLE raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN raena_analytics.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id and o.status <>'CANCELLED' +WHERE Sub_order_shipping_id IS NOT NULL; + +drop table if exists raena_analytics.om_shipment_jtr_shipping_fee_base_11; + +create table raena_analytics.om_shipment_jtr_shipping_fee_base_11 +as +select A.*,AA.new_sku sku,compared_weight sku_weight , +sum((compared_weight/order_weight)*expected_jtr_shipping_fee) sku_level_jtr_expected_shipping_fee, +sum((compared_weight/order_weight)*actual_jtr_shipping_fee) sku_level_jtr_actual_shipping_fee +from raena_analytics.om_shipment_jtr_shipping_fee_base_11_v11 A +left join +(SELECT order_id, reference_id ,new_sku, + sum(CASE + WHEN new_volume_weight>new_actual_weight THEN new_volume_weight + WHEN new_volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight0 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, + last_transaction +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, + sum(total_retail_price) total_retail_price, + 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, + max(B.transaction_date) last_transaction, + 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, 5, + 6, + 7, + 13,14) 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, + sum(total_retail_amount)total_retail_amount +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, + sum(quantity*retail_price) total_retail_amount + 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, + B.total_retail_amount, + 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, + total_impression, + os_version , + app_version , + city, + A.reseller_mobile , + province , + ttl_order , + ttl_amount total_amount, + total_retail_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, + attributed_touch_time, + event_time , + bucket, + CASE WHEN date_trunc('Month',A.event_time)::date = date_trunc('Month',install_date)::date + AND date_trunc('Month',A.event_time)::date = date_trunc('Month',conversion_date)::date THEN 'New' + WHEN date_trunc('Month',A.event_time)::date-date_trunc('Month',install_date)::date>= 30 + AND date_trunc('Month',A.event_time)::date = date_trunc('Month',conversion_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.event_time)::date-date_trunc('Month',install_date)::date>=30 + AND date_trunc('Month',A.event_time)::date - date_trunc('Month',conversion_date)::date>=30 THEN 'Existing' + WHEN (date_part('Month',install_date) =2 or date_part('Month',conversion_date)=2 ) and + date_trunc('Month',A.event_time)::date-date_trunc('Month',install_date)::date>= 28 + AND date_trunc('Month',A.event_time)::date = date_trunc('Month',conversion_date)::date THEN 'Activation' + WHEN (date_part('Month',install_date) =2 or date_part('Month',conversion_date)=2 ) and + date_trunc('Month',A.event_time)::date-date_trunc('Month',install_date)::date>=28 + AND date_trunc('Month',A.event_time)::date - date_trunc('Month',conversion_date)::date>=28 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag +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 and mobile <> '') B ON A.reseller_mobile= B.mobile + left join (select A.*, replace(B.mobile,'+','') mobile from raena_analytics.cohort_level_reseller A + left join raena_user_management.user B on A.reseller_id = B.id) C on A.reseller_mobile = C.mobile + --LEFT JOIN raena_analytics.user_type_table flag ON replace(A.reseller_mobile,'+','')=flag.reseller_mobile + ; + + diff --git a/redshift/sql_code/gamification_etl.sql b/redshift/sql_code/gamification_etl.sql new file mode 100644 index 0000000..0c14e7f --- /dev/null +++ b/redshift/sql_code/gamification_etl.sql @@ -0,0 +1,889 @@ + + +DROP TABLE raena_analytics.challenge_user_wise_rewards; + + +CREATE TABLE raena_analytics.challenge_user_wise_rewards AS +SELECT challenge_id, + challenge_name, + challenge_type, + challenge_status, + CC.name , + CC.email, + CC.mobile, + rnk +FROM + (SELECT C.challenge_id, + challenge_name, + challenge_type, + challenge_status, + reseller_id , + current_progress, + rank()over(partition BY C.challenge_id + ORDER BY current_progress DESC ,gross_profit DESC,total_revenue DESC ,randomness DESC) rnk + FROM + (SELECT DISTINCT challengeid challenge_id , + name challenge_name, + TYPE challenge_type, + status challenge_status + FROM raena_analytics_challenge_management.challenge_management_main cm ) D + LEFT JOIN + (SELECT * + FROM + (SELECT reseller_id, + challenge_id , + current_progress, + gross_profit, + total_revenue, + randomness, + row_number() over (partition BY reseller_id,challenge_id + ORDER BY updated_at DESC) rn + FROM raena_challenge_management.rank_progress ) A + WHERE rn= 1) C ON C.challenge_id = D.challenge_id + ORDER BY C.challenge_id , + rnk, + reseller_id) AA +LEFT JOIN raena_user_management.user CC ON AA.reseller_id = cast(CC.id AS varchar) +ORDER BY challenge_id, + rnk; +--------- + +DROP TABLE raena_analytics.cohort_level_reseller; + + +CREATE TABLE raena_analytics.cohort_level_reseller AS +SELECT reseller_id , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '0to2M' + WHEN max_payment_amount BETWEEN 2000000 AND 10000000 THEN '2Mto10M' + 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',created_at)::date trnsaction_mount, + sum(payment_amount)payment_amount + FROM raena_order_management.order + GROUP BY 1, + 2) A + GROUP BY 1) B ; + + +drop table raena_analytics.challenge_brand_sku_mapping; + +create table raena_analytics.challenge_brand_sku_mapping +as +select distinct +A.challengeid challenge_id , + A.name challenge_name , + A.type challenge_type, + A.status challenge_status, + cast(A.startdate::TIMESTAMP +interval'7 hours' AS date) challenge_start_date, + cast(A.enddate::TIMESTAMP+interval'7 hours' AS date) challenge_end_date , + cast(A.createdat::TIMESTAMP+interval'7 hours'AS date) challenge_created_date, + brandid ,productsku, + reseller_id +from raena_analytics_challenge_management.challenge_management_main A +left join raena_analytics_challenge_management.participatingbrands AA on A.challengeid = AA.challengeid +LEFT JOIN + (SELECT DISTINCT challenge_id , + reseller_id + FROM raena_challenge_management.challenge_participant) b ON A.challengeid = B.challenge_id; + + +drop table raena_analytics.reseller_revenue_gamification; + +create table raena_analytics.reseller_revenue_gamification +as +SELECT order_date, + order_id , + reseller_id , + order_placed_by, + sku, + brand_id, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity, + sum(shipping_amount) shipping_amount + FROM + (SELECT order_date, + A.order_id , + reseller_id , + shipment_id, + case when A.order_id = C.order_id then C.new_shipment_amount else D.new_shipment_amount end shipping_amount, + A.sku, + A.brand_id, + CASE WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger A + LEFT JOIN + (SELECT DISTINCT id , + created_at, + order_placed_by + FROM raena_order_management.order + WHERE created_at::date >='2023-11-01') B ON A.order_id = B.id + LEFT JOIN raena_analytics.sku_level_shipping_fee_final C + on A.order_id = C.order_id + and A.sku = C.sku + left join raena_analytics.sku_level_shipping_fee_old_final D + on A.order_id = D.external_id + and A.sku = D.sku + WHERE status NOT IN ('CANCELLED', + 'RETURNED') and order_date >='2023-11-03' + GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8)c + GROUP BY 1, + 2, + 3, + 4,5,6; + + +DROP TABLE raena_analytics.challenge_rewards_data; + + +CREATE TABLE raena_analytics.challenge_rewards_data AS +SELECT distinct A.challenge_id, + B.name AS challenge_name , + B.type AS challenge_type, + A.type AS reward_type , + A.name AS reward_name, + A.reseller_id, + A.status rewards_status, + C.name reseller_name , + C.mobile , + C.email , + challenge_target, + value AS total_rewards, + json_extract_path_text(json_extract_path_text(claim_meta,'addressDetails',TRUE),'name',TRUE) address_reseller_name , + json_extract_path_text(json_extract_path_text(claim_meta,'addressDetails',TRUE),'address',TRUE) reseller_address , + json_extract_path_text(json_extract_path_text(claim_meta,'addressDetails',TRUE),'phoneNumber',TRUE) reseller_phonenumber , + json_extract_path_text(json_extract_path_text(claim_meta,'bankDetails',TRUE),'bankId',TRUE) reseller_bankid , + json_extract_path_text(json_extract_path_text(claim_meta,'bankDetails',TRUE),'bankCode',TRUE) reseller_code , + json_extract_path_text(json_extract_path_text(claim_meta,'bankDetails',TRUE),'bankAccount',TRUE) reseller_account +FROM raena_challenge_management.challenge_reward_status A +LEFT JOIN raena_analytics_challenge_management.challenge_management_main B ON A.challenge_id = B.challengeid +LEFT JOIN raena_user_management.user C ON A.reseller_id = C.id ; + + + + + + drop table raena_analytics.challenge_rewards_base_table; + + create table raena_analytics.challenge_rewards_base_table + as + SELECT challenge_id , + reseller_id , + sum(total_rewards) rewards + FROM + (select * + from raena_analytics.challenge_rewards_data + where reward_type in('CASHBACK' ,'EXTERNAL_PRODUCTS','SPARK_POINTS')) D + GROUP BY 1, + 2; + + +drop table raena_analytics.gamification_metrics_base_table_statge1_new; + +CREATE TABLE raena_analytics.gamification_metrics_base_table_statge1_new AS +SELECT A.challenge_id , + A.challenge_name , + A.challenge_type, + A.challenge_status, + A.challenge_start_date, + A.challenge_end_date , + A.challenge_created_date, + A.reseller_id, + coalesce(cast(C.order_date::TIMESTAMP+interval'7 hours'AS date),cast(CC.order_date::TIMESTAMP+interval'7 hours'AS date)) transaction_date, + coalesce(C.order_id,CC.order_id) order_id , + A.brandid , + A.productsku, + coalesce(C.amount,CC.amount) amount, + coalesce(C.cogs_amount,CC.cogs_amount) cogs_amount, + coalesce(C.quantity,CC.quantity) quantity, + D.rewards, + coalesce(C.shipping_amount,CC.shipping_amount) new_shipment_amount, + --case when C.order_id = E.order_id then E.new_shipment_amount else EE.new_shipment_amount end new_shipment_amount, + F.name AS reseller_name , + F.email AS reseller_email, + F.mobile AS reseller_mobile, + G.bucket, + coalesce(C.order_placed_by,CC.order_placed_by) order_placed_by +FROM raena_analytics.challenge_brand_sku_mapping A +LEFT JOIN raena_analytics.reseller_revenue_gamification C +ON C.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.challenge_start_date AND A.challenge_end_date +AND A.reseller_id = C.reseller_id +and A.brandid =C.brand_id and (A.productsku is not null or A.productsku ='') +LEFT JOIN raena_analytics.reseller_revenue_gamification CC +ON CC.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.challenge_start_date AND A.challenge_end_date +AND A.reseller_id = CC.reseller_id +and A.productsku = CC.sku and (A.productsku is not null or A.productsku ='') +LEFT JOIN raena_analytics.challenge_rewards_base_table D ON A.challenge_id = D.challenge_id +AND A.reseller_id = D.reseller_id +AND C.reseller_id = D.reseller_id +LEFT JOIN raena_user_management.user F ON A.reseller_id = cast(F.id AS varchar) +LEFT JOIN raena_analytics.cohort_level_reseller G ON A.reseller_id = G.reseller_id +WHERE challenge_type <> 'MILESTONE' +UNION +SELECT A.challengeid challenge_id , + A.name challenge_name , + A.type challenge_type, + A.status challenge_status, + cast(A.startdate::TIMESTAMP +interval'7 hours' AS date) challenge_start_date, + cast(A.enddate::TIMESTAMP+interval'7 hours' AS date) challenge_end_date , + cast(A.createdat::TIMESTAMP+interval'7 hours'AS date) challenge_created_date, + B.reseller_id, + cast(C.order_date::TIMESTAMP+interval'7 hours'AS date) transaction_date, + '', + '', + '', + 0 amount, + 0 Cogs_amount, + 0 quantity, + D.rewards, + 0 , + '' , + '', + '', + G.bucket, + '' +FROM raena_analytics_challenge_management.challenge_management_main A +LEFT JOIN + (SELECT DISTINCT reseller_id + FROM raena_challenge_management.challenge_participant + UNION SELECT DISTINCT reseller_id + FROM raena_challenge_management.order_ledger) b ON 1=1 +LEFT JOIN + (SELECT order_date, + order_id , + reseller_id , + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3)c ON b.reseller_id = c.reseller_id +INNER JOIN + (SELECT challenge_id , + reseller_id , + sum(total_rewards) rewards + FROM + (select * + from raena_analytics.challenge_rewards_data + where reward_type in('CASHBACK' ,'EXTERNAL_PRODUCTS','SPARK_POINTS')) D + GROUP BY 1, + 2) D ON A.challengeid = D.challenge_id +AND B.reseller_id = D.reseller_id +LEFT JOIN raena_user_management.user F ON B.reseller_id = cast(F.id AS varchar) +LEFT JOIN raena_analytics.cohort_level_reseller G ON B.reseller_id = G.reseller_id +WHERE A.type = 'MILESTONE' +ORDER BY 1, + challenge_start_date; + +DROP TABLE raena_analytics.gamification_metrics_base_table_statge1; + + +CREATE TABLE raena_analytics.gamification_metrics_base_table_statge1 AS +SELECT A.challengeid challenge_id , + A.name challenge_name , + A.type challenge_type, + A.status challenge_status, + cast(A.startdate::TIMESTAMP +interval'7 hours' AS date) challenge_start_date, + cast(A.enddate::TIMESTAMP+interval'7 hours' AS date) challenge_end_date , + cast(A.createdat::TIMESTAMP+interval'7 hours'AS date) challenge_created_date, + B.reseller_id, + cast(C.order_date::TIMESTAMP+interval'7 hours'AS date) transaction_date, + C.order_id , + C.amount, + C.cogs_amount, + C.quantity, + D.rewards, + shipping_amount new_shipment_amount, + --case when C.order_id = E.order_id then E.new_shipment_amount else EE.new_shipment_amount end new_shipment_amount, + + F.name AS reseller_name , + F.email AS reseller_email, + F.mobile AS reseller_mobile, + G.bucket, + order_placed_by +FROM raena_analytics_challenge_management.challenge_management_main A +LEFT JOIN + (SELECT DISTINCT challenge_id , + reseller_id + FROM raena_challenge_management.challenge_participant) b ON A.challengeid = B.challenge_id +LEFT JOIN + (SELECT order_date, + order_id , + reseller_id , + order_placed_by, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity, + sum(shipping_amount) shipping_amount + FROM + (SELECT order_date, + order_id , + reseller_id , + shipment_id, + shipping_amount, + CASE WHEN order_placed_by='merchant' THEN 'Merchant' WHEN order_placed_by='admin' THEN 'Admin Panel' WHEN order_placed_by='reseller' THEN 'App' END order_placed_by, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger A + LEFT JOIN + (SELECT DISTINCT id , + created_at, + order_placed_by + FROM raena_order_management.order + WHERE created_at::date >='2023-10-01') B ON A.order_id = B.id + LEFT JOIN + (SELECT DISTINCT id , + shipping_amount + FROM raena_order_management.sales_sub_order_shipment + WHERE status NOT IN ('CANCELLED', + 'RETURNED')) C ON A.shipment_id=C.id + WHERE status NOT IN ('CANCELLED', + 'RETURNED') + GROUP BY 1, + 2, + 3, + 4, + 5, + 6)c + GROUP BY 1, + 2, + 3, + 4) C ON C.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.startdate::TIMESTAMP+interval'7 hours' + AND (A.enddate::TIMESTAMP+interval'7 hours') +AND B.reseller_id = C.reseller_id +LEFT JOIN + (SELECT challenge_id , + reseller_id , + sum(total_rewards) rewards + FROM + (select * + from raena_analytics.challenge_rewards_data + where reward_type in('CASHBACK' ,'EXTERNAL_PRODUCTS','SPARK_POINTS')) D + GROUP BY 1, + 2) D ON A.challengeid = D.challenge_id +AND B.reseller_id = D.reseller_id +AND C.reseller_id = D.reseller_id +LEFT JOIN raena_user_management.user F ON B.reseller_id = cast(F.id AS varchar) +LEFT JOIN raena_analytics.cohort_level_reseller G ON B.reseller_id = G.reseller_id +WHERE A.type <> 'MILESTONE' +UNION +SELECT A.challengeid challenge_id , + A.name challenge_name , + A.type challenge_type, + A.status challenge_status, + cast(A.startdate::TIMESTAMP +interval'7 hours' AS date) challenge_start_date, + cast(A.enddate::TIMESTAMP+interval'7 hours' AS date) challenge_end_date , + cast(A.createdat::TIMESTAMP+interval'7 hours'AS date) challenge_created_date, + B.reseller_id, + cast(C.order_date::TIMESTAMP+interval'7 hours'AS date) transaction_date, + '' , + 0 amount, + 0 Cogs_amount, + 0 quantity, + D.rewards, + 0 , + '' , + '', + '', + G.bucket, + '' +FROM raena_analytics_challenge_management.challenge_management_main A +LEFT JOIN + (SELECT DISTINCT reseller_id + FROM raena_challenge_management.challenge_participant + UNION SELECT DISTINCT reseller_id + FROM raena_challenge_management.order_ledger) b ON 1=1 +LEFT JOIN + (SELECT order_date, + order_id , + reseller_id , + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3)c ON b.reseller_id = c.reseller_id +INNER JOIN + (SELECT challenge_id , + reseller_id , + sum(total_rewards) rewards + FROM + (select * + from raena_analytics.challenge_rewards_data + where reward_type in('CASHBACK' ,'EXTERNAL_PRODUCTS','SPARK_POINTS')) D + GROUP BY 1, + 2) D ON A.challengeid = D.challenge_id +AND B.reseller_id = D.reseller_id +LEFT JOIN raena_user_management.user F ON B.reseller_id = cast(F.id AS varchar) +LEFT JOIN raena_analytics.cohort_level_reseller G ON B.reseller_id = G.reseller_id +WHERE A.type = 'MILESTONE' +ORDER BY 1, + challenge_start_date; + +DROP TABLE raena_analytics.challenge_type_retention; + + +CREATE TABLE raena_analytics.challenge_type_retention AS +SELECT A.challenge_type, + min_order_date AS first_transaction_date, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=0 THEN A.reseller_id END) AS M0, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=1 THEN A.reseller_id END) AS M1, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=2 THEN A.reseller_id END) AS M2, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=3 THEN A.reseller_id END) AS M3, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=4 THEN A.reseller_id END) AS M4, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=5 THEN A.reseller_id END) AS M5, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=6 THEN A.reseller_id END) AS M6, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)>6 THEN A.reseller_id END) AS GT_M6 +FROM + (SELECT challenge_type, + reseller_id , + min(date_trunc('Month',transaction_date)::date) min_order_date + FROM raena_analytics.gamification_metrics_base_table_statge1_new + WHERE order_id IS NOT NULL + AND reseller_id IN + (SELECT DISTINCT C.id + FROM raena_analytics.charged_events_gamification_direct_revenue A + LEFT JOIN raena_user_management.user C ON A.phone = replace(C.mobile ,'+','') + WHERE challenge_detail_page>0 + AND view_item_session>0 + AND begin_checkout_session>0 + AND finish_checkout_session>0 + AND confirm_payment_session>0 + AND charged>0) + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT DISTINCT challenge_type, + reseller_id , + date_trunc('Month',transaction_date)::date order_date + FROM raena_analytics.gamification_metrics_base_table_statge1_new + WHERE order_id IS NOT NULL + AND reseller_id IN + (SELECT DISTINCT C.id + FROM raena_analytics.charged_events_gamification_direct_revenue A + LEFT JOIN raena_user_management.user C ON A.phone = replace(C.mobile ,'+','') + WHERE challenge_detail_page>0 + AND view_item_session>0 + AND begin_checkout_session>0 + AND finish_checkout_session>0 + AND confirm_payment_session>0 + AND charged>0)) B ON A.reseller_id = B.reseller_id +AND A.challenge_type = B.challenge_type +GROUP BY 1 , + 2; + + + +drop table raena_analytics.challenge_cohort_type_retention; + +CREATE TABLE raena_analytics.challenge_cohort_type_retention AS +SELECT A.challenge_type, + bucket, + min_order_date AS first_transaction_date, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=0 THEN A.reseller_id END) AS M0, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=1 THEN A.reseller_id END) AS M1, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=2 THEN A.reseller_id END) AS M2, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=3 THEN A.reseller_id END) AS M3, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=4 THEN A.reseller_id END) AS M4, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=5 THEN A.reseller_id END) AS M5, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=6 THEN A.reseller_id END) AS M6, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)>6 THEN A.reseller_id END) AS GT_M6 +FROM + (SELECT challenge_type, + reseller_id , + min(date_trunc('Month',transaction_date)::date) min_order_date + FROM raena_analytics.gamification_metrics_base_table_statge1_new + WHERE order_id IS NOT NULL + AND reseller_id IN + (SELECT DISTINCT C.id + FROM raena_analytics.charged_events_gamification_direct_revenue A + LEFT JOIN raena_user_management.user C ON A.phone = replace(C.mobile ,'+','') + WHERE challenge_detail_page>0 + AND view_item_session>0 + AND begin_checkout_session>0 + AND finish_checkout_session>0 + AND confirm_payment_session>0 + AND charged>0) + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT DISTINCT challenge_type, + reseller_id , + date_trunc('Month',transaction_date)::date order_date + FROM raena_analytics.gamification_metrics_base_table_statge1_new + WHERE order_id IS NOT NULL + AND reseller_id IN + (SELECT DISTINCT C.id + FROM raena_analytics.charged_events_gamification_direct_revenue A + LEFT JOIN raena_user_management.user C ON A.phone = replace(C.mobile ,'+','') + WHERE challenge_detail_page>0 + AND view_item_session>0 + AND begin_checkout_session>0 + AND finish_checkout_session>0 + AND confirm_payment_session>0 + AND charged>0)) B ON A.reseller_id = B.reseller_id +AND A.challenge_type = B.challenge_type +LEFT JOIN + (SELECT * + FROM raena_analytics.cohort_level_reseller) C ON A.reseller_id= C.reseller_id +GROUP BY 1, + 2, + 3; + + +/* +DROP TABLE raena_analytics.challenge_type_retention; + +CREATE TABLE raena_analytics.challenge_type_retention AS +SELECT challenge_type, + min_order_date AS first_transaction_date, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=0 THEN A.reseller_id END) AS M0, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=1 THEN A.reseller_id END) AS M1, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=2 THEN A.reseller_id END) AS M2, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=3 THEN A.reseller_id END) AS M3, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=4 THEN A.reseller_id END) AS M4, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=5 THEN A.reseller_id END) AS M5, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=6 THEN A.reseller_id END) AS M6, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)>6 THEN A.reseller_id END) AS GT_M6 +FROM + (SELECT TYPE challenge_type, + B.reseller_id , + min(date_trunc('Month',order_date)::date) min_order_date + FROM raena_analytics_challenge_management.challenge_management_main A + LEFT JOIN + (SELECT DISTINCT challenge_id , + reseller_id + FROM raena_challenge_management.challenge_participant) b ON A.challengeid = B.challenge_id + inner JOIN + (SELECT order_date, + order_id , + reseller_id , + status order_status, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3, + 4)c ON C.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.startdate::TIMESTAMP+interval'7 hours' AND (A.enddate::TIMESTAMP+interval'7 hours') + AND B.reseller_id = C.reseller_id + WHERE TYPE <> 'MILESTONE' + GROUP BY 1, + 2 + UNION SELECT TYPE challenge_type, + B.reseller_id , + min(date_trunc('Month',order_date)::date) min_order_date + FROM raena_analytics_challenge_management.challenge_management_main A + LEFT JOIN + (SELECT DISTINCT reseller_id + FROM raena_challenge_management.challenge_participant) b ON 1=1 + inner JOIN + (SELECT order_date, + order_id , + reseller_id , + status order_status, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3, + 4)c ON b.reseller_id = c.reseller_id + WHERE TYPE = 'MILESTONE' + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT DISTINCT date_trunc('Month',order_date)::date order_date, + reseller_id + FROM raena_challenge_management.order_ledger A ) B ON A.reseller_id = B.reseller_id +GROUP BY 1 , + 2; + + +drop table raena_analytics.challenge_cohort_type_retention; + +CREATE TABLE raena_analytics.challenge_cohort_type_retention AS +SELECT challenge_type, + bucket, + min_order_date AS first_transaction_date, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=0 THEN A.reseller_id END) AS M0, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=1 THEN A.reseller_id END) AS M1, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=2 THEN A.reseller_id END) AS M2, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=3 THEN A.reseller_id END) AS M3, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=4 THEN A.reseller_id END) AS M4, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=5 THEN A.reseller_id END) AS M5, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)=6 THEN A.reseller_id END) AS M6, + count(DISTINCT CASE WHEN datediff(MONTH,A.min_order_date,B.order_date)>6 THEN A.reseller_id END) AS GT_M6 +FROM + (SELECT TYPE challenge_type, + B.reseller_id , + min(date_trunc('Month',order_date)::date) min_order_date + FROM raena_analytics_challenge_management.challenge_management_main A + LEFT JOIN + (SELECT DISTINCT challenge_id , + reseller_id + FROM raena_challenge_management.challenge_participant) b ON A.challengeid = B.challenge_id + inner JOIN + (SELECT order_date, + order_id , + reseller_id , + status order_status, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3, + 4)c ON C.order_date::TIMESTAMP+interval'7 hours' BETWEEN A.startdate::TIMESTAMP+interval'7 hours' AND (A.enddate::TIMESTAMP+interval'7 hours') + AND B.reseller_id = C.reseller_id + WHERE TYPE <> 'MILESTONE' + GROUP BY 1, + 2 + UNION SELECT TYPE challenge_type, + B.reseller_id , + min(date_trunc('Month',order_date)::date) min_order_date + FROM raena_analytics_challenge_management.challenge_management_main A + LEFT JOIN + (SELECT DISTINCT reseller_id + FROM raena_challenge_management.challenge_participant) b ON 1=1 + inner JOIN + (SELECT order_date, + order_id , + reseller_id , + status order_status, + sum(amount) amount , + sum(cogs_amount) cogs_amount, + sum(quantity) quantity + FROM raena_challenge_management.order_ledger + GROUP BY 1, + 2, + 3, + 4)c ON b.reseller_id = c.reseller_id + WHERE TYPE = 'MILESTONE' + GROUP BY 1, + 2) A +LEFT JOIN + (SELECT DISTINCT date_trunc('Month',order_date)::date order_date, + reseller_id + FROM raena_challenge_management.order_ledger A ) B ON A.reseller_id = B.reseller_id +LEFT JOIN + (SELECT * + FROM raena_analytics.cohort_level_reseller) C ON A.reseller_id= C.reseller_id +GROUP BY 1, + 2, + 3; */ + + DROP TABLE IF EXISTS raena_analytics.gamification_funnel_base_stage1; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage1 AS +SELECT ts::date , + events, + name , + email , + phone, + json_extract_path_text(event_props,'CT Session Id',TRUE) sessionid , + json_extract_path_text(event_props,'page_name',TRUE) page_name , + json_extract_path_text(event_props,'ProductList',TRUE) brand_list +FROM clevertap.clevertap_master_data +WHERE events IN ('load_challenge_home_banner' , + 'click_on_challenge_home_banner', + 'bottom_bar_challenge', + 'click_on_challenge_home_banner', + 'page_load', + 'view_item', + 'begin_checkout', + 'Charged', + 'view_cart', + 'add_to_cart', + 'confirm_payment', + 'finish_checkout', + 'click_on_milestone') + AND ts::date >='2023-11-07'; + +DROP TABLE raena_analytics.gamification_funnel_base_stage2; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage2 AS +SELECT ts::date , + sessionid, + name , + email , + phone, + count(DISTINCT CASE WHEN events ='page_load' + AND page_name ='Home' THEN sessionid END) home_banner_session, + count(DISTINCT CASE WHEN events ='click_on_challenge_home_banner' THEN sessionid END) click_challenge_home_banner_session, + count(DISTINCT CASE WHEN events ='bottom_bar_challenge' THEN sessionid END) bottom_bar_challenge, + count(distinct case when events ='click_on_milestone' then sessionid end) load_challenge_milestone_page, + count(DISTINCT CASE WHEN events ='page_load' + AND page_name ='Challenges' THEN sessionid END) challenge_home_page, + count(DISTINCT CASE WHEN events ='page_load' + AND page_name ='challenge-details' THEN sessionid END) challenge_detail_page, + count(DISTINCT CASE WHEN events ='page_load' + AND page_name ='ProductList' THEN sessionid END) brand_page, + count(DISTINCT CASE WHEN events ='view_item' THEN sessionid END) view_item_session, + count(DISTINCT CASE WHEN events ='view_cart' THEN sessionid END)view_cart_session, + count(DISTINCT CASE WHEN events ='begin_checkout' THEN sessionid END) begin_checkout_session, + count(DISTINCT CASE WHEN events ='finish_checkout' THEN sessionid END) finish_checkout_session, + count(DISTINCT CASE WHEN events ='confirm_payment' THEN sessionid END) confirm_payment_session +FROM raena_analytics.gamification_funnel_base_stage1 +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE raena_analytics.gamification_funnel_base_stage3; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage3 AS +SELECT ts::date , + sessionid, + name , + email , + phone, + count(DISTINCT CASE WHEN events ='Charged' THEN sessionid END) Charged +FROM raena_analytics.gamification_funnel_base_stage1 +WHERE ts::date >='2023-11-07' +GROUP BY 1, + 2, + 3, + 4, + 5; + + + +DROP TABLE IF EXISTS raena_analytics.gamification_funnel_base_stage1_direct_revenue; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage1_direct_revenue AS +SELECT ts::date , + events, + name , + email , + phone, + json_extract_path_text(event_props,'CT Session Id',TRUE) sessionid , + json_extract_path_text(event_props,'challengeId',TRUE) challengeid +FROM clevertap.clevertap_master_data +WHERE events ='page_load' and json_extract_path_text(event_props,'page_name',TRUE) ='challenge-details' + AND ts::date >='2023-11-07'; + + +DROP TABLE IF EXISTS raena_analytics.gamification_funnel_base_stage2_direct_revenue; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage2_direct_revenue AS +SELECT ts::date , + events, + name , + email , + phone, + json_extract_path_text(event_props,'CT Session Id',TRUE) sessionid , + json_extract_path_text(event_props,'Payment ID',TRUE) payment_id +FROM clevertap.clevertap_master_data +WHERE events IN ('view_item', + 'begin_checkout', + 'Charged', + 'confirm_payment', + 'finish_checkout') + AND ts::date >='2023-11-07'; + + +drop table raena_analytics.gamification_funnel_base_stage3_direct_revenue; + +CREATE TABLE raena_analytics.gamification_funnel_base_stage3_direct_revenue AS +SELECT ts::date , + sessionid, + name , + email , + phone, + count(DISTINCT CASE WHEN events ='view_item' THEN sessionid END) view_item_session, + count(DISTINCT CASE WHEN events ='begin_checkout' THEN sessionid END) begin_checkout_session, + count(DISTINCT CASE WHEN events ='finish_checkout' THEN sessionid END) finish_checkout_session, + count(DISTINCT CASE WHEN events ='confirm_payment' THEN sessionid END) confirm_payment_session +FROM raena_analytics.gamification_funnel_base_stage2_direct_revenue +GROUP BY 1, + 2, + 3, + 4, + 5; + +DROP TABLE raena_analytics.gamification_funnel_base_stage4_direct_revenue; + + +CREATE TABLE raena_analytics.gamification_funnel_base_stage4_direct_revenue AS +SELECT ts::date , + sessionid, + name , + email , + phone, + payment_id, + count(DISTINCT CASE WHEN events ='Charged' THEN sessionid END) Charged, + order_id +FROM raena_analytics.gamification_funnel_base_stage2_direct_revenue A +left join raena_analytics.payment_order B on A.payment_id = B.id +WHERE events = 'Charged' +GROUP BY 1, + 2, + 3, + 4, + 5,6,8; + + +DROP TABLE IF EXISTS raena_analytics.charged_events_gamification_direct_revenue; + + +CREATE TABLE raena_analytics.charged_events_gamification_direct_revenue AS +SELECT A.ts, + A.phone, + A.challengeid, + count(DISTINCT A.sessionid) challenge_detail_page, + count(DISTINCT CASE WHEN view_item_session>0 THEN A.sessionid END) view_item_session, + count(DISTINCT CASE WHEN begin_checkout_session>0 THEN A.sessionid END) begin_checkout_session, + count(DISTINCT CASE WHEN finish_checkout_session>0 THEN A.sessionid END) finish_checkout_session , + count(DISTINCT CASE WHEN confirm_payment_session>0 THEN A.sessionid END) confirm_payment_session, + count(DISTINCT CASE WHEN Charged>0 THEN A.sessionid END) charged +FROM raena_analytics.gamification_funnel_base_stage1_direct_revenue A +LEFT JOIN raena_analytics.gamification_funnel_base_stage3_direct_revenue B ON A.sessionid = B.sessionid +LEFT JOIN + (SELECT DISTINCT A.order_id , + phone , + charged , + challenge_id + FROM raena_analytics.gamification_funnel_base_stage4_direct_revenue A + INNER JOIN raena_analytics.gamification_metrics_base_table_statge1_new B ON A.order_id = B.order_id) c ON B.phone = C.phone +AND A.challengeid = C.challenge_id +GROUP BY 1, + 2, + 3; + + + + + + diff --git a/redshift/sql_code/gm_etl_cron.sql b/redshift/sql_code/gm_etl_cron.sql new file mode 100644 index 0000000..f82791b --- /dev/null +++ b/redshift/sql_code/gm_etl_cron.sql @@ -0,0 +1,2461 @@ + + + +DROP TABLE IF EXISTS raena_analytics.manual_bundle_sku_data ; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.order_level_data; + + +CREATE TABLE raena_analytics.order_level_data AS +SELECT DISTINCT external_id, + transaction_date+interval'7 hours' AS transaction_date, + discount_amount, + shipping_cost, + A.coupon_code, + replace(cast(reseller_tier_name AS varchar),'"','')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, + is_campaign, + order_placed_by, + admin_fee, + service_fee +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, + cast(is_campaign AS varchar) is_campaign, + order_placed_by, + admin_fee, + service_fee + FROM raena_order_management.order A + WHERE ((payment_status='Paid' + OR id LIKE 'PL%') + OR (payment_status = 'Initiated' + AND is_cash_on_delivery = 'true')) + AND cast(A.created_at+interval '7 hours' AS date) >='2024-05-09') A +LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage1_V1; + + +CREATE TABLE raena_analytics.base_netsuite_stage1_V1 AS +SELECT DISTINCT transaction_date 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 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.actual_wholesale_price + ELSE B.actual_wholesale_price + END actual_wholesale_price, + B.actual_wholesale_price parent_actual_wholesale_price, + CASE + WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount + ELSE B.coupon_discount + END discount_price, + CASE + WHEN discount_amount > 0 + AND A.coupon_code IS NULL + AND A.order_placed_by = 'admin' + AND coupon_applied_on IS NULL THEN 'Cart' + ELSE coupon_applied_on + END 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, + B.id sales_sub_order_id, + A.order_loyalty_discount, + B.loyalty_discount, + reseller_tier_name, + price_type, + a.is_campaign, + B.cogs_discount_type, + A.admin_fee, + A.service_fee +FROM raena_analytics.order_level_data A +LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id +AND status <> 'Cancelled' +LEFT JOIN raena_analytics.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, + actual_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+interval'7 hours' AS date)>='2024-05-09') F ON B.id = F.sales_sub_order_id +ORDER BY 1, + 2; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1_base ; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1_base 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, + actual_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, + price_type, + is_campaign, + coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs, + coalesce(cogs_type,CASE WHEN A.sku = D.sku + AND A.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + cogs_discount_type, + admin_fee, + service_fee +FROM raena_analytics.base_netsuite_stage1_V1 A +LEFT JOIN + (SELECT external_id , + sku , + final_loyalty_point + FROM raena_analytics.final_loyalty_point) B ON A.external_id = B.external_id +AND A.sku = B.sku +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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, + 4) C ON A.sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON A.sku = D.sku +AND A.transaction_date::date BETWEEN D.created_at AND last_date; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage2_v1; + + +CREATE TABLE raena_analytics.base_netsuite_stage2_v1 AS +SELECT transaction_date, + A.external_id, + A.sku, + parent_sku, + quantity quantity, + parent_quantity, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE retail_price + END retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_retail_price + END parent_retail_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE effective_wholesale_price + END effective_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_wholesale_price + END parent_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE actual_wholesale_price + END actual_wholesale_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_price + END discount_price, + coupon_applied_on, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE discount_amount + END discount_amount, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE dynamic_price + END dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE parent_dynamic_price + END parent_dynamic_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_price + END payment_price, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE payment_amount + END payment_amount, + product_class, + parent_product_class, + product_type_class, + sales_sub_order_id, + loyalty_discount, + price_type, + is_campaign, + CASE + WHEN A.sku = D.sku THEN 0 + ELSE cogs + END cogs, + cogs_flag, + cogs_discount_type, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE admin_fee + END admin_fee, + CASE + WHEN cogs = 0 + OR cogs IS NULL + OR A.sku = D.sku THEN 0 + ELSE service_fee + END service_fee +FROM raena_analytics.base_netsuite_stage2_v1_base A +LEFT JOIN + (SELECT DISTINCT sku + FROM raena_catalog_management.product + WHERE lower(name) LIKE '%not for sale%' + OR lower(name) LIKE '%free item%' + OR lower(name) LIKE '%free gift%') D ON A.sku = D.sku ; + + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage3_v1 ; + + +CREATE TABLE raena_analytics.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 + AND retail_price<>0 THEN retail_price*quantity END)over(partitionBY A.external_id) + WHEN coupon_applied_on <> 'Cart' + AND discount_price=0 THEN 0 + WHEN coupon_applied_on ='Cart' + AND discount_price>=0 THEN discount_price + WHEN coupon_applied_on ='Cart' + AND discount_price=0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id) + END*/ + discount_price AS Final_discount , + retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin, + CASE + WHEN product_type_class IN ('Flash Bundle', + 'Flash') THEN 'Flash' + WHEN coupon_applied_on ='Cart' + AND price_type ='NEGOTIATED_PRICE' THEN 'Negotiated' + WHEN coupon_applied_on = 'Shipping Fee' THEN 'Shipping' + WHEN coupon_applied_on IS NOT NULL THEN 'Brand' + END discount_type +FROM raena_analytics.base_netsuite_stage2_v1 A ; + + +DROP TABLE IF EXISTS raena_analytics.base_netsuite_stage4_v1 ; + + +CREATE TABLE raena_analytics.base_netsuite_stage4_v1 AS +SELECT A.* , + CASE + WHEN (parent_product_class= 'Bundle' + OR product_type_class IN('Bundle', 'Flash Bundle')) + AND A.external_id = B.external_id + AND A.parent_sku = B.parent_sku and ttl_wholesale_price<>0 THEN (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/ttl_wholesale_price) + END AS additional_discount, + CASE + WHEN final_discount<>0 THEN final_discount-seller_margin + END AS effective_coupon_discount +FROM raena_analytics.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 + FROM raena_analytics.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 raena_analytics.base_netsuite_final +where transaction_date::date>='2024-05-09'; + + +INSERT INTO raena_analytics.base_netsuite_final +SELECT A.external_id, + A.transaction_date 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, + B.seller_margin/B.quantity seller_margin, + ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) -coalesce((CASE WHEN final_discount >0 THEN 0 ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity END),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price, + cast(final_discount AS decimal(22,2))/quantity coupon_discount, + effective_coupon_discount/quantity 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, + 0 diff, + CASE + WHEN price_type IS NOT NULL THEN price_type + ELSE discount_type + END discount_type , + CASE + WHEN final_discount >0 THEN 0 + ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity + END promo_amount, + actual_wholesale_price, + effective_wholesale_price, + A.is_campaign, + '' sales_sub_order_id, + cogs_discount_type, + parent_sku, + null, + B.admin_fee sku_level_admin_fee, + B.service_fee sku_leel_service_fee +FROM raena_analytics.order_level_data A +LEFT JOIN raena_analytics.base_netsuite_stage4_v1 B ON A.external_id = B.external_id; + + + +UPDATE raena_analytics.base_netsuite_final AA +SET discounted_price = new_discounted_price +from (SELECT external_id , + product_sku , + cast(settlement_amount/quantity AS decimal(22,2)) new_discounted_price + FROM + (SELECT A.*, + discounted_price , + (effective_wholesale_price*B."Settlement amount")/sum(effective_wholesale_price) over (partition BY external_id) settlement_amount , + B."Settlement amount" + FROM raena_analytics.base_netsuite_final A + INNER JOIN (select distinct * from raena_analytics.mp_orders_hn_wd) B ON external_id= B."Order ID " + WHERE A.transaction_date >'2022-12-31' and effective_wholesale_price<>0 ) A) BB + where AA.external_id = BB.external_id +AND AA.product_sku = BB.product_sku +and AA.transaction_date >'2022-12-31'; + + + +update raena_analytics.base_netsuite_final A +set discounted_price = B.payment_price/quantity +from (select order_id,sku, max(payment_price) payment_price from raena_analytics.order_data_update group by 1,2) B +where A.external_id = B.order_id and A.parent_sku = B.sku and product_sku = parent_sku ; + + +update raena_analytics.base_netsuite_final A +set discounted_price = new_payment_price/A.quantity +from raena_analytics.import_data_for_sku_level_payment_amount B +where A.external_id = B.order_id and A.parent_sku = B.sku and A.product_sku <> A.parent_sku; + +with commen_data as +(select external_id order_id , product_sku sku,quantity sku_quantity, + sku_level_admin_fee*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition by external_id ),0) sku_level_admin_fee_final + from raena_analytics.base_netsuite_final + where transaction_date::date >='2024-05-09' + ) +update raena_analytics.base_netsuite_final +set sku_level_admin_fee = sku_level_admin_fee_final +from commen_data where external_id = order_id and product_sku = sku and sku_quantity = quantity +and transaction_date::date >='2024-05-09' ; + + + +with commen_data as +(select external_id order_id , product_sku sku, quantity sku_quantity, + service_fee_at_sku_level*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition by external_id ),0) sku_level_service_fee_final + from raena_analytics.base_netsuite_final + where transaction_date::date >='2024-05-09' + ) +update raena_analytics.base_netsuite_final +set service_fee_at_sku_level = sku_level_service_fee_final +from commen_data where external_id = order_id and product_sku = sku and sku_quantity = quantity +and transaction_date::date >='2024-05-09' ; + +SELECT max(transaction_date) Netsuite_base_date +FROM raena_analytics.base_netsuite_final; + +DROP TABLE IF EXISTS raena_analytics.gm_dimensions_stage1; + + +CREATE TABLE raena_analytics.gm_dimensions_stage1 AS +SELECT distinct 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, + shipping_to, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN D.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + cast(is_campaign AS varchar) is_campaign +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 +LEFT JOIN raena_order_management.channel D ON A.channel_id = D.id +WHERE (( payment_status='Paid' or A.id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + AND cast(A.created_at+interval'7 hours' AS date) >='2024-05-09'; + + + drop table if exists raena_analytics.sku_gm_mapping; + +create table raena_analytics.sku_gm_mapping +as +select A.sku ,A.name sku_name,B.name brand_name,C.name as category_name,product_type + 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 ; + + +DELETE +FROM raena_analytics.GM_dashboard +where transaction_date::date >='2024-05-09'; + +INSERT INTO raena_analytics.GM_dashboard +SELECT A.external_id , + B.transaction_date AS transaction_date, + B.order_discount_amount , + B.shipping_cost , + A.shipping_province, + B.coupon_code , + A.discount_type , + B.coupon_applied_on , + brand_name, + category_name , + product_type , + product_sku sku, + sku_name , + A.reseller_name , + A.reseller_email , + A.reseller_mobile , + reseller_tier_name tier_name, + A.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 , + coalesce(effective_cogs,coalesce(C.applied_cogs,D.cogs-coalesce(promo,0))) 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', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type , + Customer_type, + BB.gm_target, + B.discount_type discount_promo_type , + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + coalesce(case when cogs_type ='REGULAR' then 'Promo' else cogs_type end ,CASE WHEN B.product_sku = D.sku + AND B.transaction_date::date BETWEEN D.created_at AND last_date + AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag, + A.is_campaign, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END AS DSF_Flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender, + cohort, + cast(sku_level_admin_fee as decimal(22,4)), + cast(service_fee_at_sku_level as decimal(22,4)) +FROM raena_analytics.gm_dimensions_stage1 A +INNER JOIN raena_analytics.base_netsuite_final B ON A.external_id =B.external_id +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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,4) C ON B.product_sku = C.sku +AND A.external_id = C.order_id +LEFT JOIN + (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) D ON B.product_sku = D.sku +AND B.transaction_date::date BETWEEN D.created_at AND last_date +left join (select distinct order_id , + created_at+interval'7 hours' AS created_date, + parent_sku sku, + effective_cogs + from raena_order_management.sales_sub_order + where price_type is not null and parent_sku not like 'BAZ%')XX + on A.external_Id=XX.order_id and B.product_sku= XX.sku +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 B.product_sku = BB.sku +AND A.reseller_tier_name =BB.tierName +LEFT JOIN raena_analytics.sku_gm_mapping AAA ON B.product_sku = AAA.sku +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) BBB on B.product_sku=BBB.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND payment_status= 'Paid') ; + + +SELECT max(transaction_date) gm_dashboard_date +FROM raena_analytics.GM_dashboard; + +DROP TABLE IF EXISTS raena_analytics.GM_dashboard_v1; + + +CREATE TABLE raena_analytics.GM_dashboard_v1 AS +SELECT * +FROM raena_analytics.GM_dashboard +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +DELETE +FROM raena_analytics.GM_dashboard +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.GM_dashboard +SELECT external_id, + transaction_date, + order_discount_amount, + shipping_cost, + shipping_province, + coupon_code, + discount_type, + coupon_applied_on, + brand_name, + category_name, + product_type, + sku, + sku_name, + reseller_name, + reseller_email, + reseller_mobile, + tier_name, + reseller_id, + quantity, + 0 retail_price, + 0 seller_margin, + 0 , + 0 , + 0, + 0, + item_type, + order_placed_by, + 0 cogs, + order_recipient, + brand_type, + customer_type, + gm_target, + discount_promo_type, + 0 promo_amount, + 0, + 0, + cogs_flag, + is_campaign, + dsf_flag, + cogs_discount_type, + order_type, + parent_sku, + is_offender, + '<=2M', + sku_level_admin_fee, + service_fee_at_sku_level +FROM raena_analytics.GM_dashboard_v1 +WHERE (lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) + AND external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.gm_dimensions_stage1); + + + +UPDATE raena_analytics.gm_dashboard AA +SET cogs = 0 +from (SELECT distinct order_id, sku + from raena_analytics.beau_cogs_update) BB + where AA.external_id = BB.order_id +AND AA.sku = BB.sku; + +update raena_analytics.gm_dashboard +set retail_price = 0 , +seller_margin = 0 , +discounted_price = 0 , +additional_discount = 0 , +cogs = 0 , +actual_wholesale_price = 0 , +effective_wholesale_price = 0 , +sku_level_admin_fee = 0 +where external_id = 'OD1716365410302838REG' +and sku = 'MAX005'; + +update raena_analytics.gm_dashboard +set retail_price = 0 , +seller_margin = 0 , +discounted_price = 0 , +additional_discount = 0 , +cogs = 0 , +actual_wholesale_price = 0 , +effective_wholesale_price = 0 , +sku_level_admin_fee = 0 +where parent_sku like 'BAZ%' +and sku = 'SBM018X'; + + + + +DROP TABLE IF EXISTS raena_analytics.business_dimensions_stage1; + + +CREATE TABLE raena_analytics.business_dimensions_stage1 AS +SELECT distinct A.id AS external_id , + reseller_id, + customer_id, + cart_id, + A.flash_sale_id, + shipping_to, + payment_status, + A.status, + A.stock_type, + CASE + WHEN provider='dsf' THEN 'DSF' + WHEN provider='rdash360' THEN 'RDash' + WHEN order_placed_by='merchant' THEN 'Merchant' + WHEN order_placed_by='admin' THEN 'Admin Panel' + WHEN order_placed_by='reseller' THEN 'App' + END order_placed_by, + CASE + WHEN is_campaign='true' THEN 'Campaign Order' + WHEN is_consignment='true' THEN 'Consignment Order' + WHEN provider='dsf' THEN 'DSF Order' + WHEN order_placed_by='merchant' THEN 'Merchant Order' + WHEN E.name IS NOT NULL THEN 'Channel Order' + ELSE 'Reseller Order' + END order_type, + 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,'province',TRUE) reseller_province, + 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 , + cast(is_bank_transfer AS varchar) is_bank_transfer , + cast(is_campaign AS varchar)is_campaign , + discount_type, + shipping_coupon_discount +FROM raena_order_management.order A +LEFT JOIN raena_analytics.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 +LEFT JOIN raena_order_management.channel E ON A.channel_id = E.id +WHERE cast(A.created_at+interval'7 hours' AS date) >='2024-05-09' ; + +DELETE +FROM raena_analytics.business_report +where created_date::date >='2024-05-09'; + +INSERT INTO raena_analytics.business_report +SELECT DISTINCT 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, + A.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 , + a.is_campaign , + C.Country, + B.quantity , + B.retail_price, + seller_margin, + discounted_price, + loyalty_discount, + effective_coupon_discount , + additional_discount, + item_type, + B.discount_type discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + CASE + WHEN A.external_id LIKE '%DSF%' THEN 'Yes' + ELSE 'No' + END DSF_flag, + CASE + WHEN D.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 D.name IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC', + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type, + A.order_type, + F.is_offender, + cohort, + coalesce(effective_cogs,coalesce(CA.applied_cogs,CD.cogs-coalesce(promo,0))) cogs, + cast(sku_level_admin_fee as decimal(22,4)), + cast(service_fee_at_sku_level as decimal(22,4)) + FROM raena_analytics.business_dimensions_stage1 A +LEFT JOIN raena_analytics.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 +left join (select sku product_table_sku ,is_offender from raena_catalog_management.product ) F on B.product_sku= F.product_table_sku +left join (select distinct reseller_id, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end cohort + from (SELECT reseller_id, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id AS reseller_id , + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + where (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )) + group by 1,2 ) A + GROUP BY 1) BB ) CCC on A.reseller_id = CCC.reseller_id +LEFT JOIN + (SELECT B.order_id , + A.sku , + 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, + 4) CA ON B.product_sku = CA.sku +AND A.external_id = CA.order_id +LEFT JOIN + (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) CD ON B.product_sku = CD.sku +AND B.transaction_date::date BETWEEN CD.created_at AND last_date +LEFT JOIN + (SELECT DISTINCT order_id , + created_at+interval'7 hours' AS created_date, + parent_sku sku, + effective_cogs + FROM raena_order_management.sales_sub_order + WHERE price_type IS NOT NULL + AND parent_sku NOT LIKE 'BAZ%')XX ON A.external_Id=XX.order_id +AND B.product_sku= XX.sku +WHERE A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND (( payment_status='Paid' or id like 'PL%') and (payment_status = 'Initiated' and is_cash_on_delivery = 'true' ))); + + DROP TABLE IF EXISTS raena_analytics.business_report_v1; + + +CREATE TABLE raena_analytics.business_report_v1 AS +SELECT * +FROM raena_analytics.business_report +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + + +DELETE +FROM raena_analytics.business_report USING + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B +WHERE raena_analytics.business_report.external_id = B.external_id + AND raena_analytics.business_report.sku=B.sku + AND raena_analytics.business_report.external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + + + +INSERT INTO raena_analytics.business_report +SELECT external_id, + created_date, + order_discount_amount, + shipping_cost, + coupon_code, + brand_name, + product_type, + category_name, + sku_name, + sku, + reseller_id, + customer_id, + cart_id, + flash_sale_id, + shipping_to, + payment_status, + status, + stock_type, + order_placed_by, + order_warehouse, + channel_id, + medium, + marketplace, + provider, + 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, + country, + quantity, + 0 retail_price, + 0 seller_margin, + 0 discount_price, + 0 loyalty_discount, + 0 effective_coupon_discount, + 0 additional_discount, + item_type, + discount_promo_type, + 0 promo_amount, + 0 actual_wholesale_price, + 0 effective_wholesale_price, + reseller_province, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + 0 cogs, + sku_level_admin_fee, + service_fee_at_sku_level +FROM + (SELECT raena_analytics.business_report_v1.* + FROM raena_analytics.business_report_v1 + INNER JOIN + (SELECT DISTINCT sku, + sku_name, + external_id + FROM raena_analytics.gm_dashboard gd + WHERE lower(sku_name) LIKE '%not for sale%' + OR lower(sku_name) LIKE '%free item%' + OR lower(sku_name) LIKE '%free gift%' + OR cogs IS NULL + OR COGS =0) B ON raena_analytics.business_report_v1.external_id = B.external_id + AND raena_analytics.business_report_v1.sku=B.sku) A +WHERE external_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.business_dimensions_stage1); + +SELECT max(created_date) business_date +FROM raena_analytics.business_report; + + +DELETE +FROM raena_analytics.base_netsuite_final +WHERE external_id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + +DELETE +FROM raena_analytics.business_report +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + DELETE +FROM raena_analytics.gm_dashboard +WHERE external_Id IN + (SELECT DISTINCT order_id + FROM raena_analytics.sk_product) or external_id is null; + + + +insert into raena_analytics.base_netsuite_final +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +''coupon_applied_on, +''tier, +sku product_sku, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +(pre_discount_rev-post_discount_revenue)/quantity coupon_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 loyalty_discount, +0 additional_discount, +'' item_type, +0 diff, +'' discount_type, +0 promo_amount, +pre_discount_rev/quantity actual_wholesale_price, +pre_discount_rev/quantity effective_wholesale_price, +'false' is_campaign, +'' sales_sub_order_id, +'' cogs_discount_type, +'' parent_sku, +0 +from raena_analytics.sk_product; + + +update raena_analytics.gm_dashboard +set cogs=0 +where discounted_price=0 and cogs <>0 +and transaction_date::date >='2024-01-01'; + +update raena_analytics.gm_dashboard +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.gm_dashboard +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + +update raena_analytics.gm_dashboard +set effective_coupon_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and effective_coupon_discount >0; + + +update raena_analytics.OM_GM_DB_Product_category +set retail_price = 79900.00 +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08'; + +update raena_analytics.OM_GM_DB_Product_category +set additional_discount = ((retail_price-seller_margin)-discounted_price) +where sku ='FTG001' and transaction_date::date between '2023-06-01' and '2023-07-08' +and additional_discount >0; + + + +insert into raena_analytics.gm_dashboard +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +B.province shipping_province, +'SK Product'coupon_code, +'' discount_type, +'' coupon_applied_on, +brand_name, +category_name, +product_type, +A.sku, +sku_name, +B.name reseller_name, +reseller reseller_email, +B.mobile reseller_mobile, +D.name tier_name, +B.id reseller_id, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 additional_discount, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +'' item_type, +'Admin Panel'order_placed_by, +cogs/quantity cogs, +'' order_recipient, +'' brand_type, +'SK product' customer_type, +gm_target, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +'' cogs_flag, +'false' is_campaign, +'false' dsf_flag, +'' cogs_discount_type, +'SK product' order_type, +'' parent_sku, +'' is_offender, +'' cohort, +0 +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.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 D.name =BB.tierName ; + + +insert into raena_analytics.business_report +select +order_id , +transaction_date, +sum(pre_discount_rev-post_discount_revenue) over(partition by order_id) order_discount_amount, +sum(shipping_fee_sku_level) over (partition by order_id)shipping_cost, +'SK Product'coupon_code, +brand_name, +product_type, +category_name, +sku_name, +A.sku, +B.id reseller_id, +null customer_id, +null cart_id, +null flash_sale_id, +null shipping_to, +'Paid' payment_status, +'' status, +'' stock_type, +'Admin Panel' order_placed_by, +'' order_warehouse, +'' channel_id, +'' medium, +'' marketplace, +'' provider, +D.name tier, +city reseller_city, +B.name reseller_name, +reseller reseller_email, +mobile reseller_mobile, +store reseller_store, +address_line1 shipping_address_line1, +address_line2 shipping_address_line2, +pincode shipping_pincode, +district shipping_district, +city shipping_city, +province shipping_province, +'false' is_bank_transfer, +'false' is_campaign, +'' country, +quantity, +pre_discount_rev/quantity retail_price, +0 seller_margin, +post_discount_revenue/quantity discounted_price, +0 loyalty_discount, +(pre_discount_rev-post_discount_revenue)/quantity effective_coupon_discount, +0 additional_discount, +'' item_type, +'' discount_promo_type, +0 promo_amount, +(pre_discount_rev)/quantity actual_wholesale_price, +(pre_discount_rev)/quantity effective_wholesale_price, +province reseller_province, +'false' dsf_flag, +case when "el/pl_flag"= 'Yes' then 'EL/PL' end brand_type, +'SK Product 'order_type, +'' cohort, +0 cogs, +0 +from raena_analytics.sk_product A inner join raena_user_management.user B on reseller = email +inner join (select sku ,p.name sku_name,B.name brand_name , C.name category_name , product_type from raena_catalog_management.product p + left join raena_catalog_management.brand b on p.brand_id= B.id + left join raena_catalog_management.category c on p.category_id=C.id) C + on A.sku = C.sku + inner join raena_user_management.tier D on B.tier_id = D.id ; + + + +select 'raena_analytics.OM_GM_DB_Product_category'; + + +DELETE +FROM raena_analytics.OM_GM_DB_Product_category +WHERE transaction_date::date >='2024-05-09' ; + +--external_Id IN +-- (SELECT DISTINCT external_id +-- FROM raena_analytics.gm_dimensions_stage1); + + +INSERT INTO raena_analytics.OM_GM_DB_Product_category +SELECT external_id, transaction_date, + "Time", + order_discount_amount, + shipping_cost, + coupon_code , + discount_type , + coupon_applied_on , + brand_name, + category_name , + product_type , + sku, + sku_name , + reseller_name , + reseller_email , + base.reseller_mobile , + tier_name , + reseller_id , + quantity , + retail_price , + seller_margin , + discounted_price , + additional_discount, + item_type , + order_placed_by , + cogs, + shipping_province , + Flag , + wholesale_price, + order_recipient, + brand_type, + Customer_type, + gm_target, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + CASE WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 28 + AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN (date_part('Month',first_install_date) =2 or date_part('Month',First_transaction_date)=2 ) and + date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=28 + AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=28 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + is_campaign, + dsf_flag, + cogs_flag, + cogs_discount_type, + order_type, + parent_sku, + First_transaction_date, + is_offender, + cohort, + sku_level_admin_fee, + service_fee_at_sku_level +FROM + (SELECT A.external_id , + cast(transaction_date AS date) 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, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + is_campaign, + A.dsf_flag, + A.cogs_flag, + A.cogs_discount_type, + A.order_type, + parent_sku, + is_offender, + cohort, + sku_level_admin_fee, + service_fee_at_sku_level + FROM raena_analytics.GM_dashboard A + LEFT JOIN + (SELECT DISTINCT external_id + FROM + (SELECT external_id, + discounted_price , + sku , + (((retail_price-seller_margin)*quantity) -(cogs*quantity))/nullif((CASE WHEN retail_price<>0 THEN (retail_price-seller_margin)*quantity END),0) Pre_discount, + CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/nullif((discounted_price*quantity),0) ELSE 0 END Post_discount + FROM raena_analytics.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 raena_analytics.pricing_sheet P ON A.sku=p.skucode + WHERE cast(transaction_date AS date) >='2024-05-09' + AND A.external_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE status IN ('Cancelled') + AND (( payment_status='Paid' or id like 'PL%') or (payment_status = 'Initiated' and is_cash_on_delivery = 'true' )))) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + +SELECT max(transaction_date) om_gm_db_product_date +FROM raena_analytics.OM_GM_DB_Product_category; + +update raena_analytics.gm_dashboard + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.gm_dashboard where sku like '______X'); + + update raena_analytics.om_gm_db_product_category + set cogs_discount_type='BUY_N_GET_X' + where external_id in (select distinct external_id from raena_analytics.om_gm_db_product_category where sku like '______X'); + +update raena_analytics.OM_GM_DB_Product_category +set retail_price=0, + seller_margin=0, + additional_discount=0, + wholesale_price=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 + where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + +update raena_analytics.gm_dashboard +set retail_price=0, + seller_margin=0, + loyalty_discount=0, + effective_coupon_discount=0, + additional_discount=0, + promo_amount=0, + actual_wholesale_price=0, + effective_wholesale_price=0, + cogs =0 +where sku = 'BEL006' and date_trunc('Month',transaction_date)::date between '2023-04-01' and '2023-05-01' and discounted_price in (0,1,2,3,4,5); + + DELETE +FROM raena_analytics.GM_GROWTH_TAB1 +WHERE created_date >='2024-05-09'; + +INSERT INTO raena_analytics.GM_GROWTH_TAB1 +SELECT tier, + 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, + base.reseller_mobile, + Number_of_Orders , + RSP , + wholesale_price , + Payment_Price , + Effective_Coupon_Discount, + Total_Loyalty_point, + additional_discount, + seller_margin, + AOV, + quantity, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + CASE + WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + external_id, + order_type, + First_transaction_date, + is_offender, + cohort, + cogs, + item_type +FROM + (SELECT tier, + 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, + reseller_mobile, + 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, + discount_promo_type, + sum(promo_amount) promo_amount, + sum(actual_wholesale_price* quantity) actual_wholesale_price, + sum(effective_wholesale_price*quantity) effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + external_id, + order_type, + is_offender, + cohort, + cogs, + item_type + FROM + (SELECT DISTINCT coalesce(a.tier,'NA') AS tier, + 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, + reseller_mobile, + external_id, + retail_price, + quantity, + seller_margin, + discounted_price, + effective_coupon_discount, + loyalty_discount, + additional_discount, + discount_promo_type, + promo_amount, + actual_wholesale_price, + effective_wholesale_price, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + cogs, + item_type, + row_number() over (partition BY external_id + ORDER BY external_id) AS R + FROM raena_analytics.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) >= '2024-05-09' + AND channel_id IS NOT NULL + ) b ON a.external_id=b.id + left join (select distinct id, is_cash_on_delivery + from raena_order_management.order + where cast(created_at AS date) >= '2024-05-09' and is_cash_on_delivery='true') D + on A.external_id = D.id + WHERE (( payment_status='Paid' or a.external_id like 'PL%') or (payment_status='Initiated' and is_cash_on_delivery='true')) + AND created_date >= '2024-05-09') A + GROUP BY tier, + 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, + reseller_mobile, + external_id, + discount_promo_type, + reseller_province, + is_campaign, + dsf_flag, + brand_type, + order_type, + is_offender, + cohort, + cogs, + item_type) base +LEFT JOIN raena_analytics.user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile; + + +SELECT max(created_date) gm_growth_date +FROM raena_analytics.GM_GROWTH_TAB1; + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB2; + + +CREATE TABLE raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'quarter' AS frequency, + To_char(created_date,'quarter') AS month_name, + date_part('quarter',created_date) AS sort , + * +FROM raena_analytics.GM_GROWTH_TAB1 +UNION +SELECT 'month' AS frequency, + To_char(created_date,'month') AS month_name, + date_part('month',created_date) AS sort , + * +FROM raena_analytics.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 raena_analytics.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 raena_analytics.GM_GROWTH_TAB1 +ORDER BY 3; + + +DROP TABLE IF EXISTS raena_analytics.GM_GROWTH_TAB3; +CREATE TABLE raena_analytics.GM_GROWTH_TAB3 AS +( +SELECT 'year' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'year' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('year',transaction_date) AS varchar) AS time, + cast(date_part('year',transaction_date) AS int) AS sort,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'quarter' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'quarter' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'quarter') AS month_name, + date_part('quarter',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'month' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'month' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'month') AS month_name, + date_part('month',transaction_date) as sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) +union +( +SELECT 'week' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'week' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + To_char(transaction_date,'week') AS month_name, + cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +union +( +SELECT 'day' AS frequency, + 'All' AS brand_name, + 'All' AS product_type, + 'All' AS category_name, + 'All' AS sku, + 'All' AS sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 7,8,9,10,11,12,13,14,15,16,17 + union +SELECT 'day' AS frequency, + brand_name, + product_type, + category_name, + sku, + sku_name,tier_name,new_existing_flag, + cast(date_part('day',transaction_date) as varchar) AS month_name, + cast(date_part('day',transaction_date) AS int) AS sort ,o.status, + order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date, + order_type, + sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator +from raena_analytics.OM_GM_DB_Product_category a +inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id +group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 +) +ORDER BY 3; + +DROP TABLE raena_analytics.shipping_fee_base_table; + + +CREATE TABLE raena_analytics.shipping_fee_base_table AS +SELECT A.order_id , + sum(CASE WHEN A.sales_sub_order_shipment_id = C.reference_id THEN ((C.shipping_amount)/(C.order_weight))*(C.final_weight) ELSE B.shipping_amount END) shipping_amount +FROM + (SELECT DISTINCT order_id , + sales_sub_order_shipment_id + FROM raena_order_management.sales_sub_order) A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.sales_sub_order_shipment_id = B.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping C ON A.sales_sub_order_shipment_id = C.reference_id +GROUP BY A.order_id; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage1 ; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage1 AS +SELECT A.order_id , + A.id , + coalesce(B.sku,A.parent_sku) Sku , + A.sales_sub_order_shipment_id , + coalesce((B.quantity*A.quantity),A.quantity) Quantity, + CASE + WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight) + ELSE C.shipping_amount + END shipping_amount , + A.product_class +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_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id +LEFT JOIN raena_analytics.OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ; + + + + +DROP TABLE raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.shipping_fee_sku_base_stage2_v1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2_v1 AS +SELECT order_id , + sales_sub_order_shipment_id, + CASE + WHEN (sum(B.weight*Quantity))<1.3 THEN 1 + WHEN (sum(B.weight*Quantity))>=1.3 + AND (ABS(sum(B.weight*Quantity)) - FLOOR(ABS(sum(B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.weight*Quantity))+1 + ELSE FLOOR(sum(B.weight*Quantity)) + END AS sku_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +GROUP BY 1, + 2; + + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage2 AS +SELECT A.*, + B.weight*sku_weight/sum(B.weight) over(partition BY A.order_id , A.sales_sub_order_shipment_id) Final_weight +FROM raena_analytics.shipping_fee_sku_base_stage1 A +LEFT JOIN raena_analytics.shipping_fee_sku_base_stage2_v1 C ON A.order_id = C.order_id +AND A.sales_sub_order_shipment_id = C.sales_sub_order_shipment_id +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_stage3; + +CREATE TABLE raena_analytics.shipping_fee_sku_base_stage3 AS +SELECT A.*, + (Final_weight*coalesce(coalesce(B.Calculated_shipping_fee,C.Calculated_shipping_fee),shipping_amount))/sum(Final_weight) over(partition BY A.sales_sub_order_shipment_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage2 A +left join raena_analytics.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.sales_sub_order_shipment_id = B.reference_id +left join raena_analytics.calculated_feb_shipping_fee C on A.order_id = C.order_id and A.sales_sub_order_shipment_id = C.reference_id ; + +DROP TABLE raena_analytics.sku_level_shipping_fee_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_final AS +SELECT order_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_stage3 +GROUP BY 1, + 2, + 3; + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage1; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage1 AS +SELECT external_id , + A.sku , + quantity, + shipping_cost , + CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END product_class, + weight*Quantity sku_weight, + CASE + WHEN (weight*Quantity)<1.3 THEN 1 + WHEN (weight*Quantity)>=1.3 + AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1 + ELSE FLOOR((weight*Quantity)) + END AS final_weight +FROM raena_analytics.gm_dashboard A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku +where A.order_type <> 'SK product'; + + + +DROP TABLE raena_analytics.shipping_fee_sku_base_old_stage2; + + +CREATE TABLE raena_analytics.shipping_fee_sku_base_old_stage2 AS +SELECT *, + (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage1 ; + + +DROP TABLE raena_analytics.sku_level_shipping_fee_old_final; + + +CREATE TABLE raena_analytics.sku_level_shipping_fee_old_final AS +SELECT external_id , + sku, + product_class , + sum(new_shipment_amount) new_shipment_amount +FROM raena_analytics.shipping_fee_sku_base_old_stage2 +GROUP BY 1, + 2, + 3 +union + select distinct order_id , sku , 'Product' , + sum(shipping_fee_sku_level) + from raena_analytics.sk_product +group by 1,2 ; + + +WITH commen_data AS + (SELECT external_id order_id , + sku product_sku , + service_fee, + quantity sku_quantity, + round(service_fee*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition BY external_id),0)) sku_level_service_fee_final + FROM raena_analytics.gm_dashboard A + INNER JOIN + (SELECT id , + service_fee + FROM raena_order_management.order + WHERE service_fee>0) B ON A.external_id = B.id + ORDER BY 1) +UPDATE raena_analytics.gm_dashboard +SET service_fee_at_sku_level = sku_level_service_fee_final +FROM commen_data +WHERE external_id = order_id + AND sku = product_sku + AND quantity=sku_quantity ; + + + + WITH commen_data AS + (SELECT external_id order_id , + sku product_sku , + service_fee, + quantity sku_quantity, + round(service_fee*(quantity*discounted_price)/nullif(sum(quantity*discounted_price) over (partition BY external_id),0)) sku_level_service_fee_final + FROM raena_analytics.gm_dashboard A + INNER JOIN + (SELECT id , + service_fee + FROM raena_order_management. + ORDER + WHERE service_fee>0) B ON A.external_id = B.id + ORDER BY 1) + UPDATE raena_analytics.om_gm_db_product_category + SET service_fee_at_sku_level = sku_level_service_fee_final + FROM commen_data WHERE external_id = order_id + AND sku = product_sku + AND quantity=sku_quantity; + + +UPDATE raena_analytics.om_gm_db_product_category + SET service_fee_at_sku_level = 0 + where service_fee_at_sku_level is null ; + +UPDATE raena_analytics.gm_dashboard + SET service_fee_at_sku_level = 0 + where service_fee_at_sku_level is null ; + + +UPDATE raena_analytics.om_gm_db_product_category + SET sku_level_admin_fee = 0 + where sku_level_admin_fee is null ; + +UPDATE raena_analytics.gm_dashboard + SET sku_level_admin_fee = 0 + where sku_level_admin_fee is null ; + + + + +DROP TABLE IF EXISTS raena_analytics.payment_order_stage; + + +CREATE TABLE raena_analytics.payment_order_stage 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, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM NS +INNER JOIN raena_order_management.payment B ON NS.n <= REGEXP_COUNT(B.order_ids , ',') + 1 +GROUP BY 1, 3,4,5,6,7; + + +DROP TABLE IF EXISTS raena_analytics.payment_order; + + +CREATE TABLE raena_analytics.payment_order AS +SELECT A.id , + A.created_at, + A.order_id, + payment_provider, + payment_type, + payment_category, + payment_channel +FROM raena_analytics.payment_order_stage A +INNER JOIN + (SELECT order_id , + max(created_at) max_created_at + FROM raena_analytics.payment_order_stage + GROUP BY order_id) B ON A.created_at= B.max_created_at +AND A.order_id = B.order_id; + +select count(1) from raena_analytics.payment_order; + +drop table raena_analytics.om_app_admin_dump_table; + + +CREATE TABLE raena_analytics.om_app_admin_dump_table AS +SELECT A.id AS order_id, + (A.created_at+interval '7 hours') AS order_date, + (A.created_at+interval '7 hours') AS id_order_date, + A.status AS order_status, + A.Payment_status, + D.name AS channel_name, + cast(A.is_bank_transfer AS varchar) AS bank_transfer, + cast(A.is_campaign AS varchar) AS campaign, + dynamic_price, + total_dynamic_and_tier_price AS total_tier_price, + total_retail_price AS total_MRP, + tier_discount AS total_tier_discount, + discount_amount AS coupon_discount, + GB.payment_amount, + coalesce(GB.new_shipping_amount_final,shipping_amount) AS Actual_shipping_amount, + applied_shipping_amount, + shipping_coupon_discount, + Earnings, + A.Coupon_code, + json_extract_path_text(reseller_info, 'name') AS Reseller_name, + json_extract_path_text(reseller_info, 'email') AS Reseller_email, + json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile, + concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address, + json_extract_path_text(reseller_info, 'store') AS store_name, + json_extract_path_text(reseller_info,'tierName',TRUE) AS tier, + json_extract_path_text(customer_info, 'name') AS customer_name, + json_extract_path_text(customer_info, 'email') AS customer_email, + concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address, + Shipping_to AS Order_recipient, + shipping_address_line1 , + shipping_address_line2 , + shipping_city , + shipping_district , + shipping_sub_district , + A.shipping_province , + shipping_country_code , + shipping_pincode , + tracking_url , + CASE + WHEN A.order_placed_by ='reseller' THEN 'App Order' + ELSE 'Admin' + END AS order_placed_by, + A.stock_type AS product_stock_type, + coalesce(GB.sku,oi.sku) AS product_sku, + coalesce(gb.sku_name,oi.name) AS product_name, + product_code, + date_key, + coalesce(GB.product_type,oi.product_type) product_type, + batch, + country, + coalesce(GB.quantity,oi.quantity) quantity, + coalesce(GB.actual_wholesale_price,oi.wholesale_price) wholesale_price, + coalesce(GB.retail_price,oi.retail_price) retail_price, + ((GB.retail_price*GB.quantity)-(GB.seller_margin*GB.quantity)) total_price, + Total_earnings, + A.is_slash_price_enabled , + coalesce(gb.brand_name,oi.Brand_name) brand_name, + coalesce(gb.category_name,oi.category_name) category_name, + oi.product_class, + source_order_id , + marketplace_provider_order_id AS provider_order_id, + medium, + marketplace, + provider, + C.id as payment_id, + (GB.cogs) as total_cogs, + case when oi.parent_sku is null then 'NA' else oi.parent_sku end bundle_sku, + new_shipping_amount sku_level_shipping_amount, + GB.discounted_price*GB.quantity sku_level_payment_amount, + is_cash_on_delivery, + payment_provider, + payment_type, + payment_category, + payment_channel, + admin_fee, + service_fee +FROM raena_order_management.order A +inner join (select AD.* , + sum(new_shipping_amount) over(partition by AD.external_id) new_shipping_amount_final , + sum(discounted_price*quantity) over(partition by AD.external_id) payment_amount + from (SELECT A.*, + discounted_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_discounted_price, + actual_wholesale_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_actual_wholesale_price , + retail_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_retail_price, + seller_margin/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_seller_margin , + cogs/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_cogs, + (case when (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)/count(1) over(partition BY A.external_id,A.sku,A.item_type) new_shipping_amount + 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) AD + )GB + ON A.id = GB.external_id AND GB.sku not like 'BAZ%' + left JOIN (select A.*,B.sku parent_sku + from raena_order_management.order_item A + left join raena_order_management.order_item B on A.parent_item_id = B.id ) oi + ON GB.external_id=oi.order_id + AND OI.SKU not like 'BAZ%' + AND GB.sku=oi.sku + and GB.parent_sku= oi.parent_sku +left join raena_analytics.payment_order C on A.id=C.order_id +left join raena_order_management.channel D on A.channel_id = D.id +WHERE cast(A.created_at+interval'7 hours' as date)>='2021-10-28'; + +DROP TABLE IF EXISTS raena_analytics.remove_duplicate_from_app_admin; + + + +CREATE TABLE raena_analytics.remove_duplicate_from_app_admin AS +SELECT A.external_id, + wholesale_price_gm, + wholesale_price_app_admin, + wholesale_price_gm-wholesale_price_app_admin diff +FROM + (SELECT external_id , + sum(discounted_price*quantity) P1, + sum((retail_price*quantity)-(seller_margin*quantity)) wholesale_price_gm + FROM raena_analytics.gm_dashboard gd + GROUP BY external_id) A +FULL OUTER JOIN + (SELECT order_id , + payment_amount p2, + sum(total_price) wholesale_price_app_admin + FROM raena_analytics.om_app_admin_dump_table oaadt + GROUP BY order_id, + payment_amount) B ON A.external_id= B.order_id +WHERE wholesale_price_gm-wholesale_price_app_admin<>0; + +DROP TABLE IF EXISTS raena_analytics.dubplicate_record_app_admin; + +CREATE TABLE raena_analytics.dubplicate_record_app_admin AS +SELECT DISTINCT * +FROM raena_analytics.om_app_admin_dump_table oaadt +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + + +DELETE +FROM raena_analytics.om_app_admin_dump_table +WHERE order_id IN + (SELECT DISTINCT external_id + FROM raena_analytics.remove_duplicate_from_app_admin); + +INSERT INTO raena_analytics.om_app_admin_dump_table +SELECT * +FROM raena_analytics.dubplicate_record_app_admin; + + diff --git a/redshift/sql_code/home_page_pdp_flash.sql b/redshift/sql_code/home_page_pdp_flash.sql new file mode 100644 index 0000000..6e26e17 --- /dev/null +++ b/redshift/sql_code/home_page_pdp_flash.sql @@ -0,0 +1,735 @@ + + +drop table if exists raena_analytics.homepage_base_stage1_v1; + +create table raena_analytics.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 >= '2024-06-23' ; + +delete from raena_analytics.homepage_base_stage1 +where ts::date >= '2024-06-23'; + +insert into raena_analytics.homepage_base_stage1 +SELECT ts, + events, + name , + email, + phone, + objectid, + json_extract_path_text(profiledata,'tier',true) tier, + json_extract_path_text(profiledata,'isguestuser',true) isguestuser, + split_part(json_extract_path_text(replace(event_props,'\\xa0',' '),'url' ,true),'/',2) collection, + split_part(json_extract_path_text(replace(event_props,'\\xa0',' '),'url',true) ,'/',3) collection_id, + json_extract_path_text(replace(event_props,'\\xa0',' '),'userId',true) userId, + json_extract_path_text(replace(event_props,'\\xa0',' '),'sectionName',true) sectionName, + json_extract_path_text(replace(event_props,'\\xa0',' '),'guest_user',true) guest_user, + json_extract_path_text(replace(event_props,'\\xa0',' '),'tier',true) tier_id, + json_extract_path_text(replace(event_props,'\\xa0',' '),'categoryName',true) categoryName, + json_extract_path_text(replace(event_props,'\\xa0',' '),'brand_code',true) brand_code, + json_extract_path_text(replace(event_props,'\\xa0',' '),'brand_name',true) brand_name, + case when events ='home_product_infinite' then json_extract_path_text(replace(event_props,'\\xa0',' '),'brand',true) end brand_id, + case when events ='home_product_Caraousel' then json_extract_path_text(replace(event_props,'\\xa0',' '),'brand',true) end brand, + json_extract_path_text(replace(event_props,'\\xa0',' '),'productId',true) productId, + json_extract_path_text(replace(event_props,'\\xa0',' '),'videoId',true) videoId, + json_extract_path_text(replace(event_props,'\\xa0',' '),'pageName',true) pageName, + json_extract_path_text(replace(event_props,'\\xa0',' '),'SKU_code',true) SKU_code +FROM raena_analytics.homepage_base_stage1_v1; + +drop table if exists raena_analytics.homepage_base; + +create table raena_analytics.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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11 + 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, + 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 raena_analytics.homepage_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +LEFT JOIN raena_user_management.tier C ON tier_id= cast(C.id as varchar) +LEFT JOIN raena_catalog_management.brand D ON brand_code=cast(D.id as varchar) +LEFT JOIN raena_catalog_management.brand E ON collection_id= cast(E.id as varchar) +LEFT JOIN raena_catalog_management.category F ON collection_id= cast(F.id as varchar) +OR brand_id = cast(D.id as varchar) +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11; + + + drop table if exists raena_analytics.pdp_base_stage1_v1; + +create table raena_analytics.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 >= '2024-06-23' ; + + +delete from raena_analytics.pdp_base_stage1 +where ts::date >= '2024-06-23' ; + +insert into raena_analytics.pdp_base_stage1 +SELECT ts, + events, + name , + email, + phone, + objectid, + json_extract_path_text(profiledata,'tier',true) tier, + json_extract_path_text(profiledata,'isguestuser',true) isguestuser, + json_extract_path_text(replace(event_props,'\\xa0',' '),'item_brand',true) brand_name, + json_extract_path_text(replace(event_props,'\\xa0',' '),'item_id',true) SKU_code, + json_extract_path_text(replace(event_props,'\\xa0',' '),'categoryPageId',true) categoryPageId, + json_extract_path_text(replace(event_props,'\\xa0',' '),'screen',true) screen +FROM raena_analytics.pdp_base_stage1_v1; + + + +drop table if exists raena_analytics.pdp_base; + +create table raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.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 raena_analytics.pdp_base_stage1 +LEFT JOIN raena_analytics.user_type_table ON phone = reseller_mobile +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9; + + +drop table if exists raena_analytics.flash_sale_order_stage_v1; + +create table raena_analytics.flash_sale_order_stage_v1 +as +SELECT BB.*,AA.flash_sale_id + FROM (select external_id , + sku, + brand_name, + tier_name, + sum(quantity)quantity, + sum((discounted_price*quantity)) discounted_price, + raena_analytics.gm_dashboard.reseller_id, + transaction_date::date transaction_date + from raena_analytics.gm_dashboard + where item_type like 'Flash%' + group by 1,2,3,4,7,8) BB + INNER JOIN (select id,flash_sale_id from raena_order_management.order where flash_sale_id IS NOT NULL)AA ON external_id = id ; + +DROP TABLE IF EXISTS raena_analytics.flash_sale_order; + + +CREATE TABLE raena_analytics.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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +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) post_discount_revenue, + sum(quantity) total_quantity, + count(distinct reseller_id) number_of_reseller +FROM raena_analytics.flash_sale_order_stage_v1 +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +delete from raena_analytics.flash_base_stage1 +where ts::date >= '2024-06-23'; + +insert into raena_analytics.flash_base_stage1 +SELECT ts, + name , + email, + events, + phone, + objectid, + json_extract_path_text(profiledata,'tier',true) tier, + json_extract_path_text(replace(event_props,'\\xa0',' '),'item_brand',true) brand_name, + json_extract_path_text(replace(event_props,'\\xa0',' '),'item_id',true) SKU_code +FROM clevertap.clevertap_master_data cmd +WHERE events in ('flashsale_carousel_item_press','flashsale_carousel_view_all_press') + and ts::date >= '2024-06-23' ; + +DROP TABLE IF EXISTS raena_analytics.flash_sale_event_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.flash_base_stage1 +GROUP BY 1, + 2, + 3, + 4, + 5; + + + diff --git a/redshift/sql_code/increase_cogs_alert.sql b/redshift/sql_code/increase_cogs_alert.sql new file mode 100644 index 0000000..7475e82 --- /dev/null +++ b/redshift/sql_code/increase_cogs_alert.sql @@ -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, + json_extract_path_text(raena_catalog_management.product.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',true) wholesale_gold_price, + json_extract_path_text(raena_catalog_management.product.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',true) wholesale_silver_price, + json_extract_path_text(raena_catalog_management.product.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',true) 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; + + diff --git a/redshift/sql_code/loyalty_point_etl.sql b/redshift/sql_code/loyalty_point_etl.sql new file mode 100644 index 0000000..d11f74f --- /dev/null +++ b/redshift/sql_code/loyalty_point_etl.sql @@ -0,0 +1,69 @@ + + +DROP TABLE IF EXISTS raena_analytics.loyalty_reseller_stage1; + +CREATE TABLE raena_analytics.loyalty_reseller_stage1 AS +SELECT 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 (select distinct * from raena_analytics.lms_transactions) A +LEFT JOIN raena_user_management.user B ON A.resellerid = cast(B.id as varchar) +LEFT JOIN raena_user_management.tier C ON A.tierid = cast(C.id as varchar) ; + + +DROP TABLE IF EXISTS raena_analytics.loyalty_total_orders; + +CREATE TABLE raena_analytics.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 >='2021-01-01'; + + +drop table if exists raena_analytics.loyalty_base_1; + + +create table raena_analytics.loyalty_base_1 +as +select B.created_date, + coalesce(B.resellerid,A.reseller_id) reseller_id, + A.reseller_name, + coalesce(B.email,A.reseller_email) reseller_email, + coalesce(B.mobile,A.reseller_mobile) reseller_mobile, + coalesce(B.tier_name,A.reseller_tier_name) reseller_tier_name, + A.payment_status, + A.order_id, + A.payment_price, + A.order_status, + B.transactionid, + 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 raena_analytics.loyalty_reseller_stage1 B left join raena_analytics.loyalty_total_orders A + on A.order_id = B.order_id; + + diff --git a/redshift/sql_code/new_user_funnel_etl.sql b/redshift/sql_code/new_user_funnel_etl.sql new file mode 100644 index 0000000..faf4018 --- /dev/null +++ b/redshift/sql_code/new_user_funnel_etl.sql @@ -0,0 +1,285 @@ + + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may; + + +CREATE TABLE raena_analytics.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 cast('2022-04-01' as date) AS App_install_date, + email, + phone, + email AS user_id + FROM raena_analytics.clevertap_april_csv + UNION SELECT '2022-03-30' AS App_install_date, + email, + phone, + email AS user_id + FROM raena_analytics.clevertap_march_csv + WHERE phone NOT IN + (SELECT DISTINCT profile_phone + FROM raena_analytics.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)A)B +WHERE R=1 ; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_2; + +CREATE TABLE raena_analytics.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 raena_analytics.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)A + WHERE R=1 + AND email is NOT NULL) 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)A; + + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_3; + + +CREATE TABLE raena_analytics.om_clevertap_install_jan_may_3 AS +SELECT a.*, + cast(b.created_at AS date) AS transaction_date, + transacted_email +FROM raena_analytics.om_clevertap_install_jan_may_2 a +LEFT JOIN + (SELECT reseller_email AS transacted_email , + min(transaction_date) AS created_at + FROM raena_analytics.gm_dashboard + GROUP BY 1) b ON a.email=transacted_email; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_final; + + +CREATE TABLE raena_analytics.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<=app_install_date+interval'30 day' THEN 'Yes' + ELSE 'No' + END AS transacted_flag +FROM raena_analytics.om_clevertap_install_jan_may_3 ; + +DROP TABLE IF EXISTS raena_analytics.om_clevertap_install_jan_may_order; + + +CREATE TABLE raena_analytics.om_clevertap_install_jan_may_order AS +SELECT * +FROM raena_analytics.om_clevertap_install_jan_may_final +WHERE transacted_email NOTNULL + AND rtrim(ltrim(transacted_email)) != ''; ; + +INSERT INTO raena_analytics.Test_1_week_om_clevertap_install_jan_may_final +SELECT *, + CURRENT_DATE-1 AS Report_date +FROM raena_analytics.om_clevertap_install_jan_may_final; + + +DROP TABLE IF EXISTS raena_analytics.om_reseller_info; + + +CREATE TABLE raena_analytics.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 raena_analytics.OM_Order_id_payment_id; + + +CREATE TABLE raena_analytics.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; + +delete from raena_analytics.OM_Events_user_type +where cast(ts as date)>='2024-06-22'; + +insert into raena_analytics.OM_Events_user_type +SELECT A.*, + CASE + WHEN date_trunc('Month',A.ts)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.ts)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.ts)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.ts)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.ts)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag +FROM (select * from clevertap.clevertap_master_data 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') and ts::date >= '2024-06-22') A +LEFT JOIN raena_analytics.user_type_table flag ON replace(A.phone,'+','')=flag.reseller_mobile; + + +DROP TABLE IF EXISTS raena_analytics.om_order_brand_type; + + +DROP TABLE IF EXISTS raena_analytics.om_order_brand_type; + + +CREATE TABLE raena_analytics.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 (select distinct id,order_id,parent_sku from raena_order_management.sales_sub_order) sso +LEFT JOIN (select distinct sales_sub_order_id,sku from 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; + + + diff --git a/redshift/sql_code/notification_doashboard_anchor_brand.sql b/redshift/sql_code/notification_doashboard_anchor_brand.sql new file mode 100644 index 0000000..cf51084 --- /dev/null +++ b/redshift/sql_code/notification_doashboard_anchor_brand.sql @@ -0,0 +1,605 @@ + + +------- Notification code + +drop table raena_analytics.notification_session_base_event; + +create table raena_analytics.notification_session_base_event +as +SELECT DISTINCT ts::date created_date, + events, + email, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid, + CASE + WHEN json_extract_path_text(event_props,'url',true) LIKE '%source=email%' THEN 'email' + WHEN json_extract_path_text(event_props,'url',true) LIKE '%source=push%' THEN 'push' + WHEN json_extract_path_text(event_props,'url',true) LIKE '%source=whatsapp%' THEN 'whatsapp' + END SOURCE, + split_part(json_extract_path_text(event_props,'url',true),'&requestId=', 2) requestid +FROM clevertap.clevertap_master_data cmd +WHERE events IN ('opened_using_link') + AND (json_extract_path_text(event_props,'url',true) LIKE '%source=email%' + OR json_extract_path_text(event_props,'url',true) LIKE '%source=push%' + OR json_extract_path_text(event_props,'url',true) LIKE '%source=whatsapp%') + AND ts::date >='2023-06-16'; + + +drop table raena_analytics.clevertab_session_id_base_data ; + +CREATE TABLE raena_analytics.clevertab_session_id_base_data AS +SELECT created_date, + email, + phone, + sessionid, + count(CASE WHEN events ='view_item' THEN email END) view_item, + count(CASE WHEN events ='view_cart' THEN email END) view_cart, + count(CASE WHEN events ='add_to_cart' THEN email END) add_to_cart, + count(CASE WHEN events ='begin_checkout' THEN email END) begin_checkout, + count(CASE WHEN events ='page_load_checkout_continue' THEN email END) page_load_checkout_continue, + count(CASE WHEN events ='page_load_finish_checkout' THEN email END) page_load_finish_checkout, + count(CASE WHEN events ='finish_checkout' THEN email END) finish_checkout, + count(CASE WHEN events ='Charged' THEN email END) Charged +FROM + (SELECT ts::date created_date, + events, + email, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid --identity + + FROM clevertap.clevertap_master_data cmd + WHERE events IN ('add_to_cart', + 'begin_checkout', + 'finish_checkout', + 'page_load_checkout_continue', + 'page_load_finish_checkout', + 'view_cart', + 'view_item', + 'Charged') + AND ts::date >='2023-06-16') A +GROUP BY 1, + 2, + 3, + 4; + + +drop table raena_analytics.clevertab_charged_base_data ; + +CREATE TABLE raena_analytics.clevertab_charged_base_data AS +SELECT created_date, + email, + phone, + count(CASE WHEN events ='Charged' THEN email END) Charged +FROM + (SELECT ts::date created_date, + events, + email, + phone + FROM clevertap.clevertap_master_data cmd + WHERE events IN ('Charged') + AND ts::date >='2023-06-16') A +GROUP BY 1, + 2, + 3; + + +drop table raena_analytics.notification_base_table ; + +create table raena_analytics.notification_base_table +as +select created_at::date created_at, + id notification_id, + user_id , + event_name, + notification_type, + request_id, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'sku',true) sku, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'price',true) price, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'price',true),'07030fbe-5801-4318-9e97-fe33fa169894',true) bronze_price, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'price',true),'8eb95d6e-915a-4a91-9c12-fa43db995e19',true) silver_price, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'price',true),'bf645e97-8a48-4977-8367-e987489760f9',true) gold_price, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'margin',true) margin, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'flashSaleId',true) flashSaleId, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'newQty',true) newQty, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'oldQty',true) oldQty, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'retailPrice',true) retailPrice, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'applicablePrice',true) applicablePrice, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'applicablePrice',true),'new',true) new_applicablePrice, + json_extract_path_text(json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'applicablePrice',true),'old',true) old_applicablePrice, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'percentageChange',true) percentageChange, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'name',true) user_name, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'email',true) user_email, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'mobile',true) user_mobile, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'tierId',true) user_tierid, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'lastLoggedInAt',true) user_last_loggedin_at +from raena_notification_management.notifications_log A +left join (select Notification_type, id template_id from raena_notification_management.notification_template) B on A.template_id = B.template_id +where created_at ::date >='2023-06-01' +and event_name in ('flash-sale-start','back-in-stock','price-drop'); + + +delete from raena_analytics.notification_funnel_report +where created_at >='2023-06-30'; + + +INSERT INTO raena_analytics.notification_funnel_report +SELECT A.created_at, + A.event_name, + A.notification_type, + A.user_name, + A.User_mobile, + A.user_email, + count(DISTINCT CASE WHEN unique_user>0 THEN 1 END) unique_user, + count(DISTINCT CASE WHEN view_item>0 THEN 1 END) view_item, + count(DISTINCT CASE WHEN view_cart>0 THEN 1 END) view_cart, + count(DISTINCT CASE WHEN add_to_cart>0 THEN 1 END) add_to_cart, + count(DISTINCT CASE WHEN begin_checkout>0 THEN 1 END) begin_checkout, + count(DISTINCT CASE WHEN page_load_checkout_continue>0 THEN 1 END) page_load_checkout_continue, + count(DISTINCT CASE WHEN page_load_finish_checkout>0 THEN 1 END) page_load_finish_checkout, + count(DISTINCT CASE WHEN finish_checkout>0 THEN 1 END) finish_checkout, + count(DISTINCT CASE WHEN Charged>0 THEN 1 END) Charged +FROM + (SELECT DISTINCT created_at , + event_name, + notification_type, + user_name, + User_mobile, + user_email, + request_id + FROM raena_analytics.notification_base_table + WHERE created_at>='2023-06-30') A +LEFT JOIN + (SELECT A.created_date, + A.email, + A.phone, + A.source, + A.requestid, + count(DISTINCT A.email) unique_user, + count(DISTINCT CASE WHEN view_item >0 THEN A.email END) view_item, + count(DISTINCT CASE WHEN view_cart >0 THEN A.email END) view_cart, + count(DISTINCT CASE WHEN add_to_cart >0 THEN A.email END) add_to_cart, + count(DISTINCT CASE WHEN begin_checkout >0 THEN A.email END) begin_checkout, + count(DISTINCT CASE WHEN page_load_checkout_continue >0 THEN A.email END) page_load_checkout_continue, + count(DISTINCT CASE WHEN page_load_finish_checkout >0 THEN A.email END) page_load_finish_checkout, + count(DISTINCT CASE WHEN finish_checkout >0 THEN A.email END) finish_checkout, + count(DISTINCT CASE WHEN C.Charged >0 THEN A.email END) Charged + FROM raena_analytics.notification_session_base_event A + LEFT JOIN raena_analytics.clevertab_session_id_base_data B ON A.sessionid = B.sessionid + LEFT JOIN raena_analytics.clevertab_charged_base_data C ON A.email = C.email + AND A.phone = C.phone + AND A.created_date = C.created_date + WHERE A.created_date >='2023-06-30' + GROUP BY 1, + 2, + 3, + 4, + 5) B ON --(replace(A.User_mobile,'+','') = B.phone +A.user_email = B.email +AND A.request_id = B.requestid +GROUP BY 1, + 2, + 3, + 4, + 5, + 6; + + +DROP TABLE raena_analytics.notification_order_base ; + + +CREATE TABLE raena_analytics.notification_order_base AS +SELECT reseller_id, + reseller_mobile , + transaction_date::date transaction_date, + external_id , + sku , + brand_name , + category_name , + discounted_price*quantity payment_amount , + quantity , + sum(discounted_price*quantity) over(partition BY external_id) order_level_amount +FROM raena_analytics.gm_dashboard gd +WHERE transaction_date ::date >='2023-06-15' + AND order_placed_by <>'Admin Panel'; + + +DROP TABLE raena_analytics.segement_raw_data; +CREATE TABLE raena_analytics.segement_raw_data AS +SELECT id notification_id, + user_id , + event_name, + notification_type, + request_id, + json_extract_path_text(json_extract_path_text(event_param,'eventDetail',true),'sku',true) sku, + json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'segment',true) segment +FROM raena_notification_management.notifications_log A +LEFT JOIN + (SELECT Notification_type, + template_id + FROM raena_notification_management.notification_event_template_mapping) B ON A.template_id = B.template_id +WHERE created_at ::date >='2023-06-30' + AND event_name IN ('flash-sale-start', + 'back-in-stock', + 'price-drop') + AND json_extract_path_text(json_extract_path_text(event_param,'userContactDetail',true),'segment',true) IS NOT NULL ; + + +DROP TABLE IF EXISTS raena_analytics.user_bucket; + +CREATE TABLE raena_analytics.user_bucket AS +SELECT reseller_id , + max(payment_amount) max_payment_amount +FROM + (SELECT reseller_id , + id , + sum(payment_amount)payment_amount + FROM raena_order_management.order + WHERE payment_status ='Paid' + GROUP BY 1, + 2) A +GROUP BY 1; + +delete from raena_analytics.transaction_notification_mapping +where created_at >='2023-06-30'; + +INSERT INTO raena_analytics.transaction_notification_mapping +SELECT D.name brand_name, + A.*, + B.external_id , + B.quantity, + CASE + WHEN A.sku = B.sku THEN payment_amount + END payment_amount, + CASE + WHEN A.sku IS NOT NULL + AND B.sku IS NOT NULL + AND A.sku = B.sku + AND payment_amount>0 THEN order_level_amount + END order_level_amount, + payment_amount indirect_payment_amount, + CASE + WHEN date_trunc('Month',A.created_at)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.created_at)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.created_at)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.created_at)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END AS New_existing_flag, + DD.source, + XX.segment, + case when max_payment_amount between 0 and 2000000 then '0to2M' + when max_payment_amount between 2000000.00001 and 10000000 then '2Mto10M' + when max_payment_amount >= 10000000.000001 then 'greaterthan10M' end bucket +FROM raena_analytics.notification_base_table A +INNER JOIN raena_catalog_management.product C ON A.sku = C.sku +INNER JOIN raena_catalog_management.brand D ON C.brand_id = D.id +left join raena_analytics.segement_raw_data XX on A.notification_id = XX.notification_id +and A.user_id = XX.user_id +and A.event_name = XX.event_name +and A.notification_type = XX.notification_type +and A.request_id = XX.request_id +and A.sku = XX.sku +left join raena_analytics.user_bucket YY +on cast(A.user_id as varchar) = YY.reseller_id +LEFT JOIN raena_analytics.user_type_table flag ON replace(A.user_mobile,'+','')=flag.reseller_mobile +LEFT JOIN raena_analytics.notification_session_base_event dd ON replace(A.user_mobile,'+','')=DD.phone +AND --(date_part('day',A.created_at::date)-date_part('Day',created_date::date) = 1) and (date_part('Month',A.created_at::date)-date_part('Month',created_date::date) = 0 ) and + lower(A.notification_type) =lower(source) +AND A.request_id = dd.requestid +LEFT JOIN raena_analytics.notification_order_base B ON cast(A.user_id AS varchar) = B.reseller_id +and (transaction_date::date-A.created_at::date) between 0 and 3 +AND replace(B.reseller_mobile,'+','')=DD.phone +WHERE A.created_at >='2023-06-30'; + +drop table raena_analytics.anchor_brand_level_data ; + +CREATE TABLE raena_analytics.anchor_brand_level_data AS +SELECT * , + rank()over (partition BY external_id + ORDER BY brand_contribution DESC) rnk +FROM + (SELECT * , + CASE + WHEN order_level_payment_amount <2000000 THEN '0 to 2M' + WHEN order_level_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' + WHEN order_level_payment_amount >10000000 THEN '>10M' + END bucket, + (brand_payment_amount/nullif(order_level_payment_amount,0))*100 AS brand_contribution + FROM + (SELECT *, + sum(brand_payment_amount) over(partition BY external_id) order_level_payment_amount + FROM + (SELECT date_trunc('Year',transaction_date)::date transaction_date , + external_id , + brand_name, + sum(discounted_price*quantity) brand_payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2, + 3) A + WHERE brand_payment_amount>10) B) C +WHERE brand_contribution<100; + + drop table raena_analytics.anchor_brand_data; + +CREATE TABLE raena_analytics.anchor_brand_data AS +SELECT cast(date_part('Year',transaction_date) AS varchar) transaction_date2, + bucket, + brand_name , + avg(brand_contribution) avg_brand_contribution, + avg(brand_payment_amount) avg_payment_amount , + anchor_brand_name , + avg(anchor_brand_contribution) avg_anchor_brand_contribution , + avg(anchor_payment_amount) avg_anchor_payment_amount +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +GROUP BY 1, + 2, + 3, + 6 +ORDER BY transaction_date2 , + bucket, + brand_name , + avg_anchor_brand_contribution DESC; + + +DROP TABLE raena_analytics.reseller_level_anchor_brand_level_data ; + +CREATE TABLE raena_analytics.reseller_level_anchor_brand_level_data AS +SELECT * , + rank()over (partition BY external_id + ORDER BY brand_contribution DESC) rnk +FROM + (SELECT B.* , + CASE + WHEN bucket_level_payment_amount <2000000 THEN '0 to 2M' + WHEN bucket_level_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M to 10M' + WHEN bucket_level_payment_amount >10000000 THEN '>10M' + END bucket, + (brand_payment_amount/nullif(order_level_payment_amount,0))*100 AS brand_contribution + FROM + (SELECT *, + sum(brand_payment_amount) over(partition BY external_id) order_level_payment_amount + FROM + (SELECT date_trunc('Year',transaction_date)::date transaction_date, + reseller_id, + reseller_mobile , + reseller_email , + external_id , + brand_name, + sum(discounted_price*quantity) brand_payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) A + WHERE brand_payment_amount>10) B + LEFT JOIN + (SELECT reseller_id, + max(payment_amount) bucket_level_payment_amount + FROM + (SELECT reseller_id , + external_id , + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard gd + GROUP BY 1, + 2) A + GROUP BY 1) C ON B.reseller_id = C.reseller_id) D +WHERE brand_contribution<100; + +drop table raena_analytics.reseller_level_anchor_brand_data; + +CREATE TABLE raena_analytics.reseller_level_anchor_brand_data AS +SELECT reseller_id , + reseller_email , + reseller_mobile , + cast(date_part('Year',transaction_date) AS varchar) transaction_date2, + bucket, + brand_name , + avg(brand_contribution) avg_brand_contribution, + avg(brand_payment_amount) avg_payment_amount , + anchor_brand_name , + avg(anchor_brand_contribution) avg_anchor_brand_contribution , + avg(anchor_payment_amount) avg_anchor_payment_amount +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +GROUP BY 1, + 2, + 3, + 4, + 5, + 6, + 9 +ORDER BY transaction_date2 , + bucket, + brand_name , + avg_anchor_brand_contribution DESC; + +DROP TABLE raena_analytics.anchor_analysis; + + +CREATE TABLE raena_analytics.anchor_analysis AS +SELECT BB.reseller_id , + brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_2023, + avg(CASE WHEN transaction_date <'2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_less_2023, + last_transaction_date +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +GROUP BY 1, + 2 , + 3, + 5, + 8 +ORDER BY 1, + 2, + 4; + +DROP TABLE raena_analytics.anchor_bucket_wise_for2023; + + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date BETWEEN '2023-01-01' AND '2023-05-31' + AND transaction_date ='2023-01-01' +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + +DROP TABLE raena_analytics.anchor_bucket_wise_for2023_churn_before2023; + + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023_churn_before2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date <'2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_before_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date <'2023-01-01' + AND transaction_date <'2023-01-01' +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + + +drop table raena_analytics.anchor_bucket_wise_for2023_churn_in2023; + +CREATE TABLE raena_analytics.anchor_bucket_wise_for2023_churn_in2023 AS +SELECT brand_name , + bucket, + avg(brand_contribution) avg_brand_contribution, + anchor_brand_name , + avg(CASE WHEN transaction_date ='2023-01-01' THEN anchor_brand_contribution END) avg_anchor_brand_contribution_in_2023 +FROM + (SELECT A.* , + B.brand_name anchor_brand_name, + B.brand_contribution anchor_brand_contribution, + B.rnk anchor_brnad_rnk , + B.brand_payment_amount anchor_payment_amount + FROM + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk= 1)A + LEFT JOIN + (SELECT * + FROM raena_analytics.reseller_level_anchor_brand_level_data + WHERE rnk>1) B ON A.external_id = B.external_id + AND A.bucket=B.bucket) BB +LEFT JOIN + (SELECT reseller_id, + max(transaction_date) last_transaction_date + FROM raena_analytics.gm_dashboard gd + GROUP BY 1) C ON BB.reseller_id = C.reseller_id +WHERE last_transaction_date::date ='2023-01-01' + AND brand_name IN + (SELECT DISTINCT brand_name + FROM raena_analytics.anchor_bucket_wise_for2023_churn_before2023) +GROUP BY 1, + 2, + 4 +ORDER BY 1, + 2, + 4; + + + diff --git a/redshift/sql_code/offender_dashboard.sql b/redshift/sql_code/offender_dashboard.sql new file mode 100644 index 0000000..45d34c7 --- /dev/null +++ b/redshift/sql_code/offender_dashboard.sql @@ -0,0 +1,942 @@ + +DROP TABLE raena_analytics.base_analytics_dump; + + +CREATE TABLE raena_analytics.base_analytics_dump AS +SELECT ts, + events, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid , + json_extract_path_text(event_props,'page_name',true) pagename +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events IN ('load_home_page', + 'page_load', + 'add_to_cart', + 'begin_checkout', + 'finish_checkout', + 'Charged', + 'page_load_checkout_continue', + 'page_load_finish_checkout', + 'confirm_payment', + 'view_cart', + 'view_item' ) + AND event_props NOT LIKE '%Focallure%'; + +DROP TABLE raena_analytics.add_to_cart_base_1; + + +CREATE TABLE raena_analytics.add_to_cart_base_1 AS +SELECT DISTINCT ts::date, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid , + json_extract_path_text(event_props,'screen',true) pagename, + json_extract_path_text(event_props,'item_id',true) sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='add_to_cart' + AND json_extract_path_text(event_props,'item_id',true) IS NOT NULL ; + + +DROP TABLE raena_analytics.view_item_base_1; + + +CREATE TABLE raena_analytics.view_item_base_1 AS +SELECT DISTINCT ts::date, + phone, + json_extract_path_text(event_props,'CT Session Id',true) sessionid , + json_extract_path_text(event_props,'screen',true) pagename, + json_extract_path_text(event_props,'item_id',true) sku +FROM clevertap.clevertap_master_data cmd +WHERE ts::date >='2023-01-01' + AND events ='view_item' + AND json_extract_path_text(event_props,'item_id',true) IS NOT NULL ; + + +DROP TABLE raena_analytics.begin_checkout_base_1; + + +CREATE TABLE raena_analytics.begin_checkout_base_1 AS +SELECT DISTINCT + ts::date, + phone, + json_extract_path_text(event_props, 'CT Session Id', true) AS sessionid, + REPLACE(REPLACE(SPLIT_PART(json_extract_path_text(event_props, 'items', true), ',', 1), ']', ''), '[', '') AS sku +FROM + clevertap.clevertap_master_data cmd +WHERE + ts::date >= '2023-01-01' + AND events ='begin_checkout'; + + + +drop table raena_analytics.finish_checkout_base_1; + +create table raena_analytics.finish_checkout_base_1 +as +select A.*,P.sku +from +( SELECT DISTINCT + ts::date, + phone, + json_extract_path_text(event_props, 'CT Session Id', true) AS sessionid, + REPLACE(REPLACE(SPLIT_PART(json_extract_path_text(event_props, 'items', true), ',', 1), ']', ''), '[', '') AS pagename +FROM + clevertap.clevertap_master_data cmd +WHERE + ts::date >= '2023-01-01' +and events ='finish_checkout') A +left join raena_catalog_management.product P on A.pagename = cast(P.id as varchar) ; + +drop table raena_analytics.pagename_homepage_data; + +create table raena_analytics.pagename_homepage_data +as +select distinct sessionid ,phone,pagename,cast(ts as date) ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Home'; + + +drop table if exists raena_analytics.pagename_homepage_data_remaining_funnel_stage1; + +CREATE TABLE raena_analytics.pagename_homepage_data_remaining_funnel_stage1 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_homepage_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Home' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + +drop table raena_analytics.offender_funnel_report; + +CREATE TABLE raena_analytics.offender_funnel_report AS +SELECT ts, + pagename, + C.is_offender , + D.name, + count(phone) count_view_item, + count(add_to_cart_phone) count_add_to_cart, + count(begin_checkout_cart) begin_checkout_cart, + count(finish_checkout_cart) finish_checkout_cart +FROM + (SELECT DISTINCT A.ts, + A.sessionid, + A.phone, + A.pagename , + A.sku , + B.phone add_to_cart_phone , + C.phone begin_checkout_cart, + D.phone finish_checkout_cart + FROM raena_analytics.view_item_base_1 A + LEFT JOIN raena_analytics.add_to_cart_base_1 B ON A.sessionid = B.sessionid + AND A.sku = B.sku + LEFT JOIN raena_analytics.begin_checkout_base_1 C ON A.sessionid = C.sessionid + AND A.sku = C.sku + LEFT JOIN raena_analytics.finish_checkout_base_1 D ON A.sessionid = D.sessionid + AND A.sku = C.sku) AA +LEFT JOIN raena_catalog_management.product C ON AA.sku = C.sku +LEFT JOIN raena_catalog_management.brand D ON C.brand_id = D.id +GROUP BY 1, + 2, + 3, + 4; + +drop table raena_analytics.pagename_Brands_data; + +create table raena_analytics.pagename_Brands_data +as +select distinct sessionid ,phone,pagename,ts::date ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Brands'; + + +drop table if exists raena_analytics.pagename_brand_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_brand_data_remaining_funnel_stage2 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_brands_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Brands' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + + +drop table raena_analytics.pagename_search_data; + +create table raena_analytics.pagename_search_data +as +select distinct sessionid ,phone,'Search' pagename,ts::date ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename like '%Search%'; + + +drop table if exists raena_analytics.pagename_search_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_search_data_remaining_funnel_stage2 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_search_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename LIKE '%Search%' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + +-----CategoryList---- + + + +drop table raena_analytics.pagename_CategoryList_data; + +create table raena_analytics.pagename_CategoryList_data +as +select distinct sessionid ,phone,pagename,ts::date ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'CategoryList'; + + +drop table if exists raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_CategoryList_data_remaining_funnel_stage2 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_CategoryList_data A + LEFT JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'CategoryList' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + + + +-----Cart----- + + + +drop table raena_analytics.pagename_Cart_data; + +create table raena_analytics.pagename_Cart_data +as +select distinct sessionid ,phone,pagename,ts::date ts_date +from raena_analytics.base_analytics_dump +where events = 'page_load' and pagename = 'Cart'; + + +drop table if exists raena_analytics.pagename_Cart_data_remaining_funnel_stage2; + +CREATE TABLE raena_analytics.pagename_Cart_data_remaining_funnel_stage2 AS +SELECT AA.ts_date, + AA.pagename , + sum(cnt_pageload_home) cnt_pageload_home , + count(DISTINCT CASE WHEN cnt_pageload_home>0 THEN sessionid END) unique_pageload_home, + sum(cnt_view_item) cnt_view_item , + count(DISTINCT CASE WHEN cnt_view_item>0 THEN sessionid END) unique_view_item, + sum(cnt_add_to_cart) cnt_add_to_cart , + count(DISTINCT CASE WHEN cnt_add_to_cart>0 THEN sessionid END) unique_add_to_cart, + sum(cnt_view_cart) cnt_view_cart , + count(DISTINCT CASE WHEN cnt_view_cart>0 THEN sessionid END) unique_view_cart, + sum(cnt_begin_checkout) cnt_begin_checkout , + count(DISTINCT CASE WHEN cnt_begin_checkout>0 THEN sessionid END) unique_begin_checkout, + sum(cnt_page_load_checkout_continue) cnt_page_load_checkout_continue , + count(DISTINCT CASE WHEN cnt_page_load_checkout_continue>0 THEN sessionid END) unique_page_load_checkout_continue, + sum(cnt_load_payment_method) cnt_load_payment_method , + count(DISTINCT CASE WHEN cnt_load_payment_method>0 THEN sessionid END) unique_load_payment_method, + sum(cnt_confirm_payment) cnt_confirm_payment , + count(DISTINCT CASE WHEN cnt_confirm_payment>0 THEN sessionid END) unique_confirm_payment +FROM + (SELECT A.sessionid , + A.phone, + A.pagename, + A.ts_date , + B.cnt_pageload_home, + cnt_view_item, + cnt_add_to_cart, + cnt_view_cart, + cnt_begin_checkout, + cnt_page_load_checkout_continue, + cnt_load_payment_method, + cnt_confirm_payment + FROM raena_analytics.pagename_Cart_data A + INNER JOIN + (SELECT sessionid, + phone , + count(CASE WHEN events = 'page_load' + AND pagename= 'Cart' THEN sessionid END)cnt_pageload_home, + count(CASE WHEN events = 'view_item' THEN sessionid END) cnt_view_item , + count(CASE WHEN events = 'add_to_cart' THEN sessionid END) cnt_add_to_cart , + count(CASE WHEN events = 'view_cart' THEN sessionid END) cnt_view_cart , + count(CASE WHEN events = 'begin_checkout' THEN sessionid END) cnt_begin_checkout , + count(CASE WHEN events = 'page_load_checkout_continue' THEN sessionid END) cnt_page_load_checkout_continue , + count(CASE WHEN events = 'page_load' + AND pagename ='PaymentMethods' THEN sessionid END) cnt_load_payment_method, + count(CASE WHEN events = 'confirm_payment' THEN sessionid END) cnt_confirm_payment + FROM raena_analytics.base_analytics_dump + GROUP BY 1, + 2) B ON A.sessionid = B.sessionid + AND A.phone = B.phone) AA +GROUP BY 1, + 2; + +drop table if exists raena_analytics.offender_dashboard_base_table; + +CREATE TABLE raena_analytics.offender_dashboard_base_table AS +SELECT gd.transaction_date , + category_name , + product_type , + brand_name , + gd.sku , + gd.shipping_province , + order_recipient , + gd.order_placed_by , + order_type , + tier_name , + gd.external_id , + sum(quantity) quantity , + sum(discounted_price*quantity) payment_price, + sum(cogs*quantity) total_cogs, + sum((retail_price*quantity)-(seller_margin*quantity)) total_wholesale_price , + C.name channel_name , + O.status, + payment_amount order_level_payment_amount, + reseller_mobile , + reseller_email , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END bucket, + is_offender, + sum(CASE WHEN gd.external_id = BB.order_id + AND gd.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount +FROM raena_analytics.gm_dashboard gd +LEFT JOIN raena_order_management.order O ON gd.external_id = O.id +LEFT JOIN raena_order_management.channel C ON O.channel_id = C.id +LEFT JOIN + (SELECT reseller_id , + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_id, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' + GROUP BY 1, + 2) A + GROUP BY 1) D ON gd.reseller_id = D.reseller_id +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON gd.external_id = BB.order_id +AND gd.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON gd.external_id = CC.external_id +AND gd.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +GROUP BY gd.transaction_date , + category_name , + product_type , + brand_name , + gd.sku , + gd.shipping_province , + order_recipient , + gd.order_placed_by , + order_type , + tier_name , + gd.external_id , + C.name , + O.status, + payment_amount , + reseller_mobile , + reseller_email , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END , + is_offender; + + +drop table if exists raena_analytics.rentention_bucket_wise ; + +create table raena_analytics.rentention_bucket_wise +as +select month_sort , bucket, + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user, + count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user, + count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user, + count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user, + count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user, + count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user, + count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user, + count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user, + count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user +from ( +SELECT mobile , month_sort , bucket,is_offender,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end bucket, + is_offender, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT * + FROM + (SELECT DISTINCT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + is_offender, + discounted_price*quantity payment_amount + FROM raena_analytics.gm_dashboard gd + ) A + GROUP BY 1, + 2,3) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') AS mobile, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3,4) BB + group by 1,2 + order by 1,2; + + + +drop table if exists raena_analytics.rentention_overall ; + +create table raena_analytics.rentention_overall +as +select month_sort , + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user, + count(distinct case when m0>0 and is_offender='true' then mobile end) offender_m0_user, + count(distinct case when m1>0 and is_offender='true' then mobile end) offender_m1_user, + count(distinct case when m2>0 and is_offender='true' then mobile end) offender_m2_user, + count(distinct case when m3>0 and is_offender='true' then mobile end) offender_m3_user, + count(distinct case when m4>0 and is_offender='true' then mobile end) offender_m4_user, + count(distinct case when m5>0 and is_offender='true' then mobile end) offender_m5_user, + count(distinct case when m6>0 and is_offender='true' then mobile end) offender_m6_user, + count(distinct case when gt_m6>0 and is_offender='true' then mobile end) offender_gt_m6_user +from ( +SELECT mobile , month_sort,is_offender,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + is_offender, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT * + FROM + (SELECT DISTINCT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + is_offender, + discounted_price*quantity payment_amount + FROM raena_analytics.gm_dashboard gd + ) A + GROUP BY 1, + 2,3) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') AS mobile, + date_trunc('month',created_at)::date AS created_date, + sum(payment_amount) payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3) BB + group by 1 + order by 1; + + + + +drop table if exists raena_analytics.rentention_brand_wise ; + +create table raena_analytics.rentention_brand_wise +as +select month_sort ,bucket,brand_name,is_offender, + count(distinct case when m0>0 then mobile end) m0_user, + count(distinct case when m1>0 then mobile end) m1_user, + count(distinct case when m2>0 then mobile end) m2_user, + count(distinct case when m3>0 then mobile end) m3_user, + count(distinct case when m4>0 then mobile end) m4_user, + count(distinct case when m5>0 then mobile end) m5_user, + count(distinct case when m6>0 then mobile end) m6_user, + count(distinct case when gt_m6>0 then mobile end) gt_m6_user +from ( +SELECT mobile , month_sort ,bucket,brand_name,sum(M0) M0 , sum(M1) m1, +sum(M2) M2 , sum(M3) m3,sum(M4) M4 , sum(M5) m5,sum(M6) M6 , sum(gt_M6) gt_m6 +FROM + (SELECT DISTINCT base_1.mobile, + date_trunc('MONTH',base_1.created_date)::date AS month_sort, + base_1.created_date, + case when max_payment_amount between 0 and 2000000 then '<=2M' + when max_payment_amount between 2000001 and 10000000 then '2M-10M' + when max_payment_amount >10000000 then '>10M' end bucket, + base_1.brand_name, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date) =0 THEN base_2.payment_amount + END AS M0, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=1 THEN base_2.payment_amount + END AS M1, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=2 THEN base_2.payment_amount + END AS M2, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=3 THEN base_2.payment_amount + END AS M3, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=4 THEN base_2.payment_amount + END AS M4, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=5 THEN base_2.payment_amount + END AS M5, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)=6 THEN base_2.payment_amount + END AS M6, + CASE + WHEN (DATE_PART('year', base_2.created_date) - DATE_PART('year', base_1.created_date)) * 12 + + date_part('Month',base_2.created_date)-date_part('Month',base_1.created_date)>6 THEN base_2.payment_amount + END AS GT_M6 + From + (SELECT distinct * + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date)::date created_date, + brand_name, + discounted_price*quantity payment_amount, + row_number() over(partition BY reseller_mobile + ORDER BY cast(transaction_date AS Date)) created_rnk + FROM raena_analytics.gm_dashboard o + ) a + WHERE a.created_rnk=1) base_1 + INNER join + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + discounted_price*quantity payment_amount, + row_number() over(partition BY replace(cast(reseller_mobile AS varchar),'"',''), date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date)) + ORDER BY date_part('YEAR',cast(transaction_date AS Date)),date_part('MONTH',cast(transaction_date AS Date))) created_rnk + FROM raena_analytics.gm_dashboard o + ) A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile + INNER join + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_mobile AS mobile, + date_trunc('month',transaction_date)::date AS created_date, + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard o + group by 1,2 ) A + GROUP BY 1) base_3 ON base_1.mobile=base_3.mobile) bucket_final + group by 1,2,3,4) BB + left join (select distinct name, is_offender from raena_catalog_management.brand ) CC on BB.brand_name = CC.name + group by 1,2,3,4 + order by 1,2,3; + + + drop table raena_analytics.churn_reseller_data; + + CREATE TABLE raena_analytics.churn_reseller_data AS +SELECT date_trunc('Month',A.first_transaction_date)::date , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000001 AND 10000000 THEN '2M-10M' + WHEN max_payment_amount >10000000 THEN '>10M' + END bucket, + count(DISTINCT A.mobile) M0_user, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' THEN B.mobile END) churn, + count(DISTINCT CASE WHEN created_date ='2023-05-01' THEN base_2.mobile END) may_retention, + count(DISTINCT CASE WHEN created_date ='2023-06-01' THEN base_2.mobile END) jun_retention, + count(DISTINCT CASE WHEN created_date ='2023-07-01' THEN base_2.mobile END) july_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-05-01' THEN base_2.mobile END) from_churn_may_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-06-01' THEN base_2.mobile END) from_churn_jun_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-07-01' THEN base_2.mobile END) from_churn_july_retention, + count(DISTINCT CASE WHEN created_date ='2023-05-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_may_retention, + count(DISTINCT CASE WHEN created_date ='2023-06-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_jun_retention, + count(DISTINCT CASE WHEN created_date ='2023-07-01' + AND is_offender = 'true' THEN base_2.mobile END) offender_july_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-05-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_may_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-06-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_jun_retention, + count(DISTINCT CASE WHEN last_transaction_date NOT BETWEEN '2023-04-01' AND '2023-07-17' + AND created_date ='2023-07-01' + AND is_offender = 'true' THEN base_2.mobile END) from_churn_offender_july_retention +FROM + (SELECT DISTINCT mobile, + created_date first_transaction_date, + payment_amount + FROM + (SELECT DISTINCT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') + ORDER BY cast(created_at AS Date)::date) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' ) a + WHERE a.created_rnk=1) A +LEFT JOIN + (SELECT DISTINCT mobile, + created_date last_transaction_date, + payment_amount + FROM + (SELECT DISTINCT replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') AS mobile, + cast(created_at AS Date)::date created_date, + payment_amount, + row_number() over(partition BY replace(cast(json_extract_path_text(o.reseller_info,'mobile',true) AS varchar),'"','') + ORDER BY cast(created_at AS Date)::date DESC) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + AND cast(created_at AS Date)::date <='2023-07-17' ) a + WHERE a.created_rnk=1) B ON A.mobile = B.mobile +LEFT JOIN + (SELECT mobile, + date_trunc('month',created_date)::date AS created_date, + is_offender, + sum(payment_amount) AS payment_amount + FROM + (SELECT DISTINCT reseller_mobile AS mobile, + cast(transaction_date AS Date) created_date, + discounted_price*quantity payment_amount, + p.is_offender + FROM raena_analytics.gm_dashboard o + LEFT JOIN raena_catalog_management.product P ON o.sku = P.sku + WHERE cast(transaction_date AS Date) >= date_trunc('Month',CURRENT_DATE)::date +interval'-4 Month' ) A + GROUP BY 1, + 2, + 3) base_2 ON A.mobile=base_2.mobile +LEFT JOIN + (SELECT mobile, + max(payment_amount) AS max_payment_amount + FROM + (SELECT reseller_mobile AS mobile, + date_trunc('month',transaction_date)::date AS created_date, + sum(discounted_price*quantity) payment_amount + FROM raena_analytics.gm_dashboard o + GROUP BY 1, + 2) A + GROUP BY 1) base_3 ON A.mobile=base_3.mobile +GROUP BY 1, + 2; + diff --git a/redshift/sql_code/oos_restock_etl.sql b/redshift/sql_code/oos_restock_etl.sql new file mode 100644 index 0000000..22d1342 --- /dev/null +++ b/redshift/sql_code/oos_restock_etl.sql @@ -0,0 +1,632 @@ + + +--------------------------OOS ------------------------- +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_1; + + +CREATE TABLE raena_analytics.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, + to_timestamp(snp_date || ' ' || snp_time,'YY-MM-DD HH24:MI:SS') +interval'7 hours' AS snp_date_time, + sku_code, + inventory + FROM raena_analytics.inventory_snapshot + where snp_time+interval'7 hours'>='23:30' + )A ) B +WHERE R=1; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_2; + +CREATE TABLE raena_analytics.om_oos_soldout_2 AS +select *, datediff(day,cast(snp_date_time as date),current_date) as age +FROM raena_analytics.om_oos_soldout_1; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_2_med; + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_soldout_2 +WHERE age>0; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_3; + + +CREATE TABLE raena_analytics.om_oos_soldout_3 AS +SELECT a.*, + b.inventory AS Maximum_occurred_inventory +FROM raena_analytics.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 raena_analytics.om_oos_soldout_2_med + GROUP BY inventory, + age_group, + sku_code + ORDER BY 4 DESC,1) A) AA + 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 raena_analytics.om_oos_soldout_3_med; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_soldout_3 + GROUP BY 1 + ORDER BY sku_code) b ON a.sku_code=b.sku_code ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_3_med_2; + + +CREATE TABLE raena_analytics.om_oos_soldout_3_med_2 AS +SELECT a.*, + b.sold_out_fl +FROM raena_analytics.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 + FROM raena_analytics.om_oos_soldout_3_med + )A) B + WHERE R=1) b ON a.sku_code=b.sku_code ; + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_4; + + +CREATE TABLE raena_analytics.om_oos_soldout_4 AS +SELECT a.*, + b.brand_name, + b."EL/PL Flag", b.give_away, + b.active_status +FROM raena_analytics.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 b.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 raena_analytics.demand_forecasting_configs c ON a.sku=c.sku) b ON a.sku_code=b.sku ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_5; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_soldout_4 a +LEFT JOIN raena_analytics.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' or o.id like 'PL%') + AND cast(o.is_archived AS varchar)='false' + GROUP BY sku) d ON a.sku_code=d.sku ; + + +DROP TABLE IF EXISTS raena_analytics.om_oos_soldout_6; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.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 IS NULL + AND payment_amount is NOT NULL + 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 raena_analytics.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 raena_analytics.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')AAA) AA + 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 raena_analytics.om_oos_restock_1; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_restock_1_med; + +CREATE TABLE raena_analytics.om_oos_restock_1_med AS +SELECT * +FROM raena_analytics.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 raena_analytics.om_oos_restock_2; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_restock_1_med +WHERE age>0; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_3; + + +CREATE TABLE raena_analytics.om_oos_restock_3 AS +SELECT a.*, + b.restock AS Maximum_occurred_inventory +FROM raena_analytics.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 raena_analytics.om_oos_restock_2 + GROUP BY cnt, + age_group, + warehouse, + sku + ORDER BY 5 DESC,1) A) B + 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 raena_analytics.om_oos_restock_4; + + +CREATE TABLE raena_analytics.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 raena_analytics.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 raena_analytics.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 raena_analytics.om_oos_restock_4_stage1; + +create table raena_analytics.om_oos_restock_4_stage1 +as + 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 s OLD 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 + FROM raena_analytics.om_oos_restock_4 + )A )B + WHERE R=1 ; + +drop table if exists raena_analytics.om_oos_restock_4_stage2; +create table raena_analytics.om_oos_restock_4_stage2 +as + SELECT sku, + sum(quantity)/count(DISTINCT cast(transaction_date AS date)) AS runrate + FROM raena_analytics.gm_dashboard + WHERE cast(transaction_date AS date)>=cast((CURRENT_DATE-90) AS date) + GROUP BY sku; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_5; + +CREATE TABLE raena_analytics.om_oos_restock_5 AS +SELECT a.*, + b.sold_out_fl, + c.runrate AS "90_days_runrate" +FROM raena_analytics.om_oos_restock_4 a +INNER JOIN raena_analytics.om_oos_restock_4_stage1 b ON a.sku=b.sku +AND a.warehouse=b.warehouse +LEFT JOIN raena_analytics.om_oos_restock_4_stage2 c ON a.sku=c.sku; + + +DROP TABLE IF EXISTS raena_analytics.weight_sku_table; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_oos_restock_5_stage1; + +CREATE TABLE raena_analytics.om_oos_restock_5_stage1 AS + SELECT sku, + sum(payment_amount)payment_amount, + sum(quantity)quantity, + sub_order_shipping_id, + sum(shipping_amount)shipping_amount + FROM raena_analytics.rate_sheet_wise_expected_warehouse + WHERE created_date>=(CURRENT_DATE-90) + AND is_campaign='false' + group by 1,4; + + + + +drop table if exists raena_analytics.om_oos_restock_5_stage2; + +CREATE TABLE raena_analytics.om_oos_restock_5_stage2 AS +SELECT DISTINCT A.sku, + A.payment_amount, + A.quantity, + A.sub_order_shipping_id, + A.shipping_amount "Actual Shipping Fee", + DD.Expected_shipping_fee, + jsf.expected_JTR_shipping_fee +FROM + raena_analytics.om_oos_restock_5_stage1 A + LEFT JOIN + (SELECT DISTINCT sub_order_shipping_id, + Expected_shipping_fee + FROM raena_analytics.rate_sheet_wise_expected_warehouse_expected_shipping_final + WHERE Expected_shipping_fee IS NOT NULL ) DD ON A.sub_order_shipping_id=DD.sub_order_shipping_id + LEFT join + (SELECT sub_order_shipping_id , + expected_JTR_shipping_fee + FROM raena_analytics.om_shipment_jtr_shipping_fee_base_11 + WHERE expected_JTR_shipping_fee IS NOT NULL) jsf ON A.sub_order_shipping_id=jsf.sub_order_shipping_id; + + DROP TABLE IF EXISTS raena_analytics.om_shipping_fee_sku_base_stage2; + + +CREATE TABLE raena_analytics.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, + payment_amount, + quantity, + sub_order_shipping_id, + coalesce(expected_JTR_shipping_fee,Expected_shipping_fee,"Actual Shipping Fee") AS Expected_shipping_fee, + "Actual Shipping Fee" + from raena_analytics.om_oos_restock_5_stage2) A +LEFT JOIN raena_analytics.weight_sku_table B ON A.sku = B.sku ; + +DROP TABLE IF EXISTS raena_analytics.om_shipping_fee_sku_base_stage3; + + +CREATE TABLE raena_analytics.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 raena_analytics.om_shipping_fee_sku_base_stage2; + +DROP TABLE IF EXISTS raena_analytics.om_oos_restock_6; + +CREATE TABLE raena_analytics.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, + "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 raena_analytics.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 raena_analytics.om_oos_restock_5 + WHERE sold_out_fl NOT LIKE '%stocked' + GROUP BY 1, + 2) b ON a.sku=b.sku +LEFT JOIN raena_analytics.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, + (payment_amount/quantity) AS payment_amount, + (new_shipment_amount/quantity) AS "Actual Shipping Fee", (new_expected_shipment_amount/quantity) AS new_expected_shipment_amount + FROM raena_analytics.om_shipping_fee_sku_base_stage3 + )A) B + WHERE payment_amount!=0 + GROUP BY 1) d ON a.sku=d.sku; + + + diff --git a/redshift/sql_code/payment_dashboard.sql b/redshift/sql_code/payment_dashboard.sql new file mode 100644 index 0000000..9ed8c31 --- /dev/null +++ b/redshift/sql_code/payment_dashboard.sql @@ -0,0 +1,135 @@ + +DROP TABLE IF EXISTS raena_analytics.google_map_search; + + +CREATE TABLE raena_analytics.google_map_search AS +SELECT * +FROM raena_crawler_management.google_map_search_results +WHERE created_at > '2024-04-19 00:00:00.000' + AND rating_category IN ('Cosmetics wholesaler', + 'Beauty school', + 'Cosmetics industry', + 'Beauty supply store', + 'Beauty product supplier', + 'Dermatologist', + 'Make-up artist', + 'Health and beauty shop', + 'Nail salon', + 'Beauty salon', + 'beautification saloon', + 'Hair salon', + 'Skin care clinic', + 'General store', + 'Beauty supplies store', + 'Beauty products wholesaler', + 'Beautician', + 'Cosmetics and perfumes supplier', + 'Facial spa', + 'Perfume store', + 'Cosmetic products manufacturer', + 'Cosmetics store', + 'Spa and health club', + 'Shop', + 'Wellness center'); + + + + +DROP TABLE IF EXISTS raena_analytics.user_type_table_payment ; + + +CREATE TABLE raena_analytics.user_type_table_payment AS +SELECT DISTINCT reseller_mobile, + reseller_id, + First_transaction_date, + created_at first_install_date +FROM + (SELECT replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') reseller_mobile, + reseller_id, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' + GROUP BY 1, + 2) A +INNER JOIN + (SELECT replace(mobile,'+','') profile_phone, + id, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1, + 2) B ON A.reseller_mobile=B.profile_phone; + + +DROP TABLE raena_analytics.Payment_category_filter_payment; + + +CREATE TABLE raena_analytics.Payment_category_filter_payment AS +SELECT PAYMENT_ID, + order_placed_by, + New_existing_flag, + CASE + WHEN payment_channel='INVOICE' + AND payment_provider='DurianPay' + AND PORDER_id LIKE '%DSF' THEN 'Invoice_DP_DSF' + WHEN payment_channel='INVOICE' + AND payment_provider='DurianPay' + AND order_placed_by='admin' THEN 'Invoice_DP_Admin' + WHEN payment_channel='INVOICE' + AND payment_provider='Xendit' THEN 'Inovice - Xendit' + WHEN payment_channel='INVOICE' + AND payment_provider='PrepaidPayment' THEN 'Inovice - PrepaidPayment' + ELSE payment_channel + END AS payment_category_filter +FROM + (SELECT a.*, + b.order_placed_by, + b.New_existing_flag + FROM (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 PAYMENT_ID, + created_at::date 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 porder_id, + payment_provider, + payment_category, + payment_channel + FROM NS + INNER JOIN raena_order_management.payment B ON NS.n <= REGEXP_COUNT(B.order_ids , ',') + 1) a + LEFT JOIN + (SELECT DISTINCT id, + order_placed_by, + New_existing_flag + FROM + (SELECT id, + order_placed_by, + CASE WHEN date_trunc('Month',o.created_at)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',o.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' WHEN date_trunc('Month',o.created_at)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',o.created_at)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' WHEN date_trunc('Month',o.created_at)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',o.created_at)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' ELSE 'Na' END AS New_existing_flag + FROM RAENA_ORDER_MANAGEMENT.order o + LEFT JOIN raena_analytics.user_type_table_payment utt ON o.reseller_id = utt.reseller_id) c) b ON a.porder_id=b.id) C; + + diff --git a/redshift/sql_code/product_matrics_etl.sql b/redshift/sql_code/product_matrics_etl.sql new file mode 100644 index 0000000..9a8b242 --- /dev/null +++ b/redshift/sql_code/product_matrics_etl.sql @@ -0,0 +1,1865 @@ + + +drop table raena_analytics.daily_recommendation_ingredients_split; + + create table raena_analytics.daily_recommendation_ingredients_split + as + select reseller_email ,sku,replace(replace(replace(ingredients,'{',''),'}',''),'"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_ingredients; + +drop table raena_analytics.daily_recommendation_concerns_split; + + create table raena_analytics.daily_recommendation_concerns_split + as + select reseller_email ,sku,replace(replace(replace(Concerns ,'{',''),'}',''),'"','') Ingredients,score,after_discount + from raena_recommendation_engine.daily_recommendation_concerns drc ; + + +DROP TABLE raena_analytics.product_metrics_calendar; + +CREATE TABLE raena_analytics.product_metrics_calendar AS +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type, + '<=2M'bucket +FROM raena_order_management.order +union +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type, + '>10M'bucket +FROM raena_order_management.order +union +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type, + '2M-10M'bucket +FROM raena_order_management.order +union +SELECT DISTINCT CREATED_AT::date transaction_date , + 'New' user_type, + 'Na'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type, + '<=2M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type, + '>10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type, + '2M-10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Activation' user_type, + 'Na'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type, + '<=2M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type, + '>10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type, + '2M-10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Existing' user_type, + 'Na'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type, + '<=2M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type, + '>10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type, + '2M-10M'bucket +FROM raena_order_management.order +UNION +SELECT DISTINCT CREATED_AT::date transaction_date , + 'Na' user_type, + 'Na'bucket +FROM raena_order_management.order; + +DROP TABLE raena_analytics.cohort_level_reseller; + + +CREATE TABLE raena_analytics.cohort_level_reseller AS +SELECT reseller_id , + CASE + WHEN max_payment_amount BETWEEN 0 AND 2000000 THEN '<=2M' + WHEN max_payment_amount BETWEEN 2000000 AND 10000000 THEN '2M-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',created_at)::date trnsaction_mount, + sum(payment_amount)payment_amount + FROM raena_order_management.order + GROUP BY 1, + 2) A + GROUP BY 1) B ; + + +drop table if exists raena_analytics.order_base_for_user_type; + +create table raena_analytics.order_base_for_user_type +as +SELECT replace(json_extract_path_text(reseller_info,'mobile',true),'+','') reseller_mobile, + min(created_at) First_transaction_date + FROM raena_order_management.order + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1; + +DROP TABLE IF EXISTS raena_analytics.user_type_table ; + + +CREATE TABLE raena_analytics.user_type_table AS +SELECT DISTINCT profile_phone reseller_mobile, + First_transaction_date, + min_created_date first_install_date +FROM raena_analytics.order_base_for_user_type A +INNER JOIN + (SELECT profile_phone, + min(created_at) min_created_date + FROM + (SELECT profile_phone, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) created_at + FROM clevertap.app_installed + GROUP BY profile_phone + UNION + SELECT replace(mobile,'+','') profile_phone, + MIN(cast(created_at AS date)) AS created_at + FROM raena_user_management.user u + GROUP BY 1) A + GROUP BY 1) B ON A.reseller_mobile=B.profile_phone; + + +DROP TABLE IF EXISTS raena_analytics.total_registered_user; + + +CREATE TABLE raena_analytics.total_registered_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + bucket, + transaction_date, + Count(DISTINCT id) Total_users +FROM (SELECT distinct id ,replace(mobile,'+','') mobile, + created_at::date transaction_date + FROM raena_user_management.user) A +LEFT JOIN raena_analytics.user_type_table B ON A.mobile = B.reseller_mobile +left join raena_analytics.cohort_level_reseller on A.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2,3; + + + +DROP TABLE IF EXISTS raena_analytics.total_uninstalled; + + +CREATE TABLE raena_analytics.total_uninstalled AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + bucket, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM (SELECT profile_objectid, + CASE + WHEN profile_phone<>'' THEN profile_phone + END profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_uninstalled) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_user_management.user C on A.profile_phone = replace(C.mobile,'+','') +left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2,3; + + +DROP TABLE IF EXISTS raena_analytics.total_installed; + + +CREATE TABLE raena_analytics.total_installed AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + bucket, + count(1) total_event , + Count(DISTINCT A.profile_phone) Total_users +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_user_management.user C on A.profile_phone = replace(C.mobile,'+','') +left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2,3; + + DROP TABLE raena_analytics.Active_user; + + +CREATE TABLE raena_analytics.Active_user AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',coalesce(first_install_date,D.created_at::date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND (date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date + OR First_transaction_date IS NULL) THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND first_transaction_date IS NULL THEN 'New' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',first_install_date)::date IS NULL + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + ELSE 'Na' + END user_type, + transaction_date, + count(distinct profile_phone) total_event , + A.profile_phone, + A.profile_objectid, + bucket +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.order_base_for_user_type B ON A.profile_phone = B.reseller_mobile +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid +LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +left join raena_analytics.cohort_level_reseller on D.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8,9; + +DROP TABLE IF EXISTS raena_analytics.total_view_item; + + +CREATE TABLE raena_analytics.total_view_item AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + bucket, + count(distinct profile_objectid) total_event, + profile_phone +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_user_management.user C on A.profile_phone = replace(C.mobile,'+','') +left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2, + 3,5; + + + +DROP TABLE IF EXISTS raena_analytics.total_checkout; + + +CREATE TABLE raena_analytics.total_checkout AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + bucket, + count(distinct profile_objectid) total_event, + profile_phone +FROM + (SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + eventname, + 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) A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_user_management.user C on A.profile_phone = replace(C.mobile,'+','') +left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2, + 3,5; + + +drop table if exists raena_analytics.order_base_user_transaction; + +create table raena_analytics.order_base_user_transaction +as +SELECT reseller_id , + replace(reseller_mobile,'+','') profile_phone, + transaction_date::date transaction_date, + count(DISTINCT external_id ) total_orders, + sum(quantity*discounted_price) AS revenue, + sum((retail_price*quantity)-(seller_margin*quantity)) as Pre_discount_revenue, + sum(quantity) total_quantity + FROM raena_analytics.gm_dashboard + WHERE is_campaign = 'false' + AND order_placed_by ='App' + GROUP BY 1, + 2, + 3; + + +DROP TABLE IF EXISTS raena_analytics.total_first_transacting_user; + + +CREATE TABLE raena_analytics.total_first_transacting_user AS +SELECT case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + transaction_date, + profile_phone, + bucket, + count(1) total_event , + Count(DISTINCT A.reseller_id) Total_users, + sum(total_orders) total_orders, + sum(revenue) revenue, + sum(Pre_discount_revenue)Pre_discount_revenue, + sum(total_quantity) total_quantity +FROM raena_analytics.order_base_user_transaction A +LEFT JOIN raena_analytics.user_type_table B ON A.profile_phone = B.reseller_mobile +left join raena_analytics.cohort_level_reseller on A.reseller_id = cohort_level_reseller.reseller_id +GROUP BY 1, + 2, + 3,4; + + +drop table if exists raena_analytics.base_matrics_table; + +CREATE TABLE raena_analytics.base_matrics_table AS +SELECT A.transaction_date, + A.user_type, + A.bucket, + A.install , + B.register, + avg_dau, + avg_wau, + avg_mau, + pdp_unique, + add_to_cart, + checkout, + transacting_user, + gmv, + uninstall +FROM + (SELECT date_trunc('Month',transaction_date)::date transaction_date , + user_type, + bucket, + Sum(Total_users)install + FROM raena_analytics.total_installed + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + Sum(total_users) register + FROM raena_analytics.total_registered_user + GROUP BY 1, + 2, + 3)B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + sum(total_user) /count(DISTINCT transaction_date) avg_dau + FROM + (SELECT transaction_date , + user_type, + bucket , + count(DISTINCT profile_phone) total_user + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A + GROUP BY 1, + 2, + 3) C ON A.transaction_date = C.transaction_date +AND A.user_type = C.user_type +AND A.bucket = C.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + sum(total_user) /count(DISTINCT transaction_date) avg_wau + FROM + (SELECT date_trunc('Week',transaction_date)::date transaction_date, + user_type, + bucket , + count(DISTINCT profile_phone) total_user + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) + GROUP BY 1, + 2, + 3) D ON A.transaction_date = D.transaction_date +AND A.user_type = D.user_type +AND A.bucket = D.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket , + count(DISTINCT profile_phone) avg_mau + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) E ON A.transaction_date = E.transaction_date +AND A.user_type = E.user_type +AND A.bucket = E.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + count(distinct profile_phone) pdp_unique + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) F ON A.transaction_date = F.transaction_date +AND A.user_type = F.user_type +AND A.bucket = F.bucket +LEFT JOIN + (SELECT date_trunc('Month',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) add_to_cart + FROM raena_analytics.OM_Events_user_type + LEFT JOIN raena_user_management.user C ON OM_Events_user_type.phone = replace(C.mobile,'+','') + LEFT JOIN raena_analytics.cohort_level_reseller ON C.id = cohort_level_reseller.reseller_id + WHERE events = 'add_to_cart' + GROUP BY 1, + 2, + 3) G ON A.transaction_date = G.transaction_date +AND A.user_type = G.user_type +AND A.bucket = G.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + count(distinct profile_phone) checkout + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) H ON A.transaction_date = H.transaction_date +AND A.user_type = H.user_type +AND A.bucket = H.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) transacting_user, + sum(Pre_discount_revenue) gmv + FROM raena_analytics.total_first_transacting_user + GROUP BY 1, + 2, + 3) I ON A.transaction_date = I.transaction_date +AND A.user_type = I.user_type +AND A.bucket = I.bucket +LEFT JOIN + (SELECT date_trunc('Month',transaction_date)::date transaction_date , + user_type, + bucket, + Sum(Total_users)uninstall + FROM raena_analytics.total_uninstalled + GROUP BY 1, + 2, + 3) J ON A.transaction_date = J.transaction_date +AND A.user_type = j.user_type +AND A.bucket = j.bucket; + +DROP TABLE IF EXISTS raena_analytics.post_discount_gm; + + +CREATE TABLE raena_analytics.post_discount_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',B.first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',B.First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',B.first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',B.First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + cohort bucket, + sum(discounted_price*quantity) discounted_price, + sum(cogs*quantity) cogs, + count(DISTINCT external_id) AS Number_of_orders +FROM raena_analytics.OM_GM_DB_Product_category A +LEFT JOIN raena_analytics.user_type_table B ON replace(A.reseller_mobile,'+','') =B.reseller_mobile +WHERE is_campaign='false' + AND order_placed_by='App' +GROUP BY 1, + 2,3; + +drop table if exists raena_analytics.Active_user_for_mau_stage; + +CREATE TABLE raena_analytics.Active_user_for_mau_stage AS + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date) transaction_date + FROM clevertap.app_installed + union + SELECT profile_objectid, + profile_phone, + profile_email, + profile_platform, + 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; + + + +DROP TABLE IF EXISTS raena_analytics.Active_user_for_mau; + + +CREATE TABLE raena_analytics.Active_user_for_mau AS +SELECT date_trunc('Month',A.transaction_date)::date event_created_date, + date_trunc('Month',first_install_date)::date install_date, + date_trunc('Month',First_transaction_date)::date first_transaction_date, + CASE + WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',(first_install_date))::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date>=date_trunc('Month',first_install_date)::date + AND (date_trunc('Month',A.transaction_date)::date <= date_trunc('Month',First_transaction_date)::date + OR date_trunc('Month',First_transaction_date)::date IS NULL) THEN 'Activation' + ELSE 'Existing' + END user_type, + transaction_date, + count(1) total_event , + A.profile_phone, + A.profile_objectid +FROM raena_analytics.Active_user_for_mau_stage A +LEFT JOIN + (SELECT profile_objectid profile_objectid, + cast(min(transaction_date) AS DATE) First_transaction_date + FROM + (SELECT profile_objectid, (cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) transaction_date + FROM clevertap.charged) A + GROUP BY 1) B ON A.profile_objectid = B.profile_objectid +LEFT JOIN + (SELECT profile_objectid, + MIN(cast(concat(concat(concat(substring(ts,0,5),'-'),concat(substring(ts,5,2),'-')),substring(ts,7,2)) AS date)) first_install_date + FROM clevertap.app_installed + GROUP BY profile_objectid) C ON A.profile_objectid= C.profile_objectid --LEFT JOIN raena_user_management.user D ON A.profile_phone = replace(D.mobile,'+','') +GROUP BY 1, + 2, + 3, + 4, + 5, + 7, + 8; + + +/* +DROP TABLE IF EXISTS raena_analytics.lm_revenue_gm; + + +CREATE TABLE raena_analytics.lm_revenue_gm AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(CASE WHEN a.external_id = BB.order_id + AND a.sku = BB.sku + AND (CASE WHEN item_type LIKE '%Bundle%' THEN 'Bundle' ELSE 'Product' END)=BB.product_class THEN BB.new_shipment_amount ELSE CC.new_shipment_amount END) shipping_amount, + sum(a.discounted_price*a.quantity) discounted_price +FROM + (SELECT * + FROM raena_analytics.OM_GM_DB_Product_category + WHERE order_placed_by ='reseller' + AND is_campaign='false') A +LEFT JOIN raena_analytics.sku_level_shipping_fee_final BB ON a.external_id = BB.order_id +AND a.sku = BB.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=BB.product_class +LEFT JOIN raena_analytics.sku_level_shipping_fee_old_final CC ON a.external_id = CC.external_id +AND a.sku = CC.sku +AND (CASE + WHEN item_type LIKE '%Bundle%' THEN 'Bundle' + ELSE 'Product' + END)=CC.product_class +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; + +DROP TABLE IF EXISTS raena_analytics.gm_order_count; + + +CREATE TABLE raena_analytics.gm_order_count AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(Customer_order) Customer_order, + sum(Marketplace_order)Marketplace_order, + sum(total_dropship_order) total_dropship_order, + sum(total_order) total_order +FROM + (SELECT transaction_date, + reseller_mobile, + count(DISTINCT CASE WHEN order_recipient= 'Customer' THEN external_id END) AS Customer_order, + count(DISTINCT CASE WHEN order_recipient= 'Marketplace' THEN external_id END) AS Marketplace_order, + count(DISTINCT CASE WHEN order_recipient IN ('Customer', 'Marketplace') THEN external_id END) total_dropship_order, + count(DISTINCT external_id) total_order + FROM OM_GM_DB_Product_category + WHERE is_campaign='false' + AND order_placed_by ='reseller' + GROUP BY 1, + 2) A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile=B.reseller_mobile +GROUP BY 1, + 2; +*/ + +DROP TABLE IF EXISTS raena_analytics.post_discount_dsf_revenue; + + +CREATE TABLE raena_analytics.post_discount_dsf_revenue AS +SELECT transaction_date, + case WHEN date_trunc('Month',A.transaction_date)::date = date_trunc('Month',first_install_date)::date + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30 + AND date_trunc('Month',A.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation' + WHEN date_trunc('Month',A.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30 + AND date_trunc('Month',A.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing' + else 'Na' + END user_type, + sum(payment_amount) discounted_price, + count(DISTINCT id) AS Number_of_orders, + count(DISTINCT reseller_id) AS total_users +FROM + (SELECT reseller_id , + external_id id , + discounted_price*quantity payment_amount , + reseller_mobile, + transaction_date + FROM raena_analytics.gm_dashboard + WHERE is_campaign='false' + AND lower(external_id) LIKE '%dsf') A +LEFT JOIN raena_analytics.user_type_table B ON A.reseller_mobile =B.reseller_mobile +GROUP BY 1, + 2; + +----OM_Events_user_type;created in db_funnel report + + + +select max(transaction_date) uninstalled_td from raena_analytics.total_uninstalled; +select max(transaction_date) installed_td from raena_analytics.total_installed; +select max(transaction_date) launched_td from raena_analytics.Active_user; +select max(transaction_date) view_td from raena_analytics.total_view_item; +select max(transaction_date) gm_td from raena_analytics.lm_revenue_gm; + + + +DROP TABLE IF EXISTS raena_analytics.overall_conversion_trend; + + +CREATE TABLE raena_analytics.overall_conversion_trend AS +SELECT date_trunc('day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('day',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(total_orders) AS decimal(22,4)) total_orders, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + sum(total_orders) total_orders + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +AND A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4, + 5; + + + +DROP TABLE IF EXISTS raena_analytics.average_dau_trend; + + +CREATE TABLE raena_analytics.average_dau_trend AS +SELECT A.transaction_date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('Day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('Day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('Day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4))/Count(distinct transaction_date) mau +FROM + (SELECT date_trunc('Day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.active_user + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS raena_analytics.overall_converting_user_trend; + + +CREATE TABLE raena_analytics.overall_converting_user_trend AS +SELECT date_trunc('Year',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('year',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Quarter',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Month',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Week',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Day',A.transaction_date)::date AS transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(total_users) AS decimal(22,4)) total_user, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) converting_user +FROM + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type, + raena_analytics.product_metrics_calendar.bucket, + count(DISTINCT profile_objectid) MAU + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.active_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.active_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.active_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.active_user.bucket,'Na') + GROUP BY 1, + 2, + 3) A +LEFT JOIN + (SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date) transaction_date, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + count(DISTINCT profile_phone) total_users + FROM raena_analytics.product_metrics_calendar + LEFT JOIN raena_analytics.total_first_transacting_user ON raena_analytics.product_metrics_calendar.transaction_date =raena_analytics.total_first_transacting_user.transaction_date + AND raena_analytics.product_metrics_calendar.user_type =coalesce(raena_analytics.total_first_transacting_user.user_type,'Na') + AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_first_transacting_user.bucket,'Na') + GROUP BY 1, + 2, + 3) B ON A.transaction_date = B.transaction_date +AND A.user_type = B.user_type +and A.bucket = B.bucket +GROUP BY 1, + 2, + 3, + 4,5; + + +DROP TABLE IF EXISTS raena_analytics.arpu_trend; + + +CREATE TABLE raena_analytics.arpu_trend AS +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_year, + 'Day'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + bucket, + sum(total_orders) total_orders, + count(distinct profile_phone) transacting_user, + cast(sum(revenue) AS decimal(22,4)) total_revenue, + cast(sum(total_quantity) as decimal(22,4)) total_quantity, + CASE + WHEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) <>0 THEN cast(count(DISTINCT profile_phone) AS decimal(22,4)) + END arpu +FROM raena_analytics.total_first_transacting_user +GROUP BY 1, + 2, + 3, + 4,5 ; + + + + +DROP TABLE IF EXISTS raena_analytics.pdp_trend; + + +CREATE TABLE raena_analytics.pdp_trend AS +SELECT A.transaction_date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_view_item + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5; + + + +DROP TABLE IF EXISTS raena_analytics.add_to_cart_trend; + + +CREATE TABLE raena_analytics.add_to_cart_trend AS +SELECT A.transaction_date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',ts)::date transaction_date, + new_existing_flag user_type, + bucket, + count(DISTINCT phone) MAU + from raena_analytics.OM_Events_user_type + left join raena_user_management.user C on OM_Events_user_type.phone = replace(C.mobile,'+','') + left join raena_analytics.cohort_level_reseller on C.id = cohort_level_reseller.reseller_id + where events = 'add_to_cart' + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5; + + + +DROP TABLE IF EXISTS raena_analytics.checkout_trend; + + +CREATE TABLE raena_analytics.checkout_trend AS +SELECT A.transaction_date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Day'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Week'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Month'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Quarter'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION ALL +SELECT date_trunc('Year',transaction_date)::date transaction_time, + date_part('Year',A.transaction_date) AS transaction_year, + 'Year'frequency, + A.user_type , + A.bucket, + cast(sum(CASE WHEN MAU<>0 THEN MAU END) AS decimal(22,4)) mau +FROM + (SELECT date_trunc('day',transaction_date)::date transaction_date, + user_type, + bucket, + count(DISTINCT profile_phone) MAU + FROM raena_analytics.total_checkout + GROUP BY 1, + 2, + 3) A +GROUP BY 1, + 2, + 3, + 4, + 5; +DROP TABLE IF EXISTS raena_analytics.install_uninstall_ratio_trend; + + +CREATE TABLE raena_analytics.install_uninstall_ratio_trend AS +SELECT date_trunc('Year',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_year, + 'Year'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Quarter',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Quarter'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Month',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Month'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Week',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Week'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Day',raena_analytics.product_metrics_calendar.transaction_date)::date AS transaction_time, + date_part('Year',raena_analytics.product_metrics_calendar.transaction_date) AS transaction_time, + 'Day'frequency, + raena_analytics.product_metrics_calendar.user_type , + raena_analytics.product_metrics_calendar.bucket , + cast(Sum(coalesce(raena_analytics.total_uninstalled.total_event,0)) AS decimal(22,4)) total_uninstall, + cast(sum(CASE WHEN raena_analytics.total_installed.total_event>0 THEN coalesce(raena_analytics.total_installed.total_event,0) END) AS decimal(22,4)) total_install +FROM raena_analytics.product_metrics_calendar +LEFT JOIN raena_analytics.total_uninstalled ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_uninstalled.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type = coalesce(raena_analytics.total_uninstalled.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket = coalesce(raena_analytics.total_uninstalled.bucket,'Na') +LEFT JOIN raena_analytics.total_installed ON raena_analytics.product_metrics_calendar.Transaction_date = raena_analytics.total_installed.Transaction_date +AND raena_analytics.product_metrics_calendar.User_type =coalesce(raena_analytics.total_installed.user_type,'Na') +AND raena_analytics.product_metrics_calendar.bucket =coalesce(raena_analytics.total_installed.bucket,'Na') +GROUP BY 1, + 2, + 3, + 4,5; + + +DROP TABLE IF EXISTS raena_analytics.mau_trend; + + +CREATE TABLE raena_analytics.mau_trend AS +SELECT date_trunc('Year',transaction_date)::date AS transaction_date, + date_part('Year',transaction_date) AS transaction_time, + 'Year'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Quarter',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Quarter'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Month',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Month'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Week',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Week'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5 +UNION ALL +SELECT date_trunc('Day',transaction_date)::date AS transaction_time, + date_part('Year',transaction_date) AS transaction_time, + 'Day'frequency, + user_type , + bucket, + cast(count(DISTINCT profile_objectid) AS decimal(22,4)) mau +FROM raena_analytics.active_user +GROUP BY 1, + 2, + 3, + 4,5; + + + diff --git a/redshift/sql_code/retention_code_etl.sql b/redshift/sql_code/retention_code_etl.sql new file mode 100644 index 0000000..3883170 --- /dev/null +++ b/redshift/sql_code/retention_code_etl.sql @@ -0,0 +1,442 @@ + + +DROP TABLE IF EXISTS raena_analytics.OM_Reseller_Wise_latest_tier; + + +CREATE TABLE raena_analytics.OM_Reseller_Wise_latest_tier AS +SELECT mobile, + tier from + ( SELECT mobile, + tier, + row_number() over (partition BY mobile ORDER BY created_date DESC) AS R + FROM + ( SELECT DISTINCT replace(json_extract_path_text(o.reseller_info,'mobile',true),'+','') AS mobile, + json_extract_path_text(o.reseller_info,'tierName',true) AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o) A ) B +WHERE R=1 + AND mobile is NOT NULL; + +DROP TABLE IF EXISTS raena_analytics.Retention_new_filters_install; + +CREATE TABLE raena_analytics.Retention_new_filters_install AS +SELECT DISTINCT TO_CHAR(o.created_at, 'Month') AS MONTH, + cast(date_part('year',o.created_at) AS varchar) AS YEAR, + CASE + WHEN shipping_to='Reseller' THEN 'Reseller' + ELSE 'Dropshipper' + END AS customer_type, + order_placed_by, + CASE + WHEN order_placed_by='admin' THEN 'admin' + ELSE marketplace + END AS Sales_channel, + provider, + c.channel_name AS channel_name, + replace(json_extract_path_text(o.reseller_info,'mobile',true),'+','') mobile, + verification_status, + tier +FROM raena_user_management.user u +LEFT JOIN raena_order_management.order o ON replace(json_extract_path_text(o.reseller_info,'mobile',true),'+','')=replace(u.mobile,'+','') +LEFT JOIN raena_analytics.OM_channel c ON cast(c.id AS text)=cast(o.channel_id AS text) +LEFT JOIN + (SELECT mobile, + tier + FROM + (SELECT mobile, + tier, + row_number() over (partition BY mobile + ORDER BY created_date DESC) AS R + FROM + (SELECT DISTINCT replace(json_extract_path_text(o.reseller_info,'mobile',true),'+','') AS mobile, + json_extract_path_text(o.reseller_info,'tierName',true) AS tier, + cast(created_at AS date) AS created_date + FROM raena_order_management.order o)A) B + WHERE R=1 + AND mobile IS NOT NULL) t ON u.mobile=t.mobile; + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION; + + + +CREATE TABLE raena_analytics.DAU_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS conversion_year, + TO_CHAR(base_1.created_date, 'Month') AS conversion_month, + date_part('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(o.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(o.reseller_info,'mobile',true),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') 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(o.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(o.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 o + WHERE payment_status = 'Paid' or id like 'PL%') b + 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 <> '' ) c ) base_3 ON base_1.mobile=base_3.active_user) bucket_final; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_REVENUE_RETENTION; + + +CREATE TABLE raena_analytics.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, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(o.reseller_info,'mobile',true),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') A + GROUP BY 1, + 2)B) C) 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(o.reseller_info,'mobile',true),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + +DROP TABLE IF EXISTS raena_analytics.INSTALLBASE_RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.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, + date_part('YEAR',base.created_date) AS acquisition_year, + TO_CHAR(base.created_date, 'Month') AS acquisition_month, + date_part('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' +) 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(o.reseller_info,'mobile',true),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%')A + GROUP BY 1, + 2)B)C) 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(o.reseller_info,'mobile',true),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1)A) bucket_check ON install_base.active_user=bucket_check.mobile) a; + + + +DROP TABLE IF EXISTS raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT; + + +CREATE TABLE raena_analytics.DAU_RETENTION_BASIS_INSTALL_COHORT AS +SELECT * +FROM + (SELECT install_base.*, + bucket.bucket + FROM + (SELECT DISTINCT a.Active_user, + date_part('YEAR',a.created_date) AS acquisition_year, + TO_CHAR(a.created_date, 'Month') AS acquisition_month, + date_part('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') 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') 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(o.reseller_info,'mobile',true),'+','') AS mobile, + cast(created_at AS Date) created_date, + payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') 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(o.reseller_info,'mobile',true),'+','') AS mobile, + sum(payment_amount) AS payment_amount + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%' + GROUP BY 1) A ) 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 raena_analytics.OMREVENUE_RETENTION; + + +CREATE TABLE raena_analytics.OMREVENUE_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('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(o.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(o.reseller_info,'mobile',true),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') 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(o.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(o.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 o + WHERE payment_status = 'Paid' or id like 'PL%') A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + +DROP TABLE IF EXISTS raena_analytics.RESELLER_RETENTION; + + +CREATE TABLE raena_analytics.RESELLER_RETENTION AS +SELECT * +FROM + (SELECT DISTINCT base_1.mobile, + cast(date_part('YEAR',base_1.created_date) AS varchar) AS acquisition_year, + TO_CHAR(base_1.created_date, 'Month') AS acquisition_month, + date_part('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(o.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(o.reseller_info,'mobile',true),'+','') + ORDER BY cast(created_at AS Date)) created_rnk + FROM raena_order_management.order o + WHERE payment_status = 'Paid' or id like 'PL%') 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(o.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(o.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 o + WHERE payment_status = 'Paid' or id like 'PL%')A + GROUP BY 1, + 2) base_2 ON base_1.mobile=base_2.mobile) bucket_final; + + diff --git a/redshift/sql_code/revenue_leakage_etl.sql b/redshift/sql_code/revenue_leakage_etl.sql new file mode 100644 index 0000000..c3c7f4b --- /dev/null +++ b/redshift/sql_code/revenue_leakage_etl.sql @@ -0,0 +1,621 @@ + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage1; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage1 AS +SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at, + payment_amount, + payment_status_sort, + quantity +FROM + (SELECT *, + row_number() over (partition BY reseller_id,sku,payment_status,cast(created_at AS date) + ORDER BY created_at,payment_status_sort,payment_amount DESC) AS R + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.payment_price AS payment_amount, + CASE WHEN payment_status='Paid' THEN 1 WHEN payment_status='Expired' THEN 2 ELSE 3 END AS payment_status_sort, + oi.quantity + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status IN ('Expired', + 'Failed') + AND o.status='Order_Placed' + AND cast(o.created_at AS date) >='2022-01-01') inn) m +WHERE R=1 ; + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage2; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage2 AS +SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.quantity, + oi.payment_price AS payment_amount +FROM + (SELECT id, + order_placed_by , + reseller_id, + customer_id, + payment_status, + created_at + FROM RAENA_ORDER_MANAGEMENT.order + WHERE payment_status='Paid' + AND cast(created_at AS date) >='2022-01-01') o +INNER JOIN + (SELECT order_id, + sku, + quantity, + payment_price + FROM RAENA_ORDER_MANAGEMENT.order_item + WHERE cast(created_at AS date) >='2022-01-01') oi ON o.id=oi.order_id; + + +DROP TABLE IF EXISTS raena_analytics.om_revenue_leakage_qty_match; + + +CREATE TABLE raena_analytics.om_revenue_leakage_qty_match AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at AS date) AS concat1 +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_amount AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + LEFT JOIN raena_analytics.revenue_leakage_new_stage2 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND a.quantity=b.quantity + AND a.customer_id IS NULL + AND b.customer_id IS NULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS' + WHERE a.customer_id IS NULL) c +WHERE b_reseller_id IS NULL +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount) AS payment_amount, + a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at AS date) +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_amount AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + LEFT JOIN raena_analytics.revenue_leakage_new_stage2 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND a.quantity=b.quantity + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS' + WHERE a.customer_id IS NOT NULL) c +WHERE b_reseller_id IS NULL; + + + +DROP TABLE IF EXISTS raena_analytics.revenue_leakage_new_stage3; + + +CREATE TABLE raena_analytics.revenue_leakage_new_stage3 AS +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price +FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >='2022-01-01' + AND customer_id IS NOT NULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist +INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT.order o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >='2022-01-01' + AND customer_id IS NOT NULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by +AND ist.reseller_id=nxt.reseller_id +AND ist.customer_id=nxt.customer_id +AND ist.sku=nxt.sku +AND ist.created_at=cast(nxt.created_at AS date); + + +DROP TABLE IF EXISTS public.om_revenue_leakage_qty_not_match; + + +CREATE TABLE public.om_revenue_leakage_qty_not_match AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount)-(b_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at AS date) AS concat2 +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + INNER JOIN raena_analytics.revenue_leakage_new_stage3 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_amount>b.payment_price + AND a.customer_id ISNULL + AND b.customer_id ISNULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_sku, + a_payment_status, + a_created_at, + (a_payment_amount)-(b_payment_amount) AS revenue_leakage, + a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at AS date) +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_amount AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + FROM raena_analytics.revenue_leakage_new_stage1 a + INNER JOIN raena_analytics.revenue_leakage_new_stage3 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_amount>b.payment_price + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +ORDER BY 2, + 4; + + + +drop table if exists raena_analytics.revenue_leakage_new_stage4; + +create table raena_analytics.revenue_leakage_new_stage4 +as +SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at, + payment_price, + payment_status_sort, + quantity + FROM + (SELECT *, + row_number() over (partition BY reseller_id,sku,payment_status,cast(created_at AS date) + ORDER BY created_at,payment_status_sort,payment_price DESC) AS R + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + oi.payment_price, + CASE WHEN payment_status='Paid' THEN 1 WHEN payment_status='Expired' THEN 2 ELSE 3 END AS payment_status_sort, + oi.quantity + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status!='Paid' + AND payment_status!='Pending' + AND o.status='Order_Placed' + AND cast(o.created_at AS date) >= '2022-01-01') inn) m + WHERE R=1; + +drop table if exists raena_analytics.revenue_leakage_new_stage5; + +create table raena_analytics.revenue_leakage_new_stage5 +as +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id ISNULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist + INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id ISNULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by + AND ist.reseller_id=nxt.reseller_id + AND ist.sku=nxt.sku + AND ist.created_at=cast(nxt.created_at AS date); + + +drop table raena_analytics.revenue_leakage_new_stage6; + +create table raena_analytics.revenue_leakage_new_stage6 +as +SELECT ist.order_placed_by, + ist.reseller_id, + ist.customer_id, + ist.sku, + ist.payment_status, + nxt.created_at, + ist.quantity, + ist.payment_price + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + cast(o.created_at AS date) AS created_at, + sum(oi.quantity) AS quantity, + sum(oi.payment_price) AS payment_price + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id NOTNULL + GROUP BY 1, + 2, + 3, + 4, + 5, + 6) ist + INNER JOIN + (SELECT order_placed_by, + reseller_id, + customer_id, + sku, + payment_status, + created_at + FROM + (SELECT DISTINCT o.order_placed_by, + o.reseller_id, + o.customer_id, + sku, + payment_status, + o.created_at, + row_number() over (partition BY o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at AS date) + ORDER BY o.created_at DESC) AS R + FROM RAENA_ORDER_MANAGEMENT. + ORDER o + INNER JOIN RAENA_ORDER_MANAGEMENT.order_item oi ON o.id=oi.order_id + WHERE o.payment_status='Paid' + AND cast(o.created_at AS date) >= '2022-01-01' + AND customer_id NOTNULL) a + WHERE a.R=1) nxt ON ist.order_placed_by=nxt.order_placed_by + AND ist.reseller_id=nxt.reseller_id + AND ist.customer_id=nxt.customer_id + AND ist.sku=nxt.sku + AND ist.created_at=cast(nxt.created_at AS date); + + +DROP TABLE IF EXISTS raena_analytics.OM_Revenue_leakage; + + +CREATE TABLE raena_analytics.OM_Revenue_leakage AS +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(revenue_leakage) AS revenue_leakage +FROM + (SELECT * + FROM + (SELECT qm.*, + qnm.concat2 + FROM raena_analytics.om_revenue_leakage_qty_match qm + LEFT JOIN raena_analytics.om_revenue_leakage_qty_not_match qnm ON qm.concat1=qnm.concat2) n + WHERE concat2 ISNULL) s +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(a_payment_amount)-sum(b_payment_amount) AS revenue_leakage +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_price AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + from raena_analytics.revenue_leakage_new_stage4 a + INNER join raena_analytics.revenue_leakage_new_stage5 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_price>b.payment_price + AND a.customer_id ISNULL + AND b.customer_id ISNULL + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +GROUP BY 1, + 2, + 3, + 4, + 5 +UNION +SELECT DISTINCT a_order_placed_by, + a_reseller_id, + a_customer_id, + a_payment_status, + a_created_at, + sum(a_payment_amount)-sum(b_payment_amount) AS revenue_leakage +FROM + (SELECT a.order_placed_by AS a_order_placed_by, + a.reseller_id AS a_reseller_id, + a.customer_id AS a_customer_id, + a.sku AS a_sku, + a.payment_status AS a_payment_status, + a.created_at AS a_created_at, + a.quantity AS a_quantity, + a.payment_price AS a_payment_amount, + b.order_placed_by AS b_order_placed_by, + b.reseller_id AS b_reseller_id, + b.customer_id AS b_customer_id, + b.sku AS b_sku, + b.payment_status AS b_payment_status, + b.created_at AS b_created_at, + b.quantity AS b_quantity, + b.payment_price AS b_payment_amount + from raena_analytics.revenue_leakage_new_stage4 a + INNER JOIN raena_analytics.revenue_leakage_new_stage6 b ON a.order_placed_by=b.order_placed_by + AND a.reseller_id=b.reseller_id + AND a.sku=b.sku + AND b.quantity < a.quantity + AND a.payment_price>b.payment_price + AND a.customer_id = b.customer_id + AND b.created_at BETWEEN a.created_at AND a.created_at + INTERVAL '24 HOURS') c +GROUP BY 1, + 2, + 3, + 4, + 5 +ORDER BY 2, + 4; + + DROP TABLE IF EXISTS raena_analytics.OM_Revenue_leakage_dump; + + +CREATE TABLE raena_analytics.OM_Revenue_leakage_dump AS +SELECT * +FROM raena_analytics.om_revenue_leakage_qty_match +UNION +SELECT * +FROM raena_analytics.om_revenue_leakage_qty_not_match; + +DROP TABLE IF EXISTS raena_analytics.om_conversion_order_data_1; + + +CREATE TABLE raena_analytics.om_conversion_order_data_1 AS +SELECT DISTINCT cast(created_at + interval '7 Hours' AS date) AS created_date, + date_part('year',created_at + interval '7 Hours') AS order_year, + date_part('month',created_at + interval '7 Hours') AS order_month, + To_char(created_at + interval '7 Hours','month') Month_name, + 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 + interval '7 Hours'),date_part('month',created_at + interval '7 Hours')) AS R +FROM raena_order_management.ORDER a +LEFT 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 + interval '7 Hours') AS order_year, + date_part('month',created_at + interval '7 Hours') AS order_month, + reseller_id , + sum(payment_amount) AS revenue + FROM raena_order_management.ORDER + WHERE (payment_status='Paid' or id like 'PL%') + AND cast(is_archived AS varchar)='false' + GROUP BY date_part('year',created_at + interval '7 Hours'), + date_part('month',created_at + interval '7 Hours'), + reseller_id ) AA + WHERE reseller_id is not NULL) b ON a.reseller_id=b.reseller_id +AND date_part('year',a.created_at + interval '7 Hours') =b.order_year +AND date_part('month',a.created_at + interval '7 Hours')=b.order_month +LEFT 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' or id like 'PL%') + AND cast(is_archived AS varchar)='false' + GROUP BY reseller_id ) BB + WHERE reseller_id is not NULL) c ON a.reseller_id=c.reseller_id +WHERE (payment_status='Paid' or id like 'PL%') + AND cast(is_archived AS varchar)='false' + AND cast(is_campaign AS varchar)='false' +ORDER BY 1, + 3; + + + diff --git a/redshift/sql_code/sold_product.sql b/redshift/sql_code/sold_product.sql new file mode 100644 index 0000000..30eb080 --- /dev/null +++ b/redshift/sql_code/sold_product.sql @@ -0,0 +1,105 @@ + + +drop table if exists raena_analytics.monthly_sold_product; + +create table raena_analytics.monthly_sold_product +as +select brand_name , brand_page_url , + sum(product_sold) product_sold +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id +where product_sold is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url +order by sum(product_sold) desc; + + +drop table if exists raena_analytics.avg_sold_product; + +create table raena_analytics.avg_sold_product +as +select distinct brand_name , brand_page_url , + avg(ratings) avg_ratings +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id +where ratings is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url +order by avg(ratings) desc; + +drop table if exists raena_analytics.total_sold_product; + +create table raena_analytics.total_sold_product +as +select brand_name , brand_page_url , + rceb.rce_source_id, + sum(product_sold_total) product_sold_total, + sum(product_sold) product_sold, + avg(ratings) avg_ratings, + count(distinct rcers.id) number_of_reseller_store_selling, + CASE + WHEN LOWER(brand_name) = LOWER(b.name) then true + ELSE false + END ss_present_in_Raena, + b.name raena_brand_name, + number_of_products_in_raena +from +( + select * + from raena_spider_management.rce_brand +) rceb +left join +( + select * + from raena_spider_management.rce_product +) rcep on rcep.rce_brand_id = rceb.id and rceb.rce_source_id= rcep.rce_source_id +left join +( + select A.name , count(distinct B.sku) number_of_products_in_raena + from raena_catalog_management.brand A + left join raena_catalog_management.product B on A.id = B.brand_id + group by 1 +) b on LOWER(b.name) = LOWER(rceb.brand_name) +left join +( + select * + from raena_spider_management.rce_reseller_store +) rcers on rcers.id = rcep.rce_store_id +where product_sold_total is not null and brand_name is not null and brand_name != '' +group by brand_name, brand_page_url,rceb.rce_source_id,CASE + WHEN LOWER(brand_name) = LOWER(b.name) then true + ELSE false + end,b.name,number_of_products_in_raena +order by sum(product_sold_total) desc; + +drop table if exists raena_analytics.inventory_snapshot_dashboard_table ; + +create table raena_analytics.inventory_snapshot_dashboard_table +as +SELECT snp_date, sku_code,/*case WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' end*/ + warehouse_code warehouse , min(inventory) inventory_remaining +FROM raena_analytics.inventory_snapshot +GROUP BY 1,2,3 +ORDER BY 1,2,3; + + + diff --git a/redshift/sql_code/whatsapp_campaign.sql b/redshift/sql_code/whatsapp_campaign.sql new file mode 100644 index 0000000..0e019a9 --- /dev/null +++ b/redshift/sql_code/whatsapp_campaign.sql @@ -0,0 +1,497 @@ + + +/* +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 , + json_extract_path_text(phone,event_props,'CT Session Id',true) sessionid , +json_extract_path_text(event_props,'item_id',true) item_id , +json_extract_path_text(event_props,'isGuestUser',true) isGuestUser , +json_extract_path_text(event_props,'CT Source',true) Source , +json_extract_path_text(event_props,'id',true) id , +json_extract_path_text(event_props,'brandName',true) brandName , +json_extract_path_text(event_props,'item_name',true) item_name , +json_extract_path_text(event_props,'offenderItems',true) offenderItems , +json_extract_path_text(event_props,'items',true) items , +json_extract_path_text(event_props,'search_term',true) search_term , +json_extract_path_text(event_props,'screen',true) 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 rows unbounded preceding) 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 ; + + diff --git a/redshift/whatsapp_notification.sh b/redshift/whatsapp_notification.sh new file mode 100644 index 0000000..58f4440 --- /dev/null +++ b/redshift/whatsapp_notification.sh @@ -0,0 +1,516 @@ + #!/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 , + json_extract_path_text(phone,event_props,'CT Session Id',true) sessionid , +json_extract_path_text(event_props,'item_id',true) item_id , +json_extract_path_text(event_props,'isGuestUser',true) isGuestUser , +json_extract_path_text(event_props,'CT Source',true) Source , +json_extract_path_text(event_props,'id',true) id , +json_extract_path_text(event_props,'brandName',true) brandName , +json_extract_path_text(event_props,'item_name',true) item_name , +json_extract_path_text(event_props,'offenderItems',true) offenderItems , +json_extract_path_text(event_props,'items',true) items , +json_extract_path_text(event_props,'search_term',true) search_term , +json_extract_path_text(event_props,'screen',true) 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 rows unbounded preceding) 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/redshift/sql_code/whatsapp_campaign.sql + +psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/whatsapp_campaign.sql + + diff --git a/retention.log b/retention.log new file mode 100644 index 0000000..0a6d71c --- /dev/null +++ b/retention.log @@ -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 diff --git a/retention/etlRetention_Metabase.log b/retention/etlRetention_Metabase.log new file mode 100644 index 0000000..e69de29 diff --git a/retention/ltv_cohorts_retention.sh b/retention/ltv_cohorts_retention.sh new file mode 100644 index 0000000..35e8889 --- /dev/null +++ b/retention/ltv_cohorts_retention.sh @@ -0,0 +1,406 @@ +#!/bin/bash + +echo " + +--------------------------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 + + +" > /home/ec2-user/cronjob/Retention/Retention_Metabase.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/Retention/Retention_Metabase.sql > etlRetention_Metabase.log diff --git a/revenue_leakage.log b/revenue_leakage.log new file mode 100644 index 0000000..9c83159 --- /dev/null +++ b/revenue_leakage.log @@ -0,0 +1,10 @@ +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT diff --git a/revenue_leakage/revenue_leakage.log b/revenue_leakage/revenue_leakage.log new file mode 100644 index 0000000..9c83159 --- /dev/null +++ b/revenue_leakage/revenue_leakage.log @@ -0,0 +1,10 @@ +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT diff --git a/revenue_leakage/revenue_leakage.sql b/revenue_leakage/revenue_leakage.sql new file mode 100644 index 0000000..f737cec --- /dev/null +++ b/revenue_leakage/revenue_leakage.sql @@ -0,0 +1,381 @@ + + +--------------------------Revenue Leakage -------------------------- + +drop table if exists public.om_revenue_leakage_qty_match; + + + +create table public.om_revenue_leakage_qty_match +as +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount) as revenue_leakage, +a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at as date) as concat1 +from ( +select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, +a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_amount as a_payment_amount, +b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, +b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_amount as b_payment_amount +from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_amount,payment_status_sort,quantity + from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_amount desc) as R + from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price as payment_amount, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%' + ) inn + ) m where R=1 +) a +left join +( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.quantity,oi.payment_price as payment_amount + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%' +) b +on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku and a.quantity=b.quantity and a.customer_id isnull and b.customer_id isnull +and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +where a.customer_id isnull +) c +where b_reseller_id isnull +--group by 1,2,3,4,5,6 +union --cusotmer_id notnull cases +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount) as payment_amount, +a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at as date) +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_amount as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_amount as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_amount,payment_status_sort,quantity + from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_amount desc) as R + from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price as payment_amount, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%' + ) inn + ) m where R=1 + ) a + left join + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.quantity,oi.payment_price as payment_amount + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%' + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku and a.quantity=b.quantity and a.customer_id = b.customer_id + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' + where a.customer_id notnull +) c +where b_reseller_id isnull; + + +drop table if exists public.om_revenue_leakage_qty_not_match; + + + + +create table public.om_revenue_leakage_qty_not_match +as +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount)-(b_payment_amount) as revenue_leakage, +a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at as date) as concat2 +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' ----and o.reseller_id='014b731e-e1f6-4d9e-b84e-8715194bc624' --and sku like 'AVS019%' + ) inn + ) m where R=1 + ) a + inner join + ( + select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status, + cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id isnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + group by 1,2,3,4,5,6 + ) ist + inner join + ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at, + row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id isnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + ) a + where a.R=1 + ) nxt + on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id + and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date) + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku + and b.quantity < a.quantity and a.payment_price>b.payment_price + and a.customer_id isnull and b.customer_id isnull + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +) c +--group by 1,2,3,4,5,6 +union --customer-d notnull +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount)-(b_payment_amount) as revenue_leakage, +a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at as date) +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity + from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R + from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' + --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + ) inn + ) m where R=1 + ) a + inner join + ( + select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status, + cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id notnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + group by 1,2,3,4,5,6 + ) ist + inner join + ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at, + row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id notnull + --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + ) a + where a.R=1 + ) nxt + on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id + and ist.customer_id=nxt.customer_id and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date) + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku + and b.quantity < a.quantity and a.payment_price>b.payment_price + and a.customer_id = b.customer_id + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +) c +--group by 1,2,3,4,5,6 +order by 2,4; + + + +drop table if exists OM_Revenue_leakage; + + + +create table OM_Revenue_leakage +as +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(revenue_leakage) as revenue_leakage from ( +select * +from +( +select qm.*,qnm.concat2 +from public.om_revenue_leakage_qty_match qm +left join public.om_revenue_leakage_qty_not_match qnm on qm.concat1=qnm.concat2 +) n where concat2 isnull +) s +group by 1,2,3,4,5 +union +--qty not match +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(a_payment_amount)-sum(b_payment_amount) as revenue_leakage +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status!='Paid' and payment_status!='Pending' and o.status='Order_Placed' and cast(o.created_at as date) >= '2022-01-01' --and o.reseller_id='014b731e-e1f6-4d9e-b84e-8715194bc624' and sku like 'AVS019%' + ) inn + ) m where R=1 + ) a + inner join + ( + select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status, + cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id isnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + group by 1,2,3,4,5,6 + ) ist + inner join + ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at, + row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id isnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + ) a + where a.R=1 + ) nxt + on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id + and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date) + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku + and b.quantity < a.quantity and a.payment_price>b.payment_price + and a.customer_id isnull and b.customer_id isnull + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +) c +group by 1,2,3,4,5 +union --customer-d notnull +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(a_payment_amount)-sum(b_payment_amount) as revenue_leakage +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity + from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R + from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status!='Paid' and payment_status!='Pending' and o.status='Order_Placed' and cast(o.created_at as date) >= '2022-01-01' --and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + ) inn + ) m where R=1 + ) a + inner join + ( + select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status, + cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id notnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + group by 1,2,3,4,5,6 + ) ist + inner join + ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at, + row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id notnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + ) a + where a.R=1 + ) nxt + on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id + and ist.customer_id=nxt.customer_id and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date) + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku + and b.quantity < a.quantity and a.payment_price>b.payment_price + and a.customer_id = b.customer_id + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +) c +group by 1,2,3,4,5 +order by 2,4; + + + + +drop table if exists OM_Revenue_leakage_dump; + + + + +create table OM_Revenue_leakage_dump +as +select * from public.om_revenue_leakage_qty_match +union +select * from public.om_revenue_leakage_qty_not_match; + + +drop table if exists public.om_conversion_order_data_1; + + + + +create table public.om_conversion_order_data_1 +as +select distinct cast(created_at + interval '7 Hours' as date) as created_date,date_part('year',created_at + interval '7 Hours') as order_year, +date_part('month',created_at + interval '7 Hours' ) as order_month,To_char(created_date + interval '7 Hours','month') Month_name, +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 + interval '7 Hours' ),date_part('month',created_at + interval '7 Hours')) as R +from raena_order_management.order a +left 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 + interval '7 Hours' ) as order_year,date_part('month',created_at + interval '7 Hours' ) 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 + interval '7 Hours' ),date_part('month',created_at + interval '7 Hours' ),reseller_id + ) where reseller_id notnull +) b on a.reseller_id=b.reseller_id and date_part('year',a.created_at + interval '7 Hours' ) =b.order_year and date_part('month',a.created_at + interval '7 Hours' )=b.order_month +left 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' and cast(is_campaign as varchar)='false' +order by 1,3; + + + + + + diff --git a/revenue_leakage/revenue_leakage_code.sh b/revenue_leakage/revenue_leakage_code.sh new file mode 100644 index 0000000..95c3f0f --- /dev/null +++ b/revenue_leakage/revenue_leakage_code.sh @@ -0,0 +1,385 @@ +#!/bin/bash + +echo " + +--------------------------Revenue Leakage -------------------------- + +drop table if exists public.om_revenue_leakage_qty_match; + + + +create table public.om_revenue_leakage_qty_match +as +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount) as revenue_leakage, +a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at as date) as concat1 +from ( +select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, +a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_amount as a_payment_amount, +b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, +b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_amount as b_payment_amount +from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_amount,payment_status_sort,quantity + from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_amount desc) as R + from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price as payment_amount, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%' + ) inn + ) m where R=1 +) a +left join +( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.quantity,oi.payment_price as payment_amount + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%' +) b +on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku and a.quantity=b.quantity and a.customer_id isnull and b.customer_id isnull +and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +where a.customer_id isnull +) c +where b_reseller_id isnull +--group by 1,2,3,4,5,6 +union --cusotmer_id notnull cases +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount) as payment_amount, +a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at as date) +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_amount as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_amount as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_amount,payment_status_sort,quantity + from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_amount desc) as R + from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price as payment_amount, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%' + ) inn + ) m where R=1 + ) a + left join + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.quantity,oi.payment_price as payment_amount + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%' + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku and a.quantity=b.quantity and a.customer_id = b.customer_id + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' + where a.customer_id notnull +) c +where b_reseller_id isnull; + + +drop table if exists public.om_revenue_leakage_qty_not_match; + + + + +create table public.om_revenue_leakage_qty_not_match +as +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount)-(b_payment_amount) as revenue_leakage, +a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at as date) as concat2 +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' ----and o.reseller_id='014b731e-e1f6-4d9e-b84e-8715194bc624' --and sku like 'AVS019%' + ) inn + ) m where R=1 + ) a + inner join + ( + select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status, + cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id isnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + group by 1,2,3,4,5,6 + ) ist + inner join + ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at, + row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id isnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + ) a + where a.R=1 + ) nxt + on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id + and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date) + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku + and b.quantity < a.quantity and a.payment_price>b.payment_price + and a.customer_id isnull and b.customer_id isnull + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +) c +--group by 1,2,3,4,5,6 +union --customer-d notnull +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount)-(b_payment_amount) as revenue_leakage, +a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at as date) +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity + from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R + from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' + --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + ) inn + ) m where R=1 + ) a + inner join + ( + select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status, + cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id notnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + group by 1,2,3,4,5,6 + ) ist + inner join + ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at, + row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id notnull + --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'SF005%' + ) a + where a.R=1 + ) nxt + on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id + and ist.customer_id=nxt.customer_id and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date) + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku + and b.quantity < a.quantity and a.payment_price>b.payment_price + and a.customer_id = b.customer_id + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +) c +--group by 1,2,3,4,5,6 +order by 2,4; + + + +drop table if exists OM_Revenue_leakage; + + + +create table OM_Revenue_leakage +as +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(revenue_leakage) as revenue_leakage from ( +select * +from +( +select qm.*,qnm.concat2 +from public.om_revenue_leakage_qty_match qm +left join public.om_revenue_leakage_qty_not_match qnm on qm.concat1=qnm.concat2 +) n where concat2 isnull +) s +group by 1,2,3,4,5 +union +--qty not match +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(a_payment_amount)-sum(b_payment_amount) as revenue_leakage +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status!='Paid' and payment_status!='Pending' and o.status='Order_Placed' and cast(o.created_at as date) >= '2022-01-01' --and o.reseller_id='014b731e-e1f6-4d9e-b84e-8715194bc624' and sku like 'AVS019%' + ) inn + ) m where R=1 + ) a + inner join + ( + select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status, + cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id isnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + group by 1,2,3,4,5,6 + ) ist + inner join + ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at, + row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id isnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + ) a + where a.R=1 + ) nxt + on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id + and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date) + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku + and b.quantity < a.quantity and a.payment_price>b.payment_price + and a.customer_id isnull and b.customer_id isnull + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +) c +group by 1,2,3,4,5 +union --customer-d notnull +select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(a_payment_amount)-sum(b_payment_amount) as revenue_leakage +from ( + select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku, + a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount, + b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku, + b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount + from ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity + from ( + select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R + from ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price, + case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status!='Paid' and payment_status!='Pending' and o.status='Order_Placed' and cast(o.created_at as date) >= '2022-01-01' --and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + ) inn + ) m where R=1 + ) a + inner join + ( + select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status, + cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id notnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + group by 1,2,3,4,5,6 + ) ist + inner join + ( + select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at + from + ( + select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at, + row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R + from RAENA_ORDER_MANAGEMENT.order o + inner join RAENA_ORDER_MANAGEMENT.order_item oi on o.id=oi.order_id + where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id notnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4' and sku like 'SF005%' + ) a + where a.R=1 + ) nxt + on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id + and ist.customer_id=nxt.customer_id and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date) + ) b + on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku + and b.quantity < a.quantity and a.payment_price>b.payment_price + and a.customer_id = b.customer_id + and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS' +) c +group by 1,2,3,4,5 +order by 2,4; + + + + +drop table if exists OM_Revenue_leakage_dump; + + + + +create table OM_Revenue_leakage_dump +as +select * from public.om_revenue_leakage_qty_match +union +select * from public.om_revenue_leakage_qty_not_match; + + +drop table if exists public.om_conversion_order_data_1; + + + + +create table public.om_conversion_order_data_1 +as +select distinct cast(created_at + interval '7 Hours' as date) as created_date,date_part('year',created_at + interval '7 Hours') as order_year, +date_part('month',created_at + interval '7 Hours' ) as order_month,To_char(created_date + interval '7 Hours','month') Month_name, +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 + interval '7 Hours' ),date_part('month',created_at + interval '7 Hours')) as R +from raena_order_management.order a +left 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 + interval '7 Hours' ) as order_year,date_part('month',created_at + interval '7 Hours' ) 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 + interval '7 Hours' ),date_part('month',created_at + interval '7 Hours' ),reseller_id + ) where reseller_id notnull +) b on a.reseller_id=b.reseller_id and date_part('year',a.created_at + interval '7 Hours' ) =b.order_year and date_part('month',a.created_at + interval '7 Hours' )=b.order_month +left 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' and cast(is_campaign as varchar)='false' +order by 1,3; + + + + + + " > /home/ec2-user/cronjob/revenue_leakage/revenue_leakage.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/revenue_leakage/revenue_leakage.sql diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..551e4e8 --- /dev/null +++ b/test.sh @@ -0,0 +1,14 @@ +#!/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 + + diff --git a/warehouseAnalysis/.etlwarehouseAnalysis.sh.swp b/warehouseAnalysis/.etlwarehouseAnalysis.sh.swp new file mode 100644 index 0000000000000000000000000000000000000000..481e37792e3c26a2aadcf5d33cc694ddfcb68fb8 GIT binary patch literal 20480 zcmeHPTZ|-C87{dj$^wEWs3DO`Qa4;XwgVDQ3=7rex{D*|sC^+6XUkeI0WBB*f@^gE|cUAyM8 zyU_)0(`mew-XsrxaW_D#`-Ge5dLu7Ydu6NU({HH+QVDD)fuYmQAH05Xda5wVi{75OjlB0gi`&&o zzo!yNC6G!Wl|U+iR0634QVFCING0%pBmrlzOL++WxkC17S>E3fyZ>8Wntbn_$h~q` z$@g7(|5>d5TeALr@_k-b^y>dhey_>zkHo%TDc|pw_vd5Zb`<3Xynl|LrxE}6&i{XVx1ziOybe4FYybw(2Q=WL zz|FubsC*7M3;Y21KJWQBqEAaNUit=aRH^Ae-1HkRTAKs-XPXh*UKky-7FK`oZ74YkKD$37+p8!t-j{%pUf}H2=qK=5pbZp(-M|GnLH-Dw z1s(?;0$RYmKnA!1cm|G)PXQY6KHwj4yqp8R4SWlD68I`$0tV0r?gp*{Uc(siar&wB zeFVcvVpmNyXrB?}Zmj8Triq7~5-UVXs>Qm@Vg`!not)CF9jjFqNVVQvsL$d@+oU~a zx7m1EHLVV_P$)~hUnHkg=4+*e8d)fnkJZRv*y`zR)lc5gs7&e5Y=N(mxLAQ)~hq&4@-dHlE}wplPJXm zemxmRr4a5Wr!*pAUnDu@SnbGyt017kCxERDH;YLC6lKY<`jTxdH*5!&Z?@rMbxQOthoJ9;YYJ?tHZl1!dJzg=T#}$dNGXvIk zSSPSMZ{5Rej{33};#LvMXg2b$>@Ka*f}3{8r&fa|MeFcSCyv%;v7(B3z+bY)rVrdX zIXOla_{Xs(?NduPI&>_;#^$vT7A6l(d9{zT4mEaK^T5oZgZurObd}mRwRT#0da_Um zm>ycRMK^ZL^HgE~p+jEFO@na>m;!#GpOc(H+x`Oj@HK(XPNbx;ix@xS;y|9t;wl| zRNQ6Qn|aV$kyOwq=i=fhN>lR|cF*zz#j>O*Ol2qRS=3<>O9DFym#g?>bNdh1krv@G z#&<1oO%Kfp?sh9)U^@fi<}+f-aUz#vP04k)x25EIJF=wYsyniyi}f}h1b;~lR5 z`A%j}i?&y*bc1vNx@=}h=8nBf`Mv%8-cBa#QqD7yA@>{k@I|Wp#8aQ;lQWsGePppd zU#pTMjrrrH1rjcp&);u&ta**$H0ZWk+*6QTkG4Tdl}c?62e5m{?Bekf&KX4;(4rlkXz>Bip^So|)r4mRbkV+txKq`S$0;vR2 z38WH8C6G$seg}v>y-f{w7-G9Xm`C|1uhWCq?m{ zF&PxQymplJ6$AuHbJ)_{Kp$i`80f|_f@#Ds9b?$*-KFRT!c*jw32E%a=_Du-lOQla z)kM-oGZKnadaUcX^krDFaa=p*x!Af0^7S;IF?NQoSTU`qyESur(F;HgA z(bjdFX`{~K;FZS!p^D9V@F|lHcL5ssu}Lp;<1Mx3xu3o2T9>gqbi;Iz+->~)Pb9Tm*E58 zv3(230PhEQ62W=+1ik}&5jX?f4G`c~;3fD7&I7*%z5+Z7bbu2;5y${H1HXa;;>W-f zz^8$A-~r$$Fb&)S>;^6%{(m0$Ch$qX0j7Z~fwvL+{~ho{;7fo5oCFR7w*hY<=Ff8j zJ`bz`O<)4J0r(SQ{?7qV0CT`3a5d0H-2Y9)_@4#N05iZF5Zuea3&69$mw|@?8<+$> z09*_F6>;hgzeE${TQJ?}8fL*{pF)qIc&H;Rk#5h6R(&jzK zHh&ydR^LGi=k^db<4-L?D8gPH1`pwMfaE#APZHQB4d9Xt2^j(%%N$7E*1PBtED95> zn_6&5G~p(cq9UY?|41?J+D}KL4);fLe_$5*1>V(y6?R16J?H4mn&rN1gd_A`j`Yoz z-eV+329~)7Gsn+GtP7g6FEF>d* z$s@XT_u)}Zl)@ zC83UFiBmYKy1L0pEbW{El(jR&l5|qWqtKNiXrW7fH$p7%jZV0IvE$MBTZFke1z|!Z z)ia{j1~;NLYPDCWl?7$@yNIrZKJcc3C4D@;Tn0mPF*ZlU!tG5C;pJ3oo5KN;fCfWr zVA@zut8xtdhA`GuKc|Us_Cy42o3Wfqj+_PJpbjS$zx+<2%}bw40;-5p3o=R5OQe)A z?9mA)DC5c_Y3hlWal@ja86_yN`E)c|W@KUtinzSQrl{bvpNqDnib=Vomi?((Y?y+_ z@LU!U7@d<6d*kcT7Ob8gQUi%D;i)=k2iZTX!$i_@21xuCpWhM-MH7pXyBVGR5>j1w z-jSQj2LfE{70LT%t0T8?&))fR>6Q1@`cie;ozEhxAq>}W#ytkJm~MH7k!E(?(}*t> zd)Ob3jZ~JzUPKCeLzeY^_+LlX=(xdbW+d+Wm|<*Y4Bq>gR=G4HN*`I1d@lK#jB)7e zwvAmv-B`m0z_g+on7Bw^^b)-kTB5@X^cMCLSKdG~BTI8wHUHbYO9+c^lDA4X-F`dC zW6XZmoarxm*f~~f&5GDH`r8YOVO8rvS9y-CSZ*3PX8P~2G*-jUt`YHF2zHKq`9$ss zPZ1gG(w^<+ow$1sPjOs5LxDM}cf=kll=I=LQ79cnaP1i?w{M<+;TbV?-1JFl%;QAn zO@`QwrNMCu;+hdX&$`(yWz|k{6YxA2nL$SWLY)!{9&#th6}UCDb%WWq z<_7Nxl3(CF2u)o)f0mtrJBPahuqDE-9iNecA~wcilLPkLP0m(=iZL2p;E^w@+hR(B z&568~ePz{~K;Z0gx8bfyD^f!I79%QL6|(n%Y4C!W(A{*Skfh?fed1?ab6NR=g+2$B X@DLlZK*BP$un|oATz0O;U^M;%@yj!f literal 0 HcmV?d00001 diff --git a/warehouseAnalysis/1 b/warehouseAnalysis/1 new file mode 100644 index 0000000..e35d520 --- /dev/null +++ b/warehouseAnalysis/1 @@ -0,0 +1,23 @@ +" ============================================================================ +" Netrw Directory Listing (netrw v171) +" /home/ec2-user/cronjob/warehouseAnalysis +" Sorted by name +" Sort sequence: [\/]$,\,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$ +" Quick Help: :help -:go up dir D:delete R:rename s:sort-by x:special +" ============================================================================== +../ +./ +OM_New_VS_Existing.sql +OM_Test_Launched.sql +etl_appsflyer.log +etlwarehouseAnalysis.log +etlwarehouseAnalysis_v1.sh +etlwarehouseAnalysis_v2.sh +etlwarehouseAnalysis_v3.sh +etlwarehouseAnalysis_v4.sh +install_uninstall_rate.sql +retention.log +retention.sql +retention_code.sh +warehouseAnalysis.sql +.etlwarehouseAnalysis.sh.swp diff --git a/warehouseAnalysis/OM_New_VS_Existing.sql b/warehouseAnalysis/OM_New_VS_Existing.sql new file mode 100644 index 0000000..222944e --- /dev/null +++ b/warehouseAnalysis/OM_New_VS_Existing.sql @@ -0,0 +1,32 @@ + + +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 + + diff --git a/warehouseAnalysis/OM_Test_Launched.sql b/warehouseAnalysis/OM_Test_Launched.sql new file mode 100644 index 0000000..a98ee50 --- /dev/null +++ b/warehouseAnalysis/OM_Test_Launched.sql @@ -0,0 +1,14 @@ + + +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 + + diff --git a/warehouseAnalysis/etl_appsflyer.log b/warehouseAnalysis/etl_appsflyer.log new file mode 100644 index 0000000..0a6d71c --- /dev/null +++ b/warehouseAnalysis/etl_appsflyer.log @@ -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 diff --git a/warehouseAnalysis/etlwarehouseAnalysis.log b/warehouseAnalysis/etlwarehouseAnalysis.log new file mode 100644 index 0000000..2edd9df --- /dev/null +++ b/warehouseAnalysis/etlwarehouseAnalysis.log @@ -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 +SELECT diff --git a/warehouseAnalysis/etlwarehouseAnalysis_v1.sh b/warehouseAnalysis/etlwarehouseAnalysis_v1.sh new file mode 100644 index 0000000..593910e --- /dev/null +++ b/warehouseAnalysis/etlwarehouseAnalysis_v1.sh @@ -0,0 +1,1005 @@ +!/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"0 day" "+%Y-%m-%d") +echo 'reportDate'=$reportDate + +echo " + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1 ; + + +CREATE TABLE public.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) + SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name) AA +LEFT JOIN + (SELECT DISTINCT min_price, + service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price,service_name,destination_area_id + ORDER BY min_price ,service_name ,destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON AA.service_name = BB.service_name +AND AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3; + +CREATE TABLE public.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + CASE + WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN ( select order_id , sales_sub_order_shipment_id, coalesce( sku ,parent_sku) sku, sum(coalesce(BB.quantity , AA.quantity)) Quantity , + sum(coalesce(BB.payment_amount,AA.payment_amount)) payment_amount from raena_order_management.sales_sub_order AA + left join raena_order_management.sales_sub_order_parent_child BB + ON AA.id = BB.sales_sub_order_id + group by 1,2,3) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' and A.status in ('RETURNED','DELIVERED','PICKED_UP','CREATED'); + +DROP TABLE public.warehouse_analysis_stage3_final; + +CREATE TABLE public.warehouse_analysis_stage3_final AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + B.sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM public.warehouse_analysis_stage3 A +LEFT JOIN gm_dashboard B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM public.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_1; + + +CREATE TABLE public.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + Z.order_placed_by, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.warehouse_analysis_stage3_final A ON Z.id=A.order_id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_2; + + +CREATE TABLE public.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + BB.shipping_cost shipping_amount, + BB.Brand_name AS Brand_name , + shipping_to order_type, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + Z.order_placed_by, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.gm_dashboard BB ON Z.id = BB.external_id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01' + AND BB.external_id NOT IN + (SELECT order_id + FROM public.warehouse_analysis_stage3_final) ; + + ; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3; + + +CREATE TABLE public.warehouse_analysis_stage1_3 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to order_type, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + Z.order_placed_by, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN public.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN public.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage2; + + +CREATE TABLE public.warehouse_analysis_stage2 AS +SELECT A.*, + B.brand_name +FROM public.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + + +/* + +CREATE TABLE public.warehouse_analysis_stage2 AS +SELECT DISTINCT coalesce(A.order_id,Z.id) order_id , + A.reference_id, + service_name , + actual_warehouse, + A.rate_id, + coalesce(A.sku,BB.sku) sku, + coalesce(A.quantity,BB.quantity) quantity, + coalesce(A.shipping_amount,Z.shipping_amount) shipping_amount, + BB.Brand_name AS Brand_name , + shipping_to order_type, + coalesce(A.payment_amount,BB.discounted_price*BB.quantity) payment_amount, + payment_status , + Z.status order_status, + Z.order_placed_by, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN public.warehouse_analysis_stage3 A ON Z.id=case when A.order_id is null then A.reference_id else A.order_id end +LEFT JOIN public.gm_dashboard BB ON Z.reference_id = BB.external_id +WHERE Z.payment_status ='Paid' AND cast(Z.created_at AS date)>='2022-04-01'; +*/ +DROP TABLE IF EXISTS public.shipping_fee_coupon_purpose; + + +CREATE TABLE public.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to order_type, + order_placed_by, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT id AS order_id , + reseller_id, + json_extract_path_text(A.reseller_info,'tierName',TRUE) tierName, + json_extract_path_text(A.reseller_info,'email',TRUE) email, + json_extract_path_text(A.reseller_info,'mobile',TRUE) mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + order_placed_by , + created_at + FROM raena_order_management.order A + WHERE is_archived = 'false' + AND cast(created_at AS date)>='2022-04-01' + ORDER BY created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN + (SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN public.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management. + ORDER + WHERE payment_status= 'Paid' + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse ; + + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + actual_warehouse , + expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + am_name, + am_email, + sum(quantity)quantity, + sum(shipping_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date +FROM + (SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + shipping_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + actual_warehouse , + expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'yes' + ELSE 'No' + END , + service_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) ; + + +DROP TABLE quantity_trend_new; +CREATE TABLE quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT is_campaign , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM public.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + +DROP TABLE quantity_trend; + + +CREATE TABLE quantity_trend AS +SELECT * +FROM quantity_trend_new; + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + actual_warehouse , + expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + am_name, + am_email, + sum(quantity)quantity, + sum(shipping_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM + (SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + shipping_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + order_type, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + actual_warehouse , + expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'yes' + ELSE 'No' + END , + service_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + +DROP TABLE IF EXISTS om_distr_actaul_origin; + + +CREATE TABLE om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse='Samarinda' THEN 36700 + END AS expected_origin +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS om_distr_actaul_origin_2; + + +CREATE TABLE om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id is NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_4; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS public.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE public.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + + --order_id for suborders + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_6; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id); + + ---sku tagging + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight10; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_11; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+insurance_fee) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+insurance_fee) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN public.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id +WHERE Sub_order_shipping_id IS NOT NULL; + +drop table if exists public.om_shipment_jtr_shipping_fee_base_12; +create table public.om_shipment_jtr_shipping_fee_base_12 +as +select b.*,a.expected_eta_min +from public.rate_sheet_wise_expected_warehouse_expected_shipping_final a +inner join public.om_shipment_jtr_shipping_fee_base_11 b on a.Sub_order_shipping_id=b.Sub_order_shipping_id and expected__JTR_min_eta-a.expected_eta_min<=3; + + " > /home/ec2-user/cronjob/warehouseAnalysis/warehouseAnalysis.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 > etlwarehouseAnalysis.log diff --git a/warehouseAnalysis/etlwarehouseAnalysis_v2.sh b/warehouseAnalysis/etlwarehouseAnalysis_v2.sh new file mode 100644 index 0000000..49371cc --- /dev/null +++ b/warehouseAnalysis/etlwarehouseAnalysis_v2.sh @@ -0,0 +1,1190 @@ +!/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.sku_type_warehouse; + +create table public.sku_type_warehouse +as +select distinct sku , sku_type from public.final_basic_calucation_table; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1 ; + + +CREATE TABLE public.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) +SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name) AA +LEFT JOIN + (SELECT DISTINCT min_price, + service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price,service_name,destination_area_id + ORDER BY min_price ,service_name ,destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON AA.service_name = BB.service_name +AND AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; +DROP TABLE IF EXISTS public.warehouse_analysis_stage3; + + +CREATE TABLE public.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + CASE + WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount*C.Quantity payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN + (SELECT AA.order_id , + AA.sales_sub_order_shipment_id, + AA.sku, + CC.Quantity , + CC.discounted_price payment_amount + FROM + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + GROUP BY 1, + 2, + 3) AA + LEFT JOIN + (SELECT DISTINCT external_id , + product_sku , + quantity, + discounted_price + FROM base_netsuite_final) CC ON AA.order_id = CC.external_id + AND AA.sku = CC.product_sku ) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' + AND A.status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED'); + + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3_final_v1; + + +CREATE TABLE public.warehouse_analysis_stage3_final_v1 AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + product_sku sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM public.warehouse_analysis_stage3 A +LEFT JOIN base_netsuite_final B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM public.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3_final; + + +CREATE TABLE public.warehouse_analysis_stage3_final AS +SELECT * +FROM public.warehouse_analysis_stage3_final_v1 +WHERE order_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + AND payment_amount IS NOT NULL +UNION +SELECT A.Order_id, + reference_id, + service_name, + actual_warehouse, + rate_id, + A.sku , + A.quantity, + shipping_amount, + B.payment_price +FROM public.warehouse_analysis_stage3_final_v1 A +INNER JOIN + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity, + sum(coalesce(BB.payment_amount , AA.payment_amount)) payment_price + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + WHERE order_id IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + GROUP BY 1, + 2, + 3) B ON A.order_id = B.order_id +AND A.reference_id= B.sales_sub_order_shipment_id +AND A.sku = B.sku; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_1; + + +CREATE TABLE public.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + Z.order_placed_by, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.warehouse_analysis_stage3_final A ON Z.id=A.order_id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_2; + + +CREATE TABLE public.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + BB.shipping_cost shipping_amount, + BB.Brand_name AS Brand_name , + shipping_to order_type, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + Z.order_placed_by, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.gm_dashboard BB ON Z.id = BB.external_id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01' + AND BB.external_id NOT IN + (SELECT order_id + FROM public.warehouse_analysis_stage3_final) ; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3_v1; + + +CREATE TABLE public.warehouse_analysis_stage1_3_v1 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to order_type, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + Z.order_placed_by, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN public.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN public.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3; + + +CREATE TABLE public.warehouse_analysis_stage1_3 AS +SELECT order_id, + A.reference_id, + service_name, + actual_warehouse, + rate_id, + sku, + quantity, + case when b.shipping_amount is not null then ((b.shipping_amount)/(b.order_weight))*(b.final_weight) else A.shipping_amount end shipping_amount, + order_type, + payment_amount, + A.payment_status, + order_status, + order_placed_by, + created_at, + is_campaign, + created_by +FROM public.warehouse_analysis_stage1_3_v1 A +LEFT JOIN OM_Logistic_final_shipping B ON A.reference_id= B.reference_id; + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage2_v1; + + +CREATE TABLE public.warehouse_analysis_stage2_v1 AS +SELECT A.*, + B.brand_name, + CASE + WHEN (B.Final_weight*A.Quantity)<1.3 THEN 1 + WHEN (B.Final_weight*A.Quantity)>=1.3 + AND (ABS((B.Final_weight*A.Quantity)) - FLOOR(ABS((B.Final_weight*A.Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((B.Final_weight*A.Quantity))+1 + ELSE FLOOR((B.Final_weight*A.Quantity)) + END AS final_weight +FROM public.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage2; + + +CREATE TABLE public.warehouse_analysis_stage2 AS +SELECT *, + (final_weight*shipping_amount)/sum(final_weight) over(partition BY reference_id) new_shipment_amount +FROM public.warehouse_analysis_stage2_v1; + +select count(1) from public.warehouse_analysis_stage2 ; + + +DROP TABLE IF EXISTS public.shipping_fee_coupon_purpose; + + +CREATE TABLE public.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to order_type, + order_placed_by, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT id AS order_id , + reseller_id, + json_extract_path_text(A.reseller_info,'tierName',TRUE) tierName, + json_extract_path_text(A.reseller_info,'email',TRUE) email, + json_extract_path_text(A.reseller_info,'mobile',TRUE) mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + order_placed_by , + created_at + FROM raena_order_management.order A + WHERE is_archived = 'false' + AND cast(created_at AS date)>='2022-04-01' + ORDER BY created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN + (SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN public.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management. + ORDER + WHERE payment_status= 'Paid' + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + +select count(1) from public.shipping_fee_coupon_purpose; + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse ; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date, + DSF_flag, + brand_type +FROM + ( + SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at, + case when A.order_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + CASE + WHEN coalesce(Y.Name,A.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 coalesce(Y.Name,A.Brand_name) IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC') THEN 'EL/PL' + END brand_type + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date), + DSF_flag, + brand_type; + + +update public.rate_sheet_wise_expected_warehouse + set payment_amount =0 + from public.gm_dashboard + where discounted_price= 0 + and public.rate_sheet_wise_expected_warehouse.order_id = public.gm_dashboard.external_id + and public.rate_sheet_wise_expected_warehouse.sku = public.gm_dashboard.sku ; + + + + select count(1) from public.rate_sheet_wise_expected_warehouse ; + +DROP TABLE IF EXISTS quantity_trend_new; + + +CREATE TABLE quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT is_campaign , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM public.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS quantity_trend; + + +CREATE TABLE quantity_trend AS +SELECT * +FROM quantity_trend_new; + + ; + + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM + (SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + order_type, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + order_type, + payment_amount, + payment_status , + order_status, + order_placed_by, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + select count(1) from public.rate_sheet_wise_expected_warehouse_expected_shipping ; + +DROP TABLE IF EXISTS om_distr_actaul_origin; + + +CREATE TABLE om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + B.partner_id, + B.service_id, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse='Samarinda' THEN 36700 + END AS expected_origin +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS om_distr_actaul_origin_2; + + +CREATE TABLE om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +and b.service_id = c.service_id +and b.partner_id = c.partner_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id IS NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_4; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS public.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE public.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_6; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id); + + ---sku tagging + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + + --final dataset +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight=10; + + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_11; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+insurance_fee) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+insurance_fee) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN public.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id and o.status <>'CANCELLED' +WHERE Sub_order_shipping_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_12; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_12 AS +SELECT b.*, + a.expected_eta_min +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping_final a +INNER JOIN public.om_shipment_jtr_shipping_fee_base_11 b ON a.Sub_order_shipping_id=b.Sub_order_shipping_id +AND expected__JTR_min_eta-a.expected_eta_min<=3; + +drop table if exists public.rate_sheet_wise_expected_warehouse_destination_area_id; + +CREATE TABLE public.rate_sheet_wise_expected_warehouse_destination_area_id AS +select distinct order_id , + reference_id Sub_order_shipping_id, + destination_area_id +FROM + (SELECT DISTINCT A.order_id , + reference_id, + destination_area_id + FROM public.warehouse_analysis_stage2 A + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name + WHERE sku IS NOT null + )AA; + + " > /home/ec2-user/cronjob/warehouseAnalysis/warehouseAnalysis.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 + diff --git a/warehouseAnalysis/etlwarehouseAnalysis_v3.sh b/warehouseAnalysis/etlwarehouseAnalysis_v3.sh new file mode 100644 index 0000000..4941512 --- /dev/null +++ b/warehouseAnalysis/etlwarehouseAnalysis_v3.sh @@ -0,0 +1,1253 @@ +!/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.sku_type_warehouse; + +create table public.sku_type_warehouse +as +select distinct sku , sku_type from public.final_basic_calucation_table; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1 ; + + +CREATE TABLE public.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) +SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name) AA +LEFT JOIN + (SELECT DISTINCT min_price, + service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price,service_name,destination_area_id + ORDER BY min_price ,service_name ,destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON AA.service_name = BB.service_name +AND AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; +DROP TABLE IF EXISTS public.warehouse_analysis_stage3; + + +CREATE TABLE public.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + CASE + WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount*C.Quantity payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN + (SELECT AA.order_id , + AA.sales_sub_order_shipment_id, + AA.sku, + CC.Quantity , + CC.discounted_price payment_amount + FROM + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + GROUP BY 1, + 2, + 3) AA + LEFT JOIN + (SELECT DISTINCT external_id , + product_sku , + quantity, + discounted_price + FROM base_netsuite_final) CC ON AA.order_id = CC.external_id + AND AA.sku = CC.product_sku ) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' + AND A.status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED'); + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3_final_v1; + + +CREATE TABLE public.warehouse_analysis_stage3_final_v1 AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + product_sku sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM public.warehouse_analysis_stage3 A +LEFT JOIN base_netsuite_final B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM public.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3_final; + + + +CREATE TABLE public.warehouse_analysis_stage3_final AS +SELECT * +FROM public.warehouse_analysis_stage3_final_v1 +WHERE order_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + AND payment_amount IS NOT NULL +UNION +SELECT A.Order_id, + reference_id, + service_name, + actual_warehouse, + rate_id, + A.sku , + A.quantity, + shipping_amount, + B.payment_price +FROM public.warehouse_analysis_stage3_final_v1 A +INNER JOIN + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity, + sum(coalesce(BB.payment_amount , AA.payment_amount)) payment_price + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + WHERE order_id IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + GROUP BY 1, + 2, + 3) B ON A.order_id = B.order_id +AND A.reference_id= B.sales_sub_order_shipment_id +AND A.sku = B.sku; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_1; + +CREATE TABLE public.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.warehouse_analysis_stage3_final A ON Z.id=A.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_2; + + +CREATE TABLE public.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + BB.shipping_cost shipping_amount, + BB.Brand_name AS Brand_name , + shipping_to shipping_to, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.gm_dashboard BB ON Z.id = BB.external_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01' + AND BB.external_id NOT IN + (SELECT order_id + FROM public.warehouse_analysis_stage3_final) ; + + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3_v1; + + +CREATE TABLE public.warehouse_analysis_stage1_3_v1 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to shipping_to, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN public.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN public.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3; + + +CREATE TABLE public.warehouse_analysis_stage1_3 AS +SELECT order_id, + A.reference_id, + service_name, + actual_warehouse, + rate_id, + sku, + quantity, + case when b.shipping_amount is not null then ((b.shipping_amount)/(b.order_weight))*(b.final_weight) else A.shipping_amount end shipping_amount, + shipping_to, + payment_amount, + A.payment_status, + order_status, + order_placed_by, + order_type, + created_at, + is_campaign, + created_by +FROM public.warehouse_analysis_stage1_3_v1 A +LEFT JOIN OM_Logistic_final_shipping B ON A.reference_id= B.reference_id; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage2_v1; + + +CREATE TABLE public.warehouse_analysis_stage2_v1 AS +SELECT A.*, + B.brand_name, + CASE + WHEN (B.Final_weight*A.Quantity)<1.3 THEN 1 + WHEN (B.Final_weight*A.Quantity)>=1.3 + AND (ABS((B.Final_weight*A.Quantity)) - FLOOR(ABS((B.Final_weight*A.Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((B.Final_weight*A.Quantity))+1 + ELSE FLOOR((B.Final_weight*A.Quantity)) + END AS final_weight +FROM public.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage2; + + +CREATE TABLE public.warehouse_analysis_stage2 AS +SELECT *, + (final_weight*shipping_amount)/sum(final_weight) over(partition BY reference_id) new_shipment_amount +FROM public.warehouse_analysis_stage2_v1; + +select count(1) from public.warehouse_analysis_stage2 ; + + +DROP TABLE IF EXISTS public.shipping_fee_coupon_purpose; + + +CREATE TABLE public.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to shipping_to, + order_placed_by, + order_type, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT A.id AS order_id , + reseller_id, + json_extract_path_text(A.reseller_info,'tierName',TRUE) tierName, + json_extract_path_text(A.reseller_info,'email',TRUE) email, + json_extract_path_text(A.reseller_info,'mobile',TRUE) mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + A.created_at + FROM raena_order_management.order A + left join raena_order_management.channel D on A.channel_id = D.id + WHERE A.is_archived = 'false' + AND cast(A.created_at AS date)>='2022-04-01' + ORDER BY A.created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN +(SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN public.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management. + ORDER + WHERE payment_status= 'Paid' + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + +select count(1) from public.shipping_fee_coupon_purpose; + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse ; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date, + DSF_flag, + brand_type +FROM + ( + SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at, + case when A.order_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + CASE + WHEN coalesce(Y.Name,A.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 coalesce(Y.Name,A.Brand_name) IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC') THEN 'EL/PL' + END brand_type + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date), + DSF_flag, + brand_type; + + + +update public.rate_sheet_wise_expected_warehouse + set payment_amount =0 + from public.gm_dashboard + where discounted_price= 0 + and public.rate_sheet_wise_expected_warehouse.order_id = public.gm_dashboard.external_id + and public.rate_sheet_wise_expected_warehouse.sku = public.gm_dashboard.sku ; + + + select count(1) from public.rate_sheet_wise_expected_warehouse ; + +DROP TABLE IF EXISTS quantity_trend_new; + + +CREATE TABLE quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT is_campaign , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM public.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS quantity_trend; + + +CREATE TABLE quantity_trend AS +SELECT * +FROM quantity_trend_new; + + ; + + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM +(SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + select count(1) from public.rate_sheet_wise_expected_warehouse_expected_shipping ; + +DROP TABLE IF EXISTS om_distr_actaul_origin; + + +CREATE TABLE om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + B.partner_id, + B.service_id, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse='Samarinda' THEN 36700 + END AS expected_origin +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS om_distr_actaul_origin_2; + + +CREATE TABLE om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +and b.service_id = c.service_id +and b.partner_id = c.partner_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id IS NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_4; + + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS public.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE public.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM +(SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_6; + + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id); + + ---sku tagging + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + + --final dataset +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight=10; + + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_11; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+insurance_fee) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+insurance_fee) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN public.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id and o.status <>'CANCELLED' +WHERE Sub_order_shipping_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_12; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_12 AS +SELECT b.*, + a.expected_eta_min +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping_final a +INNER JOIN public.om_shipment_jtr_shipping_fee_base_11 b ON a.Sub_order_shipping_id=b.Sub_order_shipping_id +AND expected__JTR_min_eta-a.expected_eta_min<=3; + + +drop table if exists public.rate_sheet_wise_expected_warehouse_destination_area_id; + +CREATE TABLE public.rate_sheet_wise_expected_warehouse_destination_area_id AS +select distinct order_id , + reference_id Sub_order_shipping_id, + destination_area_id +FROM + (SELECT DISTINCT A.order_id , + reference_id, + destination_area_id + FROM public.warehouse_analysis_stage2 A + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name + WHERE sku IS NOT null + )AA; + + " > /home/ec2-user/cronjob/warehouseAnalysis/warehouseAnalysis.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 + + diff --git a/warehouseAnalysis/etlwarehouseAnalysis_v4.sh b/warehouseAnalysis/etlwarehouseAnalysis_v4.sh new file mode 100644 index 0000000..4afd42b --- /dev/null +++ b/warehouseAnalysis/etlwarehouseAnalysis_v4.sh @@ -0,0 +1,1407 @@ +!/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.sku_type_warehouse; + +create table public.sku_type_warehouse +as +select distinct sku , sku_type from public.final_basic_calucation_table; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1 ; + + +CREATE TABLE public.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) +SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name) AA +LEFT JOIN + (SELECT DISTINCT min_price, + service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price,service_name,destination_area_id + ORDER BY min_price ,service_name ,destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON AA.service_name = BB.service_name +AND AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; +DROP TABLE IF EXISTS public.warehouse_analysis_stage3; + + +CREATE TABLE public.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + E.type service_type , + EE.name partner_name , + CASE + WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount*C.Quantity payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN raena_transport_management.logistic_partner EE ON A.partner_id = EE.id +LEFT JOIN + (SELECT AA.order_id , + AA.sales_sub_order_shipment_id, + AA.sku, + CC.Quantity , + CC.discounted_price payment_amount + FROM + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + GROUP BY 1, + 2, + 3) AA + LEFT JOIN + (SELECT DISTINCT external_id , + product_sku , + quantity, + discounted_price + FROM base_netsuite_final) CC ON AA.order_id = CC.external_id + AND AA.sku = CC.product_sku and AA.Quantity= CC.quantity + ) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' + AND A.status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED'); + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3_final_v1; + + +CREATE TABLE public.warehouse_analysis_stage3_final_v1 AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + service_type , + partner_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + product_sku sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM public.warehouse_analysis_stage3 A +LEFT JOIN base_netsuite_final B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM public.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3_final; + + + +CREATE TABLE public.warehouse_analysis_stage3_final AS +SELECT * +FROM public.warehouse_analysis_stage3_final_v1 +WHERE order_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + AND payment_amount IS NOT NULL +UNION +SELECT A.Order_id, + reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + A.sku , + A.quantity, + shipping_amount, + B.payment_price +FROM public.warehouse_analysis_stage3_final_v1 A +INNER JOIN + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity, + sum(coalesce(BB.payment_amount , AA.payment_amount)) payment_price + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + WHERE order_id IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + GROUP BY 1, + 2, + 3) B ON A.order_id = B.order_id +AND A.reference_id= B.sales_sub_order_shipment_id +AND A.sku = B.sku; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_1; + +CREATE TABLE public.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.warehouse_analysis_stage3_final A ON Z.id=A.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_2; + + +CREATE TABLE public.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + --BB.shipping_cost shipping_amount, + case when BB.external_id = B.order_id +and BB.sku = B.sku +and (case when BB.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, + BB.Brand_name AS Brand_name , + shipping_to shipping_to, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.gm_dashboard BB ON Z.id = BB.external_id +left join public.sku_level_shipping_fee_final B +on BB.external_id = B.order_id +and BB.sku = B.sku +and (case when BB.item_type like '%Bundle%' then 'Bundle' else 'Product' end )=B.product_class +left join public.sku_level_shipping_fee_old_final C +on BB.external_id = C.external_id +and BB.sku = C.sku +and (case when BB.item_type like '%Bundle%' then 'Bundle' else 'Product' end )=C.product_class +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01' + AND BB.external_id NOT IN + (SELECT order_id + FROM public.warehouse_analysis_stage3_final) ; + + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3_v1; + + +CREATE TABLE public.warehouse_analysis_stage1_3_v1 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.service_type , + A.partner_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to shipping_to, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN public.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN public.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3; + + +CREATE TABLE public.warehouse_analysis_stage1_3 AS +SELECT order_id, + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + sku, + quantity, + case when b.shipping_amount is not null then ((b.shipping_amount)/(b.order_weight))*(b.final_weight) else A.shipping_amount end shipping_amount, + shipping_to, + payment_amount, + A.payment_status, + order_status, + order_placed_by, + order_type, + created_at, + is_campaign, + created_by +FROM public.warehouse_analysis_stage1_3_v1 A +LEFT JOIN OM_Logistic_final_shipping B ON A.reference_id= B.reference_id; + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage2_v1_final_weight; + + +CREATE TABLE public.warehouse_analysis_stage2_v1_final_weight AS +SELECT order_id , reference_id , service_name , + service_type , + partner_name , + actual_warehouse, + CASE + WHEN (sum(B.Final_weight*A.Quantity))<1.3 THEN 1 + WHEN (sum(B.Final_weight*A.Quantity))>=1.3 + AND (ABS(sum(B.Final_weight*A.Quantity)) - FLOOR(ABS(sum(B.Final_weight*A.Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.Final_weight*A.Quantity))+1 + ELSE FLOOR(sum(B.Final_weight*A.Quantity)) + END AS sku_weight +FROM public.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku + group by 1,2,3,4,5,6; + + + DROP TABLE IF EXISTS public.warehouse_analysis_stage2_v1; + + +CREATE TABLE public.warehouse_analysis_stage2_v1 AS +SELECT A.*, + B.brand_name, + B.Final_weight*sku_weight/sum(B.Final_weight) over(partition by A.order_id , A.reference_id ) Final_weight +FROM public.warehouse_analysis_stage1_3 A +left join public.warehouse_analysis_stage2_v1_final_weight C on A.order_id = C.order_id and A.reference_id = C.reference_id +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + +drop table if exists public.calculated_ninja_jne_oct_shipping_fee; + +CREATE TABLE public.calculated_ninja_jne_oct_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(CASE WHEN C.partner_id = 5 THEN C.insurance_fee ELSE C.insurance_fee END) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM public.warehouse_analysis_stage2_v1 + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + WHERE A.created_at::date BETWEEN '2022-10-01' AND '2022-10-14') +GROUP BY 1, + 2, + 3, + 4; + + + + DROP TABLE IF EXISTS public.warehouse_analysis_stage2; + + +CREATE TABLE public.warehouse_analysis_stage2 AS +SELECT A.*, + (final_weight*coalesce(Calculated_shipping_fee,shipping_amount))/sum(final_weight) over(partition BY A.reference_id) new_shipment_amount +FROM public.warehouse_analysis_stage2_v1 A +left join public.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.reference_id = B.reference_id ; + +select count(1) from public.warehouse_analysis_stage2 ; + + +DROP TABLE IF EXISTS public.shipping_fee_coupon_purpose; + + +CREATE TABLE public.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to shipping_to, + order_placed_by, + order_type, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT A.id AS order_id , + reseller_id, + json_extract_path_text(A.reseller_info,'tierName',TRUE) tierName, + json_extract_path_text(A.reseller_info,'email',TRUE) email, + json_extract_path_text(A.reseller_info,'mobile',TRUE) mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + A.created_at + FROM raena_order_management.order A + left join raena_order_management.channel D on A.channel_id = D.id + WHERE A.is_archived = 'false' + AND cast(A.created_at AS date)>='2022-04-01' + ORDER BY A.created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN +(SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN public.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management. + ORDER + WHERE payment_status= 'Paid' + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + +select count(1) from public.shipping_fee_coupon_purpose; + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse ; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date, + DSF_flag, + brand_type +FROM + ( + SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + A.service_type , + A.partner_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + coalesce(new_shipment_amount,shipping_amount)new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at, + case when A.order_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + CASE + WHEN coalesce(Y.Name,A.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 coalesce(Y.Name,A.Brand_name) IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC' + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date), + DSF_flag, + brand_type; + +update public.rate_sheet_wise_expected_warehouse + set payment_amount =0 + from public.gm_dashboard + where discounted_price= 0 + and public.rate_sheet_wise_expected_warehouse.order_id = public.gm_dashboard.external_id + and public.rate_sheet_wise_expected_warehouse.sku = public.gm_dashboard.sku ; + + + select count(1) from public.rate_sheet_wise_expected_warehouse ; + +DROP TABLE IF EXISTS quantity_trend_new; + + +CREATE TABLE quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT is_campaign , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM public.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS quantity_trend; + + +CREATE TABLE quantity_trend AS +SELECT * +FROM quantity_trend_new; + + ; + + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM +(SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + select count(1) from public.rate_sheet_wise_expected_warehouse_expected_shipping ; + +DROP TABLE IF EXISTS om_distr_actaul_origin; + + +CREATE TABLE om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + B.partner_id, + B.service_id, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse='Samarinda' THEN 36700 + END AS expected_origin +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS om_distr_actaul_origin_2; + + +CREATE TABLE om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +and b.service_id = c.service_id +and b.partner_id = c.partner_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id IS NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_4; + + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS public.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE public.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM +(SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_6; + + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id); + + ---sku tagging + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + + --final dataset +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight=10; + + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_11_v11 ; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_11_v11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN public.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id and o.status <>'CANCELLED' +WHERE Sub_order_shipping_id IS NOT NULL; + +drop table if exists public.om_shipment_jtr_shipping_fee_base_11; + +create table public.om_shipment_jtr_shipping_fee_base_11 +as +select A.*,AA.new_sku sku,compared_weight sku_weight , +sum((compared_weight/order_weight)*expected_jtr_shipping_fee) sku_level_jtr_expected_shipping_fee, +sum((compared_weight/order_weight)*actual_jtr_shipping_fee) sku_level_jtr_actual_shipping_fee +from public.om_shipment_jtr_shipping_fee_base_11_v11 A +left join +(SELECT order_id , sales_sub_order_id , reference_id ,new_sku, + sum(CASE + WHEN new_volume_weight>new_actual_weight THEN new_volume_weight + WHEN new_volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight /home/ec2-user/cronjob/warehouseAnalysis/warehouseAnalysis.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 diff --git a/warehouseAnalysis/install_uninstall_rate.sql b/warehouseAnalysis/install_uninstall_rate.sql new file mode 100644 index 0000000..09b8108 --- /dev/null +++ b/warehouseAnalysis/install_uninstall_rate.sql @@ -0,0 +1,69 @@ + +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 ; + + diff --git a/warehouseAnalysis/retention.log b/warehouseAnalysis/retention.log new file mode 100644 index 0000000..efa0000 --- /dev/null +++ b/warehouseAnalysis/retention.log @@ -0,0 +1,14 @@ +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT +DROP TABLE +SELECT diff --git a/warehouseAnalysis/retention.sql b/warehouseAnalysis/retention.sql new file mode 100644 index 0000000..307cebc --- /dev/null +++ b/warehouseAnalysis/retention.sql @@ -0,0 +1,470 @@ + + +--------------------------Retention Data -------------------------- +drop table if exists OM_Reseller_Wise_latest_tier; + + create table OM_Reseller_Wise_latest_tier + as + select mobile,tier from( + select mobile,tier,row_number() over (partition by mobile order by created_date desc) as R + from ( + select distinct replace(json_extract_path_text(o.reseller_info,'mobile',TRUE),'+','') as mobile, + json_extract_path_text(o.reseller_info,'tierName',TRUE) as tier, + cast(created_at as date) as created_date + from raena_order_management.order o + ) + ) where R=1 and mobile notnull; + +Drop table if exists Retention_new_filters_install; + + +create table Retention_new_filters_install + as + select distinct TO_CHAR(o.created_at, 'Month') as month,cast(date_part('year',o.created_at) as varchar) as year, + case when shipping_to='Reseller' then 'Reseller' else 'Dropshipper' end as customer_type, + order_placed_by, + case when order_placed_by='admin' then 'admin' else marketplace end as Sales_channel,provider, + c.channel_name as channel_name, + replace(json_extract_path_text(o.reseller_info,'mobile',TRUE),'+','') mobile, + verification_status,tier + FROM raena_user_management.user u + left join raena_order_management.order o + on replace(json_extract_path_text(o.reseller_info,'mobile',TRUE),'+','')=replace(u.mobile,'+','') + left join OM_channel c on cast(c.id as text)=cast(o.channel_id as text) + left join ( + select mobile,tier from( + select mobile,tier,row_number() over (partition by mobile order by created_date desc) as R + from ( + select distinct replace(json_extract_path_text(o.reseller_info,'mobile',TRUE),'+','') as mobile, + json_extract_path_text(o.reseller_info,'tierName',TRUE) as tier, + cast(created_at as date) as created_date + from raena_order_management.order o + ) + ) where R=1 and mobile notnull + ) t on u.mobile=t.mobile; + + +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; + + + + + + + + + + + + + + diff --git a/warehouseAnalysis/retention_code.sh b/warehouseAnalysis/retention_code.sh new file mode 100644 index 0000000..6fe9b86 --- /dev/null +++ b/warehouseAnalysis/retention_code.sh @@ -0,0 +1,474 @@ +#!/bin/bash + +echo " + +--------------------------Retention Data -------------------------- +drop table if exists OM_Reseller_Wise_latest_tier; + + create table OM_Reseller_Wise_latest_tier + as + select mobile,tier from( + select mobile,tier,row_number() over (partition by mobile order by created_date desc) as R + from ( + select distinct replace(json_extract_path_text(o.reseller_info,'mobile',TRUE),'+','') as mobile, + json_extract_path_text(o.reseller_info,'tierName',TRUE) as tier, + cast(created_at as date) as created_date + from raena_order_management.order o + ) + ) where R=1 and mobile notnull; + +Drop table if exists Retention_new_filters_install; + + +create table Retention_new_filters_install + as + select distinct TO_CHAR(o.created_at, 'Month') as month,cast(date_part('year',o.created_at) as varchar) as year, + case when shipping_to='Reseller' then 'Reseller' else 'Dropshipper' end as customer_type, + order_placed_by, + case when order_placed_by='admin' then 'admin' else marketplace end as Sales_channel,provider, + c.channel_name as channel_name, + replace(json_extract_path_text(o.reseller_info,'mobile',TRUE),'+','') mobile, + verification_status,tier + FROM raena_user_management."user" u + left join raena_order_management.order o + on replace(json_extract_path_text(o.reseller_info,'mobile',TRUE),'+','')=replace(u.mobile,'+','') + left join OM_channel c on cast(c.id as text)=cast(o.channel_id as text) + left join ( + select mobile,tier from( + select mobile,tier,row_number() over (partition by mobile order by created_date desc) as R + from ( + select distinct replace(json_extract_path_text(o.reseller_info,'mobile',TRUE),'+','') as mobile, + json_extract_path_text(o.reseller_info,'tierName',TRUE) as tier, + cast(created_at as date) as created_date + from raena_order_management.order o + ) + ) where R=1 and mobile notnull + ) t on u.mobile=t.mobile; + + +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; + + + + + + + + + + + + + + " > /home/ec2-user/cronjob/warehouseAnalysis/retention.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/retention.sql > retention.log diff --git a/warehouseAnalysis/warehouseAnalysis.sql b/warehouseAnalysis/warehouseAnalysis.sql new file mode 100644 index 0000000..d3ef68c --- /dev/null +++ b/warehouseAnalysis/warehouseAnalysis.sql @@ -0,0 +1,1392 @@ + + +drop table if exists public.sku_type_warehouse; + +create table public.sku_type_warehouse +as +select distinct sku , sku_type from public.final_basic_calucation_table; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1 ; + + +CREATE TABLE public.warehouse_analysis_stage1 AS WITH basetable AS + (SELECT A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END origin_warehouse , + destination_area_id , + min(price) price , + B.name partner_name, + C.name Service_name + FROM raena_transport_management.logistic_rate A + LEFT JOIN raena_transport_management.logistic_partner B ON A.partner_id = B.id + LEFT JOIN raena_transport_management.logistic_service C ON A.service_id = C.id + WHERE A.active_status ='true' + GROUP BY A.id , + CASE + WHEN origin_area_id= '12416' THEN 'Cikarang' + WHEN origin_area_id= '21599' THEN 'Semarang' + WHEN origin_area_id= '30110' THEN 'Surabaya' + WHEN origin_area_id= '64985' THEN 'Makassar' + WHEN origin_area_id= '81092' THEN 'Medan' + WHEN origin_area_id= '36700' THEN 'Samarinda' + END , + destination_area_id , + B.name , + C.name) +SELECT AA.*, + BB.origin_warehouse expected_warehouse +FROM + (SELECT A.*, + B.min_price + FROM basetable A + LEFT JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name) AA +LEFT JOIN + (SELECT DISTINCT min_price, + service_name, + destination_area_id , + origin_warehouse, + row_number () over (partition BY min_price,service_name,destination_area_id + ORDER BY min_price ,service_name ,destination_area_id) rnk + FROM + (SELECT DISTINCT A.destination_area_id, + A.origin_warehouse , + A.service_name, + min_price --A.*,B.min_price +FROM basetable A + INNER JOIN + (SELECT destination_area_id , + min(price) min_price, + partner_name, + Service_name + FROM basetable + GROUP BY destination_area_id , + partner_name, + Service_name) B ON A.destination_area_id=B.destination_area_id + AND A.partner_name =B.partner_name + AND A.Service_name= B.Service_name + AND min_price = Price)AA) BB ON AA.service_name = BB.service_name +AND AA.min_price = BB.min_price +AND AA.destination_area_id= BB.destination_area_id +AND rnk = 1 ; +DROP TABLE IF EXISTS public.warehouse_analysis_stage3; + + +CREATE TABLE public.warehouse_analysis_stage3 AS +SELECT DISTINCT coalesce(B.order_id) order_id , + A.reference_id, + E.name service_name , + E.type service_type , + EE.name partner_name , + CASE + WHEN warehouse_code='WH-CGK45' THEN 'Cikarang' + WHEN warehouse_code='WH-SRG18' THEN 'Semarang' + WHEN warehouse_code='WH-SUB51' THEN 'Surabaya' + WHEN warehouse_code='WH-UPG04' THEN 'Makassar' + WHEN warehouse_code='WH-MES07' THEN 'Medan' + WHEN warehouse_code='WH-AAP02' THEN 'Samarinda' + END actual_warehouse, + B.rate_id, + sku, + C.quantity quantity, + B.shipping_amount shipping_amount, + C.payment_amount*C.Quantity payment_amount +FROM raena_transport_management.logistic_order A +LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.reference_id = B.id +LEFT JOIN raena_transport_management.logistic_service E ON A.service_id = E.id +LEFT JOIN raena_transport_management.logistic_partner EE ON A.partner_id = EE.id +LEFT JOIN + (SELECT AA.order_id , + AA.sales_sub_order_shipment_id, + AA.sku, + CC.Quantity , + CC.discounted_price payment_amount + FROM + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + GROUP BY 1, + 2, + 3) AA + LEFT JOIN + (SELECT DISTINCT external_id , + product_sku , + quantity, + discounted_price + FROM base_netsuite_final) CC ON AA.order_id = CC.external_id + AND AA.sku = CC.product_sku and AA.Quantity= CC.quantity + ) C ON A.reference_id = C.sales_sub_order_shipment_id +WHERE cast(A.created_at AS date)>='2022-04-01' + AND A.status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED'); + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3_final_v1; + + +CREATE TABLE public.warehouse_analysis_stage3_final_v1 AS +SELECT A.reference_id AS Order_id , + reference_id , + service_name , + service_type , + partner_name , + 'Cikarang' actual_warehouse , + -1 rate_id , + product_sku sku , + B.quantity , + B.shipping_cost shipping_amount , + B.discounted_price*B.quantity payment_amount +FROM public.warehouse_analysis_stage3 A +LEFT JOIN base_netsuite_final B ON A.reference_id = B.external_id +WHERE A.order_id IS NULL +UNION +SELECT * +FROM public.warehouse_analysis_stage3 +WHERE order_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage3_final; + + + +CREATE TABLE public.warehouse_analysis_stage3_final AS +SELECT * +FROM public.warehouse_analysis_stage3_final_v1 +WHERE order_id NOT IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + AND payment_amount IS NOT NULL +UNION +SELECT A.Order_id, + reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + A.sku , + A.quantity, + shipping_amount, + B.payment_price +FROM public.warehouse_analysis_stage3_final_v1 A +INNER JOIN + (SELECT order_id , + sales_sub_order_shipment_id, + coalesce(BB.sku ,parent_sku) sku, + sum(coalesce(BB.quantity , AA.quantity)) Quantity, + sum(coalesce(BB.payment_amount , AA.payment_amount)) payment_price + FROM raena_order_management.sales_sub_order AA + LEFT JOIN raena_order_management.sales_sub_order_parent_child BB ON AA.id = BB.sales_sub_order_id + WHERE order_id IN + (SELECT DISTINCT id + FROM raena_order_management.order + WHERE is_campaign='true') + GROUP BY 1, + 2, + 3) B ON A.order_id = B.order_id +AND A.reference_id= B.sales_sub_order_shipment_id +AND A.sku = B.sku; + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_1; + +CREATE TABLE public.warehouse_analysis_stage1_1 AS +SELECT DISTINCT Z.id order_id , + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + A.rate_id, + A.sku, + A.quantity, + A.shipping_amount, + A.payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.warehouse_analysis_stage3_final A ON Z.id=A.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_2; + + +CREATE TABLE public.warehouse_analysis_stage1_2 AS +SELECT DISTINCT Z.id order_id , + BB.sku sku, + BB.quantity quantity, + --BB.shipping_cost shipping_amount, + case when BB.external_id = B.order_id +and BB.sku = B.sku +and (case when BB.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, + BB.Brand_name AS Brand_name , + shipping_to shipping_to, + BB.discounted_price*BB.quantity payment_amount, + payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +INNER JOIN public.gm_dashboard BB ON Z.id = BB.external_id +left join public.sku_level_shipping_fee_final B +on BB.external_id = B.order_id +and BB.sku = B.sku +and (case when BB.item_type like '%Bundle%' then 'Bundle' else 'Product' end )=B.product_class +left join public.sku_level_shipping_fee_old_final C +on BB.external_id = C.external_id +and BB.sku = C.sku +and (case when BB.item_type like '%Bundle%' then 'Bundle' else 'Product' end )=C.product_class +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01' + AND BB.external_id NOT IN + (SELECT order_id + FROM public.warehouse_analysis_stage3_final) ; + + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3_v1; + + +CREATE TABLE public.warehouse_analysis_stage1_3_v1 AS +SELECT Z.id AS order_id , + A.reference_id, + A.service_name , + A.service_type , + A.partner_name , + A.actual_warehouse, + A.rate_id, + coalesce(A.sku,B.sku) sku, + coalesce(A.quantity,B.quantity) quantity, + coalesce(A.shipping_amount,B.shipping_amount) shipping_amount, + Z.shipping_to shipping_to, + coalesce(A.payment_amount,B.payment_amount) payment_amount, + Z.payment_status , + Z.status order_status, + case when Z.provider='dsf' then 'DSF' + when Z.provider='rdash360' then 'RDash' + when Z.order_placed_by='merchant' then 'Merchant' + when Z.order_placed_by='admin' then 'Admin Panel' + when Z.order_placed_by='reseller' then 'App' end order_placed_by, + case + when Z.is_campaign='true' then 'Campaign Order' + when Z.is_consignment='true' then 'Consignment Order' + when Z.provider='dsf' then 'DSF Order' + when Z.order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + Z.created_at, + z.is_campaign, + Z.created_by +FROM raena_order_management.order Z +LEFT JOIN public.warehouse_analysis_stage1_1 A ON Z.id = A.order_id +LEFT JOIN public.warehouse_analysis_stage1_2 B ON Z.id = B.order_id +left join raena_order_management.channel D on Z.channel_id = D.id +WHERE Z.payment_status ='Paid' + AND cast(Z.created_at AS date)>='2022-04-01'; + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage1_3; + + +CREATE TABLE public.warehouse_analysis_stage1_3 AS +SELECT order_id, + A.reference_id, + service_name , + service_type , + partner_name , + actual_warehouse, + rate_id, + sku, + quantity, + case when b.shipping_amount is not null then ((b.shipping_amount)/(b.order_weight))*(b.final_weight) else A.shipping_amount end shipping_amount, + shipping_to, + payment_amount, + A.payment_status, + order_status, + order_placed_by, + order_type, + created_at, + is_campaign, + created_by +FROM public.warehouse_analysis_stage1_3_v1 A +LEFT JOIN OM_Logistic_final_shipping B ON A.reference_id= B.reference_id; + + + +DROP TABLE IF EXISTS public.warehouse_analysis_stage2_v1_final_weight; + + +CREATE TABLE public.warehouse_analysis_stage2_v1_final_weight AS +SELECT order_id , reference_id , service_name , + service_type , + partner_name , + actual_warehouse, + CASE + WHEN (sum(B.Final_weight*A.Quantity))<1.3 THEN 1 + WHEN (sum(B.Final_weight*A.Quantity))>=1.3 + AND (ABS(sum(B.Final_weight*A.Quantity)) - FLOOR(ABS(sum(B.Final_weight*A.Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.Final_weight*A.Quantity))+1 + ELSE FLOOR(sum(B.Final_weight*A.Quantity)) + END AS sku_weight +FROM public.warehouse_analysis_stage1_3 A +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku + group by 1,2,3,4,5,6; + + + DROP TABLE IF EXISTS public.warehouse_analysis_stage2_v1; + + +CREATE TABLE public.warehouse_analysis_stage2_v1 AS +SELECT A.*, + B.brand_name, + B.Final_weight*sku_weight/sum(B.Final_weight) over(partition by A.order_id , A.reference_id ) Final_weight +FROM public.warehouse_analysis_stage1_3 A +left join public.warehouse_analysis_stage2_v1_final_weight C on A.order_id = C.order_id and A.reference_id = C.reference_id +LEFT JOIN + (SELECT sku , + B.name brand_name, + CASE + WHEN height*width*LENGTH/6000 > weight THEN height*width*LENGTH/6000 + ELSE weight + END Final_weight, + height*width*LENGTH/6000 vw, + weight + FROM raena_catalog_management.product A + LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id) B ON A.sku = B.sku; + + +drop table if exists public.calculated_ninja_jne_oct_shipping_fee; + +CREATE TABLE public.calculated_ninja_jne_oct_shipping_fee AS +SELECT order_id , + reference_id , + service_name , + partner_id, + sum(Calculated_shipping_fee+insurance_fee) Calculated_shipping_fee +FROM + (SELECT order_id , + A.reference_id , + service_name , + C.partner_id, + B.price*final_weight Calculated_shipping_fee , + B.price, + final_weight, + (B.price*final_weight)*(CASE WHEN C.partner_id = 5 THEN C.insurance_fee ELSE C.insurance_fee END) /sum(B.price*final_weight)over(partition BY A.reference_id) AS insurance_fee + FROM + (SELECT order_id , + reference_id , + rate_id, + service_name , + created_at, + sum(final_weight) final_weight + FROM public.warehouse_analysis_stage2_v1 + GROUP BY 1, + 2, + 3, + 4, + 5) A + LEFT JOIN raena_transport_management.logistic_rate B ON A.rate_id = B.id + LEFT JOIN + (SELECT DISTINCT reference_id , + insurance_fee, + partner_id , + service_id + FROM raena_transport_management.logistic_order + WHERE status IN ('RETURNED', + 'DELIVERED', + 'PICKED_UP', + 'CREATED')) C ON A.reference_id=C.reference_id + WHERE A.created_at::date BETWEEN '2022-10-01' AND '2022-10-14') +GROUP BY 1, + 2, + 3, + 4; + + + + DROP TABLE IF EXISTS public.warehouse_analysis_stage2; + + +CREATE TABLE public.warehouse_analysis_stage2 AS +SELECT A.*, + (final_weight*coalesce(Calculated_shipping_fee,shipping_amount))/sum(final_weight) over(partition BY A.reference_id) new_shipment_amount +FROM public.warehouse_analysis_stage2_v1 A +left join public.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.reference_id = B.reference_id ; + +select count(1) from public.warehouse_analysis_stage2 ; + + +DROP TABLE IF EXISTS public.shipping_fee_coupon_purpose; + + +CREATE TABLE public.shipping_fee_coupon_purpose AS +SELECT O.order_id , + C.sub_order_id, + payment_status, + order_status, + sub_order_status, + sub_order_shipment_status, + tierName, + payment_amount, + shipping_amount AS shipping_fee, + applied_shipping_amount AS applied_shipping_fee, + shipping_to shipping_to, + order_placed_by, + order_type, + email, + mobile, + CASE + WHEN D.min_date = O.Created_at + AND O.reseller_id = D.reseller_id THEN 'New' + ELSE 'Old' + END User_type , + province, + created_at +FROM + (SELECT A.id AS order_id , + reseller_id, + json_extract_path_text(A.reseller_info,'tierName',TRUE) tierName, + json_extract_path_text(A.reseller_info,'email',TRUE) email, + json_extract_path_text(A.reseller_info,'mobile',TRUE) mobile, + lower(shipping_province) province, + payment_status, + status order_status, + shipping_to , + case when provider='dsf' then 'DSF' + when provider='rdash360' then 'RDash' + when order_placed_by='merchant' then 'Merchant' + when order_placed_by='admin' then 'Admin Panel' + when order_placed_by='reseller' then 'App' end order_placed_by, + case + when is_campaign='true' then 'Campaign Order' + when is_consignment='true' then 'Consignment Order' + when provider='dsf' then 'DSF Order' + when order_placed_by='merchant' then 'Merchant Order' + when D.name is not null then 'Channel Order' + else 'Reseller Order' end order_type, + A.created_at + FROM raena_order_management.order A + left join raena_order_management.channel D on A.channel_id = D.id + WHERE A.is_archived = 'false' + AND cast(A.created_at AS date)>='2022-04-01' + ORDER BY A.created_at DESC) O +LEFT JOIN + (SELECT id sub_order_shipping_id, + order_id, + shipping_amount, + applied_shipping_amount, + status sub_order_shipment_status + FROM raena_order_management.sales_sub_order_shipment + ORDER BY 2) B ON O.order_id = B.order_id +LEFT JOIN +(SELECT id sub_order_id , + order_id, + A.status sub_order_status, + sales_sub_order_shipment_id , + B.discounted_price*B.quantity payment_amount + FROM raena_order_management.sales_sub_order A + INNER JOIN public.business_report B ON A.order_id = B.external_id + AND A.parent_sku = B.sku + ORDER BY 2, + 4 DESC) C ON B.sub_order_shipping_id= C.sales_sub_order_shipment_id +LEFT JOIN + (SELECT reseller_id, + min(created_at) min_date + FROM raena_order_management. + ORDER + WHERE payment_status= 'Paid' + AND is_archived = 'false' + GROUP BY reseller_id) D ON O.reseller_id = D.reseller_id +ORDER BY o.created_at DESC; + +select count(1) from public.shipping_fee_coupon_purpose; + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse ; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date) AS created_date, + DSF_flag, + brand_type +FROM + ( + SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + A.service_type , + A.partner_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + coalesce(new_shipment_amount,shipping_amount)new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + is_campaign, + A.created_at, + case when A.order_id like '%DSF%' then 'Yes' Else 'No' end DSF_flag, + CASE + WHEN coalesce(Y.Name,A.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 coalesce(Y.Name,A.Brand_name) IN ('W DRESSROOM', + 'BEAUSTA', + 'Dewycel', + 'GLUTANEX', + 'HISTOIRE NATURELLE', + 'FORENCOS', + 'BELLFLOWER', + 'MAXCLINIC' + 'INGRID', + 'ONE THING', + 'ITFER') THEN 'EL/PL' + END brand_type + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + service_type , + partner_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + is_campaign, + cast((created_at+interval'7 Hours') AS date), + DSF_flag, + brand_type; + +update public.rate_sheet_wise_expected_warehouse + set payment_amount =0 + from public.gm_dashboard + where discounted_price= 0 + and public.rate_sheet_wise_expected_warehouse.order_id = public.gm_dashboard.external_id + and public.rate_sheet_wise_expected_warehouse.sku = public.gm_dashboard.sku ; + + + select count(1) from public.rate_sheet_wise_expected_warehouse ; + +DROP TABLE IF EXISTS quantity_trend_new; + + +CREATE TABLE quantity_trend_new AS +SELECT is_campaign , + frequency , + transaction_date , + child_sku , + warehouse , + quantity +FROM quantity_trend_v1 +WHERE transaction_date <'2022-04-01' +UNION +SELECT is_campaign , + 'Month', + date_trunc('Month',created_date) :: date , + sku , + actual_warehouse, + sum(quantity) +FROM public.rate_sheet_wise_expected_warehouse +GROUP BY 1, + 2, + 3, + 4, + 5; + + +DROP TABLE IF EXISTS quantity_trend; + + +CREATE TABLE quantity_trend AS +SELECT * +FROM quantity_trend_new; + + ; + + +DROP TABLE IF EXISTS public.rate_sheet_wise_expected_warehouse_expected_shipping; + + +CREATE TABLE public.rate_sheet_wise_expected_warehouse_expected_shipping AS +SELECT order_id , + reference_id Sub_order_shipping_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end actual_warehouse , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end expected_warehouse, + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END AS missed_Inventory, + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + sum(quantity)quantity, + sum(new_shipment_amount) shipping_amount, + sum(price) Per_kg_cost, + count(order_id) number_of_order, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date) AS created_date +FROM +(SELECT DISTINCT A.order_id , + reference_id, + A.service_name , + actual_warehouse, + rate_id, + A.sku, + quantity, + new_shipment_amount, + expected_warehouse, + coalesce(Y.Name,A.Brand_name) AS Brand_name , + shipping_to, + payment_amount, + D.price, + payment_status , + order_status, + order_placed_by, + order_type, + AA.name am_name, + AA.email am_email, + tierName, + DD.email, + mobile , + province , + User_type, + A.created_at + FROM public.warehouse_analysis_stage2 A + LEFT JOIN + (SELECT DISTINCT order_id , + tierName, + email, + mobile , + province , + User_type + FROM public.shipping_fee_coupon_purpose) DD ON A.order_id = DD.order_id + LEFT JOIN raena_user_management.admin_user AA ON cast(A.created_by AS varchar) = cast(AA.id AS varchar) + LEFT JOIN raena_catalog_management.product X ON A.sku = X.sku + LEFT JOIN raena_catalog_management.brand Y ON X.brand_id =Y.id + LEFT JOIN public.warehouse_analysis_stage1 D ON A.rate_id = D.id + AND A.service_name = D.service_name)AA +WHERE sku IS NOT NULL +GROUP BY order_id , + reference_id, + sku , + case when actual_warehouse is null then 'Cikarang' else actual_warehouse end , + case when expected_warehouse is null then 'Cikarang' else expected_warehouse end , + CASE + WHEN actual_warehouse <>expected_warehouse THEN 'Yes' + ELSE 'No' + END , + service_name , + shipping_to, + payment_amount, + payment_status , + order_status, + order_placed_by, + order_type, + am_name, + am_email, + brand_name, + tierName, + email, + mobile , + province , + User_type, + cast(created_at AS date); + + select count(1) from public.rate_sheet_wise_expected_warehouse_expected_shipping ; + +DROP TABLE IF EXISTS om_distr_actaul_origin; + + +CREATE TABLE om_distr_actaul_origin AS +SELECT DISTINCT sub_order_shipping_id, + service_name, + actual_warehouse, + expected_warehouse, + order_weight, + B.partner_id, + B.service_id, + rate_id, + destination_area_id, + origin_area_id AS actual_origin, + CASE + WHEN expected_warehouse='Cikarang' THEN 12416 + WHEN expected_warehouse='Semarang' THEN 21599 + WHEN expected_warehouse='Surabaya' THEN 30110 + WHEN expected_warehouse='Makassar' THEN 64985 + WHEN expected_warehouse='Medan' THEN 81092 + WHEN expected_warehouse='Samarinda' THEN 36700 + END AS expected_origin +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +INNER JOIN raena_transport_management.logistic_order b ON a.sub_order_shipping_id=b.reference_id +LEFT JOIN raena_transport_management.logistic_rate c ON b.rate_id=c.id; + + +DROP TABLE IF EXISTS om_distr_actaul_origin_2; + + +CREATE TABLE om_distr_actaul_origin_2 AS +SELECT DISTINCT sub_order_shipping_id, + actual_warehouse, + expected_warehouse, + c.price, + c.eta_min +FROM om_distr_actaul_origin b +INNER JOIN raena_transport_management.logistic_rate c ON b.expected_origin=c.origin_area_id +AND b.destination_area_id=c.destination_area_id +and b.service_id = c.service_id +and b.partner_id = c.partner_id +INNER JOIN raena_transport_management.logistic_service s ON s.id=c.service_id +AND s.name=b.service_name +WHERE rate_id NOTNULL + AND actual_warehouse!=expected_warehouse; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse AS +SELECT DISTINCT cast(lo.created_at AS date) AS created_at, + reference_id, + order_weight, + lo.shipping_amount, + rate_id, + lr.price AS Shipping_fee_per_kg_weight, + lr.eta_min AS expected_eta_min +FROM raena_transport_management.logistic_order lo +INNER JOIN om_distr_actaul_origin_2 lr ON lo.reference_id=lr.sub_order_shipping_id +WHERE lo.created_at >= '2022-04-01' + AND rate_id IS NOT NULL; + + --order_id for suborders + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_2; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_2 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + rate_id, + shipping_fee_per_kg_weight, + expected_eta_min, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.OM_Logistic_base_exepected_warehouse a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id) A; + + ---sku tagging + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_3; + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_3 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.OM_Logistic_base_exepected_warehouse_2 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.OM_Logistic_base_exepected_warehouse_4; + + + +CREATE TABLE public.OM_Logistic_base_exepected_warehouse_4 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + shipping_fee_per_kg_weight, + expected_eta_min, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.OM_Logistic_base_exepected_warehouse_3 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + --final dataset + +DROP TABLE IF EXISTS public.OM_Logistic_final_shipping_exepected_warehouse_base; + + +CREATE TABLE public.OM_Logistic_final_shipping_exepected_warehouse_base AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + Shipping_fee_per_kg_weight, + expected_eta_min, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM +(SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight= '2022-04-01' + AND lo.rate_id IS NOT NULL; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_6; + + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_6 AS +SELECT created_at, + reference_id, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + coalesce(order_id,reference_id) AS order_id, + sku, + payment_amount, + sales_sub_order_id, + quantity +FROM + (SELECT a.*, + b.order_id, + b.parent_sku AS sku, + b.payment_amount , + b.id AS sales_sub_order_id, + b.quantity + FROM public.om_shipment_jtr_shipping_fee_base_5 a + LEFT JOIN raena_order_management.sales_sub_order b ON a.reference_id=b.sales_sub_order_shipment_id); + + ---sku tagging + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_7; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_7 AS +SELECT DISTINCT a.*, + coalesce(b.sku,a.sku) AS new_sku, + coalesce(b.quantity,a.quantity) AS new_quantity +FROM public.om_shipment_jtr_shipping_fee_base_6 a +LEFT JOIN raena_order_management.sales_sub_order_parent_child b ON a.sales_sub_order_id=b.sales_sub_order_id; + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_8; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_8 AS +SELECT Created_at, + order_id, + reference_id, + sales_sub_order_id, + new_quantity AS quantity, + order_weight, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + new_sku, + payment_amount, + b.weight AS actual_weight, + b.volume_weight +FROM public.om_shipment_jtr_shipping_fee_base_7 a +LEFT JOIN + (SELECT sku, + id, + weight, + (height*width*LENGTH)/6000 AS volume_weight + FROM raena_catalog_management.product) b ON a.new_sku=b.sku +WHERE reference_id LIKE 'SH%' +ORDER BY 2; + + + --final dataset +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_9; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_9 AS +SELECT Created_at, + order_id, + reference_id, + paymnet_amount, + shipping_amount, + actual__JTR_min_eta, + actual_JTR_per_kg_price, + expected__JTR_min_eta, + expected_JTR_per_kg_price, + CASE + WHEN order_weight<1.3 THEN 1 + WHEN order_weight>=1.3 + AND (ABS(order_weight) - FLOOR(ABS(order_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(order_weight)+1 + ELSE FLOOR(order_weight) + END AS order_weight, + CASE + WHEN compared_weight<1.3 THEN 1 + WHEN compared_weight>=1.3 + AND (ABS(compared_weight) - FLOOR(ABS(compared_weight))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(compared_weight)+1 + ELSE FLOOR(compared_weight) + END AS final_weight +FROM + (SELECT *, + CASE + WHEN volume_weight>actual_weight THEN volume_weight + WHEN volume_weight=10; + + + +DROP TABLE IF EXISTS public.om_shipment_jtr_shipping_fee_base_11_v11 ; + + +CREATE TABLE public.om_shipment_jtr_shipping_fee_base_11_v11 AS +SELECT DISTINCT a.Sub_order_shipping_id, + (b.actual_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS actual_JTR_shipping_fee, + actual__JTR_min_eta, + (b.expected_JTR_shipping_fee+(CASE WHEN o.partner_id = 5 then insurance_fee/100 else insurance_fee end )) AS expected_JTR_shipping_fee, + expected__JTR_min_eta +FROM public.rate_sheet_wise_expected_warehouse_expected_shipping a +LEFT JOIN public.om_shipment_jtr_shipping_fee_base_10 b ON a.Sub_order_shipping_id=b.reference_id +INNER JOIN raena_transport_management.logistic_order o ON a.Sub_order_shipping_id=o.reference_id and o.status <>'CANCELLED' +WHERE Sub_order_shipping_id IS NOT NULL; + +drop table if exists public.om_shipment_jtr_shipping_fee_base_11; + +create table public.om_shipment_jtr_shipping_fee_base_11 +as +select A.*,AA.new_sku sku,compared_weight sku_weight , +sum((compared_weight/order_weight)*expected_jtr_shipping_fee) sku_level_jtr_expected_shipping_fee, +sum((compared_weight/order_weight)*actual_jtr_shipping_fee) sku_level_jtr_actual_shipping_fee +from public.om_shipment_jtr_shipping_fee_base_11_v11 A +left join +(SELECT order_id , sales_sub_order_id , reference_id ,new_sku, + sum(CASE + WHEN new_volume_weight>new_actual_weight THEN new_volume_weight + WHEN new_volume_weightnew_actual_weight THEN new_volume_weight + WHEN new_volume_weight