Skip to content
Advertisement

Return Most Recent Record group by 2 ID fields

I need to display the current status of a part (‘approved’ or ‘not approved’) I am setting up the status table as MaterialNo, ToolID, Status, Asofdate

the idea is I may have several tools approved and several not approved. I want to be able to see the most recent status for each tool but I don’t want to see the historical status.

have tried to use MAX(Asofdate) and group by MaterialNo, ToolID, and Status but it returns all of the status history for each.

The Status Table has this data:

I Want to see:

Advertisement

Answer

One solution that usually has very good performance is a correlated subquery:

For optimal performance, you want an index on (MaterialNo, ToolID, asof).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement