DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: decode in order by

  1. #1
    Join Date
    Feb 2009
    Posts
    2

    decode in order by

    Hi,
    I want to use decode in order by in following way -

    select * from dept
    order by decode(.......)

    if deptno=1 then order by deptname desc, deptcity asc
    if deptno=2 then order by deptno desc, deptname asc, deptcity desc
    else
    order by deptno desc, deptcity asc.

    Can anyone please help me?

    Thank you in advance.

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    I think decode() is not the tool to hammer that specific nail.

    I would go with
    ORDER BY
    CASE
    WHEN...
    WHEN...
    ELSE...
    END
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Feb 2009
    Posts
    2

    decode in order by

    Can u please give an example ?

  4. #4
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Look at the first result when you Google: Oracle, order by case statement ...

    http://www.compoundtheory.com/?action=displayPost&ID=39

    ... isn't it a beauty?
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

Posting Permissions

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


Click Here to Expand Forum to Full Width