September 5, 2018 at 9:15 am
Hi,
I am trying to select the 10 most recent entries from my table. I am not sure how to adjust a query so that instead of looking for the top # or top % it returns the top 10 most recent.
Can you please help?
Thanks,
September 5, 2018 at 9:21 am
Do you have an incremental ID field or a datetime field with a datetime stamp in it? If so you could use an ORDER BY clause sorted descending on those fields and then use the TOP n to get your results.
September 5, 2018 at 10:00 am
I tried using this
select TOP 10 from DateExpensed where * from dbo.Table_1 order by DateExpensed ASC
however it has not worked
September 5, 2018 at 10:12 am
cjeans - Wednesday, September 5, 2018 10:00 AMI tried using thisselect TOP 10 from DateExpensed where * from dbo.Table_1 order by DateExpensed ASC
however it has not worked
You need to add some columns to the select!SELECT TOP 10 * FROM ...
September 5, 2018 at 10:20 am
select top 10 * from dbo.Table_1 and order by DateExpensed ASC
I tried this but again is not working
September 5, 2018 at 10:22 am
cjeans - Wednesday, September 5, 2018 10:20 AMselect top 10 * from dbo.Table_1 and order by DateExpensed ASCI tried this but again is not working
you can't have an "and" before the order byselect top 10 * from dbo.Table_1 order by DateExpensed ASC
September 5, 2018 at 10:23 am
cjeans - Wednesday, September 5, 2018 10:00 AMI tried using thisselect TOP 10 from DateExpensed where * from dbo.Table_1 order by DateExpensed ASC
however it has not worked
Either change your TOP to BOTTOM or your ASC to DESC. ASC will sort the "earliest" records on top. Since you want the "latest" you either want to reverse your sort or take records from the bottom.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
September 5, 2018 at 10:27 am
thanks all. working now
September 5, 2018 at 11:13 am
drew.allen - Wednesday, September 5, 2018 10:23 AMcjeans - Wednesday, September 5, 2018 10:00 AMI tried using thisselect TOP 10 from DateExpensed where * from dbo.Table_1 order by DateExpensed ASC
however it has not worked
Either change your TOP to BOTTOM or your ASC to DESC. ASC will sort the "earliest" records on top. Since you want the "latest" you either want to reverse your sort or take records from the bottom.
Drew
Is BOTTOM valid in T-SQL?
September 5, 2018 at 11:47 am
Jonathan AC Roberts - Wednesday, September 5, 2018 11:13 AMdrew.allen - Wednesday, September 5, 2018 10:23 AMcjeans - Wednesday, September 5, 2018 10:00 AMI tried using thisselect TOP 10 from DateExpensed where * from dbo.Table_1 order by DateExpensed ASC
however it has not worked
Either change your TOP to BOTTOM or your ASC to DESC. ASC will sort the "earliest" records on top. Since you want the "latest" you either want to reverse your sort or take records from the bottom.
Drew
Is BOTTOM valid in T-SQL?
I think it was at one point. It's not valid in SQL 2016, so I could be wrong.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply