raena_analytics_scripts/redshift/sql_code/payment_dashboard.sql

136 lines
5.3 KiB
MySQL
Raw Normal View History

2024-06-24 12:26:08 +00:00
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;