Skip to content
Advertisement

SQL server query with multiple (same ID different date and different image) (filter by last date)

Sample and result how I want

I want to fetch data from a huge db that same ID number each yea , but different photo for each date

I want query to filter and show result with Last date including (ID and photo) to the given ID

Advertisement

Answer

Possibly the most efficient method is:

select n.*
from nid n
where n.date = (select max(n2.date) from nid n2 where n2.id = n.id);

For performance, you want an index on nid(id, date).

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