This content has been marked as final. Show 2 replies
It looks like you're really interested in the two latest values of tmstmp, not the two latest records (or "rows" as they are called in relational databases).
That's called a Top-N Query, and here's one way to do it:
If you'd care to post CREATE TABLE and INSERT statemnts for the sample data, then I could test it.
WITH got_r_num AS ( SELECT id, change_type, changed_attr, tmstmp -- TIMESTAMP is not a good column name , DENSE_RANK () OVER (ORDER BY tmstmp DESC NULLS LAST) AS r_num FROM table_x ) SELECT id, change_type, changed_attr, tmstmp FROM got_r_num WHERE r_num <= 2 ;
After you look up the DENSE_RANK fucntion, look up its close relatives RANK and ROW_NUMBER; they're all useful for similar problems.