raena_analytics_scripts/etlTransaction_job.sh

103 lines
3.6 KiB
Bash
Raw Permalink Normal View History

2024-06-24 12:26:08 +00:00
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
echo 'reportDate'=$reportDate
echo "
DROP TABLE IF EXISTS public.manual_bundle_sku_data ;
CREATE TABLE public.manual_bundle_sku_data AS
SELECT upper(bundle_sku) Parent_sku ,
Upper(A.sku) child_sku ,
A.quantity child_quantity,
'Bundle' parent_sku_class,
cast(B.retail_price AS decimal(22,2)) bundle_retail_price,
cast(json_extract_path_text(C.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) AS decimal(22,2)) bronze_price,
cast(json_extract_path_text(C.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) AS decimal(22,2)) silver_price,
cast(json_extract_path_text(C.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price,
cast(C.retail_price AS decimal(22,2)) child_retail_price
FROM bundle_data_manual_new A
INNER JOIN raena_catalog_management.product B ON upper(A.bundle_sku) = upper(B.sku)
INNER JOIN raena_catalog_management.product C ON upper(A.sku) = upper(C.sku);
DROP TABLE IF EXISTS public.order_level_data;
CREATE TABLE public.order_level_data AS
SELECT external_id,
transaction_date AS transaction_date,
discount_amount,
shipping_cost,
A.coupon_code,
reseller_tier_name,
CASE
WHEN flash_sale_id IS NOT NULL THEN 'Flash'
END Product_type,
order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount,
coupon_applied_on
FROM
(SELECT A.id AS external_id ,
(A.created_at) AS transaction_date ,
A.discount_amount,
applied_shipping_amount shipping_cost,
A.coupon_code,
json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
flash_sale_id,
loyalty_discount AS order_loyalty_discount,
total_amount ,
total_retail_price ,
tier_discount,
total_dynamic_and_tier_price,
payment_amount
FROM raena_order_management.order A
WHERE payment_status='Paid'
AND cast(A.created_at AS date) >='$reportDate'
AND is_campaign = 'false' ) A
LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code;
DROP TABLE IF EXISTS public.base_netsuite_stage1;
CREATE TABLE public.base_netsuite_stage1 AS
SELECT DISTINCT transaction_date,
A.external_id,
B.country,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_sku
ELSE B.sku
END sku,
CASE
WHEN B.parent_item_id = F.id THEN F.sku
ELSE B.sku
END parent_sku,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_quantity*B.quantity
ELSE B.quantity
END quantity,
CASE
WHEN B.parent_item_id = F.id THEN F.quantity
ELSE B.quantity
END parent_quantity,
CASE
WHEN cast(B.sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_retail_price
ELSE B.retail_price
END retail_price,