Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-01
3
Medium Priority
?
249 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 80 total points
ID: 38735672
you can use order by to not chage the order

SELECT * FROM TableA
order by Col1
0
 
LVL 23

Accepted Solution

by:
Steve Wales earned 720 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

971 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