I have the following tables
Groups
- Id
- Name
People
- Id
- GroupId
- EntryDate (DateTime)
Now I want to retrieve all groups sorted by group’s People entrydate that’s the latest.
//pseudo code
select * from groups order by (select top 1 from people order by entrydate desc)
Would it be better to add a LatestEntryDate field to Groups and update that when a person is added? since this will be a common query.
No, I wouldn’t add a
LatestEntryDatefield to theGroupstable. This is easily calculated.As @danihp pointed out, you may want to use a left join here. The above query will require that a value exists in the
Peopletable. What you choose to use depends on your specific requirements.UPDATE
To answer your question about doing this in Linq, I think it would be something like this: