Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
cpirie
Starting Member
2 Posts |
Posted - 2010-02-09 : 14:29:41
|
I have a feeling it's my lack of knowledge of terms that's stopping google from answering this, so here's a forum post. :)For simplicity, I have 2 tables in a one to many. What I'm attempting to do is get a list of all records in Table A (list of people), and the most recent document in table B (list of documents) if there is one at all.The sql statement below is about as close as I've gotten to the goal so far, but unfortunately with the TOP 1 involved in the outer join query makes it only return 1 result for all records returned from the left side of the query.Is there a way to get the right side of the join to execute for every record on the left side?SELECT p.ResourceID, d.ExpiryDateFROM People pLEFT OUTER JOIN (SELECT TOP 1 ExpiryDate, ResourceID FROM Documents ORDER BY ExpiryDate DESC) dON p.ResourceID = d.ResourceID Results with the query are currently:57, 1/1/201069, NULL80, NULL120, NULL134, NULLWhen the results I'm going for should be like:57, 1/1/201069, 4/26/200980, NULL120, 3/8/2006134, 9/28/1999Any help would be greatly appreciated! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-09 : 14:35:21
|
[code]SELECT p.ResourceID, d.ExpiryDateFROM People pOUTER APPLY (SELECT TOP 1 ExpiryDate FROM Documents WHERE ResourceID = p.ResourceID ORDER BY ExpiryDate DESC) d[/code]------------------------------------------------------------------------------------------------------SQL Server MVP |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-02-09 : 14:45:53
|
[code]SELECT p.ResourceID, d.ExpiryDateFROM People pLEFT OUTER JOIN ( SELECT MAX(ExpiryDate) AS ExpiryDate, ResourceID FROM Documents GROUP BY ResourceID) dON p.ResourceID = d.ResourceID[/code] |
|
|
cpirie
Starting Member
2 Posts |
Posted - 2010-02-09 : 15:22:58
|
Works like a charm! Thanks a bunch vishakh16!quote: Originally posted by visakh16
SELECT p.ResourceID, d.ExpiryDateFROM People pOUTER APPLY (SELECT TOP 1 ExpiryDate FROM Documents WHERE ResourceID = p.ResourceID ORDER BY ExpiryDate DESC) d ------------------------------------------------------------------------------------------------------SQL Server MVP
|
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-02-09 : 15:25:05
|
[code]SELECT p.ResourceID, MAX(ExpiryDate) FROM People p JOIN Documents d ON p.ResourceID = d.ResourceID GROUP BY p.ResourceID UNION ALLSELECT p.ResourceID, NULL FROM People p WHERE NOT EXISTS (SELECT * FROM Documents d WHERE p.ResourceID = d.ResourceID);[/code] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-10 : 01:25:16
|
quote: Originally posted by cpirie Works like a charm! Thanks a bunch vishakh16!quote: Originally posted by visakh16
SELECT p.ResourceID, d.ExpiryDateFROM People pOUTER APPLY (SELECT TOP 1 ExpiryDate FROM Documents WHERE ResourceID = p.ResourceID ORDER BY ExpiryDate DESC) d ------------------------------------------------------------------------------------------------------SQL Server MVP
welcome ------------------------------------------------------------------------------------------------------SQL Server MVP |
|
|
|
|
|
|
|