Order By Decode - Question
DBAsupport.com Forums - Powered by vBulletin
Results 1 to 3 of 3

Thread: Order By Decode - Question

Hybrid View

  1. #1
    Join Date
    Jul 2001
    Posts
    334

    Order By Decode - Question

    Hi,

    I have a query below

    select * from emp
    where deptno = &dpt
    order by decode(.......)

    If I enter dpt 10 then I need
    order by ename

    If I enter dpt 20 then I need
    order by hiredate,job,sal

    If I enter dpt 30 then I need
    order by deptno,ename

    How I can achive this. Thanks in advance.

  2. #2
    Join Date
    Jul 2001
    Posts
    334
    Hi,
    Please correct me if I am wrong

    select * from emp
    where deptno = &dpt
    order by decode(&dpt,10,ename,
    20,hiredate||','||job||','||sal,
    30,deptno||','||ename,'')


    Thanks

  3. #3
    Join Date
    Mar 2001
    Location
    Reading, U.K
    Posts
    598
    A slight change in your query since you are anyway putting a filter on deptno:

    select * from emp
    where deptno = &dpt
    order by decode(deptno,10,ename,
    20,hiredate||','||job||','||sal,
    30,deptno||','||ename,'')

    Another observation is if you are already putting a filter on deptno, the sorting order for deptnos 10 and 30 pretty much amounts to the same, i.e., it would be on ename.
    Cheers!
    OraKid.

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