Solved

Sql query not sorting

Posted on 2012-03-22
4
365 Views
Last Modified: 2012-04-03
Hi All,

I have two stored proc's that are a literally copy of each other. One runs again one table, the other another table.  They started off as the same query I just changed the table alias. The columns they are running again are the same type, eg Datetime and nvarchange. The issue I have is one of the queries is returning the datetime  order by month, the other isn't. I need it ordered by month. When looking at the excution plan one is doing a sort the other is doing a hash compare.

I've checked the table structure, the only different is the nvarchar column in one is 255 in size, the other (non sorting) is 20. Neither of these columns are used to sort. The index's are the same, and not on the open time column. Collation is also the same. It is very frustrating

Top one doesn't sort, bottom one does
Select 
sdi.sdnumber,
DATENAME(Month, SDi.OpenTime)as openmonth , 
CAST(YEAR(sdi.OpenTime) AS nvarchar) AS openyear
from ServiceDeskInteractions sdi
GROUP BY dateadd(month,datediff(month, 0, SDi.OpenTime),0),SDi.OpenTime,sdi.sdnumber


Select
TC.[Incident ID],
DATENAME(Month, TC.[Open Time])as openmonth , 
CAST(YEAR(TC.[Open Time]) AS nvarchar) AS openyear
from TargetCalls tc
GROUP BY dateadd(month,datediff(month, 0, TC.[Open Time]),0),TC.[Open Time],TC.[Incident ID]

Open in new window


Any hints or suggestions would be great.

Cheers
JL
0
Comment
Question by:jaLouden
4 Comments
 
LVL 27

Accepted Solution

by:
tliotta earned 167 total points
ID: 37755367
Any hints or suggestions would be great.

The only way to guarantee a sort order with SQL is to include an ORDER BY clause. I realize this is for SQL Server, so it's only "mostly SQL"; but I would expect the same behavior.

Tom
0
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 37755430
Yep. Without a order by its random. One of them probably just happens to be in the right order.
0
 
LVL 16

Assisted Solution

by:DcpKing
DcpKing earned 167 total points
ID: 37755536
There's no reason for either of your pieces of code to return results in any particular order. Add an ORDER BY to each one and life will improve for you.

But why does one return ordered and the other not ?   you ask. A good question!

In the "sorting" one you probably entered the records in the order you now want them to appear, and you haven't deleted any, or inserted any, and your input process was fortunate enough to get a sequential set of pages to write into, and probably a few other fortunate things too.  

In the "non-sorting" table you may have entered the records in any old order; you might have added a few incorrectly, so deleted them and then added different ones before re-entering the corrected versions, etc., etc. I'm sure other contributors here can tell you of other reasons why SQL Server would or would not happen to end up with your records in perfect sequence.

Our point is, SQL (from SQL Server, Oracle, Access, Paradox, Postgres, or whomever) makes no promises about how you'll get your data back unless you make explicit specifications.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 166 total points
ID: 37757619
Yes, order is only guaranteed if you add ORDER BY.

So add this at the end of both of your existing queries:

ORDER BY openmonth

Or, if you need year - month to be in order, add this:

ORDER BY openyear, openmonth
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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

680 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