0% found this document useful (0 votes)
5 views4 pages

Percentile

Uploaded by

fakhry.syahbana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Percentile

Uploaded by

fakhry.syahbana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

WITH

raw AS (
SELECT
fosl.date_key,
CASE WHEN fulfill_location LIKE '%Hub%' THEN fsof.hub_id ELSE
fosl.location_id END AS hub_id,
dl.location_name,
fosl.order_id,
fosl.order_type AS order_origin,
customer_id,
fosl.pool_id AS _pool_id,
fosl.pool_number AS _pool_number,
pool_trigger,
-- buyer_quantity,
-- quantity_fulfilled,
CASE
WHEN is_astro_kitchen = TRUE OR fosl.order_type IN ('SUPER',
'SCHEDULED_INSTANT') THEN NULL
WHEN buyer_quantity = 0 THEN NULL
WHEN quantity_fulfilled / buyer_quantity < 1 THEN 0
ELSE 1
END AS fulfilled,
sla_target,
is_astro_kitchen AS contains_astro_kitchen,
CASE WHEN fosl.order_type = 'SCHEDULED_INSTANT' THEN true ELSE false END AS
is_scheduled_instant,
in_review_to_new_order * 60 AS in_review_to_new_order_sec,
CASE WHEN fosl.order_type IN ('SUPER', 'SCHEDULED_INSTANT') THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE new_order_to_pooling * 60 END AS new_order_to_pooling_time_sec,
CASE WHEN fosl.order_type IN ('SUPER', 'SCHEDULED_INSTANT') THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE payment_to_picking END * 60 AS waiting_picking_time_sec,
CASE WHEN fosl.order_type IN ('SUPER', 'SCHEDULED_INSTANT') OR
is_astro_kitchen = TRUE THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE picking_to_packing END * 60 AS picking_time_sec,
CASE WHEN fosl.order_type IN ('SUPER', 'SCHEDULED_INSTANT') OR
is_astro_kitchen = TRUE THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE packing_to_waiting_driver END * 60 AS packing_time_sec,
CASE WHEN fosl.order_type IN ('SUPER', 'SCHEDULED_INSTANT') OR
is_astro_kitchen = TRUE THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE waiting_packer END * 60 AS waiting_packer_sec,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE waiting_driver END * 60 AS waiting_driver_time_sec,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE waiting_driver_to_on_delivery END * 60 AS waiting_delivery_time_sec,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE in_transit_to_delivery END * 60 in_transit_time_sec,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE on_delivery_to_arrived END * 60 AS delivery_time_sec,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE ready_for_delivery_to_completed END * 60 AS driving_time_sec,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE arrived_to_completed END * 60 AS receiving_time_sec,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE payment_to_arrived END * 60 AS payment_to_completed_time_sec,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
ELSE back_to_hub END * 60 AS back_to_hub_time_sec,
CASE
WHEN fosl.order_type NOT IN ('INSTANT', 'SCHEDULED_INSTANT') THEN NULL
WHEN is_astro_kitchen = TRUE THEN NULL
WHEN (sla_status != 'Missed SLA' OR sla_status IS NULL) AND fosl.order_type
IN ('INSTANT', 'SCHEDULED_INSTANT') THEN 1
WHEN sla_status = 'Missed SLA' THEN 0
END AS sla_achieved,
CASE
WHEN fosl.order_type NOT IN ('INSTANT', 'SCHEDULED_INSTANT') THEN NULL
WHEN fosl.new_order_time BETWEEN oe.start_timestamp AND oe.end_timestamp
THEN NULL
WHEN is_astro_kitchen = TRUE THEN NULL
WHEN (extreme_sla_status != 'Extreme SLA' OR extreme_sla_status IS NULL)
AND fosl.order_type IN ('INSTANT', 'SCHEDULED_INSTANT') THEN 0
WHEN extreme_sla_status = 'Extreme SLA' THEN 1
END AS extreme_sla,
-- distance_meter,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL WHEN
distance_arrived_to_customer_location_in_meter > 200 THEN 1 ELSE 0 END
arrived_at_wrong_location_200,
CASE WHEN fosl.order_type != 'INSTANT' THEN NULL WHEN
distance_arrived_to_customer_location_in_meter > 100 THEN 1 ELSE 0 END
arrived_at_wrong_location_100,
payment_to_arrived AS sla_time_minute,
CASE WHEN is_external_drivers = TRUE AND fosl.order_type != 'GRAB_MART' THEN
1 ELSE 0 END delivered_by_3pl,
gmv,
fosl.scheduled_instant_order_source,
-- sla_status,
-- extreme_sla_status,
fosl.is_reroute,
rdwd.driver_effective_working_duration_min
FROM `astro-data-prd.astro_dataset.fact_order_sla_details` fosl
LEFT JOIN `astro-data-prd.astro_dataset.fact_super_order_fulfillments` fsof
USING(order_id)
LEFT JOIN `astro-data-prd.astro_dataset.dim_blacklisted_order` dbo
USING(order_id)
LEFT JOIN `astro-data-prd.astro_dataset.dim_locations` dl ON dl.location_id =
CASE WHEN fulfill_location LIKE '%Hub%' THEN hub_id ELSE fosl.location_id END
LEFT JOIN `astro-data-prd.astro_datamart.rpt_driver_effective_working_duration`
rdwd USING(order_id)
LEFT JOIN `astro-data-prd.astro_google_sheet_bq_table.order_exception_native`
oe ON SAFE_CAST(oe.hub_id AS INT64) = CASE WHEN fulfill_location LIKE '%Hub%' THEN
fsof.hub_id ELSE fosl.location_id END
WHERE TRUE
AND order_status = 'Completed'
AND dbo.order_id IS NULL
AND (reference_order IS NULL OR reference_order = '')
AND location_detail_id = 1
-- AND fosl.date_key = '2023-08-18'
AND location_type = 'HUB'
),

pool_2h AS (
SELECT
_pool_number,
SUM(CASE WHEN sla_target = 120 THEN 1 ELSE 0 END) pool_with_2h_order
FROM raw
WHERE _pool_number IS NOT NULL AND order_origin != 'SUPER'
GROUP BY 1
HAVING pool_with_2h_order > 0
),

final AS (
SELECT

case when extract(day from date_key) >=1 and extract(day from date_key) <= 7 then
concat('1-7 ',FORMAT_DATETIME("%b",date_key),' ',extract(year from date_key))
when extract(day from date_key) >=8 and extract(day from date_key) <= 14 then
concat('8-14 ',FORMAT_DATETIME("%b",date_key),' ',extract(year from date_key))
when extract(day from date_key) >=15 and extract(day from date_key) <= 21 then
concat('15-21 ',FORMAT_DATETIME("%b",date_key),' ',extract(year from date_key))
else
concat('22-',extract(day from DATETIME_SUB(DATETIME_ADD(DATETIME_TRUNC(date_key,
month),interval 1 month), interval 1 day)),' ',FORMAT_DATETIME("%b",date_key),'
',extract(year from date_key))
END as month_week,

raw.*,
CASE WHEN _pool_number IN (SELECT _pool_number FROM pool_2h) THEN 1 END AS
pooled_with_2h_order,
FROM raw
LEFT JOIN pool_2h USING(_pool_number)
)

select
month_week,sla_target,
(PERCENTILE_DISC(picking_time_sec, 0.5) OVER (PARTITION BY
month_week,sla_target))/60 as test2,
(PERCENTILE_CONT(picking_time_sec, 0.5) OVER (PARTITION BY
month_week,sla_target))/60 as test3
from final

where date_key >= '2024-04-15' and date_key <= '2024-04-21' and sla_target = 30

You might also like