Anda di halaman 1dari 14

pca_offer_mpc

pca_campaign_cur
pca_offer_mpc
drop function piirestricted.pca_insert_offer_mpc()

CREATE OR REPLACE VIEW piirestrictedmstr.pca_campaign_cur AS


SELECT pca_campaign.pca_version_nb, pca_campaign.prtnr_cd, pca_campaign.proj_cd,
pca_campaign.proj_nm, pca_campaign.campaign_type, pca_campaign.mpc_list,
pca_campaign.prtnr_margin_rate, pca_campaign.base_mile_rate,
pca_campaign.bonus_mile_rate, pca_campaign.avg_acq_rate,
pca_campaign.avg_react_rate, pca_campaign.def_lapsed_mths,
pca_campaign.promo_start, pca_campaign.promo_end, pca_campaign.pp_start,
pca_campaign.pp_end, pca_campaign.pp_start_2, pca_campaign.pp_end_2,
pca_campaign.pp_start_3, pca_campaign.pp_end_3, pca_campaign.pp_start_4,
pca_campaign.pp_end_4, pca_campaign.coalition_ind, pca_campaign.cur_ind,
to_char(pca_campaign.promo_start::timestamp without time zone, 'DY'::text) AS
offer_start_day, to_char(pca_campaign.promo_end::timestamp without time zone,
'DY'::text) AS offer_end_day,
CASE
WHEN pca_campaign.coalition_ind = 1 THEN 'AE'::character varying
ELSE pca_campaign.prtnr_cd
END AS campaign_prtnr_cd,
CASE
WHEN pca_campaign.prtnr_cd::text = 'IOL'::text OR
pca_campaign.prtnr_cd::text = 'HHA'::text OR pca_campaign.prtnr_cd::text =
'BRI'::text THEN pca_campaign.prtnr_cd
ELSE 'ALL'::character varying
END AS retail_seasonal_prtnr_cd
FROM piirestrictedmstr.pca_campaign
WHERE pca_campaign.cur_ind = 1;

drop table piirestricted.pca_campaign;


CREATE TABLE piirestricted.pca_campaign (
pca_version_nb int4 NULL,
prtnr_cd varchar(3) NULL,
proj_cd varchar(100) NULL,
proj_nm varchar(256) NULL,
campaign_type varchar(20) NULL,
mpc_list varchar(1000) NULL,
prtnr_margin_rate numeric(10,5) NULL,
base_mile_rate numeric(10,5) NULL,
bonus_mile_rate numeric(10,5) NULL,
avg_acq_rate numeric(10,5) NULL,
avg_react_rate numeric(10,5) NULL,
def_lapsed_mths int4 NULL,
promo_start date NULL,
promo_end date NULL,
pp_start date NULL,
pp_end date NULL,
pp_start_2 date NULL,
pp_end_2 date NULL,
pp_start_3 date NULL,
pp_end_3 date NULL,
pp_start_4 date NULL,
pp_end_4 date NULL,
coalition_ind int4 NULL,
cur_ind int4 NULL
);

drop view piirestricted.pca_campaign_cur;


CREATE OR REPLACE VIEW piirestricted.pca_campaign_cur AS
SELECT pca_campaign.pca_version_nb, pca_campaign.prtnr_cd, pca_campaign.proj_cd,
pca_campaign.proj_nm, pca_campaign.campaign_type, pca_campaign.mpc_list,
pca_campaign.prtnr_margin_rate, pca_campaign.base_mile_rate,
pca_campaign.bonus_mile_rate, pca_campaign.avg_acq_rate,
pca_campaign.avg_react_rate, pca_campaign.def_lapsed_mths,
pca_campaign.promo_start, pca_campaign.promo_end, pca_campaign.pp_start,
pca_campaign.pp_end, pca_campaign.pp_start_2, pca_campaign.pp_end_2,
pca_campaign.pp_start_3, pca_campaign.pp_end_3, pca_campaign.pp_start_4,
pca_campaign.pp_end_4, pca_campaign.coalition_ind, pca_campaign.cur_ind,
to_char(pca_campaign.promo_start::timestamp without time zone, 'DY'::text) AS
offer_start_day, to_char(pca_campaign.promo_end::timestamp without time zone,
'DY'::text) AS offer_end_day,
CASE
WHEN pca_campaign.coalition_ind = 1 THEN 'AE'::character varying
ELSE pca_campaign.prtnr_cd
END AS campaign_prtnr_cd,
CASE
WHEN pca_campaign.prtnr_cd::text = 'IOL'::text OR
pca_campaign.prtnr_cd::text = 'HHA'::text OR pca_campaign.prtnr_cd::text =
'BRI'::text THEN pca_campaign.prtnr_cd
ELSE 'ALL'::character varying
END AS retail_seasonal_prtnr_cd
FROM piirestricted.pca_campaign
WHERE pca_campaign.cur_ind = 1;

INSERT INTO piirestricted.pca_campaign


values (
1,
'x',
'proj_cd',
'proj_nm',
'campaign_type',
'mpc_list',
0,
0,
0,
0,
0,
0,
current_date,
current_date,
current_date,
current_date,
current_date,
current_date,
current_date,
current_date,
current_date,
current_date,
0,
0
);

dbms_output.put_line ('');

makeynagumera@gmail.com | hermiOne85

insert into pca_offer_mpc (prtnr_cd, proj_cd, pca_version_nb, mktg_pgm_cd) values


(''' || campaign.prtnr_cd || ''',''' || campaign.proj_cd || ''',' ||
campaign.pca_version_nb || ', ''' || trim(arrinp[i]) || ''')

select * from pca_offer_mpc;


drop table pca_offer_mpc;
create table pca_offer_mpc (
prtnr_cd varchar(3) NULL,
proj_cd varchar(100) NULL,
pca_version_nb int4 NULL,
mktg_pgm_cd varchar(100)
);

SQL Error [22P02]: ERROR: invalid input syntax for integer: "mpc_list"
Where: SQL statement "insert into pca_offer_mpc (prtnr_cd, proj_cd,
pca_version_nb, mktg_pgm_cd) values ('x','proj_cd',2, 'mpc_list')"
PL/pgSQL function "pca_insert_offer_mpc" line 23 at execute statement
ERROR: invalid input syntax for integer: "mpc_list"
Where: SQL statement "insert into pca_offer_mpc (prtnr_cd, proj_cd,
pca_version_nb, mktg_pgm_cd) values ('x','proj_cd',2, 'mpc_list')"
PL/pgSQL function "pca_insert_offer_mpc" line 23 at execute statement
ERROR: invalid input syntax for integer: "mpc_list"
Where: SQL statement "insert into pca_offer_mpc (prtnr_cd, proj_cd,
pca_version_nb, mktg_pgm_cd) values ('x','proj_cd',2, 'mpc_list')"
PL/pgSQL function "pca_insert_offer_mpc" line 23 at execute statement

https://sci-hub.tw/10.1002/9781119183419

+++++++++++++++++++++++++++
DWH

CREATE TABLE dwh.account_dim (


account_id int8 NOT NULL DEFAULT "identity"(1062566, 0, '1,1'::text),
account_no varchar(50) NULL,
card_type_id int2 NULL,
account_attr_1 varchar(100) NULL,
account_attr_2 varchar(100) NULL,
account_attr_3 varchar(100) NULL,
account_attr_4 varchar(100) NULL,
account_attr_5 varchar(100) NULL,
account_attr_6 varchar(100) NULL,
account_attr_7 varchar(100) NULL,
account_attr_8 varchar(100) NULL,
account_attr_9 varchar(100) NULL,
account_attr_10 varchar(100) NULL,
record_start_date timestamp NULL DEFAULT '1900-01-01 00:00:00'::timestamp
without time zone,
record_end_date timestamp NULL DEFAULT '9999-12-31 00:00:00'::timestamp
without time zone,
record_active_flag bool NULL DEFAULT 1,
record_change_reason varchar(50) NULL DEFAULT 'NEW'::character varying
)
WITH (
OIDS=TRUE
) ;

CREATE TABLE dwh.customer_dim (


customer_id int4 NOT NULL DEFAULT "identity"(1062593, 0, '1,1'::text),
account_id int8 NULL,
customer_no varchar(50) NULL,
first_name varchar(50) NULL,
last_name varchar(50) NULL,
email_address varchar(200) NULL,
primary_flag bool NULL,
active_flag bool NULL,
opt_in_flag bool NULL,
account_status varchar(50) NULL,
opt_in_date_id int4 NULL,
first_opt_in_date_id int4 NULL,
registration_date_id int4 NULL,
mobile_opt_in_flag bool NULL,
mobile_phone_number varchar(50) NULL,
opt_out_flag bool NULL,
opt_out_date_id bool NULL,
lang_code varchar(2) NULL,
address_line_1 varchar(100) NULL,
address_line_2 varchar(100) NULL,
address_line_3 varchar(100) NULL,
address_line_4 varchar(100) NULL,
city varchar(50) NULL,
postal_zip_code varchar(15) NULL,
province_state_code varchar(3) NULL,
province_state_name varchar(50) NULL,
country_code varchar(5) NULL,
country_name varchar(50) NULL,
phone_number_1 varchar(50) NULL,
phone_number_2 varchar(50) NULL,
fax_number varchar(50) NULL,
primary_email varchar(100) NULL,
secondary_email varchar(100) NULL,
po_box varchar(10) NULL,
customer_attr_1 varchar(100) NULL,
customer_attr_2 varchar(100) NULL,
customer_attr_3 varchar(100) NULL,
customer_attr_4 varchar(100) NULL,
customer_attr_5 varchar(100) NULL,
customer_attr_6 varchar(100) NULL,
customer_attr_7 varchar(100) NULL,
customer_attr_8 varchar(100) NULL,
customer_attr_9 varchar(100) NULL,
customer_attr_10 varchar(100) NULL,
record_start_date timestamp NULL DEFAULT '1900-01-01 00:00:00'::timestamp
without time zone,
record_end_date timestamp NULL DEFAULT '9999-12-31 00:00:00'::timestamp
without time zone,
record_active_flag bool NULL DEFAULT 1,
record_change_reason varchar(50) NULL DEFAULT 'NEW'::character varying
)
WITH (
OIDS=TRUE
) ;

CREATE TABLE dwh.travel_order_fact (


travel_order_id int8 NOT NULL DEFAULT "identity"(1062708, 0, '1,1'::text),
account_id int8 NULL,
customer_id int8 NULL,
agent_id int4 NULL,
booking_date_id int4 NULL,
points_range_id int4 NULL,
program_id int4 NULL,
order_no varchar(50) NULL,
booking_type varchar(10) NULL,
pnr_locator varchar(40) NULL,
booked_by_type varchar(10) NULL,
booking_complete_date_id int4 NULL,
booking_source varchar(10) NULL,
redemption_type varchar(15) NULL,
total_passenger_count int4 NULL,
travel_item_count int4 NULL,
points_amt numeric(11,2) NULL,
points_redeemed int4 NULL,
cash_portion_amt numeric(11,2) NULL,
bonus_points int4 NULL,
cost_amt numeric(11,2) NULL,
tax_1_amt numeric(11,2) NULL,
tax_2_amt numeric(11,2) NULL,
tax_3_amt numeric(11,2) NULL,
tax_4_amt numeric(11,2) NULL,
total_taxes_amt numeric(11,2) NULL,
order_fact_1 varchar(100) NULL,
order_fact_2 varchar(100) NULL,
order_fact_3 varchar(100) NULL,
order_fact_4 varchar(100) NULL,
order_fact_5 varchar(100) NULL
)
WITH (
OIDS=TRUE
) ;

CREATE TABLE dwh.card_type_dim (


card_type_id int4 NOT NULL DEFAULT "identity"(1062584, 0, '1,1'::text),
card_bin_no varchar(6) NULL,
card_type_code bpchar(10) NULL,
card_type_desc varchar(50) NULL,
program_code varchar(50) NULL,
program_id int4 NULL,
card_type_attr_1 varchar(100) NULL,
card_type_attr_2 varchar(100) NULL,
card_type_attr_3 varchar(100) NULL,
card_type_attr_4 varchar(100) NULL,
card_type_attr_5 varchar(100) NULL,
card_type_attr_6 varchar(100) NULL,
card_type_attr_7 varchar(100) NULL,
card_type_attr_8 varchar(100) NULL,
card_type_attr_9 varchar(100) NULL,
card_type_attr_10 varchar(100) NULL,
record_start_date timestamp NULL DEFAULT '1900-01-01 00:00:00'::timestamp
without time zone,
record_end_date timestamp NULL DEFAULT '9999-12-31 00:00:00'::timestamp
without time zone,
record_active_flag bool NULL DEFAULT 1,
record_change_reason varchar(50) NULL DEFAULT 'NEW'::character varying
)
WITH (
OIDS=TRUE
) ;

CREATE TABLE travel_order_fact (


travel_order_id int8 NOT NULL DEFAULT "identity"(1062708, 0, '1,1'::text),
account_id int8 NULL,
customer_id int8 NULL,
agent_id int4 NULL,
booking_date_id int4 NULL,
points_range_id int4 NULL,
program_id int4 NULL,
order_no varchar(50) NULL,
booking_type varchar(10) NULL,
pnr_locator varchar(40) NULL,
booked_by_type varchar(10) NULL,
booking_complete_date_id int4 NULL,
booking_source varchar(10) NULL,
redemption_type varchar(15) NULL,
total_passenger_count int4 NULL,
travel_item_count int4 NULL,
points_amt numeric(11,2) NULL,
points_redeemed int4 NULL,
cash_portion_amt numeric(11,2) NULL,
bonus_points int4 NULL,
cost_amt numeric(11,2) NULL,
tax_1_amt numeric(11,2) NULL,
tax_2_amt numeric(11,2) NULL,
tax_3_amt numeric(11,2) NULL,
tax_4_amt numeric(11,2) NULL,
total_taxes_amt numeric(11,2) NULL,
order_fact_1 varchar(100) NULL,
order_fact_2 varchar(100) NULL,
order_fact_3 varchar(100) NULL,
order_fact_4 varchar(100) NULL,
order_fact_5 varchar(100) NULL
)
WITH (
OIDS=TRUE
) ;

CREATE TABLE product_dim (


product_id int8 NOT NULL DEFAULT "identity"(1062667, 0, '1,1'::text),
product_code varchar(50) NULL,
product_item_desc varchar(200) NULL,
product_active_date timestamp NULL,
product_delist_date timestamp NULL,
size_unit_of_measure_code varchar(20) NULL,
size_unit_of_measure_desc varchar(50) NULL,
product_size_1_code varchar(20) NULL,
product_size_1_desc varchar(50) NULL,
product_size_2_code varchar(20) NULL,
product_size_2_desc varchar(50) NULL,
product_size_3_code varchar(20) NULL,
product_size_3_desc varchar(50) NULL,
weight_unit_of_measure_code varchar(20) NULL,
weight_unit_of_measure_desc varchar(50) NULL,
product_weight_code varchar(20) NULL,
product_weight_desc varchar(50) NULL,
colour_code varchar(20) NULL,
colour_desc varchar(50) NULL,
company_code varchar(50) NULL,
company_name varchar(100) NULL,
product_group_1_code varchar(50) NULL,
product_group_1_name varchar(100) NULL,
product_group_2_code varchar(50) NULL,
product_group_2_name varchar(100) NULL,
product_group_3_code varchar(50) NULL,
product_group_3_name varchar(100) NULL,
product_group_4_code varchar(50) NULL,
product_group_4_name varchar(100) NULL,
product_group_5_code varchar(50) NULL,
product_group_5_name varchar(100) NULL,
product_attr_1 varchar(100) NULL,
product_attr_2 varchar(100) NULL,
product_attr_3 varchar(100) NULL,
product_attr_4 varchar(100) NULL,
product_attr_5 varchar(100) NULL,
product_attr_6 varchar(100) NULL,
product_attr_7 varchar(100) NULL,
product_attr_8 varchar(100) NULL,
product_attr_9 varchar(100) NULL,
product_attr_10 varchar(100) NULL,
record_start_date timestamp NULL DEFAULT '1900-01-01 00:00:00'::timestamp
without time zone,
record_end_date timestamp NULL DEFAULT '9999-12-31 00:00:00'::timestamp
without time zone,
record_active_flag bool NULL DEFAULT 1,
record_change_reason varchar(50) NULL DEFAULT 'NEW'::character varying
)
WITH (
OIDS=TRUE
) ;

+++++++++++++++++++++++++++
CREATE TABLE elcdw.zb_ptsbalance
(
account_id int,
points_balance int,
update_date_id int,
points_balance_fact_1 varchar(100),
points_balance_fact_2 varchar(100),
points_balance_fact_3 varchar(100),
points_balance_fact_4 varchar(100),
points_balance_fact_5 varchar(100),
points_balance_id int,
Program varchar(50),
Program_Code varchar(50),
Card_Type_ID int
);

create temp table ma_bmo_report_1 as


select
a.Program,
a.program_Code,
a.Cardholder,
a.bkgdate bookingdate,
a.booking_complete_date_id,
a.order_id,
a.account_id,
a.account_no,
a.Booking_Type category,
a.payment,
a.channel,
case when b.isfirstbookingitem = 'Y' then a.Points_Redeemed else 0 end
pointsused,
case when b.isfirstbookingitem = 'Y' then a.Cash_Portion_amt else 0 end
cashportion,
case when b.isfirstbookingitem = 'Y' then a.totcost else 0 end totcost,
case when b.isfirstbookingitem = 'Y' then a.travel_item_count else 0 end
tot_compnt_cnt,
case when b.isfirstbookingitem = 'Y' then a.total_passenger_count else 0 end
passengercount,
b.componentcost itemprice,
b.componentcnt
/*isnull(b.componentcnt, a.travel_item_count)*/
qty,
b.isfirstbookingitem,
case b.booking_item_type when 'SCHEDULEDFLIGHT' then 'Air' when 'CHARTERFLIGHT'
then 'Air' when 'HOTEL' then 'Hotel' when 'PASSES' then 'Package' when 'PACKAGE'
then 'Package' when 'TOUR' then 'Package' when 'CAR' then 'Car' when
'TRAVELINSURANCE' then 'Insurance' when 'RAIL' then 'Rail' when 'CRUISE' then
'Cruise' else b.booking_item_type end subcat,
b.Destination ordertype,
0 pointsrequired_perunit,
'' product_code
from
#booking a left join #component b on a.order_id = b.order_id
where
a.Booking_Type = 'TRAVEL'
union all
select
a.Program,
a.program_Code,
a.Cardholder,
a.bkgdate bookingdate,
a.booking_complete_date_id,
a.order_id,
a.account_id,
a.account_no,
b.category,
a.payment,
a.channel,
case when b.isfirstbookingitem = 'Y' then a.Points_Redeemed else 0 end
pointsused,
case when b.isfirstbookingitem = 'Y' then a.Cash_Portion_amt else 0 end
cashportion,
case when b.isfirstbookingitem = 'Y' then a.totcost else 0 end totcost,
null tot_compnt_cnt,
null passengercount,
b.PointsRequired_perunit * b.Quantity / 100 itemprice,
b.Quantity qty,
b.isfirstbookingitem,
b.subcat,
b.product_item_desc ordertype,
b.PointsRequired_perunit,
b.product_code
from
#booking a left join #rewards b on a.order_id = concat(b.order_id,'a')

where
booking_type = 'MERCH'
union all
select
a.Program,
a.program_Code,
a.Cardholder,
a.bkgdate bookingdate,
a.booking_complete_date_id,
a.order_id,
a.account_id,
a.account_no,
case a.Booking_Type when 'CHARITY' then 'Charity' when 'INVEST' then 'Financial
Product' end category,
a.payment,
a.channel,
a.Points_Redeemed pointsused,
a.Cash_Portion_amt,
a.totcost,
null tot_compnt_cnt,
null passengercount,
totcost,
1 qty,
'Y' isfirstbookingitem,
case booking_type when 'CHARITY' then 'Charity' when 'INVEST' then 'Financial
Product' end subcat,
case booking_type when 'CHARITY' then 'Charity' when 'INVEST' then 'Financial
Product' end ordertype,
0 pointsrequired_perunit,
'' product_code
from
#booking a
where
booking_type in ('CHARITY', 'INVEST');

<-- jsf : remove comment

insert into mbna_cpp


select
*,
0 as with_frieght,
0 as taxes,
0 as fin_billing,
(quantity * points) as tot_pp,
(25 * quantity) as tot_bp,
(
25 * cast(quantity as float)
)/(quantity * points) as CPP,
case when (
product_code like 'CCS%'
or product_code like 'CWF%'
or product_code like ' CHR%'
or product_code like 'CHAR%'
or product_code like 'UWC%'
or cat = 'chr'
) then 'Charity' end as category
from
merge
where
(
product_code like 'CCS%'
or product_code like 'CWF%'
or product_code like ' CHR%'
or product_code like 'CHAR%'
or product_code like 'UWC%'
or cat = 'chr'
)
and points > 0
and quantity > 0
and program_desc like '%CUETS%'
union all
select
*,
(
cast(points_billed as float) * 1.08
) / 100 as with_freight,
0 as tax,
0 as fin_billing,
quantity * points as tot_pp,
(
(
cast(points_billed as float) * 1.08
) / 100
) * quantity as tot_bp,
(
(
(
cast(points_billed as float) * 1.08
) / 100
) * quantity
) / (quantity * points) as CPP,
case when (
product_code like 'RC%'
or cat = 'GIFT'
or cat = 'VGC'
) then 'Gift Certificate' end as category
from
merge
where
(
product_code like 'RC%'
or cat = 'GIFT'
or cat = 'VGC'
)
and points > 0
and quantity > 0
union all
select
*,
((cast(points_billed as float) * 1.08 ) ) / 100 as with_freight,
( ( cast(points_billed as float) * 1.08 ) ) / 100 * tax_rate as taxes,
((cast(points_billed as float) * 1.08 ) ) / 100 + ( ( ( cast(points_billed as
float) * 1.08 ) ) / 100 * tax_rate ) as fin_billing,
quantity * points as tot_pp, (((cast(points_billed as float) * 1.08)) / 100 + ((
(
cast(points_billed as float) * 1.08
)
) / 100 * tax_rate
)
) * quantity as tot_bp,
(
(
(
(
cast(points_billed as float) * 1.08
)
)/ 100 + (
(
(
cast(points_billed as float) * 1.08
)
) / 100 * tax_rate
)
) * quantity
) / (quantity * points) as CPP,
case when (
product_code not like 'RC%'
and cat not in ('CHR', 'GIFT', 'VGC')
) then 'Merchandise' end as category
from
merge
where
product_code not like 'RC%'
and cat not in ('CHR', 'GIFT', 'VGC')
and points > 0
and quantity > 0
union all
select
*,
0 as with_freight,
0 as taxes,
0 as fin_billing,
points as tot_pp,
(cost_amt - cash_portion_amt) as tot_bp,
0 as CPP,
'Travel' as category
from
travel
union all
select
*,
0 as with_freight,
0 as taxes,
0 as fin_billing,
points as tot_pp,
points / 100 as tot_bp,
0.01 as CPP,
cat as category
from
cuets_cb
union all
select
*,
0 as with_freight,
0 as taxes,
0 as fin_billing,
points as tot_pp,
points / 100 as tot_bp,
0.01 as CPP,
case when cat = 'CashBack' then 'Cashback' else cat end as category
from
mbna_cb;

insert into BOA_freq


select
programname, cust_no, account_no,
case when mth >= ${mth_cnt} then 'Monthly' else case when mth < ${mth_cnt}
and mth >= ${bimth_cnt} then 'Bi_Monthly' else case when qtr >= ${qtr_cnt} then
'Quarterly' else case when qtr < ${qtr_cnt}
and qtr >= ${semi_cnt} then 'Semi_Annually' else case when yr >= ${yr_cnt} then
'Annually' else case when yr < ${yr_cnt}
and yr >= ${biannual_cnt} then 'Bi_Annually' end end end end end end freq
from
(
select
programname, cust_no, account_no,
count(distinct date_part('month', bookingdate)) mth,
count (distinct cast(date_part ('year', bookingdate) as varchar)+
cast(extract(quarter from bookingdate) as varchar)) qtr,
count(distinct date_part('year', bookingdate)) yr
from
boa_report
where
program_grp <> 'CUETS'
group by
programname, cust_no, account_no
) a
union all
select
programname,
cust_no,
account_no,
case when mth >= ${mth_cnt_cu} then 'Monthly' else case when mth < ${mth_cnt_cu}
and mth >= ${bimth_cnt_cu} then 'Bi_Monthly' else case when qtr >= ${qtr_cnt_cu}
then 'Quarterly' else case when qtr < ${qtr_cnt_cu}
and qtr >= ${semi_cnt_cu} then 'Semi_Annually' else case when yr >= ${yr_cnt_cu}
then 'Annually' else case when yr < ${yr_cnt_cu}
and yr >= ${biannual_cnt_cu} then 'Bi_Annually' end end end end end end freq
from
(
select
programname, cust_no, account_no,
count(distinct date_part('month', bookingdate)) mth,
count (distinct cast(date_part ('year', bookingdate) as varchar)+
cast(extract(quarter from bookingdate) as varchar)) qtr,
count(distinct date_part('year', bookingdate)) yr
from
boa_report
where
program_grp = 'CUETS'
group by programname, cust_no, account_no) a;

drop table if exists zm_booking;


drop table if exists zm_component;
drop table if exists zm_rewards;
drop table if exists zm_boa_report;
drop table if exists zm_BOA_ch_count;

drop table if exists zm_booking;


drop table if exists zm_component;
drop table if exists zm_rewards;
drop table if exists zm_mbna_report;
drop table if exists zm_cb;
drop table if exists zm_freq;
drop table if exists zm_freq2;
drop table if exists zm_mbna_ch_count;
create temp table zm_rewards as
select
b.order_id,
b.order_item_id,
case WHEN a.product_group_2_code in ('REGULAR', 'EXP', 'GIFT')
and (
SUBSTRING(a.product_code, 1, 2) IN (
'AR', 'BC', 'CF', 'EE', 'EO', 'HA', 'HD',
'HT', 'MC', 'ML', 'SR', 'TR', 'VC'
)
or a.product_code like 'SPLHABS%'
) then 'Team Merchandise' when charindex('DONATION', a.product_item_desc)<> 0
then 'Charity' when charindex(
'CONTRIBUTION', a.product_item_desc
)<> 0 then 'Investment' else 'Merchandise' end producttype,
a.product_item_desc,
'N' isfirstbookingitem,
rank() over (
partition by b.order_id
order by
b.order_item_id,
cat.category
) rk,
b.quantity,
ltrim(
rtrim(a.product_code)
) product_code,
a.product_group_2_code,
case when a.product_group_2_code in ('REGULAR', 'EXP', 'GIFT')
and (
SUBSTRING(a.product_code, 1, 2) IN (
'AR', 'BC', 'CF', 'EE', 'EO', 'HA', 'HD',
'HT', 'MC', 'ML', 'SR', 'TR', 'VC'
)
or a.product_code like 'SPLHABS%'
) then case when cat.category IS not null then cat.category when
a.product_group_2_code = 'EXP' then 'Experiences' when charindex(
'AUTOGRAPH', a.product_item_desc
)> 0 then 'Memorabilia' else 'Merchandise' end when a.product_code = 'HD01' then
a.product_item_desc when a.product_code IN ('CJD03', 'CJD04') then 'CJD CashBack'
else isnull(
a.product_group_1_name, a.product_group_2_code
) end subcat
from
dwh.order_item_fact b
left join dwh.product_dim a on a.product_id = b.product_id
left join dwh.ma_mbna_exp_cat cat on a.product_code = cat.Product_Code
where
charindex(
'CATALOGUE', a.product_item_desc
)= 0
/*and b.order_id='5242''292178'*/;