Top 5 rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scadhasini
    New Member
    • Dec 2006
    • 21

    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?
  • vinaykeshav
    New Member
    • Dec 2006
    • 25

    #2
    if u have an date col under this table then..
    select top 5 * from test order by desc of date col

    Comment

    • Scadhasini
      New Member
      • Dec 2006
      • 21

      #3
      Originally posted by vinaykeshav
      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

      Comment

      • almaz
        Recognized Expert New Member
        • Dec 2006
        • 168

        #4
        Originally posted by Scadhasini
        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

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Originally posted by Scadhasini
          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?

          Comment

          • Scadhasini
            New Member
            • Dec 2006
            • 21

            #6
            Originally posted by willakawill
            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.CreatedByU ser = 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 .

            Comment

            • cyberdwarf
              Recognized Expert New Member
              • Nov 2006
              • 218

              #7
              Try adding:-

              Code:
              Select top 5.....
              at the top of your query and....
              Code:
              order by ItemId Desc
              at the bottom.

              HTH

              Steve

              Comment

              • Scadhasini
                New Member
                • Dec 2006
                • 21

                #8
                Originally posted by cyberdwarf
                Try adding:-

                Code:
                Select top 5.....
                at the top of your query and....
                Code:
                order by ItemId Desc
                at the bottom.

                HTH

                Steve

                thank u i solved .....

                Comment

                Working...