SELECT
[Machine_Events_ID],
[Event_TimeStamp],
[Event_EndTimeStamp],
[Bucket_ID],
[PartNumber]
FROM
[A5].[dbo].[Machine_Events]
WHERE
DATEPART(hour, Event_TimeStamp) > shift1DateTime
AND DATEPART(hour, Event_EndTimeStamp) < shift2DateTime
There is a lot to explain here, since the code is far from what I need.
I need to pass in a shift set like (1,2,3) or (1,3) like this where depending on which shifts or in that set I would only return rows from those shifts going off of the two timestamps. In addition to that, if an event transitions to the previous or next shift this query would return that time as the actual end of the shift like this idea:
CASE
WHEN a.Event_TimeStamp <= '2023-09-15 10:00:00.000'
THEN '2023-09-15 10:00:00.000'
ELSE a.Event_TimeStamp
END
CASE
WHEN a.Event_EndTimeStamp >= '2023-09-20 10:00:00.000'
THEN '2023-09-20 10:00:00.000'
ELSE a.Event_EndTimeStamp
END
I am going to pass in the start and end times for the overall time range I am looking to limit on in addition to passing in the shift ranges,
In my example I am passing in '2023-09-15 10:00:00.000' as the start time and '2023-09-20 10:00:00.000' as the end time.
1条答案
按热度按时间k5hmc34c1#
When asking questions like this it's really helpful to provide example DDL/DML to demonstrate what you're working with. In this case it's pretty trivial to just have the engine spit out some randomize data within a range:
This gives us a set of three shifts, each 8 hours long over today and some data with events occurring somewhat randomly throughout the day.
With that done, it's fairly straight forward to collect the data by shift and manipulate the results when there's a shift foundry cross: