Solved

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

Posted on 2013-01-01
3
240 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
3 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now