Solved

SQL Query reult output sequence changes using RANK (Top 2*)

Posted on 2013-01-01
3
244 Views
Last Modified: 2013-01-06
Hello Experts,

Any ideas why everytime i run a query in my database the sort order of the output changes from time to time.
SELECT * FROM TableA

OUTPUT 1st run:
Col1 , Col2, Col3, Col4, Col5
1      , 1      , 1      , 1      , 1  
2      , 2      , 2      , 2      , 2  
3      , 3      , 3      , 3      , 3  
4      , 4      , 4      , 4      , 4  

OUTPU 2nd run:
Col1 , Col2, Col3, Col4, Col5
2      , 2      , 2      , 2      , 2    
4      , 4      , 4      , 4      , 4
1      , 1      , 1      , 1      , 1
3      , 3      , 3      , 3      , 3

The problem this poses is that i've got a stored procedure which uses ranking (or Top 2*) based on the output of the table and thus giving me inconsistent data.
0
Comment
Question by:jsuanque
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 20 total points
ID: 38735672
you can use order by to not chage the order

SELECT * FROM TableA
order by Col1
0
 
LVL 22

Accepted Solution

by:
Steve Wales earned 180 total points
ID: 38735677
You should never assume that data from a select statement will be returned in any order.

If the data is being read from the Clustered Index on a table, then you'll probably see your data coming out in the order of the index (being how a Clustered Index is the physical ordering of the pages for the table on disk) but other than that, you have no guarantees of the order at any time.

If you require a specific order, include an ORDER BY clause.

Found a discussion on this topic on stackoverflow:
http://stackoverflow.com/questions/10064532/the-order-of-a-sql-select-statement-without-order-by-clause

Most of the discussion in that thread is based on Oracle but I'm 99.9% certain that SQL Server is going to have the same issues.
0
 

Author Closing Comment

by:jsuanque
ID: 38749338
Hello pratima_mcs,
Thanks for the response. But i forgot to mention that the query is neseted and is aggregated (Grouped by) to achieve ranking as required. Thus simply ordering it by won't suffice.

Hello sjwales,
Thnaks for the response. Yes your suggestion fixed my issue. In short, i added a Non-clustered index on the table on multiple columns i'm interested with.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question