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 > Using Case When In Order By Clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-08-09, 07:00
ERDEMONK ERDEMONK is offline
Registered User
 
Join Date: Apr 2009
Posts: 2
Using Case When In Order By Clause

I CANT ORDER BY MULTIPLE COLUMNS WHILE USING CASE WHEN


i can execute this query

SELECT C1,C2,C3,C4 FROM T1

ORDER BY
CASE WHEN C1=2 THEN C2
WHEN C1=3 THEN C3 END

BUT I CANT EXECUTE THIS QUERY

SELECT C1,C2,C3,C4 FROM T1

ORDER BY
CASE WHEN C1=2 THEN C2,C4
WHEN C1=3 THEN C3,C2,C4 END


THANK YOU;
Reply With Quote
  #2 (permalink)  
Old 04-08-09, 08:43
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
the THEN clause allows only ONE expression. Therefore you can't use

... THEN C2,C4.

Consider using a construkt like this:

ORDER BY
CASE WHEN C1=2 THEN C2 CONCAT C4
WHEN C1=3 THEN C3 CONCAT C2 CONCAT C4 END


or use a separat CASE clause for each column:

ORDER BY
CASE WHEN C1=2 THEN C2 WHEN C1=3 THEN c3 END ,
CASE WHEN C1=2 THEN C4 WHEN C1=3 THEN C2 END ,
CASE WHEN C1=3 THEN C4 END

Last edited by umayer; 04-08-09 at 08:57.
Reply With Quote
  #3 (permalink)  
Old 04-08-09, 12:03
ERDEMONK ERDEMONK is offline
Registered User
 
Join Date: Apr 2009
Posts: 2
thank u

but also i need to use asc and desc
so i need to write a stored procedure i think or
i will do it in 2 select queries
Reply With Quote
  #4 (permalink)  
Old 04-09-09, 03:16
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Could you describe in plain words how the ordering shall be done? We have a single result set and you want to apply different sorting criteria for each row - how should the overall result be sorted, then?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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