Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 ORDER BY statement starting at a random spot

Author  Topic 

nateny
Starting Member

2 Posts

Posted - 2009-11-11 : 18:40:25
I have been running a simple SQL code to order a dataset by claimID. The code is listed below:

if object_Id ('dbo.outOUTPATIENT_DETAIL_Diffsort') is not null
drop table dbo.outOUTPATIENT_DETAIL_Diffsort

SELECT *
INTO dbo.outOUTPATIENT_DETAIL_Diffsort
FROM dbo.outOUTPATIENT_DETAIL
ORDER BY ClaimID, LineNum

When I go into enterprise manager and return the top 1000 rows, the claimID is in ascending order, but it always starts at a random claim number (such as CLM_00180580). If I take out the INTO statement and run the code and just have the results shown in query analyzer, the file starts correctly at claim 1. Has anyone else ever run into this problem?


Nate

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-11-11 : 19:07:40
It sounds like you are just doing to TOP 1000 without specifying an ORDER BY clause. There is no way guarantee the order without an ORDER BY clause. hence, the randomness you are seeing.
Go to Top of Page

nateny
Starting Member

2 Posts

Posted - 2009-11-11 : 19:25:46
But shouldnt the top 1000 start at Claim1 since it was ordered by claimID before I pulled the top 1000?

Nate
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-11 : 22:24:10
no. the only way to guarantee the sort order is by specifying an ORDER BY clause in your query
Go to Top of Page
   

- Advertisement -