Skip to content
Advertisement

Select one of each matching results from group last record from date

I have multiple customers that are a part of a group designated by a group id.

I would like to retrieve 1 record from a related table for each of the matching group members (last record before a certain date).

Currently I query for a list of group members then for each member i run another query to retrieve last record from a date.

I would like to do this with one query since i can pull up the associated table records using group id – however this returns all the records associated to group (bad).

If i use first or default i only get results for first group found. I want 1 record from each group member.

My Code (returns all associated records of group members):

But i just want one from each instead of all.

Code I use now:

Advertisement

Answer

I think this could work

It is translated into one sql query. That means it uses one roundtrip to the server. That could be quite a big difference in performace when compared to the foreach loop. If you look at the generated sql, it would be something like

In terms of performace of the query itself, I would not expect problems here, sql server should not have problems optimizing this form (compared to other ways you could write this query).

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