如何创建由NULL行分隔的唯一行组



创建一些调度分析时,我需要能够将唯一的ID应用于调度块,以便计算该块的时间范围。使用SQL Server Management Studio。

目前的最终结果是这样的:

1[/tr><1><2>3
a b c
0700 考试
0715 考试 2
0730 没有检查
0745 检查
0800 考试
0815 考试

如果我理解正确:

  • 第一列是时间
  • 您希望在某个设定时间段内将间隔扩展到15分钟
  • d列枚举";连续的";考试期间

然后,此查询执行您想要的操作:

select t, b, c,
(case when c is not null then dense_rank() over (order by almost_d) end) as d
from (select times.t, coalesce(t.b, 'no exam') as b, c,
sum(case when c is null then 1 else 0 end) over (order by times.t) as almost_d
from times left join
t
on times.t = t.a
) t

这里有一个db<gt;小提琴/

您可以使用lagsum窗口函数,如下所示:

Select a, b, c,
Case when b = 'exam' 
then Sum(case when prev is null or prev = 'no exam'
then 1
end) over (order by a) 
end as d
(Select t.*,
Lag(b) over (order by a) as prev
From your_table t) t

最新更新