Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    2

    Unanswered: 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;

  2. #2
    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 09:57.

  3. #3
    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

  4. #4
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •