Solved

Sql query not sorting

Posted on 2012-03-22
4
360 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 38

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:ScottPletcher
ScottPletcher 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now