我的表包含以下格式的两个工厂的关闭事件:
ID Shutdown_Start Shutdown_End Plant DayCode Event_Description
1 2017-03-01 06:00 017-03-01 15:00 2 1 AA
2 2017-03-01 09:00 017-03-01 20:00 1 1 BB
3 2017-03-02 08:00 017-03-02 19:00 2 2 CC
4 2017-03-06 13:00 017-03-06 23:00 1 6 DD
5 2017-03-20 09:00 017-03-21 06:00 2 20 EE
6 2017-03-23 06:00 017-03-24 06:00 1 23 FF
7 2017-03-23 06:00 017-03-23 22:00 2 23 GG
DayCode列表示从第二天的06:00到06:00开始的每一天的特殊代码。 我需要做的是计算两个工厂中的任何一个工厂正在运行或者两个工厂同时运行的总时间。我现在需要总时间接收输入馈送,无论是植物还是其中任何一种植物。
在上表中,每个工厂每天可以有多个事件。
以下是2017年12月的实际数据,其中至少有一家工厂没有关闭的小时数为685.85小时,而下面的更新查询显示为528小时。为了给出更多的解释,在这种情况下,工厂14全月停工,而工厂15关闭不同时段共58.15小时,因此在本月余下时间我至少有一家工厂在运行。计算结果为31 * 24 - 58.15 = 685.85。
EventID DayCode Plant START FINISH
76093 4516 14 2017-12-01 06:00:00.000 2017-12-02 06:00:00.000
76098 4516 15 2017-12-01 15:30:00.000 2017-12-02 06:00:00.000
76099 4517 14 2017-12-02 06:00:00.000 2017-12-03 06:00:00.000
76101 4517 15 2017-12-02 06:00:00.000 2017-12-03 06:00:00.000
76106 4518 14 2017-12-03 06:00:00.000 2017-12-04 06:00:00.000
76127 4518 15 2017-12-03 06:00:00.000 2017-12-03 17:40:00.000
76112 4519 14 2017-12-04 06:00:00.000 2017-12-05 06:00:00.000
76117 4520 14 2017-12-05 06:00:00.000 2017-12-06 06:00:00.000
76122 4521 14 2017-12-06 06:00:00.000 2017-12-07 06:00:00.000
76128 4522 14 2017-12-07 06:00:00.000 2017-12-08 06:00:00.000
76133 4523 14 2017-12-08 06:00:00.000 2017-12-09 06:00:00.000
76138 4524 14 2017-12-09 06:00:00.000 2017-12-10 06:00:00.000
76151 4525 14 2017-12-10 06:00:00.000 2017-12-11 06:00:00.000
76155 4526 14 2017-12-11 06:00:00.000 2017-12-12 06:00:00.000
76159 4527 14 2017-12-12 06:00:00.000 2017-12-13 06:00:00.000
76163 4528 14 2017-12-13 06:00:00.000 2017-12-14 06:00:00.000
76168 4529 14 2017-12-14 06:00:00.000 2017-12-15 06:00:00.000
76172 4530 14 2017-12-15 06:00:00.000 2017-12-16 06:00:00.000
76176 4531 14 2017-12-16 06:00:00.000 2017-12-17 06:00:00.000
76180 4532 14 2017-12-17 06:00:00.000 2017-12-18 06:00:00.000
76184 4533 14 2017-12-18 06:00:00.000 2017-12-19 06:00:00.000
76188 4534 14 2017-12-19 06:00:00.000 2017-12-20 06:00:00.000
76192 4535 14 2017-12-20 06:00:00.000 2017-12-21 06:00:00.000
76196 4536 14 2017-12-21 06:00:00.000 2017-12-22 06:00:00.000
76199 4537 14 2017-12-22 06:00:00.000 2017-12-23 06:00:00.000
76202 4538 14 2017-12-23 06:00:00.000 2017-12-24 06:00:00.000
76205 4539 14 2017-12-24 06:00:00.000 2017-12-25 06:00:00.000
76207 4540 14 2017-12-25 06:00:00.000 2017-12-26 06:00:00.000
76209 4541 14 2017-12-26 06:00:00.000 2017-12-27 06:00:00.000
76211 4542 14 2017-12-27 06:00:00.000 2017-12-28 06:00:00.000
76213 4543 14 2017-12-28 06:00:00.000 2017-12-29 06:00:00.000
76215 4544 14 2017-12-29 06:00:00.000 2017-12-30 06:00:00.000
76217 4545 14 2017-12-30 06:00:00.000 2017-12-31 01:00:00.000
76221 4545 15 2017-12-30 11:58:00.000 2017-12-30 12:57:00.000
76218 4545 14 2017-12-31 01:00:00.000 2017-12-31 06:00:00.000
76223 4545 15 2017-12-31 01:30:00.000 2017-12-31 06:00:00.000
76225 4546 14 2017-12-31 06:00:00.000 2017-12-31 08:30:00.000
76230 4546 15 2017-12-31 06:00:00.000 2017-12-31 08:30:00.000
76229 4546 14 2017-12-31 08:30:00.000 2018-01-01 06:00:00.000
答案 0 :(得分:0)
我理解你的问题的方式是你试图了解在特定月份至少有一家工厂运行了多少小时。计算总差距更容易,然后只需减去总小时数即可获得您想要的数字。请查看下面的示例是否有帮助。
编辑:添加HAVING COUNT(*) = @countOfPlants
仅查看已关闭所有工厂的日期。这应该解决第一个版本的问题。如果有帮助,请告诉我。
DECLARE
@year INT = 2017,
@month INT = 3,
@countOfPlants int = 2,
@totalGapInHours INT,
@totalUptime INT
WITH InactivePerDay (DayCode, HoursBothPlantsShutdown)
AS
(
SELECT
DayCode,
-- take latest Shutdown_Start and earliest Shutdown_End that day
-- get hour difference, if it's less than 0 - we have no gap - take 0
MAX(DATEDIFF(HOUR, MAX(Shutdown_Start), MIN(Shutdown_End)), 0) AS [HoursBothPlantsShutdown]
FROM Table1
-- filter on the month you want
WHERE DATEPART(YEAR, Shutdown_Start) = @year
AND DATEPART(MONTH, Shutdown_Start) = @month
GROUP BY DayCode
HAVING COUNT(*) = @countOfPlants
)
-- just sum up all the gaps
SELECT @totalGapInHours = SUM(HoursBothPlantsShutdown)
FROM InactivePerDay
-- assume month has 30 days and substract total gap from number of hours in a month
SET @totalUptime = 24*30 - @totalGapInHours
SELECT @totalUptime AS [Total hours at least one plant was running]