Solved

SQL Join on Most Recent Record

Posted on 2007-11-20
3
888 Views
Last Modified: 2010-05-18
I have two tables in SQL, table one has information, and table two has more dynamic information that is related to table one.  

Table1
ID
LRSN
INFO
MOREINFO

Table 2
ID
LRSN
DESCRIPTION
LAST_UPDATE

I would like to join the two tables on LRSN, and pull the Description of the most recent Last_Update record from table two.  Can someone help me out with this relationship?  Thanks in advance for the help!
0
Comment
Question by:jayh99
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 20319619
select ...
from table1 t1
join table2 t2
  on t1.lrsn = t2.lrsn
 and t2.last_update = ( select max(i.last_update) from table2 i where i.lrsn = t2.lrsn )
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20319625
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.LRSN = t2.Lrsn
WHERE T2.Last_Update = (SELECT MAx(last_update) from tabel2 where lrsn = t2.lrsn)
0
 
LVL 1

Author Closing Comment

by:jayh99
ID: 31410115
Excellent!  Thanks for the quick response!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question