This is probably simple, but i'm looking for the raw SQL to perform an INNER JOIN
but only return one of the matches on the second table based on criteria.
Given two tables:
**TableOne**
ID Name
1 abc
2 def
**TableTwo**
ID Date
1 12/1/2014
1 12/2/2014
2 12/3/2014
2 12/4/2014
2 12/5/2014
I want to join but only return the latest date from the second table:
Expected Result:
1 abc 12/2/2014
2 def 12/5/2014
I can easily accomplish this in LINQ like so:
TableOne.Select(x=> new { x.ID, x.Name, Date = x.TableTwo.Max(y=>y.Date) });
So in other words, what does the above LINQ statement translate into in raw SQL?
You could join the first table with an aggregate query:
SELECT t1.id, d
FROM TableOne t1
JOIN (SELECT id, MAX[date] AS d
FROM TableTwo
GROUP BY id) t2 ON t1.id = t2.id
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments