如果序列中的另一行不在SQL Server之后,如何返回该行

如果序列中的另一行不在SQL Server之后,如何返回该行,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,我有下面的场景,我能想到的唯一方法是将所有数据拉回到临时表中,然后使用游标在其中循环,根据当前记录更新一条记录或最后一条记录 基本上,如果发生了一个称为澄清的操作,我希望返回此记录,但如果澄清后发生了澄清搁置,则我不希望返回澄清 现在的问题是,任何数量的澄清都可能在没有澄清暂停的情况下连续发生,因此,如果5次澄清发生,然后一次暂停,我想返回4条记录。在暂停后也可以进行澄清,在这种情况下,我想展示澄清 有人对比光标更好的解决方案有什么建议吗?我对您的数据结构做了一些假设,但该理论仍然适用于您 --

我有下面的场景,我能想到的唯一方法是将所有数据拉回到临时表中,然后使用游标在其中循环,根据当前记录更新一条记录或最后一条记录

基本上,如果发生了一个称为澄清的操作,我希望返回此记录,但如果澄清后发生了澄清搁置,则我不希望返回澄清

现在的问题是,任何数量的澄清都可能在没有澄清暂停的情况下连续发生,因此,如果5次澄清发生,然后一次暂停,我想返回4条记录。在暂停后也可以进行澄清,在这种情况下,我想展示澄清


有人对比光标更好的解决方案有什么建议吗?

我对您的数据结构做了一些假设,但该理论仍然适用于您

-- Example table
DECLARE @t table (
   action_description   char(20)
 , when_occured         datetime
);

-- Set up data
INSERT INTO @t (action_description, when_occured)
  VALUES ('clarification'     , DateAdd(mi, -9, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, -8, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, -7, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, -6, Current_Timestamp))
       , ('clarification hold', DateAdd(mi, -5, Current_Timestamp))
       , ('clarification'     , DateAdd(mi, -4, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, -3, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, -2, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, -1, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi,  0, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, +1, Current_Timestamp))
       , ('clarification hold', DateAdd(mi, +2, Current_Timestamp))
       , ('clarification'     , DateAdd(mi, +3, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, +4, Current_Timestamp))
       , ('clarification hold', DateAdd(mi, +5, Current_Timestamp))
       , ('clarification hold', DateAdd(mi, +6, Current_Timestamp))
       , ('clarification'     , DateAdd(mi, +7, Current_Timestamp)) -- Include
       , ('clarification'     , DateAdd(mi, +8, Current_Timestamp))
       , ('clarification hold', DateAdd(mi, +9, Current_Timestamp));

; WITH all_actions AS (
  SELECT action_description
       , when_occured
       , Row_Number() OVER (ORDER BY when_occured) As row_num -- Provide a sequence for joining
  FROM   @t
)
SELECT a1.action_description
     , a1.when_occured
FROM   all_actions As a1
 LEFT
  JOIN all_actions As a2                            -- Join back to self
    ON a2.row_num = a1.row_num + 1                  -- on previous row
   AND a2.action_description = 'clarification hold' -- where the previous row had this action
WHERE  a1.action_description = 'clarification' -- Only want to show clarification actions
AND    a2.action_description IS NULL           --  and exclude those who had a row in the join

您能提供一些关于您的数据结构的信息吗?它是一个sql server 2005数据库,所有信息都集中在一个表中。这些字段是它使用的源代码\u workbj\u id int action\u log\u id int action\u typ\u cd char(8)从\u queue\u cd char(8)到\u queue\u cd char(8)lst\u upd\u dtm datetimeSomething应该可以通过临时表、超前/滞后函数在分区上实现的。但我不确定这些是否在SQL Server 2005上工作,并且目前无法正确测试。@user1103990:
LEAD
/
LAG
在2012年和更新的版本中都可用,仅在Tanks gvee上,我以前从未接触过所有的操作。其工作原理与Dream类似在此上下文中带有关键字的
表示公共表表达式(简称CTE)的开始。你可以读到他们。