If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Case statement in ORDER BY Clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-28-11, 16:00
gpaulose27 gpaulose27 is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
Case statement in ORDER BY Clause

Hi,

I need to do case statement in ORDER BY Clause. It works if ORDER BY is only for one column. How do I do for multiple column.

It works if

Select EmpNo, EmpName, Salary
from employee
where EmpNo > 1000
order by
case when :sort-by-name = 'y'
then EmpName
else Salary
end


It doesn't work

Select EmpNo, EmpName, Salary
from employee
where EmpNo > 1000
order by
case when :sort-by-name = 'y'
then EmpName, EmpNo
else Salary, EmpNo
end
Reply With Quote
  #2 (permalink)  
Old 10-28-11, 20:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
SELECT EmpNo
     , EmpName
     , Salary
  FROM employee
 WHERE EmpNo > 1000
ORDER 
    BY CASE WHEN :sort-by-name = 'y'
            THEN EmpName
            ELSE Salary
        END
     , EmpNo
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-28-11, 20:50
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
It works if

Select EmpNo, EmpName, Salary
from employee
where EmpNo > 1000
order by
case when :sort-by-name = 'y'
then EmpName
else Salary
end
Apart from your issue, was it really worked?
Because, usually EmpName must be CHAR or VARCHAR and Salary must be number.

See Numeric in Rules for result data types - IBM DB2 9.7 for Linux, UNIX, and Windows
Quote:
Code:
If one operand is… And the other operand is… The data type of the result is… 
...
...
INTEGER            String                     DECFLOAT(34) 
...
DECIMAL(w,x)       String                     DECFLOAT(34)
So, result datatype must be DECFLOAT and most EmpName may not be able to convert to DECFLOAT.
I guessed that you may want to replace "else Salary" to "else CHAR(Salary)".

Anyhow, please try...
Code:
SELECT EmpNo, EmpName, Salary
 FROM  employee
 WHERE EmpNo > 1000
 ORDER BY
       CASE
       WHEN :sort-by-name = 'y' THEN
            EmpName
       ELSE CHAR(Salary)
       END
     , EmpNo
;
Reply With Quote
  #4 (permalink)  
Old 10-29-11, 18:42
gpaulose27 gpaulose27 is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
Thanks a lot tonkuma!! It worked.

The Empno and salary were just and example I gave. In actual scenario, the ORDER BY clause contains only CHAR. So it worked.

But, DESC won't work. Is there a way to sort it on DESC?
Reply With Quote
  #5 (permalink)  
Old 10-29-11, 22:18
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Specify DESC after END of case expression and/or after ", EmpNo".
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On