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

Sql 如何创建由空行分隔的唯一行组,sql,sql-server,Sql,Sql Server,创建一些调度分析时,我需要能够将唯一ID应用于调度块,以便计算该块的时间范围。使用SQLServerManagementStudio 最终结果目前看起来像这样: A. B C 0700 考试 1. 0715 考试 2. 0730 没有考试 0745 考试 1. 0800 考试 2. 0815 考试 3. 如果我理解正确: 第一列是时间 您希望在某个设定的时间段内将间隔扩展到15分钟 d列列举了“连续”的考试周期 然后,此查询执行您想要的操作: select t, b, c, (

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

最终结果目前看起来像这样:

A. B C 0700 考试 1. 0715 考试 2. 0730 没有考试 0745 考试 1. 0800 考试 2. 0815 考试 3.
如果我理解正确:

  • 第一列是时间
  • 您希望在某个设定的时间段内将间隔扩展到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

是一个dbfiddle/

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

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

我不知道数据来自哪里。0915和0930的行来自哪里?
a
的数据类型是什么?
d
是如何计算的?为什么你决定前三个是一组,第二个是三组?似乎我们需要更多的解释。您能提供一些有意义的列名吗?源数据和您尝试过的代码?很好,非常感谢@姆托宾。如果这回答了你的问题,你可以接受答案。