469,097 Members | 1,458 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

Top 5 rows

Hi,
How to get top 5 newly entered rows in a table. i am trying with "select top 5 * from Test" but its giving old entries.if i want new entries how?
Dec 15 '06 #1
7 10744
if u have an date col under this table then..
select top 5 * from test order by desc of date col
Dec 15 '06 #2
if u have an date col under this table then..
select top 5 * from test order by desc of date col
Hi Vinay,
i have date col, but i am joining with another table. i am using where clause for date its not giving top 5 rows
Dec 15 '06 #3
almaz
168 Expert 100+
Hi Vinay,
i have date col, but i am joining with another table. i am using where clause for date its not giving top 5 rows
Once again: There is no term "order of records in database".
If you want to get records with some order, you must provide an ORDER BY clause. Without ORDER BY clause select returns unordered list of records, that occasionally may be returned in the order you want. If you don't have a field that you can use to determine latest records - you wouldn't be able to get the last records in consistent manner. If you do have such a field - use ORDER BY [this field] DESC/ASC and select top records.
If you want to get more information about your particular case - provide us with DDL (data definition language: scripts for table creations) scripts and queries you're trying to run
Dec 15 '06 #4
willakawill
1,646 1GB
Hi Vinay,
i have date col, but i am joining with another table. i am using where clause for date its not giving top 5 rows
Hi
is there an auto number id field for this table?
Dec 15 '06 #5
Hi
is there an auto number id field for this table?

yes it is there "itemid". this is my query

Select ModuleId,
ItemId,
Content,
CreatedByUser,
CreatedDate
from Test with (nolock)
Left Outer Join Users On Test.CreatedByUser = Users.UserId
Where CreatedDate in (Select Top 5 CreatedDate from Test Order By CreatedDate Desc) And ModuleId = @ModuleId

with this @ModuleId i have so many rows in the table.but its returning always 3rows only .
Dec 15 '06 #6
cyberdwarf
218 Expert 100+
Try adding:-

Expand|Select|Wrap|Line Numbers
  1. Select top 5.....
at the top of your query and....
Expand|Select|Wrap|Line Numbers
  1. order by ItemId Desc
at the bottom.

HTH

Steve
Dec 15 '06 #7
Try adding:-

Expand|Select|Wrap|Line Numbers
  1. Select top 5.....
at the top of your query and....
Expand|Select|Wrap|Line Numbers
  1. order by ItemId Desc
at the bottom.

HTH

Steve

thank u i solved .....
Dec 15 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by GaryB | last post: by
reply views Thread by Subba Rao via DotNetMonster.com | last post: by
12 posts views Thread by Graham Blandford | last post: by
68 posts views Thread by Martin Joergensen | last post: by
11 posts views Thread by jimstruckster | last post: by
reply views Thread by rn5a | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.