Top and Order by

  • Hi

    I clarify me in Order by causal

    DECLARE @TEMP1 TABLE (IDS INT,Date datetime)

    DECLARE @DATE1 DATETIME

    SET @DATE1=GETDATE()

    INSERT INTO @TEMP1 SELECT 4029,@DATE1

    INSERT INTO @TEMP1 SELECT 9841,@DATE1

    INSERT INTO @TEMP1 SELECT 11463,@DATE1

    INSERT INTO @TEMP1 SELECT 15293,@DATE1

    INSERT INTO @TEMP1 SELECT 185,@DATE1

    Select * from @TEMP1

    Select Top 5 * from @TEMP1 ORDER BY Date

    Select Top 5 * from @TEMP1 ORDER BY Date desc

    Select * from @TEMP1 ORDER BY Date

    why if i specify Order by date in top it is returning differently

    even though specify "Desc" it is returning differently

    give me tips on this

    Thanks

    Parthi

    Thanks
    Parthi

  • What do you mean by 'returning differently'? Bear in mind that all 5 rows have exactly the same value for the Date column, so in this case, any order of the returned rows is fine, it's only the order by Date (which is the same) that's guaranteed.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Hi thanks

    I am asking this because in a table i am having the same record and i am returning the value from that table If it is 2nd time called i have to take them depending on the status flag.

    While 2nd time i have to use order by date Asc depending on the status flag, thats why i am asking

    Thanks
    Parthi

  • Sorry, don't understand. Table structure, sample data and some details on what should be returned when and how would be useful.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply