You are on page 1of 4

WITH category_tab as (

select c0.category_id
, c2.name main_category
, c1.name sub_category
, c0.name L3_category
from
digitalpurchase.shopee_digital_product_item_vn_db__category_tab__reg_daily_s0_live
c0
left join
digitalpurchase.shopee_digital_product_item_vn_db__category_tab__reg_daily_s0_live
c1 on c1.category_id = c0.parent_category
left join
digitalpurchase.shopee_digital_product_item_vn_db__category_tab__reg_daily_s0_live
c2 on c2.category_id = c1.parent_category
)

, e_voucher0 as (

select
o.order_id
, from_unixtime(o.create_time-3600) "Thời Gian Tạo Đơn"
, from_unixtime(o.fulfillment_time-3600) "Thời Gian Giao Code"
, o.item_name "Tên Sản Phẩm"
, cast(json_extract(json_parse(details), '$.pins') as ARRAY<MAP<varchar,
varchar>>) as items
, CAST(json_extract(json_parse(t1.fulfillment_detail), '$.fulfillment_items')
AS ARRAY(MAP(VARCHAR, JSON))
) as check_items

from
digitalpurchase.shopee_digital_product_order_v2_vn_db__order_tab__reg_daily_s0_live
AS o
LEFT join
digitalpurchase.shopee_digital_product_order_v2_vn_db__order_item_tab__reg_daily_s0
_live t1
ON o.order_id = t1.order_id AND o.item_id=t1.item_id
left join category_tab c on o.category_id = c.category_id

where true
and (o.final_price != 0 or (o.payment_status = 'P2' and o.final_price = 0)
) -- valid order
and o.payment_status IN ('P2') and o.refund_status in ('R0', 'MR0') -- net
order, unsuccessful order thì trong cột details hông có unique voucher code
and o.platform = 1
AND o.fulfillment_status NOT IN ('F7')
and c.L3_category = 'E-Voucher'
and date (from_unixtime(o.fulfillment_time - 3600)) >= date '2021-01-01'
and date (from_unixtime(o.payment_time - 3600)) = current_date - interval '1'
day --hoaian.nguyenthi: updated on 29th Sep 2022
)

, preprocess AS (

select
order_id
, CAST(check_item['evoucher_fulfillment_detail'] AS JSON) pre_item

from e_voucher0
cross join unnest(check_items) as check_items(check_item)
)

, e_voucher as

(
SELECT
order_id
, CAST(json_extract(pre_item, '$.code') AS VARCHAR) e_voucher
, CAST(json_extract(pre_item, '$.serial_number') AS VARCHAR) sn
, CAST(json_extract(pre_item, '$.code_url') AS VARCHAR) url
FROM preprocess
)

,merchants as (
select
distinct try_cast(dp_merchant_id as bigint) merchant_id,
dp_merchant_name merchant_name -- , m.name merchant_name_
,
company_name,
coalesce(try_CAST(commission_rate AS DOUBLE), 0) commission_rate,
try_cast(status as bigint) status,
TRY_CAST(send_report as bigint) send_report
from
vnapbi_general.dp_smartvoucher_merchant m_ -- left join
shopee_vn.shopee_digital_product_item_vn_db__evoucher_merchant_outlet_tab m on
try_cast(m_.dp_merchant_id as bigint) = m.merchant_id
where
true
and ingestion_timestamp = (
select
max (ingestion_timestamp)
from
vnapbi_general.dp_smartvoucher_merchant
) -- AND TRY_CAST(status as bigint) = 1
-- AND TRY_CAST(send_report as bigint) = 1
)
,spf_sponsor as (--filter item_id spf SE flash sale sponsor by Shopee, applied from
Dec 2021
select distinct
o.item_id
, date_trunc('month', date (cast(create_date as timestamp )) )
month_applied
from digitalpurchase.vn_promotion_dim_dp_prom_activity_item_tab_info o
join ( select try_cast ( item_promotion_id as int ) item_promotion_id
-- , promotion_id
, max(ingestion_timestamp) max_
from vnbi_mkt.crm_wp_campaign_input
where campaign_name= 'ShopeeFood Initiatives'
group by 1
) i on o.activity_id =i.item_promotion_id
where cast ( grass_date as date)= current_date - interval '1' day
and nomination_err_msg=''
and sub_category_2=30102
)
, final as (
select
'#' || o.order_id "Mã Đơn Hàng"
, from_unixtime(o.create_time-3600) "Thời Gian Tạo Đơn"
, from_unixtime(o.fulfillment_time-3600) "Thời Gian Giao Code"
, from_unixtime(o.payment_time-3600) "Thời Gian Thanh Toán"
, o.item_name "Tên Sản Phẩm"
, case when e.sn = '' then null else e.sn end "Mã Series"
, i.merchant_id "ID Đối Tác"
, m.name AS "Tên Đối Tác"
, o.market_price / 100000 "Giá Trị Voucher"
, (o.market_price - o.original_discount_price) / 100000 +
(coalesce(p_.supplier_subsidy, 0) / 100000) "Phần Trợ Giá từ Đối Tác" -- base +
extra
, (o.market_price / 100000) - ((o.market_price - o.original_discount_price) /
100000 + (coalesce(p_.supplier_subsidy, 0) / 100000)) "Giá Cung Cấp từ Đối Tác"
, m_.commission_rate "Phí Dịch Vụ"
, ((o.market_price / 100000) - ((o.market_price - o.original_discount_price) /
100000 + (coalesce(p_.supplier_subsidy, 0) / 100000))) * (1 - m_.commission_rate)
"Số Tiền Thanh Toán"

-- ✨ for payment internal check ✨--

, case when (o.original_discount_price - o.discount_price) / 100000 -


(coalesce(p_.shopee_subsidy, 0) / 100000 + coalesce(p_.supplier_subsidy, 0) /
100000) = 0 THEN 'matched' ELSE 're-check' END check_rebate
, CAST(json_extract_scalar(items, '$[0].quantity') AS DOUBLE) qty
, o.market_price / 100000 market_price
, o.original_discount_price / 100000 original_discount_price
, o.discount_price / 100000 promotion_price
, (o.original_discount_price - o.discount_price) / 100000 system_rebate

, (o.market_price - o.original_discount_price) / 100000 base_supplier_subsidy


, (coalesce(p_.supplier_subsidy, 0) / 100000) extra_supplier_subsidy
, (o.market_price - o.original_discount_price) / 100000 +
(coalesce(p_.supplier_subsidy, 0) / 100000) total_supplier_subsidy

, if(date(from_unixtime(o.payment_time-3600) ) between date '2021-09-01' and


date '2021-09-30'
, case when i.merchant_id in (1406, 1143276) then
coalesce(p_.shopee_subsidy, 0) / 100000 else 0 end
, if(date(from_unixtime(o.payment_time-3600)) < date '2021-06-01', case
when i.merchant_id not in (1406, 1775) then coalesce(p_.shopee_subsidy, 0) / 100000
else 0 end,
if (date(from_unixtime(o.payment_time-3600)) >= date '2021-12-01',
case when spf.item_id is not null then coalesce(p_.shopee_subsidy, 0) / 100000
else 0 end
, 0)
)
) shopee_subsidy

, if(date(from_unixtime(o.payment_time-3600) ) between date '2021-09-01' and


date '2021-09-30'
, case when i.merchant_id not in (1406, 1143276) then
coalesce(p_.shopee_subsidy, 0) / 100000 else 0 end
, if(date(from_unixtime(o.payment_time-3600)) < date '2021-06-01', case
when i.merchant_id in (1406, 1775) then coalesce(p_.shopee_subsidy, 0) / 100000
else 0 end,
if (date(from_unixtime(o.payment_time-3600)) >= date '2021-12-01',
case when spf.item_id is null then coalesce(p_.shopee_subsidy, 0) / 100000 else 0
end
, coalesce(p_.shopee_subsidy, 0) / 100000)
)
) airpay_subsidy
, cast(json_extract_scalar(items, '$[0].promotion_activity_id') AS DOUBLE)
promotion_activity_id
, case when p_.activity_type = 1 then 'promo' when p_.activity_type = 2 then
'flashsale' when p_.activity_type is null then 'no promo' else 'others' end
promo_type
, o.final_price / 100000 / CAST(json_extract_scalar(items, '$[0].quantity') AS
DOUBLE) final_price -- gmv vnd
, i.item_id item_id

from
digitalpurchase.shopee_digital_product_order_v2_vn_db__order_tab__reg_daily_s0_live
AS o
left join
digitalpurchase.shopee_digital_product_item_vn_db__item_tab__reg_daily_s0_live i on
i.item_id = o.item_id -- item
left join merchants m_ ON m_.merchant_id = i.merchant_id
left join
digitalpurchase.shopee_digital_product_item_vn_db__evoucher_merchant_tab__reg_daily
_s0_live m on i.merchant_id = m.id
left join category_tab c on c.category_id = o.category_id
left join
digitalpurchase.shopee_digital_product_promotion_vn_db__activity_item_tab__reg_dail
y_s0_live AS p_ ON o.item_id = p_.item_id AND CAST(json_extract_scalar(items,
'$[0].promotion_activity_id') AS DOUBLE) = p_.activity_id AND (o.create_time -
3600) BETWEEN (p_.start_time - 3600) AND (p_.end_time - 3600)
left join e_voucher AS e ON o.order_id = e.order_id
left join
digitalpurchase.shopee_integration_hub_order_vn_db__evoucher_epin_tab__reg_daily_s0
_live sn_ on cast(sn_.booking_id as varchar) = o.order_id and sn_.code =
e.e_voucher and e.url = sn_.url
left join
digitalpurchase.shopee_digital_product_promotion_vn_db__activity_tab__reg_daily_s0_
live p__ on p_.id = p__.id
left join spf_sponsor spf ON spf.item_id=o.item_id AND spf.month_applied=
date_trunc('month',date (from_unixtime(o.create_time-3600)))

where true
and date (from_unixtime(o.payment_time - 3600)) = current_date - interval
'1' day
and (o.final_price != 0 or (o.payment_status = 'P2' and o.final_price = 0))
-- valid order
and o.order_status NOT IN ('OR2', 'OR4', 'OR7') -- net order, unsuccessful
order thì trong cột details hông có unique voucher code
and o.platform = 1
and c.L3_category = 'E-Voucher'
order by 3
)

select * from final

You might also like