简体   繁体   中英

SQL Query Oracle: Two date columns on same table for different rows

For CallLogTable....I need to write a query which finds scenarios where the created_ts timestamp of one row has an earlier timestamp than the updated_ts timestamp of the row immediately BEFORE. Rows 1 and 2 are an example (The created_ts of row 2 is earlier than the updated_ts of row 1). I have seen use of Rownums but not sure how to apply it here. Many thanks in advance for your help!

通话记录表

You can use window functions. lag() lets you access the previous row, and you can then compare the relevant columns:

select *
from (
    select  
        t.*,
        lag(updated_ts) over(order by id) lag_updated_ts
    from mytable t
) t
where created_ts < lag_updated_ts

This assumes that column id can be used to order the records. If you want a different column for ordering, then you can change the order by clause of the window function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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