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

;with LoadingBayData as (

SELECT
[Region]
,[Slot]
,[Hour]
,USed
,Idle
,[Loaded Trucks]
FROM [MobileApps_PlantAutomation].[Queue].[Loading Bay Analysis]
where [Hour] <= datepart(hour,getdate())
),

LoadingBayAnalysis as (
SELECT
[Region]
,[Slot]
, SUM([Loaded Trucks]) [Loaded Trucks]
FROM LoadingBayData
group by Region,Slot
),

CountofTrucks as (
SELECT [Region]
,[Slot]
,SUM([Loaded Trucks]) OVER (
PARTITION BY [Region], Slot ORDER BY [Hour]
) AS TruckCountRT
FROM LoadingBayData
----and Slot = 'Team A'
),

LoadTime as (
SELECT [Region]
,[Slot]
,SUM(USed) OVER (
PARTITION BY [Region], Slot ORDER BY [Hour]
) AS LoadRT
FROM LoadingBayData
----and Slot = 'Team A'
),

IdleTime as (
SELECT [Region]
,[Slot]
,SUM(Idle) OVER (
PARTITION BY [Region], Slot ORDER BY [Hour]
) AS IdleRT
FROM [LoadingBayData]
------and Slot = 'Team A'

), RunningTotal as (
select
lba.Region,
lba.Slot,
lba.[Loaded Trucks] as [Count of Trucks],
max(ct.TruckCountRT) as TruckCountRT,
max(lt.LoadRT) as RT_Load,
max(it.IdleRT) as RT_Idle
from LoadingBayAnalysis lba
left join LoadTime lt on
lt.Region = lba.Region
and lt.Slot = lba.Slot
left join IdleTime it on
it.Region = lba.Region
and it.Slot = lba.Slot
left join CountofTrucks ct on
ct.Region = lt.Region
and ct.Slot = lt.Slot
group by lba.Region,lba.Slot,lba.[Loaded Trucks]

select
Region,
Slot,
[Count of Trucks],
case when TruckCountRT = 0 then 0 else RT_Idle/TruckCountRT end as [Running
Total(Idle)],
case when TruckCountRT = 0 then 0 else RT_Load/TruckCountRT end as [Running
Total(Load)],
case when TruckCountRT = 0 then 0 else (RT_Idle/TruckCountRT) +
(RT_Load/TruckCountRT) end as [Total]
from RunningTotal

You might also like