Let's say that I have the following result table from my SQL select:
DocumentId CreationDate InstanceId ABC 10th Jan 0c60f4e2-02fc-4244-9ec5-4d259ea5774d ABC 11th Jan 2168ab5d-d6ca-4db3-90f0-b621d72108b8 BCA 4th Jan cb7cdf24-b50f-4bd9-b2b5-d58a14793dd8
Notice that InstanceId
is different for every now returned; it's essentially the primary key for the table.
How would I modify my select so that I returned only one row per DocumentId
, picking the 'newest' (determined by CreationDate
), making sure that the InstanceId
on the row is the correct one.
So the example results above would instead return:
DocumentId CreationDate InstanceId ABC 11th Jan 2168ab5d-d6ca-4db3-90f0-b621d72108b8 BCA 4th Jan cb7cdf24-b50f-4bd9-b2b5-d58a14793dd8
(btw, sorry for the horrid question title, feel free to change it to something more suitable)