473,386 Members | 1,795 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,386 software developers and data experts.

CASE AROUND ORDER BY?

Hello I'm trying to have some control on how my data is ordered
depending on an input parameter
my question is

in a stored procedure how can I do something like this at the end of my
statement.
pOrder as input value where pOrder can be 1 or 0

CASE WHEN pOrder = 1
THEN
ORDER BY STREET
ELSE
ORDER BY CITY
END

May 25 '06 #1
7 6145
In article <11**********************@i39g2000cwa.googlegroups .com>,
el*********@gmail.com says...
Hello I'm trying to have some control on how my data is ordered
depending on an input parameter
my question is

in a stored procedure how can I do something like this at the end of my
statement.
pOrder as input value where pOrder can be 1 or 0

CASE WHEN pOrder = 1
THEN
ORDER BY STREET
ELSE
ORDER BY CITY
END


I guess you need to think the other way around. Add a column to your
select statement which using the case clause will be filled with STREET
or CITY and order on that column.

select case when porder = 1 then street else city end, .....
order by 1
May 25 '06 #2
mandible wrote:
Hello I'm trying to have some control on how my data is ordered
depending on an input parameter
my question is

in a stored procedure how can I do something like this at the end of my
statement.
pOrder as input value where pOrder can be 1 or 0

CASE WHEN pOrder = 1
THEN
ORDER BY STREET
ELSE
ORDER BY CITY
END

If neither column is indexed anyway then you could do:
ORDER BY CASE WHEN pOrder = 1 THEN address ELSE city END

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 25 '06 #3
Thank you serge that looks quite perfect to what I"m trying to
accomplish.

May 25 '06 #4
You missed the point thaty CASE is an expression and NOT a procedural
control statement.

Standard SQL-92 does not allow you to use a function or expression in
an ORDER BY clause. The ORDER BY clause is part of a cursor and it can
only see the column names that appear in the SELECT clause list that
was used to build the result set. Someone will now chime in that
SQL-99 (officially called "a standard in progress" and not recognized
by the U.S. Government for actual use) does allow this.

But aside from this, there is the good programming practice of showing
the fields that are used for the sort to the user, usually on the left
side of each line since we read left to right.

The standard trick for picking a sorting order at run time is to use a
flag in CASE expression. If you want to sort on more than one column
and allow all possible combinations of sorting use one CASE per column:

SELECT
CASE @flag_1
WHEN 'a' THEN CAST (a AS CHAR(n))
WHEN 'b' THEN CAST (b AS CHAR(n))
WHEN 'c' THEN CAST (c AS CHAR(n))
ELSE NULL END AS sort_1,
CASE @flag_2
WHEN 'x' THEN CAST (x AS CHAR(n))
WHEN 'y' THEN CAST (y AS CHAR(n))
WHEN 'z' THEN CAST (z AS CHAR(n))
ELSE NULL END AS sort_2,
...
CASE @flag_n
WHEN 'n1' THEN CAST (n1 AS CHAR(n))
WHEN 'n2' THEN CAST (n2 AS CHAR(n))
WHEN 'n3' THEN CAST (n3 AS CHAR(n))
ELSE NULL END AS sort_2,

FROM Foobar
WHERE ...
ORDER BY sort_1, sort_2, ...

More than one sort column and only a limited set of combinations then
use concatenation.

CASE @flag_1
WHEN 'ab'
THEN CAST(a AS CHAR(n)) ||' ' || CAST(b AS CHAR(n))
WHEN 'ba'
THEN CAST(b AS CHAR(n)) ||' ' || CAST(a AS CHAR(n))
ELSE NULL END AS sort_1,

If you need ASC and DESC options, then use a combination of CASE and
ORDER BY

CASE @flag_1
WHEN @flag_1 = 'a' AND @flag_1_ad = 'ASC'
THEN CAST (a AS CHAR(n))
WHEN @flag_1 = 'b' AND @flag_1_ad = 'ASC'
THEN CAST (b AS CHAR(n))
WHEN @flag_1 = 'c' AND @flag_1_ad = 'ASC'
THEN CAST (c AS CHAR(n))
ELSE NULL END AS sort_1_a,
CASE @flag_1
WHEN @flag_1 = 'a' AND @flag_1_ad = 'DESC'
THEN CAST (a AS CHAR(n))
WHEN @flag_1 = 'b' AND @flag_1_ad = 'DESC'
THEN CAST (b AS CHAR(n))
WHEN @flag_1 = 'c' AND @flag_1_ad = 'DESC'
THEN CAST (c AS CHAR(n))
ELSE NULL END AS sort_1_d

.. ORDER BY sort_1_a ASC, sort_1_d DESC

I have shown explicit CAST(<exp> AS CHAR(n)), but if the datatypes of
the THEN clause expressions were already the same, there would be no
reason to force the conversions.

You change the ELSE NULL clause to any constant of the appropriate
datatype, but it should be something useful to the reader.

A neater way of doing this is to use one column for each sorting option
so you do not have worry about CAST() operations.

SELECT ...
CASE WHEN @flag = 'a' THEN a ELSE NULL END AS sort1,
CASE WHEN @flag = 'b' THEN b ELSE NULL END AS sort2,
CASE WHEN @flag = 'c' THEN c ELSE NULL END AS sort3
FROM Foobar
WHERE ...
ORDER BY sort1, sort2, sort3;

May 26 '06 #5
--CELKO-- wrote:
CASE WHEN @flag = 'c' THEN c ELSE NULL END AS sort3
FROM Foobar
WHERE ...
ORDER BY sort1, sort2, sort3;

Joe,

Are you sure referencing to the correlation name in the ORDER BY clause
is SQL-92? That's teh first I hear of it.

Cheers
Serge

PS: Contrary to the belief of some the US Government has limited say in
this _world_wide_ newsgroup. ;-)

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 26 '06 #6
>Standard SQL-92 does not allow you to use a function or expression in
an ORDER BY clause. The ORDER BY clause is part of a cursor and it can
only see the column names that appear in the SELECT clause list that
was used to build the result set. Someone will now chime in that
SQL-99 (officially called "a standard in progress" and not recognized
by the U.S. Government for actual use) does allow this.


Hi,

Without getting into which standard allows what, nor diplomatic
controversies about whether the U.S. Government acknowledges it or not,
I would like to add that Serge's suggestion also works with DECLAREd
temp tables, at least in DB2 for LUW FP12.

Cheers,

Willy

(example follows)

declare global temporary table control (code smallint) on commit
preserve rows
insert into willy.test values
(0,'john','elm drive'),
(1,'mary','mill crescent'),
(2,'peter', 'charing cross avenue'),
(3,'paula','beaufort street')
"select * from willy.test order by case when (select code from
session.control fetch first row only) = 0 then name else address end"

CODE NAME ADDRESS
------ ------------------------------ ------------------------------
3 paula beaufort street
2 peter charing cross avenue
0 john elm drive
1 mary mill crescent

4 record(s) selected.
"select * from willy.test order by case when (select code from
session.control fetch first row only) = 1 then name else address end"

CODE NAME ADDRESS
------ ------------------------------ ------------------------------
0 john elm drive
1 mary mill crescent
3 paula beaufort street
2 peter charing cross avenue

4 record(s) selected.

May 30 '06 #7

wi********@gmail.com wrote:
Standard SQL-92 does not allow you to use a function or expression in
an ORDER BY clause. The ORDER BY clause is part of a cursor and it can
only see the column names that appear in the SELECT clause list that
was used to build the result set. Someone will now chime in that
SQL-99 (officially called "a standard in progress" and not recognized
by the U.S. Government for actual use) does allow this.


Hi,

Without getting into which standard allows what, nor diplomatic
controversies about whether the U.S. Government acknowledges it or not,
I would like to add that Serge's suggestion also works with DECLAREd
temp tables, at least in DB2 for LUW FP12.

Cheers,

Willy

(example follows)

declare global temporary table control (code smallint) on commit
preserve rows
insert into willy.test values
(0,'john','elm drive'),
(1,'mary','mill crescent'),
(2,'peter', 'charing cross avenue'),
(3,'paula','beaufort street')
"select * from willy.test order by case when (select code from
session.control fetch first row only) = 0 then name else address end"

CODE NAME ADDRESS
------ ------------------------------ ------------------------------
3 paula beaufort street
2 peter charing cross avenue
0 john elm drive
1 mary mill crescent

4 record(s) selected.
"select * from willy.test order by case when (select code from
session.control fetch first row only) = 1 then name else address end"

CODE NAME ADDRESS
------ ------------------------------ ------------------------------
0 john elm drive
1 mary mill crescent
3 paula beaufort street
2 peter charing cross avenue

4 record(s) selected.


yes serge's idea did exactly what I was looking for.

May 31 '06 #8

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

Similar topics

3
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
3
by: Bryan | last post by:
I am executing a case statement list below, USE Northwind SELECT MONTH(OrderDate) AS OrderMonth, SUM(CASE YEAR(OrderDate) WHEN 1996 THEN 1 ELSE 0 END) AS c1996,
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
4
by: Seenu | last post by:
I'm experiencing some puzzling behaviour with some of my UDFs when declaring them as ATOMIC.. Basically I'm invoking another UDF (which uses some Java code) in one branch of a CASE statment, and...
9
by: Peter | last post by:
My problem is the last bit of coding below, the like statement does not work. what I have is a product options field and in it is stored characters i.e. "avcy" etc what the query does is...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
5
by: Brian Tkatch | last post by:
Is there a way to do ORDER BY with DESC inside a CASE statement? That is, given more than one choice for an ORDER BY based on a CASE statement, but only one of the choices will also use DESC...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.