Solved

Sql query not sorting

Posted on 2012-03-22
4
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

624 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