select [IdProdPlanung] as lot, [IdArtikelvarianten] as av, [IdProdBereich], [IdMaschine], PlanVon as StartLot, PlanBis as EndLot, -- Calculate total production time in hours -- convert(float, DATEDIFF(MINUTE, PlanVon, PlanBis) / 60.0) totalProductionTime, round(PlanDauer,2) as TimeToCompleteLot, -- total production per hour round(PlanMengePaletten / plandauer,2) as palletsPerHour, --what time will it be in 24hours DATEADD(hour, 24, getdate()) as Next24hours, --time remaining CASE WHEN DATEADD(hour, 24, getdate()) <= PlanBis THEN DATEDIFF(MINUTE, getdate(), DATEADD(hour, 24, getdate())) / 60.0 ELSE DATEDIFF(MINUTE, getdate(), PlanBis) / 60.0 END as TimeRemaining, -- total pallets for the lot PlanMengePaletten as TotalPallets, --production rate per 24hours round(CASE WHEN DATEADD(hour, 24, getdate()) <= PlanBis THEN (DATEDIFF(MINUTE, getdate(), DATEADD(hour, 24, getdate())) / 60.0) * (PlanMengePaletten / plandauer) ELSE (DATEDIFF(MINUTE, getdate(), PlanBis) / 60.0 ) * (PlanMengePaletten / plandauer) END,2) as PalletsNext24Hours, --production rate per 12hours round(CASE WHEN DATEADD(hour, 12, getdate()) <= PlanBis THEN (DATEDIFF(MINUTE, getdate(), DATEADD(hour, 12, getdate())) / 60.0) * (PlanMengePaletten / plandauer) ELSE (DATEDIFF(MINUTE, getdate(), PlanBis) / 60.0 ) * (PlanMengePaletten / plandauer) END,2) as PalletsNext12Hours, round(CASE WHEN DATEADD(hour, 8, getdate()) <= PlanBis THEN (DATEDIFF(MINUTE, getdate(), DATEADD(hour, 8, getdate())) / 60.0) * (PlanMengePaletten / plandauer) ELSE (DATEDIFF(MINUTE, getdate(), PlanBis) / 60.0 ) * (PlanMengePaletten / plandauer) END,2) as PalletsNext8Hours, Bemerkung as Remarks from [AlplaPROD_usiow2].[dbo].[T_ProdPlanung] (nolock) where PlanBis between getdate()-1 and getdate()+7 --IdProdPlanung in (266882,264642,267813)