I have 2 tables:
First (Cases)
Second (Comments)
in one to many relation ; I’m storing the comments of each case in Comments table.
I need to select case information from Cases table which are presented in Comments table, but I want each case to be displayed ONCE ordered by comments added date (cDate)
I tried:
SELECT TOP 10
Cases.*,
comments.cDate
FROM
Cases
INNER JOIN comments
ON Cases.Case_ID = comments.Case_ID
WHERE comments.Case_ID IN
(
SELECT DISTINCT
Case_ID
FROM
comments
)
ORDER BY cDate DESC
but its retrieving the case multiple times if it has many comments. I need it to appear one time only
Thank you all, you helped alot ,,
I just added
Cases.Case_ID IN (SELECT Case_ID FROM comments)
and it worked perfectly.
Select statement is like this now:
SELECT top 10 Cases.*,
(SELECT MAX(comments.cDate)
FROM comments
WHERE Cases.Case_ID = comments.Case_ID ) AS cDate
FROM Cases
WHERE Cases.Case_ID
IN (SELECT Case_ID FROM comments)
ORDER BY cDate DESC
Thanks once again 🙂
Thank you all, you helped alot ,, I just added
and it worked perfectly.
Select statement is like this now:
Thanks once again 🙂