SQL Select inference with Inner Join

Here is my SQL query that does not return DISTINCT Thread titles.

SELECT DISTINCT TOP 5 tblThread.Title, tblPost.Date 
FROM tblPost 
INNER JOIN tblThread ON tblPost.ThreadID = tblThread.ThreadID 
ORDER BY tblPost.Date DESC

      

The common field between tblThread and tblPost is ThreadID.

What I want to do is return the last 5 individual topic titles based on the last 5 posts in tblPost.

Example. If a stream named ASP.NET has been posted twice and they are the last two messages, the (ASP.NET) header should appear only once and at the top of the list.

Any help would be greatly appreciated.

Stephen.

0


source to share


1 answer


Try the following:



SELECT DISTINCT TOP 5 tblThread.Title, MAX(tblPost.Date)
FROM tblPost INNER JOIN tblThread ON tblPost.ThreadID = tblThread.ThreadID 
GROUP BY tblThread.Title
ORDER BY MAX(tblPost.Date) DESC

      

+4


source







All Articles