0

Here is my SQL Statement which is not returning 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 this to do is return The latest 5 Distinct Thread Titles based on the latest 5 posts in tblPost.

Example: If a thread called ASP.NET has been posted to two times and they are the two most recent posts, the Title of the thread (ASP.NET) should only appear once and at the top of the list.

Any help would be greatly appreciated.

Stefan.

1
  • It helps to read the SQL when it's formatted across multiple lines.
    – codelogic
    Dec 18, 2008 at 10:38

1 Answer 1

4

Try this :

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
1
  • Actually you don´t need the DISTINCT keyword when you use GROUP BY.
    – bjorsig
    Dec 18, 2008 at 11:01

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.