I’m having trouble getting the latest data out of this.
I have a table with these data:
ItemId, ShipmentId, Date
Items can be shipped many times and a shipment can contain multiple items.
I need to get the latest shipment for every item.
Table looks like this:
11 12 2011-05-13
11 2 2011-07-01
12 2 2000-03-02
...
The result should be
11 2 2011-07-01
12 2 2000-03-02
I can’t find a solution to be exclusive.
How can I get the latest shipment for every item?
Assuming you’re working with a database engine that supports ranking functions, use a CTE or subquery to order the results: