SQL检查SQL中的2个相等和顺序值

时间:2014-10-15 13:08:29

标签: sql sql-server sql-server-2008

我有一个名为sapinteraction的表,它具有以下结构:

interactionId: String
interactionObject: String
action: String
interactionDate : DateTime

现在我有查询检查指定last 2 records的每个interactionObject的action

SELECT max(subTab.interactionDate) AS Maxdate, ssubTab.ts AS MaxMaxDate, subTab.interactionObject,  subTab.action As PrevLastAction , ssubTab.action AS LastAction
FROM sapinteraction  subTab
JOIN(
    SELECT max(subsubTab.interactionDate) ts, subsubTab.interactionObject, subsubTab.action
    FROM sapinteraction  subsubTab
    GROUP BY subsubTab.interactionObject,subsubTab.action
    ) ssubTab ON ssubTab.interactionObject = subTab.interactionObject AND ssubTab.ts > subTab.interactionDate
WHERE  (subTab.s_resultstatus_in <> 1) AND (subTab.interactionDate > '2013-01-01') AND subTab.action = 'DELETE' AND ssubTab.action = 'DELETE'
GROUP BY subTab.interactionObject,subTab.action , ssubTab.action 

问题: 我希望更改查询,以便retrieve any interactionObject具有操作'Delete' (example) in twice in a row(根据日期时间)。知道如何修改我的查询来实现这个目标吗?

请求一些数据(由interactionObject,interactionDate定义)

interactionId   |  interactionObject   |   action             |    interactionDate 
___________________________________________________________________________________________
1093            | Object1              | ADD                  | 2014 - 07 -01
1088            | Object1              | DELETE               | 2014 - 06 -20
1075            | Object1              | ADD                  | 2014 - 05 -13
1066            | Object1              | DELETE               | 2014 - 04 -07
1035            | Object1              | ADD                  | 2014 - 03 -15

1901            | Object2              | ADD                  | 2014 - 07 -01
1807            | Object2              | DELETE               | 2014 - 06 -05
1707            | Object2              | DELETE               | 2014 - 05 -12
1613            | Object2              | ADD                  | 2014 - 04 -13
1500            | Object2              | ADD                  | 2014 - 03 -21

1000            | Object3              | ADD                  | 2014 - 07 -22
1000            | Object3              | DELETE               | 2014 - 06 -19
1000            | Object3              | ADD                  | 2014 - 05 -16
1000            | Object3              | DELETE               | 2014 - 04 -13
1000            | Object3              | DELETE               | 2014 - 03 -10


In the case standing above: i would like to retrieve Object2 & Object3

2 个答案:

答案 0 :(得分:1)

如果我们只想查找连续两次DELETE次互动的对象,我们可以这样做:

;With Numbered as (
    SELECT *,ROW_NUMBER() OVER (
       PARTITION BY interactionObject
       ORDER BY interactionDate) as rn
    FROM sapinteraction
)
SELECT distinct n1.interactionObject
FROM
     Numbered n1
        inner join
     Numbered n2
        on
            n1.interactionObject = n2.interactionObject and
            n1.rn = n2.rn - 1
WHERE
   n1.action = 'DELETE' and
   n2.action = 'DELETE'

希望直接阅读。

ROW_NUMBERWITH Common Table Expression以供参考。并且我已经使用DISTINCT假设即使某个特定对象连续出现多个出现的两个DELETE s,您只希望它出现在结果中一次。

通过将where子句更改为n1.action = n2.action,也可以轻松更改以查找连续两次发生的任何操作对。

答案 1 :(得分:1)

在SQL Server 2012+中,LAG()函数允许您这样做,但在SQL 2008中,我认为使用外部应用来获取下一行是可行的。如果由像create index idx_date on sapinteraction(interactiondate)这样的interactivedate上的索引支持,它可能会表现得更好。

select * 
from sapinteraction t1
outer apply (
    select top 1 * 
    from sapinteraction t2 
    where T1.interactionObject = t2.interactionObject
    and t1.interactionDate < t2.interactionDate 
    order by interactionDate
    ) oa
where t1.action = 'DELETE' and oa.action = 'DELETE'

使用相关子查询也应该有用。