Old Query Bpost
Old Query Bpost
WITH contact AS
(
SELECT order_number,
ts_case_created,
ts_case_closed,
case_country,
sk_case,
sk_case_customer_segment,
case_number,
TRIM(t.shipment_number) AS shipment_number
FROM
(SELECT CASE WHEN LENGTH(CC.cc_shipmentnumber__c) = 16
THEN ARRAY[CC.cc_shipmentnumber__c]
WHEN strpos(CC.cc_shipmentnumber__c, '-') > 0
THEN split(CC.cc_shipmentnumber__c, '-')
WHEN strpos(CC.cc_shipmentnumber__c, '&') > 0
THEN split(CC.cc_shipmentnumber__c, '&')
WHEN strpos(CC.cc_shipmentnumber__c, '//') > 0
THEN split(CC.cc_shipmentnumber__c, '//')
WHEN strpos(CC.cc_shipmentnumber__c, '/') > 0
THEN split(CC.cc_shipmentnumber__c, '/')
WHEN strpos(CC.cc_shipmentnumber__c, ',') > 0
THEN split(CC.cc_shipmentnumber__c, ',')
WHEN strpos(CC.cc_shipmentnumber__c, ';') > 0
THEN split(CC.cc_shipmentnumber__c, ';')
WHEN strpos(CC.cc_shipmentnumber__c, ' ') > 0
THEN split(CC.cc_shipmentnumber__c, ' ')
WHEN CC.cc_shipmentnumber__c IS NULL THEN NULL
END
AS shipment_number_array,
CCO.ordertable_ordernumber AS order_number,
CCO.casenumber as case_number,
CC.sk_case AS sk_case,
CC.closed_date AS ts_case_closed,
CCO.case_created_date AS ts_case_created,
sf_appdomain.countryisocode AS case_country,
CC.sk_case_customer_segment as sk_case_customer_segment
parcel_item_information AS (
SELECT -- parcel related information
FCCS.shipment_number AS shipment_number,
L.inventory_stock_location_name AS outbound_warehouse,
d_carrier.carrier_name AS delivery_carrier_name,
FUL_SERV.fulfillment_service AS selected_fulfill_service,
FFSOI.ts_shipment AS ts_shipment,
beta.dangerous_goods, --added by gaurav
FCCS.ts_customer_delivery AS ts_customer_delivery,
(FCCS.is_return_to_sender = 1) AS is_return_to_sender,
OP_ADD.shipping_zip,
DPM.method_name as payment_method,
MR.sum_refund_amount AS refund_amount,
FCCS.is_delivered_to_home AS is_delivered_to_home,
FCCS.is_delivered_to_pickup_point AS is_delivered_to_pickup_point,
FCCS.is_home_target AS is_home_target,
FCCS.is_pickup_point_target AS is_pickup_point_target,
case
when FCCS.is_home_target = 1 then 'Home'
when FCCS.is_pickup_point_target = 1 then 'PuP'
else 'Unknown' end as delivery_destination,
CASE
WHEN uuid_inventory_stock_location = '2326fbd5-a5f6-424a-9a9d-3f1d47b13909' THEN
'Moissy'
WHEN uuid_inventory_stock_location = '87ad6f06-2471-4ed8-9ff5-fd66e5a33f73' THEN
'Brunna'
WHEN uuid_inventory_stock_location = 'b1ad94ac-104a-457e-b17d-d52dfe88ac3f' THEN
'Gardno'
WHEN uuid_inventory_stock_location = 'c1b71d53-d9cb-442a-8192-fb56346ebc6f' THEN
'Stradella'
WHEN uuid_inventory_stock_location = 'e74ea662-d38d-4dca-a8c7-9c82a40dfe9a' THEN
'Olsztynek'
WHEN uuid_inventory_stock_location = '05e1ed1a-6626-4d01-9dd9-d682b546d08c' THEN
'Brzoza'
WHEN uuid_inventory_stock_location = '1e57b2b6-907b-4676-8766-339a1d596e59' THEN
'Mönchengladbach'
WHEN uuid_inventory_stock_location = '32eff430-f835-44ce-947a-4a1fdce1c615' THEN
'Erfurt'
WHEN uuid_inventory_stock_location = '3f98a525-6ce7-4912-a365-198c45b3adb9' THEN
'Szczecin (IDC)'
WHEN uuid_inventory_stock_location = '40899f30-9ff9-4094-9ad6-0694b9051152' THEN
'Brieselang'
WHEN uuid_inventory_stock_location = '4cd29a96-0cc2-43c7-9a50-53602294c3a7' THEN
'Rotterdam'
WHEN uuid_inventory_stock_location = '8f1e0274-a411-4988-9652-c3195a80b57c' THEN
'Grossbeeren'
WHEN uuid_inventory_stock_location = '95eacdb2-4122-4f46-bb9e-2f1eb631a26c' THEN
'Halle (IDC)'
WHEN uuid_inventory_stock_location = '963d4dba-babc-4e3b-84ca-3d56895aa1bc' THEN
'Verona'
WHEN uuid_inventory_stock_location = '9a2c9671-fb0a-401e-86c6-285b77cc0557' THEN
'Madrid'
WHEN uuid_inventory_stock_location = 'b6f6cf2b-5e63-4693-a658-99748b871d7e' THEN
'Lahr'
WHEN uuid_inventory_stock_location = 'd9f25308-2907-460e-a8a4-8f9cea33051c' THEN
'Lodz'
WHEN uuid_inventory_stock_location = 'e52667dc-7511-45fc-98e4-b96f48bfa330' THEN
'Poznan'
WHEN uuid_inventory_stock_location = 'f4276462-6afc-4685-9471-86e6c1cb994b' THEN
'Berlin (IDC)'
ELSE uuid_inventory_stock_location
END AS warehouse,
from
dwh_confidential.f_salesorder_position
where
date(order_date) between date('2023-01-01') and date('2023-12-31')
group by
1,
2,
3
ON mr.sk_order = OP.sk_order
and mr.sk_orderposition = OP.sk_order_position
/* contact table */
LEFT JOIN contact DC
ON FFSOI.shipment_number = DC.shipment_number
JOIN hive.dwh_insider.d_sales_channels SC
ON FCCS.sk_sales_channel = SC.sk_sales_channels
overall as
(
SELECT
dst.country_code,
CAR.carrier_name,
clv.holistic_value_segment,
substring(OP_ADD.shipping_zip,1,2) as zipcode,
format_datetime(ts_shipment_shipped, 'xxxx-MMMM') AS year_month,
case
when is_home_target = 1 then 'Home'
when is_pickup_point_target = 1 then 'PuP'
else 'Unknown' end as delivery_destination,
CASE
WHEN uuid_inventory_stock_location = '2326fbd5-a5f6-424a-9a9d-3f1d47b13909' THEN
'Moissy'
WHEN uuid_inventory_stock_location = '87ad6f06-2471-4ed8-9ff5-fd66e5a33f73' THEN
'Brunna'
WHEN uuid_inventory_stock_location = 'b1ad94ac-104a-457e-b17d-d52dfe88ac3f' THEN
'Gardno'
WHEN uuid_inventory_stock_location = 'c1b71d53-d9cb-442a-8192-fb56346ebc6f' THEN
'Stradella'
WHEN uuid_inventory_stock_location = 'e74ea662-d38d-4dca-a8c7-9c82a40dfe9a' THEN
'Olsztynek'
WHEN uuid_inventory_stock_location = '05e1ed1a-6626-4d01-9dd9-d682b546d08c' THEN
'Brzoza'
WHEN uuid_inventory_stock_location = '1e57b2b6-907b-4676-8766-339a1d596e59' THEN
'Mönchengladbach'
WHEN uuid_inventory_stock_location = '32eff430-f835-44ce-947a-4a1fdce1c615' THEN
'Erfurt'
WHEN uuid_inventory_stock_location = '3f98a525-6ce7-4912-a365-198c45b3adb9' THEN
'Szczecin (IDC)'
WHEN uuid_inventory_stock_location = '40899f30-9ff9-4094-9ad6-0694b9051152' THEN
'Brieselang'
WHEN uuid_inventory_stock_location = '4cd29a96-0cc2-43c7-9a50-53602294c3a7' THEN
'Rotterdam'
WHEN uuid_inventory_stock_location = '8f1e0274-a411-4988-9652-c3195a80b57c' THEN
'Grossbeeren'
WHEN uuid_inventory_stock_location = '95eacdb2-4122-4f46-bb9e-2f1eb631a26c' THEN
'Halle (IDC)'
WHEN uuid_inventory_stock_location = '963d4dba-babc-4e3b-84ca-3d56895aa1bc' THEN
'Verona'
WHEN uuid_inventory_stock_location = '9a2c9671-fb0a-401e-86c6-285b77cc0557' THEN
'Madrid'
WHEN uuid_inventory_stock_location = 'b6f6cf2b-5e63-4693-a658-99748b871d7e' THEN
'Lahr'
WHEN uuid_inventory_stock_location = 'd9f25308-2907-460e-a8a4-8f9cea33051c' THEN
'Lodz'
WHEN uuid_inventory_stock_location = 'e52667dc-7511-45fc-98e4-b96f48bfa330' THEN
'Poznan'
WHEN uuid_inventory_stock_location = 'f4276462-6afc-4685-9471-86e6c1cb994b' THEN
'Berlin (IDC)'
ELSE uuid_inventory_stock_location
END AS warehouse,
count(distinct FCCS.shipment_number) as shipped_shipments,
count(distinct FCCS.shipment_number) filter (where FCCS.ts_customer_delivery IS NOT
NULL ) as delivered_shipments,
sum(OP.gmv_bef_return) as gmv_bef_return
)
,
all_parcels_final AS (
SELECT
p.year_month,
case_country,
p.dangerous_goods, -- added by gaurav
p.delivery_carrier_name, -- added by gaurav
SUBSTRING(shipping_zip,1,2) AS zipcode_region,
p.delivery_destination,
-- customer_segment,
p.holistic_value_segment,
p.warehouse,
o.shipped_shipments,
o.delivered_shipments,
FROM parcel_item_information as p
left join overall as o
on p.case_country = o.country_code
and SUBSTRING(p.shipping_zip,1,2) = o.zipcode
and p.delivery_carrier_name = o.carrier_name
and p.holistic_value_segment = o.holistic_value_segment
and p.delivery_destination = o.delivery_destination
and p.year_month = o.year_month
and p.warehouse = o.warehouse
SELECT *
FROM all_parcels_final