Skip to content
Advertisement

Query that merges latest reference in multiple tables into shared rows

I have the following schema which is pretty simple and straight forward:

enter image description here

I want to write an optimized query that returns me a list of all phones with their latest message and latest picture taken. In this case with the latest “CreatedAt” for both fields:

Example expected data-set:

Right now I’m not sure how to write such a query so I just grab everything and then programatically filter it out with server side code i.e:

How can I write the following query?

Advertisement

Answer

OUTER APPLY seems useful here:

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