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
 Where...Can it give you the most recent entries?

Author  Topic 

crugerenator
Posting Yak Master

126 Posts

Posted - 2007-06-12 : 09:47:28
SELECT post.C_ID, post.Comment, post.Title, post.Date, authors.U_Name
FROM authors INNER JOIN
post ON authors.A_ID = post.A_ID
ORDER BY C_ID DESC

That's the query I'm using, and I was hoping there is a way to use the WHERE clause to have only the 5 highest #'s from my primary key, which is A_ID IDENTITY(1,1).

What would be the best way for me to do this? I also have date as a datetime, but then I could be getting more or less than 5 results in my query.

Thanks

readysetstop
Posting Yak Master

123 Posts

Posted - 2007-06-12 : 09:53:10
Using 'TOP' in the select would do it.

____________________________________________________________________________________
"Believe in those who are seeking the truth. Doubt those who say they have found it." -Andre Gide
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-12 : 09:53:50
Is this?
SELECT post.C_ID, post.Comment, post.Title, post.Date, authors.U_Name
FROM authors INNER JOIN
post ON authors.A_ID = post.A_ID
where post.A_ID in (Select top 5 A_ID from post order by A_ID DESC)
ORDER BY C_ID DESC


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

crugerenator
Posting Yak Master

126 Posts

Posted - 2007-06-12 : 10:07:51
Thanks

SELECT TOP (# rows you want returned) rest of query

SELECT TOP(5) post.C_ID, post.Comment, post.Title, post.Date, authors.U_Name
FROM authors INNER JOIN
post ON authors.A_ID = post.A_ID
ORDER BY C_ID DESC

worked perfectly
Go to Top of Page
   

- Advertisement -