-
Notifications
You must be signed in to change notification settings - Fork 0
/
05 Order delivery.sql
30 lines (27 loc) · 1.21 KB
/
05 Order delivery.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- ORDER DELIVERY
-- order_stage_times_top_10_citites
SELECT
UPPER(customer_city)
AS city,
AVG(JULIANDAY(order_approved_at) - JULIANDAY(order_purchase_timestamp))
AS approved,
AVG(JULIANDAY(order_delivered_carrier_date) - JULIANDAY(order_approved_at))
AS delivered_to_carrier,
AVG(JULIANDAY(order_delivered_customer_date) - JULIANDAY(order_delivered_carrier_date))
AS delivered_to_customer,
AVG(JULIANDAY(order_estimated_delivery_date) - JULIANDAY(order_delivered_customer_date))
AS estimated_delivery
FROM orders
JOIN customers USING (customer_id)
WHERE customer_city IN ('sao bernardo do campo', 'guarulhos', 'salvador', 'porto alegre',
'campinas', 'curitiba', 'brasilia', 'belo horizonte', 'rio de janeiro', 'sao paulo')
GROUP BY customer_city
ORDER BY approved + delivered_to_carrier + delivered_to_customer DESC;
-- daily_avg_shipping_time
SELECT
DATE(order_purchase_timestamp) AS purchase_date,
AVG(JULIANDAY(order_delivered_customer_date) - JULIANDAY(order_purchase_timestamp))
AS avg_delivery_time
FROM orders
WHERE order_purchase_timestamp >= '2017-06-01' AND order_purchase_timestamp <= '2018-06-30'
GROUP BY DATE(order_purchase_timestamp);