Solved

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

Posted on 2013-01-01
3
242 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 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