Results 1 to 3 of 3

Thread: Oracle-SQL question

  1. #1

    Thumbs up Oracle-SQL question

    Oflate many of the interviews, one of the queries is getting very popular which is to display the columns as rows. Ex: 10 sales mum 20 admin chn 30 fincon del out put : 10 20 30 sales admin fincon mum chn del.. Could any tell us whether there is any oracle built in function to do this? what is the easiest way to achieve this.


  2. #2
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: Oracle-SQL question

    create table test(empno number,ename varchar2(25));

    insert into test values(10,'sales');
    insert into test values(20,'admin');


    Select max(case column# when 1 then a end) -- first column
    -- if 2nd column not null, append after comma
    ||
    case when
    max(case column# when 2 then a end)
    is not null then ','||
    max(case column# when 2 then a end)
    end
    -- if 3rd column not null, append after comma
    ||
    case when
    max(case column# when 3 then a end)
    is not null then ','||
    max(case column# when 3 then a end)
    end ||
    max(case column# when 1 then b end) -- first column
    -- if 2nd column not null, append after comma
    ||
    case when
    max(case column# when 2 then b end)
    is not null then ','||
    max(case column# when 2 then b end)
    end
    -- if 3rd column not null, append after comma
    ||
    case when
    max(case column# when 3 then b end)
    is not null then ','||
    max(case column# when 3 then b end)
    end as details
    from ( select empno a, ename b
    , row_number() over
    (order by ename ) column#
    from test )
    --group by a
    --order by a
    drop table test;


  3. #3
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: Oracle-SQL question

    you can also try the following sample example
    Code:
    select max(case deptno when 10 then ename end) d10,
            max(case deptno when 20 then ename end) d20,
            max(case deptno when 30 then ename end) d30,
            max(case job when 'CLERK' then ename end) clerks,
            max(case job when 'MANAGER' then ename end) mgrs,
            max(case job when 'PRESIDENT' then ename end) prez,
            max(case job when 'ANALYST' then ename end) anals,
            max(case job when 'SALESMAN' then ename end) sales
       from (
     select deptno, job, ename,
            row_number()over(partition by deptno order by empno) rn
       from emp
            ) x
      group by rn



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact