Solved

Can I custom sort a query without adding a field for sort order?

Posted on 2004-09-02
9
221 Views
Last Modified: 2008-03-17
I have a query and of course I can sort on the field ascending or descending. Rather than alphabetical, I want to sort first by D, then by S, then by etc.

Is there a way to Order by this field:  dbo.Tbl_Trailer_Info.CurrentLocation but do something like .CurrentLocation "D", "S", (I know that syntax won't work, just trying to give you an idea of what I'm looking for) ?
0
Comment
Question by:jamesh1031
  • 5
  • 4
9 Comments
 
LVL 9

Accepted Solution

by:
paelo earned 500 total points
ID: 11963261
ORDER BY CASE WHEN CurrentLocation='D' THEN 0 WHEN CurrentLocation='S' THEN 1 ELSE 2 END


Or, if you want to specify a larger order hierarchy, create a function to convert them to numerical values and you can call that function from the ORDER BY clause.

-Paul.
0
 

Author Comment

by:jamesh1031
ID: 11963290
0 is the first, what does the else 2 do?
0
 
LVL 9

Expert Comment

by:paelo
ID: 11963424
For all other values that aren't 'D' or 'S'.

-Paul.
0
 

Author Comment

by:jamesh1031
ID: 11963580
One more question. I actually have things with names like door_19 door_20 and I do want the "D's" to come first, do I do some kind of CurrentLocation='D?', then 0 when currentlocation="s?' then 1 etc
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 9

Expert Comment

by:paelo
ID: 11963871
ORDER BY CASE WHEN CurrentLocation LIKE 'D%' THEN 0 WHEN CurrentLocation LIKE 'S%' THEN 1 ELSE 2 END

-Paul.
0
 
LVL 9

Expert Comment

by:paelo
ID: 11964011
Thanks.

Keep in mind that this will only sort on the first character in the field.  If you need something more complicated (with several levels, multiple sort criteria) I think the best solution is to create a function with which you can rank fields numerically according to their alphanumeric contents.

-Paul.
0
 

Author Comment

by:jamesh1031
ID: 11964049
Can you tell me what you mean by "create a function"?

I did this which does sort the D's etc.

ORDER BY CASE WHEN dbo.Lkp_Location.Name LIKE 'D%' THEN 0 WHEN dbo.Lkp_Location.Name LIKE 'A%' THEN 1 WHEN dbo.Lkp_Location.Name LIKE 'B%' THEN
                       2 WHEN dbo.Lkp_Location.Name LIKE 'L%' THEN 3 WHEN dbo.Lkp_Location.Name LIKE 'S%' THEN 4 ELSE 5 END

But I have door numbers like Door_19 and you're right I don't get the doors in numerical order.

You answered my question, I probably should post a second one.
0
 
LVL 9

Expert Comment

by:paelo
ID: 11964468
Try this:

ORDER BY CASE WHEN dbo.Lkp_Location.Name LIKE 'D%' THEN 0 WHEN dbo.Lkp_Location.Name LIKE 'A%' THEN 1 WHEN dbo.Lkp_Location.Name LIKE 'B%' THEN 2 WHEN dbo.Lkp_Location.Name LIKE 'L%' THEN 3 WHEN dbo.Lkp_Location.Name LIKE 'S%' THEN 4 ELSE 5 END, dbo.Lkp_Location.Name


You'll see that I added your field as the second sort criteria.  This means it will sort by the numerical values you've imposed with the CASE statement first (D, A, B, L, S, etc.) and then it will sort on the field itself.  So all the D's will be sorted ascendingly.

Creating a user-defined function in SQLS will allow you to do a number of operations on a passed alphanumeric (such as Lkp_Location.name) and return a numerical value which represents its sort value.  You can create functions using the following syntax:

CREATE FUNCTION dbo.udfGetSortOrder( @Name varchar(200) )
RETURNS int
AS
BEGIN
...
END



And you can input it into your ORDER BY clause like this:

ORDER BY dbo.udfGetSortOrder(dbo.Lkp_Location.Name)


But you're right, this is probably more appropriate in another question.


-Paul.
0
 

Author Comment

by:jamesh1031
ID: 11964999
OK, I'm posting now, go and answer
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
I have a large data set and a SSIS package. How can I load this file in multi threading?
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

947 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

17 Experts available now in Live!

Get 1:1 Help Now