June 15, 2009 at 8:28 am
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.
June 15, 2009 at 8:42 am
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
June 15, 2009 at 9:32 am
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
June 15, 2009 at 10:57 am
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