我什至不知道从哪里开始解决这个问题,我需要从我们的 MS SQL 2012 数据库查询生产数据,该数据库具有基于记录的班次的日期时间戳,棘手的部分是我们运行 4 个 12 小时班次采用 2 开、2 关、3 开、2 关的模式,即 2013 年 1 月的前 3 周:
S M T W T F S
1 2 3 4 5
C/D A/B A/B C/D C/D
6 7 8 9 10 11 12
C/D A/B A/B C/D C/D A/B A/B
13 14 15 16 17 18 19
A/B C/D C/D A/B A/B C/D C/D
A&C 是早班,运行时间 7:00-19:00,B&D 是夜间运行,运行时间 19:00-7:00 我对 SQL 相当陌生,还没有做过类似的事情,我知道我可以轻松确定2013 年 1 月 1 日上午班次的数据为:
select *
from Line05
where L05Time BETWEEN '01/01/2013 07:00' AND '01/01/2013 19:00'
例如,但我不确定如何将其与 C Shift 联系起来。
下面列出了一个轮班表。目前尚不清楚您想要做什么,但您应该能够使用此处显示的一些计算来进行逆向工程,确定事件日期/时间的变化。
EDIT:已更正case
处理 2/2/3/2 模式。
; with Samples as (
-- Start at the beginning of 2013.
select Cast( '01-01-2013 00:00' as DateTime ) as Sample
union all
-- Add hours up to the desired end date.
select DateAdd( hour, 1, Sample )
from Samples
where Sample <= '2013-01-30'
),
ExtendedSamples as (
-- Calculate the number of days since the beginning of the first shift on 1/1/2013.
select Sample, DateDiff( hour, '01-01-2013 07:00', Sample ) / 24 as Days
from Samples ),
Shifts as (
-- Calculate the shifts for each day.
select *,
case when ( Days + 1 ) % 9 in ( 0, 1, 4, 5 ) then 'C/D' else 'A/B' end as Shifts
from ExtendedSamples )
select *,
case when DatePart( hour, Sample ) between 7 and 18 then Substring( Shifts, 1, 1 ) else Substring( Shifts, 3, 1 ) end as Shift
from Shifts
option ( maxrecursion 0 )
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)