473,419 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Ordering results by order of the "IN' clause

Consider this SQL:

SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1',
'value3')

Simple enough, but is there anyway to specify that the result should be
ordered exactly like the "IN" clause states? So when this recordset
comes back, I want it like this:

my_field
------------
value2
value1
value3

Possible?

Deane

Jul 28 '06 #1
5 11116
(de**********@gmail.com) writes:
Consider this SQL:

SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1',
'value3')

Simple enough, but is there anyway to specify that the result should be
ordered exactly like the "IN" clause states? So when this recordset
comes back, I want it like this:

my_field
------------
value2
value1
value3
No. The IN clause is just a syntactic shortcut for a bunch of OR operators.
You will need to add explicit ordering, for instance:

ORDER BY CASE my_field WHEN 'value2' THEN 1
WHEN 'value1' THEN 2
WHEN 'value3' THEN 3
END
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 28 '06 #2
Thanks.

FYI -- I've learned in the meantime that MySQL has this functionality:

ORDER BY FIELD(my_field, 'value2','value1','value3')

Syntactic sugar, to be sure, but still handy.

Deane

Erland Sommarskog wrote:
(de**********@gmail.com) writes:
Consider this SQL:

SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1',
'value3')

Simple enough, but is there anyway to specify that the result should be
ordered exactly like the "IN" clause states? So when this recordset
comes back, I want it like this:

my_field
------------
value2
value1
value3

No. The IN clause is just a syntactic shortcut for a bunch of OR operators.
You will need to add explicit ordering, for instance:

ORDER BY CASE my_field WHEN 'value2' THEN 1
WHEN 'value1' THEN 2
WHEN 'value3' THEN 3
END
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 28 '06 #3
Deane,

instead of

SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1',
'value3')

try this:

SELECT my_field FROM my_table
join (
select 'value2' c, 1 n
union all
select 'value1', 2
union all
select 'value3', 3
) t
on my_field = t.c
order by t.n

Jul 28 '06 #4
de**********@gmail.com wrote:
Thanks.

FYI -- I've learned in the meantime that MySQL has this functionality:

ORDER BY FIELD(my_field, 'value2','value1','value3')

Syntactic sugar, to be sure, but still handy.
The densest way to write it in ANSI SQL is this:
SELECT my_field FROM my_table JOIN (VALUES(1, 'value2'),
(2, 'value1'),
(3, 'value3')) AS V(I, val)
ON myfield = val
ORDER BY i

This way you don't need to repeat the values.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jul 28 '06 #5
Serge,

What are you doing here? Are you porting from SQL Server to DB2 these
days?

Jul 29 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: kinne | last post by:
The following code is supposed to reverse the date in "yyyy-mm-dd" format, but it produces different results in Firefox 1.0 and in Internet Explorer 6SP1. In Firefox, the result is correct...
3
by: Dmitry Jouravlev | last post by:
Hi, I have a number of C++ solutions in Visual Studio .NET and when i compile them using "Whole Program Optimization", certain projects report a LNK1171 error saying that c2.dll could not be...
1
by: James Jensen | last post by:
I ran into a snippet of code that uses a FOR EACH loop to step through the rows in a Dataset.Table object. I understand that the FOR EACH statement implements the MoveNext function of the...
3
by: google | last post by:
I'm using ADODB to connect to an Access database from VB6. I'm running a query that's returning strange results- the following query returns different results in VB & Access: SELECT * FROM...
7
by: vivekian | last post by:
Hi , I need to place the results of two different queries in the same result table parallel to each other. So if the result of the first query is 1 12 2 34 3 45
2
by: whiskers | last post by:
I'm debugging some code and I have to admit that I don't know yet how it works. But I ran into a problem I can't explain The program is a DLL that retrieves raw data from a camera, builds...
8
by: Mike Will via WebmasterKB.com | last post by:
I've been struggling with this for a while now. But I want to display my query results in a table where they are grouped by project. I am using a do while loop but it not achieving the look I...
0
by: =?Utf-8?B?TGVvbkc=?= | last post by:
I'm trying to upload a file using an asp:FileUpload control and an asp:LinkButton. This works fine, while there is an actual filepath in the fileupload upload control. Even an empty field works....
5
by: 848lu | last post by:
hi, i am trying to do a search with my ASP webpage, and then display the results in a DIV tag of HTML, how would i do that, thanks strconnection = New...
16
by: dougmeece | last post by:
Good day everyone, I have a database with 2 main forms. The first form is used to add records to the database and contains a command button that opens the 2nd form for records searching. On...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.