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
Solved

SQL SELECT - use ORDER BY without a column

Posted on 2011-03-11
6
697 Views
Last Modified: 2012-05-11
Is there a way to order the recordset results of a SELECT using another way than by column?

Specifically, we create SQL Select commands dynamically of the following kind:-
SELECT ID, Title, Country FROM table1 WHERE ID = 3 OR ID = 1 OR ID = 2
The values for ID are entered into one field of a form, separated by a comma, and then the Select command is created by using Split and a loop. At present, the recordset shows the results in order 1, 2, 3. But we want it to show the results in the order of ID entered into the form and thus in the Select command, ie 3, 1, 2 in the case above. Can this be done somehow?
0
Comment
Question by:pld51
6 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 400 total points
ID: 35113210
This is a bit clumsy, but I can't think of a better way right now, in your loop build an order by expression that ends up as

order by case when ID = 3 then 1 else 0 end desc, case when ID = 1 then 1 else 0 end desc, case when ID = 2 then 1 else 0 end desc
0
 
LVL 6

Assisted Solution

by:Patrick Tallarico
Patrick Tallarico earned 100 total points
ID: 35113229
My suggestion would be to add another field into your recordset to account for the order in which the items were entered.  Then you can fill that field with the proper order when your split and loop segment runs.  you can create the recordset with the fiels, and you should be able to have it present without having to select it for display with the rest of the records.
for instance

Recordset looks like this
orderid|id|etc...
1           |3 |...
2           |1 |...
3           |2 |...

Select Id, etc...
from recordset
order by orderid

0
 
LVL 40

Expert Comment

by:Sharath
ID: 35113523
How did you split your ID column? Do you have a function to do this?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:pld51
ID: 35113577
Thanks! The first solution looks easiest to implement - as I did not understand the logic I tried it out as an SQL select command and it ordered the IDs as desired. I now have to build into the loop and test again, but should work.

Second solution should work but is more complicated in terms of adding an intermediate step - updating the new field in the database. And this new field orderid is purely temporary, as the order of the ids will be constantly changed.
0
 
LVL 6

Expert Comment

by:Patrick Tallarico
ID: 35113582
  It isn't about splitting the ID column, but rather creating another column specifically to store the orderid information.
You will have to adjust your table1 to include this new column.  Once you have added this column, you should be able to add the values into it during your Split/Loop code.  As the code loops through the items, include a segment of code to add the incrementing orderid value.
   This hinges on creating this new field/column in the table that you are pulling the data from, whether it is a temporary table, or a static one.
   
0
 

Author Closing Comment

by:pld51
ID: 35115959
Thanks for both solutions, I have now tested Dale Burrell's solution by adjusting the split/loop and it works like a dream. I did not test solution of stpm11 as I don't want to enter another field into the table, but it should work also. Thanks again!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

790 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