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

x
?
Solved

Sql query not sorting

Posted on 2012-03-22
4
Medium Priority
?
370 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 668 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 668 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 664 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

926 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