我有一个带有skey列(由序列号生成)和timestamp列的表。
我需要编写一个查询,它将查找任何skey更高但时间戳比前面记录早的记录,因为它将指示一个问题。
意思是:

select * from table
where timestamp < (select max(timestamp) from table where skey less than currrecord)
order by skey desc;

最佳答案

使用self join即使用您提到的条件(skey和timestamp)将表连接到自身

Select T1.Skey HigherSkey, T2.SKey LowerSkey, T1.Timestamp LowerTimestamp, T2.Timestamp HigherTimestamp
From MyTable T1
Inner Join MyTable T2
    On T1.Timestamp < T2.Timestamp
    And T1.Skey > T2.Skey

关于sql - sql-测试记录按顺序插入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7142029/

10-13 04:36