如果我有这样的表:
位移
Id name desc
1 W_W Work-Work
2 W-E Work-WeekEnd
3 E-W WeekEnd-Work
4 E-E WeekEnd-WeekEnd
注意: day_type =3
表示week_end
,任何其他值表示work_day
我有一个数据结果包含两个记录,一个用于当天,另一个用于明天,如下所示:
如果输入:5-20-2015
结果集1:
day-type dayDate
1 5-20-2015
1 5-21-2015
如果输入:5-21-2015
结果集2:
day-type dayDate
1 5-21-2015
3 5-22-2015
等等(四种可能性) 我想得到这样的结果:
第一种情况:
day-type dayDate shift_id
1 5-20-2015 1
1 5-21-2015 -
第二种情况
day-type dayDate shift_id
1 5-21-2015 2
3 5-22-2015 -
答案 0 :(得分:1)
你的第二个测试用例是错误的,它也应该产生一个shift_id为1,因为5-21-2015和5-22-205都是工作日。至少在我的宇宙中。顺便说一句,你的SHIFTS表对于这个答案是不必要的。更改@InputDate
的值以测试不同的方案。
DECLARE @InputDate DATETIME
DECLARE @NextDate DATETIME
DECLARE @day_type1 INT
DECLARE @day_type2 INT
SET @InputDate = '20150522'
SET @NextDate = DATEADD(day, 1, @InputDate)
SELECT @day_type1 = CASE WHEN SUBSTRING(DATENAME(weekday, @InputDate), 1, 1) = 'S' THEN 3 ELSE 1 END
SELECT @day_type2 = CASE WHEN SUBSTRING(DATENAME(weekday, @NextDate), 1, 1) = 'S' THEN 3 ELSE 1 END
SELECT
@day_type1 AS [day-type],
@InputDate AS [dayDate],
CASE
WHEN @day_type1 <> 3 AND @day_type2 <> 3 THEN '1'
WHEN @day_type1 <> 3 AND @day_type2 = 3 THEN '2'
WHEN @day_type1 = 3 AND @day_type2 <> 3 THEN '3'
WHEN @day_type1 = 3 AND @day_type2 = 3 THEN '4'
END AS [shift_id]
UNION ALL
SELECT
@day_type2 AS [day-type],
@NextDate AS [dayDate],
'-'