Solved

SQL SELECT - use ORDER BY without a column

Posted on 2011-03-11
6
704 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 41

Expert Comment

by:Sharath
ID: 35113523
How did you split your ID column? Do you have a function to do this?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

735 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