计算 SQL 中的连续班次和天数
Counting consecutive shift and days in SQL
我正在尝试显示团队工作的连续班次和天数。我尝试了很多解决方案,但总不能完全正确。
数据是日期、班次、船员。有一个日班和夜班和 3 个工作人员。
ConsecutiveShift 和 ConsecutiveDay 是我要生成的字段。输出将用于根据连续 shifts/days 工作的次数查看生产力。
非常感谢任何帮助
Dateop Shift Crew
2015-12-23 00:00:00 D B Crew
2015-12-23 00:00:00 N A Crew
2015-12-24 00:00:00 D B Crew
2015-12-24 00:00:00 N A Crew
2015-12-25 00:00:00 D C Crew
2015-12-25 00:00:00 N B Crew
2015-12-26 00:00:00 D C Crew
2015-12-26 00:00:00 N B Crew
2015-12-27 00:00:00 D C Crew
2015-12-27 00:00:00 N B Crew
2015-12-28 00:00:00 D C Crew
2015-12-28 00:00:00 N B Crew
2015-12-29 00:00:00 D C Crew
2015-12-29 00:00:00 N B Crew
2015-12-30 00:00:00 D A Crew
2015-12-30 00:00:00 N C Crew
2015-12-31 00:00:00 D A Crew
2015-12-31 00:00:00 N C Crew
2016-01-01 00:00:00 D A Crew
2016-01-01 00:00:00 N C Crew
2016-01-02 00:00:00 D A Crew
2016-01-02 00:00:00 N C Crew
期望输出 - 前 3 列是原始数据,第 4 列和第 5 列是我要生成的计数
以下查询应该有效,使用 window 函数:
With cte As (
Select DateOp, Shift, Crew,
DateDiff(Day, DateOp, '2016-01-01') + Row_Number() Over (Partition By Crew, Shift Order By DateOp) As ShiftIsland,
DateDiff(Day, DateOp, '2016-01-01') + Row_Number() Over (Partition By Crew Order By DateOp) As DayIsland
From [YourTable])
Select DateOp, Shift, Crew,
Row_Number() Over (Partition By Crew, Shift, ShiftIsland Order By DateOp) As ConsecutiveShifts,
Row_Number() Over (Partition By Crew, DayIsland Order By DateOp) As ConsecutiveDays
From cte
Order By Crew, DateOp, Shift;
我认为这是一个基本的row_number()
。对于您提供的数据:
select t.*,
row_number() over (partition by shift, crew order by date) as consecutiveshift,
row_number() over (partition by crew order by date) as consecutiveday
from t;
这至少 returns 您问题中的数据。
我正在尝试显示团队工作的连续班次和天数。我尝试了很多解决方案,但总不能完全正确。
数据是日期、班次、船员。有一个日班和夜班和 3 个工作人员。 ConsecutiveShift 和 ConsecutiveDay 是我要生成的字段。输出将用于根据连续 shifts/days 工作的次数查看生产力。
非常感谢任何帮助
Dateop Shift Crew
2015-12-23 00:00:00 D B Crew
2015-12-23 00:00:00 N A Crew
2015-12-24 00:00:00 D B Crew
2015-12-24 00:00:00 N A Crew
2015-12-25 00:00:00 D C Crew
2015-12-25 00:00:00 N B Crew
2015-12-26 00:00:00 D C Crew
2015-12-26 00:00:00 N B Crew
2015-12-27 00:00:00 D C Crew
2015-12-27 00:00:00 N B Crew
2015-12-28 00:00:00 D C Crew
2015-12-28 00:00:00 N B Crew
2015-12-29 00:00:00 D C Crew
2015-12-29 00:00:00 N B Crew
2015-12-30 00:00:00 D A Crew
2015-12-30 00:00:00 N C Crew
2015-12-31 00:00:00 D A Crew
2015-12-31 00:00:00 N C Crew
2016-01-01 00:00:00 D A Crew
2016-01-01 00:00:00 N C Crew
2016-01-02 00:00:00 D A Crew
2016-01-02 00:00:00 N C Crew
期望输出 - 前 3 列是原始数据,第 4 列和第 5 列是我要生成的计数
以下查询应该有效,使用 window 函数:
With cte As (
Select DateOp, Shift, Crew,
DateDiff(Day, DateOp, '2016-01-01') + Row_Number() Over (Partition By Crew, Shift Order By DateOp) As ShiftIsland,
DateDiff(Day, DateOp, '2016-01-01') + Row_Number() Over (Partition By Crew Order By DateOp) As DayIsland
From [YourTable])
Select DateOp, Shift, Crew,
Row_Number() Over (Partition By Crew, Shift, ShiftIsland Order By DateOp) As ConsecutiveShifts,
Row_Number() Over (Partition By Crew, DayIsland Order By DateOp) As ConsecutiveDays
From cte
Order By Crew, DateOp, Shift;
我认为这是一个基本的row_number()
。对于您提供的数据:
select t.*,
row_number() over (partition by shift, crew order by date) as consecutiveshift,
row_number() over (partition by crew order by date) as consecutiveday
from t;
这至少 returns 您问题中的数据。