简体   繁体   English

SQL Server:验证两列的排序顺序相同

[英]SQL Server : verify that two columns are in same sort order

I have a table with an ID and a date column. 我有一个带有ID和日期列的表。 It's possible (likely) that when a new record is created, it gets the next larger ID and the current datetime. 创建新记录时,有可能(可能)获得下一个更大的ID和当前日期时间。 So if I were to sort by date or I were to sort by ID, the resulting data set would be in the same order. 因此,如果我要按日期排序或按ID排序,则结果数据集的顺序将相同。

How do I write a SQL query to verify this? 如何编写SQL查询来验证这一点?

It's also possible that an older record is modified and the date is updated. 还可能会修改较旧的记录并更新日期。 In that case, the records would not be in the same sort order. 在这种情况下,记录将不会具有相同的排序顺序。 I don't think this happens. 我认为这不会发生。

I'm trying to move the data to another location, and if I know that there are no modified records, that makes it a lot simpler. 我正在尝试将数据移动到另一个位置,如果我知道没有修改的记录,那将使其变得更加简单。

I'm pretty sure I only need to query those two columns: ID, RecordDate. 我敢肯定,我只需要查询这两列:ID,RecordDate。 Other links indicate I should be able to use LAG, but I'm getting an error that it isn't a built-in function name. 其他链接表明我应该可以使用LAG,但出现错误,因为它不是内置函数名。

In other words, both https://dba.stackexchange.com/questions/42985/running-total-to-the-previous-row and Is there a way to access the "previous row" value in a SELECT statement? 换句话说, https://dba.stackexchange.com/questions/42985/running-total-to-the-previous-row是否有办法访问SELECT语句中的“上一行”值? should help, but I'm still not able to make that work for what I want. 应该会有所帮助,但我仍然无法满足我的需求。

Use this: 用这个:

SELECT ID, RecordDate FROM tablename t
WHERE 
  (SELECT COUNT(*) FROM tablename WHERE tablename.ID < t.ID) 
  <> 
  (SELECT COUNT(*) FROM tablename WHERE tablename.RecordDate < t.RecordDate); 

It counts for each row, how many rows have id less than the row's id and 它为每一行计数,有多少行的id小于该行的id
how many rows have RecordDate less than the row's RecordDate . 有多少行的RecordDate小于该行的RecordDate
If these counters are not equal then it outputs this row. 如果这些计数器不相等,则输出此行。
The result is all the rows that would not be in the same position after sorting by ID and RecordDate 结果是按IDRecordDate排序后所有不在同一位置的行

If you cannot use window functions, you can use a correlated subquery and EXISTS . 如果无法使用窗口函数,则可以使用关联的子查询和EXISTS

SELECT *
       FROM elbat t1
       WHERE EXISTS (SELECT *
                            FROM elbat t2
                            WHERE t2.id < t1.id
                                  AND t2.recorddate > t1.recorddate);

It'll select all records where another record with a lower ID and a greater timestamp exists. 它会选择所有记录,其中存在另一个ID较低且时间戳较大的记录。 If the result is empty you know that no such record exists and the data is like you want it to be. 如果结果为空,则说明不存在此类记录,并且数据就像您想要的那样。

Maybe you want to restrict it a bit more by using t2.recorddate >= t1.recorddate instead of t2.recorddate > t1.recorddate . 也许您想通过使用t2.recorddate >= t1.recorddate而不是t2.recorddate > t1.recorddate来进一步限制它。 I'm not sure how you want it. 我不确定你想要什么。

the above solutions are all good but if both dates and ids are in increment then this should also work 上述解决方案都很好,但是如果日期和ID都递增,那么这也应该有效

select  modifiedid=t2.id from 
yourtable t1 join yourtable t2
on t1.id=t2.id+1 and t1.recordDate<t2.recordDate

One method uses window functions: 一种方法使用窗口函数:

select count(*)
from (select t.*,
             row_number() over (order by id) as seqnum_id,
             row_number() over (order by date, id) as seqnum_date
      from t
     ) t
where seqnum_id <> seqnum_date;

When the count is zero, then the two columns have the same ordering. 当计数为零时,则两列具有相同的顺序。 Note that the second order by includes id . 请注意,第二个order by包括id Two rows could have the same date. 两行可能具有相同的日期。 This makes the sort stable, so the comparison is valid even when date has duplicates. 这样可以使排序稳定,因此即使date重复也可以进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM