Friday, February 24, 2012

Can someone help out with this query?

Hello,

Im trying to accomplish the following:

SELECT ParentMessage, PostDate
FROM ForumMessages
WHERE (ParentMessage > 0)
UNION
SELECT parentmessage, postdate
FROM forummessages
WHERE parentmessage = 0
ORDER BY PostDate DESC

And that is ALMOST working perfectly. My only problem is with the first group of records from the first SELECT, I want DISTINCT records ONLY on ParentMessage. I dont want the Distinct to also take PostDate into consideration (but I still need the field PostDate).

Basically the above query returns records looking like the following:
26787/1/2004 2:00:46 AM
06/30/2004 12:50:40 PM
06/30/2004 10:10:00 AM
35766/29/2004 10:25:44 PM
35766/29/2004 10:11:58 PM
06/29/2004 10:04:35 AM
35386/25/2004 3:43:54 AM
33686/24/2004 9:19:32 PM
33686/24/2004 8:09:21 PM
33686/24/2004 4:40:40 PM

You can see, half of it is working correctly. I got my list of records, but theres some in there that repeat (i.e. 3576, 3368, etc.) I want the 0s to repeat, as they are Parents, but any time a child shows up I only want 1 reference to it, not duplicates.

IF anyone can help with this, I would greatly appreciate it!!

Thanks in advance for any help!If you want only one per ParentMessage, what do you want to show for the PostDate? To show only the most recent date/time:

SELECT ParentMessage, MAX(PostDate)
FROM ForumMessages
WHERE (ParentMessage > 0)
GROUP BY ParentMessage

No comments:

Post a Comment