Search code examples
sqlgreatest-n-per-group

SQL: Choose latest uploaded data


I am seeing duplicates in my data after running my sql query, and have figured out the issue stemming to our data team not updating a table but adding a new row instead. In this instance, I need to use the largest LD_SEQ_NBR to get the latest data.

Given the following table -- ORDERS

ID     ORD_NBR      LD_SEQ_NBR
0     130263789        1665
1     130263789        1870

What do I need to add to my WHERE clause to make sure I'm taking the rows with the largest LD_SEQ_NBR?


Solution

  • LD_SEQ_NBR = (SELECT MAX(LD_SEQ_NBR) FROM ORDERS A WHERE A.ORD_NBR = ORDERS.ORD_NBR)