Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Joining a table to the latest matching row of another table

Posted on 2004-09-30
Medium Priority
Last Modified: 2012-06-21
I have two tables, SupportCall and CallHistory. The CallHistory table records the conversations that happen for a support call. I want a query that returns columns for a specified SupportCall, plus columns from the latest CallHistory row. CallHistory has an IDENTITY column HistoryID. How do I join the tables in a single query?

A column SupportCallID joins the tables. CallHistory has columns HistoryID, SupportCallID, ConversationDate, and ConversationDetails.
Question by:crescendo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 15

Expert Comment

ID: 12188288
SELECT sc.*, ch.*
FROM SupportCall sc INNER JOIN CallHistory ch ON sc.SupportCallID = ch.SupportCallID
ORDER BY SupportCallID, HistoryID
LVL 26

Accepted Solution

Hilaire earned 1000 total points
ID: 12188302
SELECT TOP 1 a.*, b.*
FROM SupportCall a INNER JOIN CallHistory b ON a.SupportCallID = b.SupportCallID
WHERE a.SupportCallID = @aSpecifiedSupportCall
ORDER BY HistoryID desc
LVL 15

Expert Comment

ID: 12188303
>for a specified SupportCall

SELECT sc.*, ch.*
FROM SupportCall sc INNER JOIN CallHistory ch ON sc.SupportCallID = ch.SupportCallID
WHERE SupportCallID = 123
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

LVL 26

Expert Comment

ID: 12188323
If you need it for all support callls

SELECT a.*, c.*
FROM SupportCall a
   select SupportCallID, max(HistoryId) as HistoryID from CallHistory group by SupportCallID
) b ON a.SupportCallID = b.SupportCallID
INNER JOIN CallHistory c on b.HistoryID = c.HistoryID
LVL 15

Expert Comment

ID: 12188331
Ah, you got it right the first time, Hilaire, crescendo asked for the latest CallHistory row.

Author Comment

ID: 12188385
Thanks to everyone for the amazingly quick response. As usual, Hilaire has an elegant solution.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

715 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