raena_analytics_scripts/postgresql/loyalty_point/loyalty_point_etl.sql

121 lines
3.3 KiB
SQL

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;
*/