From 65b83cb178939be6dcb982271420c271504064ba Mon Sep 17 00:00:00 2001 From: harishkrishnaappa Date: Mon, 20 Dec 2021 16:17:48 +0530 Subject: [PATCH] existing duroflex return script --- retail_model/duroflex_return.sql | 61 ++++ retail_model/fw_ods_duroflex_dealers.sql | 96 +++++ retail_model/fw_ods_monthly_v1.2_20210715.sql | 95 +++++ retail_model/fw_ods_monthly_v2.sql | 127 +++++++ retail_model/nandus_ods_monthly.sql | 29 ++ .../nandus_waterfall_monthly_20200101.sql | 139 ++++++++ ...terfall_monthly_cust_analysis_20200101.sql | 336 ++++++++++++++++++ 7 files changed, 883 insertions(+) create mode 100644 retail_model/duroflex_return.sql create mode 100644 retail_model/fw_ods_duroflex_dealers.sql create mode 100644 retail_model/fw_ods_monthly_v1.2_20210715.sql create mode 100644 retail_model/fw_ods_monthly_v2.sql create mode 100644 retail_model/nandus_ods_monthly.sql create mode 100644 retail_model/nandus_waterfall_monthly_20200101.sql create mode 100644 retail_model/nandus_waterfall_monthly_cust_analysis_20200101.sql diff --git a/retail_model/duroflex_return.sql b/retail_model/duroflex_return.sql new file mode 100644 index 0000000..fa001f2 --- /dev/null +++ b/retail_model/duroflex_return.sql @@ -0,0 +1,61 @@ +DROP TABLE IF EXISTS fw_ods.duroflex_return; + +CREATE TABLE fw_ods.duroflex_return ( + syspk bigserial NOT NULL, + client_id fw_ods.fw_num_dimid NOT NULL, + function_id fw_ods.fw_num_dimid NOT NULL, + channel fw_ods.fw_text NULL, + dealer_code fw_ods.fw_text NULL, + dealer_name fw_ods.fw_text NULL, + dealer_policy fw_ods.fw_text NULL, + region fw_ods.fw_text NULL, + state_name fw_ods.fw_text NULL, + city fw_ods.fw_text NULL, + metro_nonmetro fw_ods.fw_text NULL, + invoice_number fw_ods.fw_text NULL, + invoice_yyyymm fw_ods.fw_num_dimid NULL, + quantity fw_ods.fw_num_dimid NULL, + amount fw_ods.fw_num_metric NULL, + return_flag fw_ods.fw_flag_yn null, + created_by fw_ods.fw_text NOT NULL, + updated_by fw_ods.fw_text NOT NULL, + create_timestamp fw_ods.fw_timestamp NOT NULL DEFAULT now(), + update_timestamp fw_ods.fw_timestamp NOT NULL DEFAULT now(), + CONSTRAINT duroflex_return_pkey PRIMARY KEY (syspk) +); + +INSERT INTO duroflex_return +(client_id, function_id, channel, dealer_code, dealer_name, dealer_policy, region, state_name, city, metro_nonmetro, invoice_number, +invoice_yyyymm, quantity, amount, return_flag, created_by, updated_by) + +select 40 as client_id, 1 as function_id, c.channelformat channel, d.dealercode dealer_code, d.dealername dealer_name, +policy dealer_policy, g.region, g.geolocationstate state_name, g.geolocationcity city, g.metro_nonmetro, invoicenumber, +concat(date_part('year'::text, f.invoicedate),lpad(date_part('month'::text, f.invoicedate)::text,2,'0'))::int invoice_yyyymm, +quantity, amount, f.returnflag, +current_user, current_user +from dbo.factsecondarysales f, dbo.dimdealer d, dbo.dimgeography g, dbo.dimchannelformat c +where f.dealerkey = d.dealerkey and f.geographykey = g.geographykey and f.channelformatkey = c.channelformatkey +and (f.returnflag = 'Y' or (f.returnflag = 'N' and amount < 0)) + +update fw_ods.duroflex_return +set channel = 'EBO' +where channel = 'STUDIO' + +update fw_ods.duroflex_return +set channel = 'MBO' +where channel = 'Others' + +update fw_ods.duroflex_return +set dealer_policy = 'STUDIO' +where dealer_policy = 'STUDIO ANNUAL' + +update fw_ods.duroflex_return +set dealer_policy = 'TIER 1' +where dealer_policy = 'TIER 1 ANNUAL' + +update fw_ods.duroflex_return +set dealer_policy = 'TIER 2' +where dealer_policy = 'TIER 2 ANNUAL' + +update fw_ods.duroflex_return +set state_name = upper(state_name) diff --git a/retail_model/fw_ods_duroflex_dealers.sql b/retail_model/fw_ods_duroflex_dealers.sql new file mode 100644 index 0000000..d4ab4f9 --- /dev/null +++ b/retail_model/fw_ods_duroflex_dealers.sql @@ -0,0 +1,96 @@ +drop table if exists fw_ods_duroflex_dealers; +CREATE TABLE fw_ods_duroflex_dealers ( + syspk bigserial NOT NULL, + client_id fw_ods.fw_num_dimid NOT NULL, + function_id fw_ods.fw_num_dimid NOT NULL, + channel fw_ods.fw_text NULL, + dealer_category fw_ods.fw_text NULL, + dealer_code fw_ods.fw_text NULL, + dealer_name fw_ods.fw_text NULL, + sale_type fw_ods.fw_text NULL, + dealer_policy fw_ods.fw_text NULL, + store_type fw_ods.fw_text NULL, + store_closed fw_ods.fw_text NULL, + revamp fw_ods.fw_flag_yn NULL, + revamp_start_date fw_ods.fw_date NULL, + revamp_age_month fw_ods.fw_num_dimid NULL, + convert_start_date fw_ods.fw_date NULL, + convert_age_month fw_ods.fw_num_dimid NULL, + region fw_ods.fw_text NULL, + state_code fw_ods.fw_text NULL, + state_name fw_ods.fw_text NULL, + metro_nonmetro fw_ods.fw_text NULL, + city fw_ods.fw_text NULL, + quantity fw_ods.fw_num_dimid NULL, + revenue_cy_ftm fw_ods.fw_num_metric NULL, + revenue_cy_ytd fw_ods.fw_num_metric NULL, + revenue_cy_ams fw_ods.fw_num_metric NULL, + revenue_py_ftm fw_ods.fw_num_metric NULL, + revenue_py_ytd fw_ods.fw_num_metric NULL, + revenue_py_ams fw_ods.fw_num_metric NULL, + revenue_goly fw_ods.fw_num_metric NULL, + target_cy_tams fw_ods.fw_num_metric NULL, + target_py_tams fw_ods.fw_num_metric NULL, + sellprice_average fw_ods.fw_num_metric NULL, + hml_growth fw_ods.fw_text NULL, + equivalent_store fw_ods.fw_num_dimid NULL, + operational_months fw_ods.fw_num_metric NULL, + operational_cy_months fw_ods.fw_num_metric NULL, + active_stores fw_ods.fw_flag_yn NULL, + invoice_yyyy fw_ods.fw_num_dimid NULL, + invoice_qq fw_ods.fw_num_dimid NULL, + invoice_mm fw_ods.fw_num_dimid NULL, + invoice_yyyymm fw_ods.fw_num_dimid NULL, + fy_name fw_ods.fw_text NULL, + fy_month fw_ods.fw_num_dimid NULL, + fy_quarter fw_ods.fw_num_dimid NULL, + district fw_ods.fw_text null, + store_lat fw_ods.fw_num_metric null, + store_long fw_ods.fw_num_metric null, + report_channel fw_ods.fw_text NULL, + report_dealer_policy fw_ods.fw_text NULL, + created_by fw_ods.fw_text NOT NULL, + updated_by fw_ods.fw_text NOT NULL, + create_timestamp fw_ods.fw_timestamp NOT NULL DEFAULT now(), + update_timestamp fw_ods.fw_timestamp NOT NULL DEFAULT now(), + CONSTRAINT fw_ods_duroflex_dealers_pkey PRIMARY KEY (syspk) +); +CREATE INDEX idx_fw_ods_duroflex_dealers_report_channel ON fw_ods.fw_ods_duroflex_dealers USING btree (report_channel); +CREATE INDEX idx_fw_ods_duroflex_dealers_dealer_code ON fw_ods.fw_ods_duroflex_dealers USING btree (dealer_code); +CREATE INDEX idx_fw_ods_duroflex_dealers_invoice_mm ON fw_ods.fw_ods_duroflex_dealers USING btree (invoice_mm); +CREATE INDEX idx_fw_ods_duroflex_dealers_invoice_yyyy ON fw_ods.fw_ods_duroflex_dealers USING btree (invoice_yyyy); + + +INSERT INTO fw_ods.fw_ods_duroflex_dealers +(client_id, function_id, channel, dealer_category, dealer_code, dealer_name, sale_type, dealer_policy, store_type, +store_closed, revamp, revamp_start_date, revamp_age_month, convert_start_date, convert_age_month, region, state_code, +state_name, metro_nonmetro, city, quantity, revenue_cy_ftm, revenue_cy_ytd, revenue_cy_ams, revenue_py_ftm, +revenue_py_ytd, revenue_py_ams, target_cy_tams, target_py_tams, hml_growth, +equivalent_store, operational_months, operational_cy_months, active_stores, invoice_yyyy, invoice_qq, +invoice_mm, invoice_yyyymm, fy_name, fy_month, fy_quarter, district, store_lat, store_long, report_channel, +report_dealer_policy, created_by, updated_by) + + +SELECT client_id, function_id, channel, dealer_category, dealer_code, dealer_name, sale_type, dealer_policy, store_type, +store_closed, revamp, revamp_start_date, revamp_age_month, convert_start_date, convert_age_month, region, state_code, +state_name, metro_nonmetro, city, sum(quantity) quantity, sum(revenue_cy_ftm) revenue_cy_ftm, sum(revenue_cy_ytd) revenue_cy_ytd, +sum(revenue_cy_ams) revenue_cy_ams, sum(revenue_py_ftm) revenue_py_ftm, sum(revenue_py_ytd) revenue_py_ytd, +sum(revenue_py_ams) revenue_py_ams, sum(target_cy_tams) target_cy_tams, sum(target_py_tams) target_py_tams, +hml_growth, equivalent_store, operational_months, operational_cy_months, active_stores, invoice_yyyy, invoice_qq, +invoice_mm, invoice_yyyymm, fy_name, fy_month, fy_quarter, district, store_lat, store_long, report_channel, +report_dealer_policy, created_by, updated_by +FROM fw_ods.fw_ods_duroflex +group by client_id, function_id, channel, dealer_category, dealer_code, dealer_name, sale_type, dealer_policy, store_type, +store_closed, revamp, revamp_start_date, revamp_age_month, convert_start_date, convert_age_month, region, state_code, +state_name, metro_nonmetro, city, hml_growth, +equivalent_store, operational_months, operational_cy_months, active_stores, invoice_yyyy, invoice_qq, +invoice_mm, invoice_yyyymm, fy_name, fy_month, fy_quarter, district, store_lat, store_long, report_channel, +report_dealer_policy, created_by, updated_by; + +update fw_ods.fw_ods_duroflex_dealers +set sellprice_average = revenue_cy_ftm / quantity +where quantity is not null and quantity <> 0; + +update fw_ods.fw_ods_duroflex_dealers +set revenue_goly = case when revenue_py_ytd <> 0 and revenue_py_ytd is not null then (revenue_cy_ytd/revenue_py_ytd) -1 +else null end; \ No newline at end of file diff --git a/retail_model/fw_ods_monthly_v1.2_20210715.sql b/retail_model/fw_ods_monthly_v1.2_20210715.sql new file mode 100644 index 0000000..2b3267f --- /dev/null +++ b/retail_model/fw_ods_monthly_v1.2_20210715.sql @@ -0,0 +1,95 @@ +-- DROP TABLE IF EXISTS fw_ods.fw_ods_monthly; + +CREATE TABLE fw_ods.fw_ods_monthly ( + syspk bigserial NOT NULL, + client_id fw_ods.fw_num_dimid NOT NULL, + function_id fw_ods.fw_num_dimid NOT NULL, + channel fw_ods.fw_text NULL, + dealer_category fw_ods.fw_text NULL, + dealer_code fw_ods.fw_text NULL, + dealer_name fw_ods.fw_text NULL, + sale_type fw_ods.fw_text NULL, + transaction_type fw_ods.fw_text NULL, + dealer_policy fw_ods.fw_text NULL, + store_type fw_ods.fw_text NULL, + store_closed fw_ods.fw_text NULL, + revamp fw_flag_YN NULL, + revamp_start_date fw_ods.fw_date NULL, + revamp_age_month fw_ods.fw_num_dimid NULL, + convert_start_date fw_ods.fw_date NULL, + convert_age_month fw_ods.fw_num_dimid NULL, + prod_mainhierachy fw_ods.fw_text NULL, + prod_category fw_ods.fw_text NULL, + prod_group fw_ods.fw_text NULL, + prod_name fw_ods.fw_text NULL, + region fw_ods.fw_text NULL, + state_code fw_ods.fw_text NULL, + state_name fw_ods.fw_text NULL, + metro_nonmetro fw_ods.fw_text NULL, + city fw_ods.fw_text NULL, + quantity fw_ods.fw_num_dimid NULL, + revenue_cy_ftm fw_ods.fw_num_metric NULL, + revenue_cy_ytd fw_ods.fw_num_metric NULL, + revenue_cy_ams fw_ods.fw_num_metric NULL, + revenue_py_ftm fw_ods.fw_num_metric NULL, + revenue_py_ytd fw_ods.fw_num_metric NULL, + revenue_py_ams fw_ods.fw_num_metric NULL, + revenue_goly fw_ods.fw_num_metric NULL, + target_cy_tams fw_ods.fw_num_metric NULL, + target_py_tams fw_ods.fw_num_metric NULL, + sellprice_average fw_ods.fw_num_metric NULL, + hml_growth fw_ods.fw_text NULL, + equivalent_store fw_ods.fw_num_dimid NULL, + operational_months fw_ods.fw_num_metric NULL, + operational_cy_months fw_ods.fw_num_metric NULL, + active_stores fw_flag_YN NULL, + invoice_yyyy fw_ods.fw_num_dimid NULL, + invoice_qq fw_ods.fw_num_dimid NULL, + invoice_mm fw_ods.fw_num_dimid NULL, + created_by fw_ods.fw_text NOT NULL, + updated_by fw_ods.fw_text NOT NULL, + create_timestamp fw_ods.fw_timestamp NOT NULL DEFAULT now(), + update_timestamp fw_ods.fw_timestamp NOT NULL DEFAULT now(), + CONSTRAINT fw_ods_monthly_pkey PRIMARY KEY (syspk) +); + + +INSERT INTO fw_ods_monthly +(client_id, function_id, channel, dealer_category, dealer_code, dealer_name, sale_type, dealer_policy, store_type, +revamp, revamp_start_yyyymm, revamp_age_month, convert_start_yyyymm, convert_age_month, prod_category, prod_group, prod_name, +region, state_code, state_name, metro_nonmetro, city, revenue_cy_ftm, revenue_cy_ytd, revenue_cy_ams, revenue_py_ftm, revenue_py_ytd, +revenue_py_ams, revenue_goly, target_cy_tams, target_py_tams, sellprice_average, hml_grouwth, equivalent_store, +operational_months, operational_cy_months, active_stores, billed_stores, invoice_yyyy, invoice_qq, invoice_mm, created_by, updated_by) + +select 40 as client_id, 1 as function_id, dcf.channelformat as channel, null as dealer_category, dde.dealercode as dealer_code, +dde.dealername as dealer_name, null as sale_type, dde.policy as dealer_policy, dde.storetype as store_type, +case when upper(revamp) = 'YES' then 'Y' when upper(revamp) = 'NO' then 'Y' else null end revamp, +case when dde.dateofrevamp is null then null else concat(date_part('year'::text, dateofrevamp),lpad(date_part('month'::text, dateofrevamp)::text,2,'0'))::int end revamp_start_yyyymm, +null::int as revamp_age_month, +case when dde.conversiondate is null then null else concat(date_part('year'::text, dde.conversiondate),lpad(date_part('month'::text, dde.conversiondate)::text,2,'0'))::int end convert_start_yyyymm, +null::int as convert_age_month, +dpr.productcategory as prod_category, dpr.productgroup as prod_group, dpr.productname as prod_name, +dge.region as region, null as state_code, dge.geolocationstate as state_name, dge.metro_nonmetro as metro_nonmetro, dge.geolocationcity as city, +sum(amount) as revenue_cy_ftm, null as revenue_cy_ytd, null as revenue_cy_ams, null as revenue_py_ftm, null as revenue_py_ytd, +null revenue_py_ams, null as revenue_goly, null as target_cy_tams, null as target_py_tams, +--case when sum(quantity) is null then null else (sum(amount)/sum(quantity)) end as sellprice_average, +null as sellprice_average, +null as hml_grouwth, dde.equivalentstore as equivalent_store, null as operational_months, null as operational_cy_months, +null as active_stores, null as billed_stores, +date_part('year'::text, invoicedate) as invoice_yyyy, date_part('quarter'::text, invoicedate) as invoice_qq, +date_part('month'::text, invoicedate) as invoice_mm, current_user, current_user +FROM dbo.factsecondarysales fss +inner join dbo.dimchannelformat dcf on fss.channelformatkey = dcf.channelformatkey +inner join dbo.dimtransactiontype dtt on fss.transactiontypekey = dtt.transactiontypekey +inner join dbo.dimdealer dde on fss.dealerkey = dde.dealerkey +inner join dbo.dimdistributor ddi on fss.distributorkey = ddi.distributorkey +inner join dbo.dimproduct dpr on fss.productkey = dpr.productkey +inner join dbo.dimgeography dge on fss.geographykey = dge.geographykey + +group by dcf.channelformat, dde.dealercode, dde.dealername, dtt.transactiontype, dde.policy, dde.storetype, +revamp, revamp_start_yyyymm, convert_start_yyyymm, dde.equivalentstore, dpr.mainhierarchy, dpr.productcategory, +dpr.productgroup, dpr.productname, dge.region, dge.geolocationstate, dge.geolocationcity, dge.metro_nonmetro, +invoice_yyyy, invoice_qq, invoice_mm; + +update fw_ods.fw_ods_duroflex +set invoice_yyyymm = concat(invoice_yyyy,lpad(invoice_mm::text,2,'0'))::int \ No newline at end of file diff --git a/retail_model/fw_ods_monthly_v2.sql b/retail_model/fw_ods_monthly_v2.sql new file mode 100644 index 0000000..b7e5750 --- /dev/null +++ b/retail_model/fw_ods_monthly_v2.sql @@ -0,0 +1,127 @@ +DROP TABLE IF EXISTS public.fw_ods_monthly; + +CREATE TABLE public.fw_ods_monthly ( +syspk serial NOT NULL unique, +client_id int , +function_id text , +d1l1_syspk int , +d1l1_id int , +d1l1_name text , +d1l2_syspk int , +d1l2_id int , +d1l2_name text , +d1l3_syspk int , +d1l3_id int , +d1l3_name text , +d1l4_syspk int , +d1l4_id int , +d1l4_name text , +d2l1_syspk int , +d2l1_id int , +d2l1_name text , +d2l2_syspk int , +d2l2_id int , +d2l2_name text , +d2l3_syspk int , +d2l3_id int , +d2l3_name text , +d3l1_syspk int , +d3l1_id int , +d3l1_name text , +d4l1_date timestamp , +d4l1_yyyy int , +d4l1_qq int , +d4l1_mm int , +d4l1_ww int , +d4l1_dd int , +d5l1_syspk int , +d5l1_id int , +d5l1_name text , +d5l2_syspk int , +d5l2_id int , +d5l2_name text , +d5l3_syspk int , +d5l3_name text , +d6l1_id int , +d6l1_name text , +m1001 int , +m1002 int , +m1003 int , +m1004 int , +m1005 int , +created_by text , +updated_by text , +create_time timestamp DEFAULT CURRENT_TIMESTAMP , +update_time timestamp +); + + +insert into fw_ods_monthly(client_id, d1l1_name, d1l2_name, d1l3_name, d1l4_id, d2l1_name, d4l1_yyyy, d4l1_qq, d4l1_mm, d5l1_id, d5l1_name, m1001, m1002, m1003, m1004) + +select +10 as client_id, +c.product_category as d1l1_name, +c.product_family as d1l2_name, +c.product_type as d1l3_name, +c.id as d1l4_id, +d.country as d2l1_name, +date_part('year'::text,b.txn_time) as d4l1_yyyy, +date_part('quarter'::text,b.txn_time) as d4l1_qq, +date_part('month'::text,b.txn_time) as d4l1_mm, +b.retailer_id as d5l1_id, +d.name as d5l1_name, +count(distinct b.txn_id) as m1001, +sum(a.quantity) as m1002, +sum(a.price) as m1003, +sum(c.cost_price) as m1004 +FROM customer_salesorder a +JOIN customer_sales b ON a.salesorder_id = b.salesorder_id +JOIN products c ON a.product_id = c.id +JOIN retailers d ON b.retailer_id = d.id +GROUP by client_id, d1l1_name, d1l2_name, d1l3_name, d1l4_id, d2l1_name, d5l1_id, d5l1_name, d4l1_yyyy, d4l1_qq, d4l1_mm; + +update public.fw_ods_monthly a +set d3l1_name = b.salesman +from public.tmp_salesman b +where trim(a.d2l1_name) = trim(b.d2) +and trim(a.d5l1_name) = trim(b.d5) +and a.d4l1_qq = b.d4l2; + +update public.fw_ods_monthly a +set d1l4_name = c.prod_name +from public.product_id_map b, product_summary c +where d1l4_id = b.id +and b.prod_id = c.prod_id; + +update public.fw_ods_monthly +set d5l2_name = 'Wholesale Retailer' +where d5l1_name in ('METRO'); + +update public.fw_ods_monthly +set d5l2_name = 'Small Scale Retailer' +where d5l1_name in ('Aeon'); + +update public.fw_ods_monthly +set d5l2_name = 'Enterprises' +where d5l1_name in ('Tesco'); + +update public.fw_ods_monthly +set d5l2_name = 'Refurbished Dealer' +where d5l1_name in ('Auchan'); + + +update public.fw_ods_monthly +set d5l2_name = 'Refurbished Retailer' +where d5l1_name in ('Kroger'); + +update public.fw_ods_monthly +set d5l2_name = 'Digital Outlet' +where d5l1_name in ('Walmart'); + +update public.fw_ods_monthly +set d5l2_name = 'Wholesale Dealer' +where d5l1_name in ('Walgreen'); + +update public.fw_ods_monthly +set d5l2_name = 'Retail Outlet' +where d5l1_name in ('Aldi'); diff --git a/retail_model/nandus_ods_monthly.sql b/retail_model/nandus_ods_monthly.sql new file mode 100644 index 0000000..b2fa8dd --- /dev/null +++ b/retail_model/nandus_ods_monthly.sql @@ -0,0 +1,29 @@ +drop table if exists public.nandus_ods_monthly; +create table public.nandus_ods_monthly as +select 3 as client_id,1 as function_id,b.item_category,b.item_name,'Bangalore' as city,a.outlet_name,a.order_type,date(a.bill_date_and_time),a.customer_phone,a.customer_name, +a.customer_type,bill_header_id,sum(b.quantity) as quantity, +b.item_rate, +sum(b.item_amt) as item_amount, +sum(b.discount_amt) as discount_amt, +sum(b.final_item_amt) as final_item_amt + from billing_header a join billing_line b on a.id=b.bill_header_id join store_master c on a.outlet_code=c.outlet_code + group by b.item_category,b.item_name,a.outlet_name,a.order_type,date(a.bill_date_and_time),a.customer_phone,a.customer_name,a.customer_type,bill_header_id,b.item_rate; + + +update public.nandus_ods_monthly set item_age_months=extract(year from age(now(), b.createdat)) * 12 +extract(month from age(now(), b.createdat)) +from public.item_master b +where nandus_ods_monthly.item_name=b.name; + +update public.nandus_ods_monthly set first_order_date = b.first_date +from (select min(date(bill_date_and_time)) as first_date,customer_phone from public.billing_header group by customer_phone)b +where nandus_ods_monthly.customer_phone=b.customer_phone; + +update public.nandus_ods_monthly A set store_age_months = (select B.store_age_months from public.store_master B where A.outlet_name = B.outlet_name); +update public.nandus_ods_monthly A set store_age_months = (select B.item_age_months from public.item_master B where A.item_name = B.name); + + +update public.nandus_ods_monthly set customer_age_months = ((current_date - DATE(first_order_date)) / 30); + +update public.nandus_ods_monthly a set week_number_moon_status=b.moonweek_sequence from public.fw_lunar_calendar b where a.date=b.date; + +update public.nandus_ods_monthly a set week_number=date_part('week',date); diff --git a/retail_model/nandus_waterfall_monthly_20200101.sql b/retail_model/nandus_waterfall_monthly_20200101.sql new file mode 100644 index 0000000..5520488 --- /dev/null +++ b/retail_model/nandus_waterfall_monthly_20200101.sql @@ -0,0 +1,139 @@ + +CREATE TABLE t_nandu_waterfallv3 ( + current_month int NULL, + previous_month int NULL, + start_count int NULL, + new_count int NULL, + returned_count int NULL, + in_active_count int NULL, + customer_type text NULL +); + +insert into t_nandu_waterfallv3 + +with tbl_in_active as ( + with tbl_returned as ( + with tbl_new as ( + with period_master as ( + select distinct d4l1_mm current_month, + d4l1_mm - 1 previous_month + from fw_ods_monthly + where client_id = 3 + and function_id = 1 + order by d4l1_mm desc + ) + select y.current_month, y.previous_month, count(distinct d5l1_phone) as start_count + from fw_ods_monthly x, period_master y + where d4l1_mm = y.previous_month + and x.client_id = 3 + and x.function_id = 1 + and x.d5l2_name = 'Wholesale' + group by y.current_month, y.previous_month + ) + select b.current_month, b.previous_month, b.start_count, + (select count(distinct d5l1_phone) from fw_ods_monthly a + where a.client_id = 3 and a.function_id = 1 + and a.d4l1_mm = b.current_month + and a.d5l2_name = 'Wholesale' + and not exists (select 1 from fw_ods_monthly c where c.client_id = 3 and c.function_id = 1 + and c.d5l2_name = 'Wholesale' + and c.d5l1_phone = a.d5l1_phone + and c.d4l1_mm <= b.previous_month)) + as new_count + from tbl_new b + ) + select b.current_month, b.previous_month, b.start_count, b.new_count, + (select count(distinct d5l1_phone) from fw_ods_monthly a + where a.client_id = 3 and a.function_id = 1 + and a.d5l2_name = 'Wholesale' + and d4l1_mm = b.current_month + and exists (select 1 from fw_ods_monthly c where c.client_id = 3 and c.function_id = 1 + and c.d5l2_name = 'Wholesale' + and c.d5l1_phone = a.d5l1_phone + and c.d4l1_mm < b.current_month + and c.d5l1_phone not in (select distinct d5l1_phone from fw_ods_monthly + where client_id = 3 and function_id = 1 + and d5l2_name = 'Wholesale' + and d4l1_mm = b.previous_month))) + as returned_count + from tbl_returned b + group by b.current_month, b.previous_month, b.start_count, b.new_count +) +select b.current_month, b.previous_month, b.start_count, b.new_count, b.returned_count, +(select count(distinct d5l1_phone) from fw_ods_monthly a +where a.client_id = 3 and a.function_id = 1 +and a.d5l2_name = 'Wholesale' +and a.d4l1_mm = b.previous_month +and not exists (select 1 from fw_ods_monthly c +where c.client_id = 3 and c.function_id = 1 +and c.d5l2_name = 'Wholesale' + and c.d4l1_mm = b.current_month +and c.d5l1_phone = a.d5l1_phone)) +as in_active_count, 'Wholesale' +from tbl_in_active b +group by b.current_month, b.previous_month, b.start_count, b.new_count, b.returned_count +order by b.current_month, b.previous_month; + + +with tbl_in_active as ( + with tbl_returned as ( + with tbl_new as ( + with period_master as ( + select distinct d4l1_mm current_month, + d4l1_mm - 1 previous_month + from fw_ods_monthly + where client_id = 3 + and function_id = 1 + order by d4l1_mm desc + ) + select y.current_month, y.previous_month, count(distinct d5l1_phone) as start_count + from fw_ods_monthly x, period_master y + where d4l1_mm = y.previous_month + and x.client_id = 3 + and x.function_id = 1 + and x.d5l2_name = 'Individual' + group by y.current_month, y.previous_month + ) + select b.current_month, b.previous_month, b.start_count, + (select count(distinct d5l1_phone) from fw_ods_monthly a + where a.client_id = 3 and a.function_id = 1 + and a.d4l1_mm = b.current_month + and a.d5l2_name = 'Individual' + and not exists (select 1 from fw_ods_monthly c where c.client_id = 3 and c.function_id = 1 + and c.d5l2_name = 'Individual' + and c.d5l1_phone = a.d5l1_phone + and c.d4l1_mm <= b.previous_month)) + as new_count + from tbl_new b + ) + select b.current_month, b.previous_month, b.start_count, b.new_count, + (select count(distinct d5l1_phone) from fw_ods_monthly a + where a.client_id = 3 and a.function_id = 1 + and a.d5l2_name = 'Individual' + and d4l1_mm = b.current_month + and exists (select 1 from fw_ods_monthly c where c.client_id = 3 and c.function_id = 1 + and c.d5l2_name = 'Individual' + and c.d5l1_phone = a.d5l1_phone + and c.d4l1_mm < b.current_month + and c.d5l1_phone not in (select distinct d5l1_phone from fw_ods_monthly + where client_id = 3 and function_id = 1 + and d5l2_name = 'Individual' + and d4l1_mm = b.previous_month))) + as returned_count + from tbl_returned b + group by b.current_month, b.previous_month, b.start_count, b.new_count +) +select b.current_month, b.previous_month, b.start_count, b.new_count, b.returned_count, +(select count(distinct d5l1_phone) from fw_ods_monthly a +where a.client_id = 3 and a.function_id = 1 +and a.d5l2_name = 'Individual' +and a.d4l1_mm = b.previous_month +and not exists (select 1 from fw_ods_monthly c +where c.client_id = 3 and c.function_id = 1 +and c.d5l2_name = 'Individual' + and c.d4l1_mm = b.current_month +and c.d5l1_phone = a.d5l1_phone)) +as in_active_count, 'Individual' +from tbl_in_active b +group by b.current_month, b.previous_month, b.start_count, b.new_count, b.returned_count +order by b.current_month, b.previous_month; \ No newline at end of file diff --git a/retail_model/nandus_waterfall_monthly_cust_analysis_20200101.sql b/retail_model/nandus_waterfall_monthly_cust_analysis_20200101.sql new file mode 100644 index 0000000..201ef34 --- /dev/null +++ b/retail_model/nandus_waterfall_monthly_cust_analysis_20200101.sql @@ -0,0 +1,336 @@ +-- this is needed to draw the waterfall viz. note that waterfall doesn't give the data for the first week/month of given period. cust analysis gives it +-- update the vm4 and vm5 ( in active count and onceanddone count for the first week from the next weeeks , to ensure consistency of data for now + +-- to get the week beginnning and end of the week. +update fw_ods_viz_data_generic a +set from_date = b.start_date, to_date = b.end_date +from ( + SELECT distinct date_trunc('week', d4l1_date)::date start_date, +(date_trunc('week', d4l1_date)+ '6 days'::interval)::date end_date, d4l1_ww week_number + from fw_ods_monthly where client_id = 3 and function_id = 1 order by d4l1_ww ) b + where a.va1::int = b.week_number + and a.client_id = 3 and function_id = 2 + +-- one and done customers + +CREATE TABLE t_nandus_cust_analysis_v3 ( + current_month int NULL, + previous_month int NULL, + customer_type Text null, + total_customers int null, + new_customers int null, + continuing_customers int NULL, + onceanddone_customers int null, + avg_revisit_frequency_percentage int null +); + +-- one and done customers + +-- total individual customers +insert into t_nandus_cust_analysis_v3 ( current_month, total_customers) +select d4l1_mm, count(distinct d5l1_phone) +from fw_ods_monthly nom +where client_id = 3 and function_id = 1 and d5l2_name = 'Individual' +group by d4l1_mm + +update t_nandus_cust_analysis_v3 set customer_type = 'Individual' where customer_type is null + +-- total wholesale customers +insert into t_nandus_cust_analysis_v3 ( current_month, total_customers) +select d4l1_mm, count(distinct d5l1_phone) +from fw_ods_monthly nom +where client_id = 3 and function_id = 1 and d5l2_name = 'Wholesale' +group by d4l1_mm + +update t_nandus_cust_analysis_v3 set customer_type = 'Wholesale' where customer_type is null + +-- new individual customers +update t_nandus_cust_analysis_v3 x +set new_customers = y.new_count +from ( +select d4l1_mm , count(distinct d5l1_phone) new_count +from fw_ods_monthly a + where a.client_id = 3 and a.function_id = 1 + and a.d5l2_name = 'Individual' + and not exists (select 1 from fw_ods_monthly c where c.client_id = 3 and c.function_id = 1 + and c.d5l2_name = 'Individual' + and c.d5l1_phone = a.d5l1_phone + and c.d4l1_mm < a.d4l1_mm) +group by d4l1_mm ) Y +where current_month = Y.d4l1_mm +and customer_type = 'Individual' + +-- new Wholesale customers +update t_nandus_cust_analysis_v3 x +set new_customers = y.new_count +from ( +select a.d4l1_mm , count(distinct a.d5l1_phone) new_count +from fw_ods_monthly a + where a.client_id = 3 and a.function_id = 1 + and a.d5l2_name = 'Wholesale' + and not exists (select 1 from fw_ods_monthly c where c.client_id = 3 and c.function_id = 1 + and c.d5l2_name = 'Wholesale' + and c.d5l1_phone = a.d5l1_phone + and c.d4l1_mm < a.d4l1_mm) +group by a.d4l1_mm ) Y +where current_month = Y.d4l1_mm +and customer_type = 'Wholesale' + + -- continuous individual + update t_nandus_cust_analysis_v3 x +set continuing_customers = y.cust_count +from ( + select a.d4l1_mm, count(distinct a.d5l1_phone) cust_count +from fw_ods_monthly a +where a.client_id = 3 and a.function_id = 1 +and a.d5l2_name = 'Individual' +and exists (select 1 from fw_ods_monthly c + where c.client_id = a.client_id + and c.function_id = a.function_id + and c.d5l2_name = a.d5l2_name + and c.d5l1_phone = a.d5l1_phone + and c.d4l1_mm = a.d4l1_mm - 1) +group by a.d4l1_mm) Y +where current_month = Y.d4l1_mm +and customer_type = 'Individual' + +/* continuous wholesale customers */ + update t_nandus_cust_analysis_v3 x +set continuing_customers = y.cust_count +from ( + select a.d4l1_mm, count(distinct a.d5l1_phone) cust_count +from fw_ods_monthly a +where a.client_id = 3 and a.function_id = 1 +and a.d5l2_name = 'Wholesale' +and exists (select 1 from fw_ods_monthly c + where c.client_id = a.client_id + and c.function_id = a.function_id + and c.d5l2_name = a.d5l2_name + and c.d5l1_phone = a.d5l1_phone + and c.d4l1_mm = a.d4l1_mm - 1) +group by a.d4l1_mm) Y +where current_month = Y.d4l1_mm +and customer_type = 'Wholesale' + + -- get onceanddone customer count - individual + update t_nandus_cust_analysis_v3 x +set onceanddone_customers = y.cust_count +from ( + select a.d4l1_mm, count( distinct a.d5l1_phone) cust_count + from fw_ods_monthly a, ( + select distinct d5l1_phone + from fw_ods_monthly + where client_id = 3 and function_id = 1 + and d5l2_name = 'Individual' + group by d5l1_phone + having count( distinct( d6l1_name) ) = 1 + ) b + where a.client_id = 3 and a.function_id = 1 + and a.d5l1_phone = b.d5l1_phone + and a.d5l2_name = 'Individual' + group by a.d4l1_mm) Y +where current_month = Y.d4l1_mm +and customer_type = 'Individual' + +-- get onceanddone customer count - wholesale + update t_nandus_cust_analysis_v3 x +set onceanddone_customers = y.cust_count +from ( + select a.d4l1_mm, count( distinct a.d5l1_phone) cust_count + from fw_ods_monthly a, ( + select distinct d5l1_phone + from fw_ods_monthly + where client_id = 3 and function_id = 1 + and d5l2_name = 'Wholesale' + group by d5l1_phone + having count( distinct( d6l1_name) ) = 1 + ) b + where a.client_id = 3 and a.function_id = 1 + and a.d5l1_phone = b.d5l1_phone + and a.d5l2_name = 'Wholesale' + group by a.d4l1_mm) Y +where current_month = Y.d4l1_mm +and customer_type = 'Wholesale' + + + update t_nandus_cust_analysis_v3 x +set previous_month = current_month - 1 + +update t_nandus_cust_analysis_v3 +set avg_revisit_frequency_percentage = round ( continuing_customers * 100/total_customers) + +update t_nandus_cust_analysis_v3 +set continuing_customers = 0 +where current_month = 6 + +select * from t_nandus_cust_analysis_v3 +order by customer_type, current_month + +-- nagaraj 27237 individual + select count(*) from ( +select customer_phone, count(distinct(bill_header_id)) as order_count from nandus_ods_monthly_6months +where fw_customer_type = 'Individual' +group by customer_phone having count(distinct(bill_header_id)) =1 +) as FOO + +--nagaraj wholesale + select count(*) from ( +select customer_phone, count(distinct(bill_header_id)) as order_count from nandus_ods_monthly_months +where fw_customer_type = 'Wholesale' +group by customer_phone having count(distinct(bill_header_id)) =1 +) as FOO + +-- checking in rfm table having frequecy of 1 to cross verify. +select count(*) from mtmp_rfm_customer where f_Value = 1 + +-- update the generic table for vm5 - once and done customers. individual +update fw_ods_viz_data_generic a set vm5 = b.onceanddone_customers +from t_nandus_cust_analysis_v3 b +where client_id = 3 and function_id = 1 +and va3 = customer_type and b.current_month::text = a.va1 and customer_type = 'Individual' + +-- update the generic table for vm5 - once and done customers. Wholesale +update fw_ods_viz_data_generic a set vm5 = b.onceanddone_customers +from t_nandus_cust_analysis_v3 b +where client_id = 3 and function_id = 1 +and va3 = customer_type and b.current_week::text = a.va1 and customer_type = 'Wholesale' + + + +select * from fw_ods_viz_data_generic where client_id = 3 and function_id = 1 and viztype_name = 'Waterfall' order by va3, va1 + + +select * from fw_ods_viz_data_generic where client_id = 3 and function_id = 1 and viztype_name = 'Waterfall' and va3 = 'Individual' order by va3, va1 + +select * from fw_ods_viz_data_generic where client_id = 3 and function_id = 1 and viztype_name = 'Waterfall' and va3 = 'Wholesale' order by va3, va1 + + + + + + + +update fw_ods_viz_data_generic set vm4 = vm6 where client_id = 3 and function_id = 1 and viztype_name = 'Waterfall' and va3 = 'Wholesale'; + +update fw_ods_viz_data_generic set vm4 = vm6 where client_id = 3 and function_id = 1 and viztype_name = 'Waterfall' and va3 = 'Individual'; + + +update fw_ods_viz_data_generic set vm7 = vm5 where client_id = 3 and function_id = 1 and viztype_name = 'Waterfall' and va3 = 'Wholesale'; + +update fw_ods_viz_data_generic set vm7 = vm5 where client_id = 3 and function_id = 1 and viztype_name = 'Waterfall' and va3 = 'Individual'; + +/* update once and done customers for the previous week ( ie calculated for currrent week ) as it is shown against the previous week */ +update fw_ods_viz_data_generic a +set vm5 = b.vm5 +from fw_ods_viz_data_generic b +where a.client_id = 3 and a.function_id = 1 and a.viztype_name = 'Waterfall' and a.va3 = 'Wholesale' +and b.client_id = 3 and b.function_id = 1 and b.viztype_name = 'Waterfall' and b.va3 = 'Wholesale' +and a.va1 = ( b.va1::int - 1)::text + +/* update actual in_active customers as initial_in_active (vm4) - onceanddone customers (vm5) and initi will have both in_active and onceanddone customers ) */ +update fw_ods_viz_data_generic a +set vm4 = a.vm4 - b.vm5 +from fw_ods_viz_data_generic b +where a.client_id = 3 and a.function_id = 1 and a.viztype_name = 'Waterfall' and a.va3 = 'Individual' +and b.client_id = 3 and b.function_id = 1 and b.viztype_name = 'Waterfall' and b.va3 = 'Individual' +and a.va1 = ( b.va1::int + 1)::text + +/* from the 6 months data */ + + select * from t_nandu_waterfallv2 + + delete from fw_ods_viz_data_generic where client_id = 3 and function_id = 2 and viztype_name = 'Waterfall' + + select * from fw_ods_viz_data_generic where client_id = 3 and function_id = 2 and viztype_name = 'Waterfall' + + insert into fw_ods_viz_data_generic ( client_id, function_id, viztype_id, viztype_name, config_screen_element_syspk, run_asof_date, time_period_type, time_period_data_scope, from_date, to_date, vm1, vm2, vm3, vm4,vm5,vm6,vm7, va1, va2, va3, created_by, updated_by) + select 3,3 , 'V03', 'Waterfall', 215, current_date, 'Monthly', 1, current_date, current_date, b.continuing_customers, b.new_customers, a.returned_count, a.in_active_count, b.onceanddone_customers, a.in_active_count, b.onceanddone_customers, a.current_week, a.previous_week, a.customer_type, 'MM', 'MM' + from t_nandu_waterfallv3 a, t_nandus_cust_analysis_v3 b + where a.current_month = b.current_week + and a.previous_week = b.previous_week + and a.customer_type = b.customer_type + order by a.current_week, a.customer_type + + + + insert into fw_ods_viz_data_generic ( client_id, function_id, viztype_id, viztype_name, config_screen_element_syspk, run_asof_date, time_period_type, time_period_data_scope, from_date, to_date, vm1, vm2, vm3, vm4,vm5,vm6,vm7, va1, va2, va3, created_by, updated_by) + + + select 3,3 , 'V03', 'Waterfall', 215, current_date, 'Monthly', 1, current_date, current_date, b.continuing_customers, b.new_customers, 0, 0, b.onceanddone_customers, 0, b.onceanddone_customers, b.current_month, b.previous_month, b.customer_type, 'MM', 'MM' + from t_nandus_cust_analysis_v3 b + order by b.customer_type, b.current_month + + update fw_ods_viz_data_generic b + set vm3 = a.returned_count, vm4 = a.in_active_count, vm6 = a.in_active_count + from t_nandu_waterfallv3 a + where b.client_id = 3 and b.function_id = 3 + and a.customer_type = b.customer_type + and a.current_month::text = b.va1 + and b.viztype_name = 'Waterfall' + + + + /* update vm5 from next week */ + update fw_ods_viz_data_generic a +set vm5 = b.vm5 +from fw_ods_viz_data_generic b +where a.client_id = 3 and a.function_id = 3 and a.viztype_name = 'Waterfall' and a.va3 = 'Wholesale' +and b.client_id = 3 and b.function_id = 3 and b.viztype_name = 'Waterfall' and b.va3 = 'Wholesale' +and a.va1 = ( b.va1::int - 1)::text; + + update fw_ods_viz_data_generic a +set vm5 = b.vm5 +from fw_ods_viz_data_generic b +where a.client_id = 3 and a.function_id = 3 and a.viztype_name = 'Waterfall' and a.va3 = 'Individual' +and b.client_id = 3 and b.function_id = 3 and b.viztype_name = 'Waterfall' and b.va3 = 'Individual' +and a.va1 = ( b.va1::int - 1)::text; + + + update fw_ods_viz_data_generic a +set vm4 = a.vm4 - b.vm5 +from fw_ods_viz_data_generic b +where a.client_id = 3 and a.function_id = 3 and a.viztype_name = 'Waterfall' and a.va3 = 'Wholesale' +and b.client_id = 3 and b.function_id = 3 and b.viztype_name = 'Waterfall' and b.va3 = 'Wholesale' +and a.va1 = ( b.va1::int + 1)::text + -- distinct 563, shopped only once 344 , more than once is + update fw_ods_viz_data_generic a +set vm4 = a.vm4 - b.vm5 +from fw_ods_viz_data_generic b +where a.client_id = 3 and a.function_id = 3 and a.viztype_name = 'Waterfall' and a.va3 = 'Individual' +and b.client_id = 3 and b.function_id = 3 and b.viztype_name = 'Waterfall' and b.va3 = 'Individual' +and a.va1 = ( b.va1::int + 1)::text + +update fw_ods_viz_data_generic set vm4 = vm6 where client_id = 3 and function_id = 3 and viztype_name = 'Waterfall' + +update fw_ods_viz_data_generic set vm4 = vm6 where client_id = 3 and function_id = 3 and viztype_name = 'Individual' + +/* queries from viz to represent the data - metric1 - continuing, m2 - returning, m3 - new, m4 - inactive , m5 oneanddone */ +select distinct c.moonweek_sequence as dim1_name, b.vm1 as metric1, b.vm3 as metric2, +b.vm2 as metric3,coalesce (( select vm4 from fw_ods_viz_data_generic a where a.client_id = 3 +and function_id=2 and a.viztype_name= 'Waterfall' and a.va3= 'Wholesale' and a.va1::int = b.va1::int + 1 ), 0 ) as metric4, +vm5 as metric5 from fw_ods_viz_data_generic b join fw_calendar_6months +c on b.va1 =c.week_number::text where b.viztype_name= 'Waterfall' +and b.va3= 'Wholesale' and b.client_id=3 and function_id=2; + +select distinct c.moonweek_sequence as dim1_name, b.vm1 as metric1, b.vm3 as metric2, +b.vm2 as metric3,coalesce (( select vm4 from fw_ods_viz_data_generic a where a.client_id = 3 +and function_id=2 and a.viztype_name= 'Waterfall' and a.va3= 'Individual' and a.va1::int = b.va1::int + 1 ), 0 ) as metric4, +vm5 as metric5 from fw_ods_viz_data_generic b join fw_calendar_6months +c on b.va1 =c.week_number::text where b.viztype_name= 'Waterfall' +and b.va3= 'Wholesale' and b.client_id=3 and function_id=2; + + +select distinct c.moonweek_sequence as dim1_name, b.vm1 as metric1, b.vm3 as metric2, +b.vm2 as metric3,coalesce (( select vm4 from zbkp_fw_ods_viz_data_generic_wf_20201225 a where a.client_id = 3 +and function_id=1and a.viztype_name= 'Waterfall' and a.va3= 'Wholesale' and a.va1::int = b.va1::int + 1 ), 0 ) as metric4, +vm5 as metric5 from zbkp_fw_ods_viz_data_generic_wf_20201225 b join fw_calendar_6months +c on b.va1 =c.week_number::text where b.viztype_name= 'Waterfall' +and b.va3= 'Wholesale' and b.client_id=3 and function_id=1; + +select distinct c.moonweek_sequence as dim1_name, b.vm1 as metric1, b.vm3 as metric2, +b.vm2 as metric3,coalesce (( select vm4 from zbkp_fw_ods_viz_data_generic_wf_20201225 a where a.client_id = 3 +and function_id=1 and a.viztype_name= 'Waterfall' and a.va3= 'Individual' and a.va1::int = b.va1::int + 1 ), 0 ) as metric4, +vm5 as metric5 from zbkp_fw_ods_viz_data_generic_wf_20201225 b join fw_calendar_6months +c on b.va1 =c.week_number::text where b.viztype_name= 'Waterfall' +and b.va3= 'Wholesale' and b.client_id=3 and function_id=1; +