alphabetical vs. chronological order

  • I am having a problem returning records in sql. I am trying to retrieve the first record in a table (chronologically), but when I use a min function it is giving me the first record alphabetically instead. Does anyone know how to change this? Thanks for the help.

  • Please post the query, table definitions, sample data and desired output. Read this to see the best way to post this to get quick responses.

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    Which column in the table has the date inserted? For getting the earliest row back you're going to need that. An identity column would also suffice.

    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
  • chburns (6/15/2009)


    I am having a problem returning records in sql. I am trying to retrieve the first record in a table (chronologically), but when I use a min function it is giving me the first record alphabetically instead. Does anyone know how to change this? Thanks for the help.

    there is no guarantee SQL will order the data in any way without an Explicit ORDER BY clause.

    the order of data returned will not follow any rules unless you explicitly use an ORDER BY [somecolumn] statement.

    without it, SQL returns the data in the last way it touched it due to the executino plan...so a simple SELECT * F$ROM comes out one way, but if you apply anything like a function,min, etc, it comes out the way it was convenient for the execution plan to order it to get the data efficiently.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • You do need some type of date. An identity could work, but since these are not guaranteed to be unique, and they can get re-seeded, I'd be careful about using those.

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

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