Script 3

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

WITH

receiving AS (
SELECT
receive_date_ot_cutoff
,HOUR(receive_time_ot) AS hora_receive
,station_id
,station_name
,lm_3pl
,turno
,COUNT(spx_tracking_number) AS receive_orders

FROM (
S

FROM (
SELECT
packing_date_ot_cutoff
,handover_time_ot
,spx_tracking_number
,station_id_receive AS station_id
,station_name_receive AS station_name
,lm_3pl
,CASE
WHEN station_id_receive = 5007 THEN
CASE
WHEN CAST(handover_time_ot AS TIME) BETWEEN CAST('06:00:00' AS
TIME) AND CAST('14:19:59' AS TIME) THEN '1T'
WHEN CAST(handover_time_ot AS TIME) BETWEEN CAST('14:20:00' AS
TIME) AND CAST('21:39:59' AS TIME) THEN '2T'
WHEN CAST(handover_time_ot AS TIME) BETWEEN CAST('21:40:00' AS
TIME) AND CAST('23:59:59' AS TIME) THEN '3T'
WHEN CAST(handover_time_ot AS TIME) BETWEEN CAST('00:00:00' AS
TIME) AND CAST('05:59:59' AS TIME) THEN '3T'
ELSE NULL
END
ELSE
CASE
WHEN CAST(handover_time_ot AS TIME) BETWEEN CAST('05:25:00' AS
TIME) AND CAST('13:44:59' AS TIME) THEN '1T'
WHEN CAST(handover_time_ot AS TIME) BETWEEN CAST('13:45:00' AS
TIME) AND CAST('21:59:59' AS TIME) THEN '2T'
WHEN CAST(handover_time_ot AS TIME) BETWEEN CAST('22:00:00' AS
TIME) AND CAST('23:59:59' AS TIME) THEN '3T'
WHEN CAST(handover_time_ot AS TIME) BETWEEN CAST('00:00:00' AS
TIME) AND CAST('05:24:59' AS TIME) THEN '3T'
ELSE NULL
END
END AS turno

FROM xd_barueri_consolidado
)

GROUP BY
packing_date_ot_cutoff
,HOUR(handover_time_ot)
,station_id
,station_name
,lm_3pl
,turno
),

handover_lh AS (
SELECT
lh_packing_date_ot_cutoff
,HOUR(lh_handover_time_ot) AS hora_lh_handover
,station_id
,station_name
,lm_3pl
,turno
,COUNT(spx_tracking_number) AS lh_handover_orders

FROM (
SELECT
lh_packing_date_ot_cutoff
,lh_handover_time_ot
,spx_tracking_number
,station_id_receive AS station_id
,station_name_receive AS station_name
,lm_3pl
,CASE
WHEN station_id_receive = 5007 THEN
CASE
WHEN CAST(lh_handover_time_ot AS TIME) BETWEEN CAST('06:00:00'
AS TIME) AND CAST('14:19:59' AS TIME) THEN '1T'
WHEN CAST(lh_handover_time_ot AS TIME) BETWEEN CAST('14:20:00'
AS TIME) AND CAST('21:39:59' AS TIME) THEN '2T'
WHEN CAST(lh_handover_time_ot AS TIME) BETWEEN CAST('21:40:00'
AS TIME) AND CAST('23:59:59' AS TIME) THEN '3T'
WHEN CAST(lh_handover_time_ot AS TIME) BETWEEN CAST('00:00:00'
AS TIME) AND CAST('05:59:59' AS TIME) THEN '3T'
ELSE NULL
END
ELSE
CASE
WHEN CAST(lh_handover_time_ot AS TIME) BETWEEN CAST('05:25:00'
AS TIME) AND CAST('13:44:59' AS TIME) THEN '1T'
WHEN CAST(lh_handover_time_ot AS TIME) BETWEEN CAST('13:45:00'
AS TIME) AND CAST('21:59:59' AS TIME) THEN '2T'
WHEN CAST(lh_handover_time_ot AS TIME) BETWEEN CAST('22:00:00'
AS TIME) AND CAST('23:59:59' AS TIME) THEN '3T'
WHEN CAST(lh_handover_time_ot AS TIME) BETWEEN CAST('00:00:00'
AS TIME) AND CAST('05:24:59' AS TIME) THEN '3T'
ELSE NULL
END
END AS turno

FROM xd_barueri_consolidado
)

GROUP BY
lh_packing_date_ot_cutoff
,HOUR(lh_handover_time_ot)
,station_id
,station_name
,lm_3pl
,turno
),
base AS (
SELECT DISTINCT
dia AS date_cutoff
,hora
,CASE hora
WHEN 23 THEN 19
WHEN 0 THEN 20
WHEN 1 THEN 21
WHEN 2 THEN 22
WHEN 3 THEN 23
WHEN 4 THEN 24
WHEN 5 THEN 1
WHEN 6 THEN 2
WHEN 7 THEN 3
WHEN 8 THEN 4
WHEN 9 THEN 5
WHEN 10 THEN 6
WHEN 11 THEN 7
WHEN 12 THEN 8
WHEN 13 THEN 9
WHEN 14 THEN 10
WHEN 15 THEN 11
WHEN 16 THEN 12
WHEN 17 THEN 13
WHEN 18 THEN 14
WHEN 19 THEN 15
WHEN 20 THEN 16
WHEN 21 THEN 17
WHEN 22 THEN 18
END AS horacorte
,station_name
,station_id

FROM data_periodo

CROSS JOIN horas

CROSS JOIN (
SELECT DISTINCT
station_id_receive AS station_id
,station_name_receive AS station_name

FROM xd_barueri_consolidado
)

WHERE dia BETWEEN CAST((date_trunc('MONTH',(CURRENT_TIMESTAMP - INTERVAL '11'


HOUR)) - INTERVAL '1' MONTH) AS DATE) AND CAST((CURRENT_TIMESTAMP - INTERVAL '11'
HOUR) AS DATE)
)

SELECT
b.date_cutoff
,b.hora
,b.horacorte
,b.station_name
,b.station_id
,xd.lm_3pl
,xd.turno
,xd.receive_parcels
,xd.packing_parcels
,xd.handover_parcels
,xd.lh_handover_parcels
,CASE WHEN xd.handover_parcels IS NULL THEN 0 ELSE xd.handover_parcels END
+ CASE WHEN xd.lh_handover_parcels IS NULL THEN 0 ELSE xd.lh_handover_parcels
END
AS tt_handover_parcels

FROM base b

LEFT JOIN (
SELECT
date_cutoff
,hora
,station_id
,station_name
,lm_3pl
,turno
,SUM(receive_orders) AS receive_parcels
,SUM(packing_orders) AS packing_parcels
,SUM(handover_orders) AS handover_parcels
,SUM(lh_handover_orders) AS lh_handover_parcels

FROM (
SELECT
receive_date_ot_cutoff AS date_cutoff
,hora_receive AS hora
,station_id
,station_name
,lm_3pl
,turno
,receive_orders
,0 AS packing_orders
,0 AS handover_orders
,0 AS lh_handover_orders

FROM receiving r

UNION

SELECT
packing_date_ot_cutoff AS date_cutoff
,hora_packing AS hora
,station_id
,station_name
,lm_3pl
,turno
,0 AS receive_orders
,packing_orders
,0 AS handover_orders
,0 AS lh_handover_orders

FROM packing p

UNION
SELECT
packing_date_ot_cutoff AS date_cutoff
,hora_handover AS hora
,station_id
,station_name
,lm_3pl
,turno
,0 AS receive_orders
,0 AS packing_orders
,handover_orders
,0 AS lh_handover_orders

FROM handover h

UNION

SELECT
lh_packing_date_ot_cutoff AS date_cutoff
,hora_lh_handover AS hora
,station_id
,station_name
,lm_3pl
,turno
,0 AS receive_orders
,0 AS packing_orders
,0 AS handover_orders
,lh_handover_orders

FROM handover_lh
)

GROUP BY
date_cutoff
,hora
,station_id
,station_name
,lm_3pl
,turno
) xd
ON b.date_cutoff = xd.date_cutoff
AND b.hora = xd.hora
AND b.station_id = xd.station_id

You might also like