Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sql query not sorting

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

664 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