create or replace type myTableType as table
of varchar2 (255);
create or replace
function in_list( p_string in varchar2 ) return myTableType
as
l_string long default p_string || ',';
l_data myTableType := myTableType();
n number;
begin
loop
exit when l_string is null;
n := instr( l_string, ',' );
l_data.extend;
l_data(l_data.count) :=
ltrim( rtrim( substr( l_string, 1, n-1 ) ) );
l_string := substr( l_string, n+1 );
end loop;
return l_data;
end;
/
/* Formatted on 12/24/2010 10:42:59 AM (QP5 v5.163.1008.3004) */
CREATE OR REPLACE PACKAGE BODY tms.bgl_otd_report
AS
PROCEDURE get_hub_summary (p_dows VARCHAR2 := 'MON,TUE,WED,THU',
p_result OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_result FOR
WITH branch
AS (SELECT location_id, business_unit, service_hub
FROM tms.tms_location
WHERE business_unit IS NOT NULL),
daterange
AS (SELECT TRUNC (ADD_MONTHS (LAST_DAY (SYSDATE), -3) + 1)
start_date,
LAST_DAY (SYSDATE)
end_date
FROM DUAL),
dowrange
AS (SELECT COLUMN_VALUE dow
FROM THE (
SELECT CAST (in_list (p_dows) AS mytabletype)
FROM DUAL
)),
nohubtohub
AS (SELECT h.*, t.business_unit hub_branch_unit
FROM tms.tms_route_header h, tms.tms_location t
WHERE h.dispatch_location_id = t.location_id
AND t.location_type = 'HUB'
AND h.download_exempt = 'N'
AND (h.branch_pick_up_flag = 'N'
OR (h.branch_pick_up_flag = 'Y'
AND parent_bill_of_lading_id IS NOT NULL))
AND h.freight_flag = 'N'
AND h.delete_flag = 'N'
AND h.host_created_dt > SYSDATE - 100
AND EXISTS
(SELECT 1
FROM dowrange
WHERE dowrange.dow = h.dow)
AND EXISTS
(SELECT 1
FROM daterange
WHERE h.scheduled_departure_date >=
daterange.start_date
AND h.scheduled_departure_date <
daterange.end_date)--AND (:hub_code IS NULL OR t.business_unit = :hub_code)
),
base
AS (SELECT /*+ ORDERED USE_NL (h s) USE_HASH (tstp l) USE_HASH (H t) USE_HASH (H ths) */
s .stop_id,
h.truck_route_id,
s.truck_route_instance_id,
s.scheduled_arrival_dt,
h.hub_branch_unit,
h.completion_program_name,
DECODE (completion_program_name,
'FASTNET', 0,
NVL2 (s.exception_code, 1, 0))
excepted,
DECODE (
completion_program_name,
'FASTNET', 1,
SIGN (
NVL (ths.early_minutes, 30)
+ (NVL (s.first_scan_time,
s.scheduled_arrival_dt)
- s.scheduled_arrival_dt)
* 1440
)
)
early,
DECODE (
completion_program_name,
'FASTNET', 1,
SIGN (
NVL (ths.ontime_minutes, 30)
- (NVL (s.first_scan_time,
s.scheduled_arrival_dt)
- s.scheduled_arrival_dt)
* 1440
)
)
late,
SIGN (
DECODE (
completion_program_name,
'FASTNET', -1,
NVL2 (
s.exception_code,
-1,
CASE
WHEN SIGN (
s.first_scan_time - s.scheduled_arrival_dt
) = 1
THEN
NVL (ths.ontime_minutes, 30)
- (s.first_scan_time
- s.scheduled_arrival_dt)
* 1440
ELSE
NVL (ths.early_minutes, 120)
+ (s.first_scan_time
- s.scheduled_arrival_dt)
* 1440
END
)
)
)
on_time,
DECODE (completion_program_name, 'FASTNET', 1, 0)
by_intf,
DECODE (completion_program_name,
'FASTNET', NULL,
1)
stops_cnt,
TO_CHAR (
TRUNC (h.scheduled_departure_date, 'MONTH'),
'YYYY-MM-DD'
)
calc_date
-- notes:if an exception stop, first_scan_time will be null, which will be considered as 'Late'
FROM nohubtohub h
INNER JOIN tms_route_stop s
ON h.truck_route_instance_id =
s.truck_route_instance_id
INNER JOIN branch
ON s.location_id = branch.location_id
LEFT JOIN tms.tms_hub_month_mv ths
ON ths.business_unit = h.hub_branch_unit
AND TO_CHAR (scheduled_departure_date,
'YYYY-MM') = ths.report_month
WHERE s.scheduled_arrival_dt IS NOT NULL
AND h.end_of_day IS NOT NULL
AND s.is_blank_stop = 'N'
AND s.host_created_dt > SYSDATE - 100),
complete_vw
AS (SELECT rpt1.hub_branch_unit,
rpt1.calc_date,
COUNT (DISTINCT rpt1.truck_route_instance_id)
total_routes,
COUNT (
DISTINCT DECODE (completion_program_name,
'FASTNET', NULL,
rpt1.truck_route_instance_id)
)
complete_routes,
COUNT (
DISTINCT DECODE (
completion_program_name,
'FASTNET', rpt1.truck_route_instance_id,
NULL
)
)
intf_routes,
COUNT (stops_cnt) total_stops,
DECODE (
COUNT (stops_cnt),
0, 0,
ROUND (
100
* SUM (
DECODE (on_time,
-1, 0,
0, 1,
on_time)
)
/ COUNT (stops_cnt),
2
)
)
ontime_percent,
DECODE (
COUNT (stops_cnt),
0, 0,
ROUND (
100
* SUM (DECODE (late, -1, 1, 0))
/ COUNT (stops_cnt),
2
)
)
late_percent,
DECODE (
COUNT (stops_cnt),
0, 0,
ROUND (
100
* SUM (DECODE (early, -1, 1, 0))
/ COUNT (stops_cnt),
2
)
)
early_percent,
DECODE (
COUNT (stops_cnt),
0, 0,
ROUND (
100 * SUM (excepted) / COUNT (stops_cnt),
2
)
)
excepted_percent,
SUM (DECODE (late, -1, 1, 0)) late_stops,
SUM (DECODE (early, -1, 1, 0)) early_stops,
SUM (excepted) excepted_stops,
SUM (by_intf) intf_stops,
0 exempt_routes,
0 incomplete_routes
FROM base rpt1
GROUP BY rpt1.hub_branch_unit, rpt1.calc_date),
incomplete_vw
AS ( /*+ materialize*/
SELECT hub_branch_unit,
calc_date,
COUNT (truck_route_instance_id) total_routes,
0 complete_routes,
0 intf_routes,
0 total_stops,
0 ontime_percent,
0 late_percent,
0 early_percent,
0 excepted_percent,
0 late_stops,
0 early_stops,
0 excepted_stops,
0 intf_stops,
0 exempt_routes,
COUNT (truck_route_instance_id) incomplete_routes
FROM (SELECT h.truck_route_instance_id,
TO_CHAR (
TRUNC (h.scheduled_departure_date,
'MONTH'),
'YYYY-MM-DD'
)
calc_date,
h.hub_branch_unit
FROM nohubtohub h
WHERE h.end_of_day IS NULL)
GROUP BY hub_branch_unit, calc_date)
SELECT route.hub_branch_unit,
route.calc_date,
route.total_routes,
route.complete_routes,
route.intf_routes,
route.incomplete_routes,
route.total_stops,
DECODE (
SIGN (
100
- (late_percent + ontime_percent + excepted_percent)
),
-1, 0,
100 - (late_percent + ontime_percent + excepted_percent)
)
early_percent,
route.late_percent,
route.ontime_percent,
route.excepted_percent,
late_stops,
early_stops,
excepted_stops,
intf_stops,
p_dows weekdays
FROM (SELECT hub_branch_unit,
calc_date,
SUM (total_routes) total_routes,
SUM (complete_routes) complete_routes,
SUM (intf_routes) intf_routes,
SUM (total_stops) total_stops,
DECODE (
SUM (complete_routes),
0, NULL,
DECODE (SUM (total_stops),
0, NULL,
SUM (ontime_percent))
)
ontime_percent,
DECODE (
SUM (complete_routes),
0, NULL,
DECODE (SUM (total_stops),
0, NULL,
SUM (late_percent))
)
late_percent,
DECODE (
SUM (complete_routes),
0, NULL,
DECODE (SUM (total_stops),
0, NULL,
SUM (early_percent))
)
early_percent,
DECODE (
SUM (complete_routes),
0, NULL,
DECODE (SUM (total_stops),
0, NULL,
SUM (excepted_percent))
)
excepted_percent,
SUM (late_stops) late_stops,
SUM (early_stops) early_stops,
SUM (excepted_stops) excepted_stops,
SUM (intf_stops) intf_stops,
SUM (incomplete_routes) incomplete_routes
FROM (SELECT * FROM complete_vw
UNION ALL
SELECT * FROM incomplete_vw) vw
GROUP BY hub_branch_unit, calc_date) route
ORDER BY route.hub_branch_unit, calc_date;
END;
END;
/