Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Inner Join and Ordering Items

Author  Topic 

calico
Starting Member

1 Post

Posted - 2010-11-15 : 14:19:36
I am performing an inner join to get the most recently commented upon ideas.

The statement in the inner join returns the idea ID's that I want, in order in which they were commented upon.

The entire query returns the correct ideas, but in a random order.

How can I get the entire query to return results in the same order that the inner join query returns them?

I tried to "flip" the queries around but then can't get to the idea's title if it's in the inner join.

Thank you!

SELECT
i.idea_id, i.title FROM bi_idea i
INNER JOIN
(SELECT
TOP 5 idea_id, MAX(date_entered) d
FROM ims_comment y
WHERE y.idea_id NOT IN
(SELECT TOP 5 idea_id FROM bi_idea ORDER BY date_entered DESC)
GROUP BY y.idea_id ORDER BY d DESC) c ON i.idea_id = c.idea_id

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-11-15 : 17:18:06
You have to use an ORDER BY clause to order the data.
Go to Top of Page
   

- Advertisement -