You are on page 1of 10

drop table if exists dev_vnapbi_general.

shopeepay_vn_prm_cost;
create table if not exists dev_vnapbi_general.shopeepay_vn_prm_cost as (
with
--------------------------------------------- MKP
--------------- Select Ingested tables

shopee_vn_bi_team__airpay_voucher as -- phương thức thanh toán not All thì


rule.spm_channel mới có giá trị
(
select distinct cast(promotionid as varchar) voucherid
from marketplace.shopee_voucher_db__promotion_tab__vn_daily_s0_live
where (
contains(rule.spm_channels, 5001800) -- SPP
or contains(rule.spm_channels, 5002900) -- SPP
or contains(rule.spm_channels, 5001300) -- SPL
)
)

-- ,shopee_vn_bi_team__airpay_sponsor_flashsale_new_batch as (
-- select v.*
-- from (SELECT max(ingestion_timestamp) max_ingestion_timestamp FROM
vnbi_fpna.shopee_vn_bi_team__airpay_sponsor_flashsale_new_batch WHERE
ingestion_timestamp <> '0') m
-- left join vnbi_fpna.shopee_vn_bi_team__airpay_sponsor_flashsale_new_batch v on
m.max_ingestion_timestamp = v.ingestion_timestamp
-- WHERE ingestion_timestamp <> '0'
-- )
-- ,shopee_vn_bi_team__airpay_sponsor_flashsale_new_scheme as (
-- select v.*
-- from (SELECT max(ingestion_timestamp) max_ingestion_timestamp FROM
vnbi_fpna.shopee_vn_bi_team__airpay_sponsor_flashsale_new_scheme WHERE
ingestion_timestamp <> '0') m
-- left join vnbi_fpna.shopee_vn_bi_team__airpay_sponsor_flashsale_new_scheme v on
m.max_ingestion_timestamp = v.ingestion_timestamp
-- WHERE ingestion_timestamp <> '0'
-- )
-- ,shopee_vn_bi_team__airpay_collection_sponsor_data as (
-- select c.*
-- from (select max(ingestion_timestamp) ingestion_timestamp from
vnbi_fpna.shopee_vn_bi_team__airpay_collection_sponsor_data where
ingestion_timestamp <> '0') m
-- left join vnbi_fpna.shopee_vn_bi_team__airpay_collection_sponsor_data c on
m.ingestion_timestamp = c.ingestion_timestamp
-- where 1=1
-- and c.ingestion_timestamp <> '0'
-- and c.status = 'Confirmed'
-- and c.itemid is not null
-- )

--------------- Freeship Voucher

,tb_fsv as (
select
a.grass_date,
sum(scheduled_net_cpo) fsv_cost_mkp
from vnbi_fpna.logistics_cost_by_order a
left join mp_order.dwd_order_all_ent_df__vn_s0_live o on a.order_id = o.order_id
left join shopee_vn_bi_team__airpay_voucher f
on try_cast(f.voucherid as BIGINT) = o.fsv_promotion_id
where 1=1
and f.voucherid is not null
and is_pick_up = 1
and a.grass_date >= date_trunc('month', current_date - interval '6' month)
group by 1)

--------------- Discount + Coin cashback voucher

,tb_voucher as (
select
Date(CAST(tb1.create_datetime as timestamp)) as grass_date,
sum(case when v.voucherid is not null then pv_rebate_by_shopee_amt_usd end)
voucher_cost_mkp,
sum(case when v.voucherid is not null then pv_coin_earn_by_shopee_amt_usd end)
coin_cost_mkp
from mp_order.dwd_order_item_all_ent_df__vn_s0_live tb1
join shopee_vn_bi_team__airpay_voucher v on try_cast(v.voucherid as BIGINT) =
tb1.pv_promotion_id
where payment_method_id in (28, 30, 5)
and order_be_status IN ('ESCROW_PENDING', 'ESCROW_VERIFIED', 'ESCROW_CREATED',
'ESCROW_PAYOUT', 'ESCROW_PAID', 'PAID', 'COMPLETED', 'UNPAID')
and order_item_status_id not in (3, 4)
and Date(CAST(tb1.create_datetime as timestamp)) >= date_trunc('month',
current_date - interval '6' month)
group by 1)

--------------- Rebate (Flashsale + Collection + Evoucher)

-- ,flash_sale as (-- extract deal flashsale sponsored by Airpay


-- select *
-- from shopee_vn_bi_team__airpay_sponsor_flashsale_new_batch
-- where product_id is not null
-- union
-- select *
-- from shopee_vn_bi_team__airpay_sponsor_flashsale_new_scheme
-- where product_id is not null)

-- , tb_rebate as (
-- select Date(CAST(tb1.create_datetime as timestamp)) as grass_date,
-- sum(tb1.item_rebate_by_shopee_amt_usd) as rebate_normal_cost_mkp
-- from flash_sale f
-- inner join mp_order.dwd_order_item_all_ent_df__vn_s0_live tb1 on
TRY_CAST(f.product_id as BIGINT) = tb1.item_id
-- and TRY_CAST(f.date as DATE) = Date(CAST(tb1.create_datetime as timestamp))
-- and tb1.item_promotion_source = 'flash_sale'
-- where 1=1
-- and Date(CAST(tb1.create_datetime as timestamp)) >= date_trunc('month',
current_date - interval '6' month)
-- and tb1.payment_method_id IN (28, 30, 5) -- Airpay payment method
-- and tb1.item_rebate_by_shopee_amt_usd <> 0
-- AND tb1.item_rebate_by_shopee_amt <> 0
-- AND tb1.order_fe_status <> 'CANCELLED'
-- AND tb1.is_returned_item = 0
-- group by 1)

-- ,tb_collection as (
-- select Date(CAST(a.create_datetime as timestamp)) as grass_date,
-- sum(a.item_rebate_by_shopee_amt_usd) as collection_cost_mkp
-- from shopee_vn_bi_team__airpay_collection_sponsor_data col
-- left join mp_order.dwd_order_item_all_ent_df__vn_s0_live a on cast(a.item_id as
varchar) = col.itemid
-- and cast(Date(CAST(a.create_datetime as timestamp)) as varchar) = col.grass_date
-- and item_promotion_source <> 'flash_sale'
-- where 1=1
-- and a.payment_method_id in (28, 30, 5)
-- and Date(CAST(a.create_datetime as timestamp)) >= date_trunc('month',
current_date - interval '6' month)
-- AND a.item_rebate_by_shopee_amt <> 0
-- AND a.order_fe_status <> 'CANCELLED'
-- AND a.is_returned_item = 0
-- group by 1)

--------------------------------------------- DP
--------------- Discount voucher + coin
-- update @ 2022-08-29: Same logic w/t voucher tracking DP report & H2 DP cost
,tb_dp as (
select DATE(FROM_UNIXTIME(a.create_time-3600)) grass_date,
sum((earn_cash/100000.00)/a.exchange_rate) as voucher_cost_dp,
sum((earn_coins*100.00)/a.exchange_rate) as coin_cost_dp
from digitalpurchase.vn_order_dwd_dp_trd_ord_item_extend_df a --replace w/t new
table
left join
digitalpurchase.shopee_digital_product_promotion_vn_db__voucher_tab__reg_daily_s0_l
ive b
on a.order_id = b.order_id
and b.use_status = 'VU2'
and b.return_status = 'VR0'
join marketplace.shopee_voucher_db__promotion_tab__vn_daily_s0_live c on
b.promotion_id = c.promotionid
where true
and date (a.grass_date) =current_date -interval '1' day --partition field (new add)
and date(from_unixtime(a.create_time-3600)) >= date_trunc('month', current_date -
interval '6' month)
and (rule.spm_channels in (array [5002900,5001800], array [5001800,5002900]))
and not regexp_like(description, 'Mã giảm giá phát hành bởi Người bán và sẽ không
được hoàn lại với bất kỳ lý do nào') -- Exclucde Seller voucher
and not regexp_like(prefix, 'SPGCDP|SPGCAP|SPGCAD|LSGS25|LSCIRCLEK|LSXINCHAO') --
Exclude voucher code from evoucher
-- and b.use_status = 'VU2' --move to left join table to reduce data processing
-- and b.return_status = 'VR0'
and a.platform = 1 -- shopee app
and a.is_gross_order=1 ---replace two manual conditions below
-- and a.payment_status in ('P2')
-- and (a.final_price != 0 or (a.payment_status = 'P2' and a.final_price = 0))
group by 1)

--------------- Rebate
-- update @ 2022-08-29: Same logic w/t voucher tracking DP report & H2 DP cost
,dp_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)

,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
)
,flash_sale_dp as (--rebate telco, game, se
select distinct
date(o.create_date) as grass_date,
o.order_id,

coalesce(p_.shopee_subsidy,0)/100000 * cast(json_extract_scalar(items,
'$[0].quantity') as double)/o.exchange_rate as rebate
from digitalpurchase.vn_order_dwd_dp_trd_ord_item_extend_df o
left join
digitalpurchase.shopee_digital_product_promotion_vn_db__voucher_tab__reg_daily_s0_l
ive v on v.order_id = o.order_id and v.use_status = 'VU2' and v.return_status =
'VR0'
left join dp_category_tab c on
c.category_id = o.category_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)) )
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 o.promotion_activity_id = p_.activity_id
and p_.nomination_err_msg=''
and (o.create_time - 3600) BETWEEN (p_.start_time - 3600) and (p_.end_time -
3600) -- promotions
where 1=1
and date (o.grass_date)= current_date - interval '1' day
and o.platform = 1 -- shopee app
and o.is_net_order =1 -- net orders
and date(from_unixtime(o.create_time - 3600)) >= date_trunc('month', current_date -
interval '6' month)
---rebate borne by SPP
--by cate & exclude sponsor by Shopee
and (
( (c.main_category in ('TELCO') or c.L3_category ='Game Vouchers') and
o.discount_price !=0)
or (c.L3_category ='E-Voucher' and spf.item_id is null )--applied since FY22
)
and p_.activity_type in (2,4)
and p_.shopee_subsidy >0
-- and order_status NOT IN ('OR2', 'OR4', 'OR7') --- net orders
-- then ((coalesce(o.original_discount_price, 0) - coalesce(o.discount_price,
0))/100000 * cast(json_extract_scalar(items, '$[0].quantity') as double)) end > 0
)

,tb_rebate_dp as (
select
grass_date,
sum (rebate) as rebate_cost_dp
-- round(sum(case when c.main_category in ('TELCO') and o.channel_id in (5001800,
5002900) and o.payment_status = 'P2' and fulfillment_status = 'F5'
-- then (coalesce(o.original_discount_price, 0) - coalesce(o.discount_price,
0))/100000/23250.00 * cast(json_extract_scalar(items, '$[0].quantity') as double)
end), 2) as rebate_cost_dp
-- from
digitalpurchase.shopee_digital_product_order_v2_vn_db__order_tab__reg_daily_s0_live
o
from flash_sale_dp
-- left join dp_category_tab c on c.category_id = o.category_id
-- left join flash_sale_dp f on f.order_id = o.order_id
-- where (o.final_price !=0 or (o.final_price=0 and o.payment_status='P2'))
-- and from_unixtime(create_time-3600) >= date_trunc('month', current_date -
interval '6' month)
-- and platform = 1 -- shopee app
group by 1)

--------------------------------------------- SP NowFood + Foody

,tp_nowfood as (
select
cast(grass_date as date) grass_date,
sum(airpay_discount_amount)/ 23250 sp_nowfood_ap_cost
from shopeefood.foody_mart__fact_order_join_detail
where app_type_id in (50, 51) -- shopee platform
and cast(grass_date as date) >= date_trunc('month', current_date - interval '6'
month)
group by 1)

,tb_foody as (
select
cast(grass_date as date) grass_date,
sum(airpay_discount_amount)/ 23250 foody_ap_cost
from shopeefood.foody_mart__fact_order_join_detail
where app_type_id not in (50, 51) -- not shopee platform
and cast(grass_date as date) >= date_trunc('month', current_date - interval '6'
month)
group by 1)

--------------------------------------------- Offline payment


,tb_offline as (
select
date(from_unixtime(valid_time-3600)) as grass_date,
sum(payment_cash_amount/1000000 + payment_payable_amount/1000000 -
topup_payable_amount/1000000 + payment_coins_num/1000000 -
topup_coins_amount/1000000)/23250 as qr_cost
from shopeepay.airpay_payment_txn_vn_db__txn_order_tab_union__reg_daily_s0_live tb1
left join shopeepay.airpay_payment_common_vn_db__channel_tab__vn_daily_s0_live c on
tb1.payment_channel_id = c.channel_id
left join (select channel_id, max_by(code, id) code from
shopeepay.airpay_payment_common_vn_db__product_code_tab__vn_daily_s0_live group by
1) pc_i on c.channel_id = pc_i.channel_id
left join (select channel_type, max_by(code, id) code from
shopeepay.airpay_payment_common_vn_db__product_code_tab__vn_daily_s0_live group by
1) pc_t on c.type = pc_t.channel_type
where tb1.memo <> 'test'
and tb1.type not in (16, 19)
and tb1.status = 8
and tb1.topup_channel_id not in (11100,13002,13003,13004,13009,13010,13011,13103)
and tb1.payment_channel_id not in (21000,
21002,21003,21004,21005,21006,21007,21008,21009,100021011,100021016,
100021017,100021040,999991,999992,999993,100021018,100021019,100021020,100021021,
100021022,100021023,100021024,100021025,100021026,100021027,100021028,100021029,
100021040,100021041,100021042,100021043,100001,110012,210001,210002,210003,210004,
210005,210006,210007,210008,210009,210010,210011,210013,210014,210015,210016,210017
,
210018,21031,21032,21033,21034,21035,21039,10200077,10200086,10200092,10200097,1100
0001,11000002,
310001,310002,11000003,100021013,100021014,100021015,21070,21011,31000,31001,
21017)
and (regexp_like(json_extract_scalar(tb1.extra_data, '$.payment.transfer.purpose'),
'CashBack###Tien thuong het han') = FALSE --exclude clawback P2P
or
json_extract_scalar(tb1.extra_data, '$.payment.transfer.purpose') is null)
and date(from_unixtime(tb1.valid_time-3600)) >= date_trunc('month', current_date -
interval '6' month)
and case when pc_i.channel_id <> 0 then pc_i.code else pc_t.code end = 'MS_OFL'
group by 1)

--------------------------------------------- ShopeePay in app


--------------- GPPC

, tb_gppc as (
select
date(from_unixtime(valid_time-3600)) as grass_date,
sum(payment_payable_amount - currency_amount)/23250000000 as gppc_cost
from shopeepay.airpay_payment_txn_vn_db__txn_order_tab_union__reg_daily_s0_live tb1
left join shopeepay.airpay_payment_common_vn_db__channel_tab__vn_daily_s0_live c on
tb1.payment_channel_id = c.channel_id
left join (select channel_id, max_by(code, id) code from
shopeepay.airpay_payment_common_vn_db__product_code_tab__vn_daily_s0_live group by
1) pc_i on c.channel_id = pc_i.channel_id
left join (select channel_type, max_by(code, id) code from
shopeepay.airpay_payment_common_vn_db__product_code_tab__vn_daily_s0_live group by
1) pc_t on c.type = pc_t.channel_type
where tb1.memo <> 'test'
and tb1.type not in (16, 19)
and tb1.status = 8
and tb1.topup_channel_id not in (11100,13002,13003,13004,13009,13010,13011,13103)
and tb1.payment_channel_id not in (21000,
21002,21003,21004,21005,21006,21007,21008,21009,100021011,100021016,
100021017,100021040,999991,999992,999993,100021018,100021019,100021020,100021021,
100021022,100021023,100021024,100021025,100021026,100021027,100021028,100021029,
100021040,100021041,100021042,100021043,100001,110012,210001,210002,210003,210004,
210005,210006,210007,210008,210009,210010,210011,210013,210014,210015,210016,210017
,
210018,21031,21032,21033,21034,21035,21039,10200077,10200086,10200092,10200097,1100
0001,11000002,
310001,310002,11000003,100021013,100021014,100021015,21070,21011,31000,31001,
21017)
and (regexp_like(json_extract_scalar(tb1.extra_data, '$.payment.transfer.purpose'),
'CashBack###Tien thuong het han') = FALSE --exclude clawback P2P
or
json_extract_scalar(tb1.extra_data, '$.payment.transfer.purpose') is null)
and date(from_unixtime(tb1.valid_time-3600)) >= date_trunc('month', current_date -
interval '6' month)
and case when pc_i.channel_id <> 0 then pc_i.code else pc_t.code end = 'gppc'
group by 1)

--------------- Payment gateway

,tb_others_merchants as (
select
date(from_unixtime(valid_time-3600)) as grass_date,
sum(payment_payable_amount - currency_amount)/23250000000 as other_mer_cost
from shopeepay.airpay_payment_txn_vn_db__txn_order_tab_union__reg_daily_s0_live tb1
left join shopeepay.airpay_payment_common_vn_db__channel_tab__vn_daily_s0_live c on
tb1.payment_channel_id = c.channel_id
left join (select channel_id, max_by(code, id) code from
shopeepay.airpay_payment_common_vn_db__product_code_tab__vn_daily_s0_live group by
1) pc_i on c.channel_id = pc_i.channel_id
left join (select channel_type, max_by(code, id) code from
shopeepay.airpay_payment_common_vn_db__product_code_tab__vn_daily_s0_live group by
1) pc_t on c.type = pc_t.channel_type
where tb1.memo <> 'test'
and tb1.type not in (16, 19)
and tb1.status = 8
and tb1.topup_channel_id not in (11100,13002,13003,13004,13009,13010,13011,13103)
and tb1.payment_channel_id not in (21000,
21002,21003,21004,21005,21006,21007,21008,21009,100021011,100021016,
100021017,100021040,999991,999992,999993,100021018,100021019,100021020,100021021,
100021022,100021023,100021024,100021025,100021026,100021027,100021028,100021029,
100021040,100021041,100021042,100021043,100001,110012,210001,210002,210003,210004,
210005,210006,210007,210008,210009,210010,210011,210013,210014,210015,210016,210017
,
210018,21031,21032,21033,21034,21035,21039,10200077,10200086,10200092,10200097,1100
0001,11000002,
310001,310002,11000003,100021013,100021014,100021015,21070,21011,31000,31001,
21017)
and (regexp_like(json_extract_scalar(tb1.extra_data, '$.payment.transfer.purpose'),
'CashBack###Tien thuong het han') = FALSE --exclude clawback P2P
or
json_extract_scalar(tb1.extra_data, '$.payment.transfer.purpose') is null)
and date(from_unixtime(tb1.valid_time-3600)) >= date_trunc('month', current_date -
interval '6' month)
and case when pc_i.channel_id <> 0 then pc_i.code else pc_t.code end in ('PGW',
'MS_ONL', 'OTH')
group by 1)
,tb_extract as (
select
date_format(tb_fsv.grass_date, '%m') as month,
tb_fsv.grass_date as date,
'APA Shopee' as main_category,
'Shopee MKP' as sub_category,
'fsv' as type_cost,
sum(fsv_cost_mkp) as PRM_cost
from tb_fsv
group by 1, 2, 3, 4, 5

-- union all

-- select
-- date_format(tb_voucher.grass_date, '%m') as month,
-- tb_voucher.grass_date as date,
-- 'APA Shopee' as main_category,
-- 'Shopee MKP' as sub_category,
-- 'voucher' as type_cost,
-- sum(voucher_cost_mkp) as PRM_cost
-- from tb_voucher
-- group by 1, 2, 3, 4, 5

-- union all

-- select
-- date_format(tb_rebate.grass_date, '%m') as month,
-- tb_rebate.grass_date as date,
-- 'APA Shopee' as main_category,
-- 'Shopee MKP' as sub_category,
-- 'rebate normal' as type_cost,
-- sum(rebate_normal_cost_mkp) as PRM_cost
-- from tb_rebate
-- group by 1, 2, 3, 4, 5

-- union all
-- SELECT
-- date_format(tb_collection.grass_date, '%m') as month,
-- tb_collection.grass_date as date,
-- 'APA Shopee' as main_category,
-- 'Shopee MKP' as sub_category,
-- 'collection' as type_cost,
-- sum(collection_cost_mkp) as PRM_cost
-- FROM tb_collection
-- group by 1,2,3,4,5

-- union all

-- select
-- date_format(tb_evoucher.grass_date, '%m') as month,
-- tb_evoucher.grass_date as date,
-- 'APA Shopee' as main_category,
-- 'Shopee MKP' as sub_category,
-- 'rebate evoucher' as type_cost,
-- sum(rebate_evoucher) as PRM_cost
-- from tb_evoucher
-- group by 1, 2, 3, 4, 5
union all

select
date_format(tb_dp.grass_date, '%m') as month,
tb_dp.grass_date as date,
'APA Shopee' as main_category,
'Shopee DP' as sub_category,
'DP voucher' as type_cost,
sum(voucher_cost_dp) as PRM_cost
from tb_dp
group by 1, 2, 3, 4, 5

union all

select
date_format(tb_dp.grass_date, '%m') as month,
tb_dp.grass_date as date,
'APA Shopee' as main_category,
'Shopee DP' as sub_category,
'DP coin' as type_cost,
sum(coin_cost_dp) as PRM_cost
from tb_dp
group by 1, 2, 3, 4, 5

union all

select
date_format(tp_nowfood.grass_date, '%m') as month,
tp_nowfood.grass_date as date,
'APA Shopee' as main_category,
'Shopee NowFood' as sub_category,
'shopee nowfood voucher' as type_cost,
sum(sp_nowfood_ap_cost) as PRM_cost
from tp_nowfood
group by 1, 2, 3, 4, 5

union all

select
date_format(tb_voucher.grass_date, '%m') as month,
tb_voucher.grass_date as date,
'APA Shopee' as main_category,
'Shopee MKP' as sub_category,
'coin_mkp' as type_cost,
sum(coin_cost_mkp) as PRM_cost
from tb_voucher
group by 1, 2, 3, 4, 5

union all

select
date_format(tb_rebate_dp.grass_date, '%m') as month,
tb_rebate_dp.grass_date as date,
'APA Shopee' as main_category,
'Shopee DP' as sub_category,
'rebate_dp' as type_cost,
sum(rebate_cost_dp) as PRM_cost
from tb_rebate_dp
group by 1, 2, 3, 4, 5
union all
select
date_format(tb_foody.grass_date, '%m') as month,
tb_foody.grass_date as date,
'APA Foody' as main_category,
'Foody' as sub_category,
'non-shopee nowfood voucher' as type_cost,
sum(foody_ap_cost) as PRM_cost
from tb_foody
group by 1, 2, 3, 4, 5

union all

select
date_format(tb_offline.grass_date, '%m') as month,
tb_offline.grass_date as date,
'APA others' as main_category,
'Offline payment' as sub_category,
'QR voucher' as type_cost,
sum(qr_cost) as PRM_cost
from tb_offline
group by 1, 2, 3, 4, 5

union all

select date_format(tb_gppc.grass_date, '%m') as month,


tb_gppc.grass_date as date,
'APA others' as main_category,
'GPPC' as sub_category,
'GPPC voucher' as type_cost,
sum(gppc_cost) as PRM_cost
from tb_gppc
group by 1, 2, 3, 4, 5

union all

select date_format(tb_others_merchants.grass_date, '%m') as month,


tb_others_merchants.grass_date as date,
'APA others' as main_category,
'Other merchant' as sub_category,
'Other merchant voucher' as type_cost,
sum(other_mer_cost) as PRM_cost
from tb_others_merchants
group by 1, 2, 3, 4, 5)

select
t1.month,
t1.date,
t1.main_category,
t1.sub_category,
t1.type_cost,
sum(cast(PRM_cost as double)) as prm_cost
from tb_extract t1
where t1.date >= date_trunc('month', current_date - interval '6' month)
and t1.date < current_date
group by 1, 2, 3, 4, 5
)

You might also like