Я пытаюсь отработать сводку с таблицей EMP, предоставленной в oracle, выбрав задание в качестве столбца с ename в качестве значений. Это запрос, который я получил до сих пор.
select distinct job, count(job) from emp group by job ... (A)
-- Result)
-- CLERK 4
-- SALESMAN 4
-- PRESIDENT 1
-- MANAGER 3
-- ANALYST 2
select t1.ename, t2.ename, t3.ename, t4.ename, t5.ename
from
(select ename, rank() over (order by ename) as n from emp where job = 'CLERK') t1,
(select ename, rank() over (order by ename) as n from emp where job = 'SALESMAN') t2,
(select ename, rank() over (order by ename) as n from emp where job = 'PRESIDENT') t3,
(select ename, rank() over (order by ename) as n from emp where job = 'MANAGER') t4,
(select ename, rank() over (order by ename) as n from emp where job = 'ANALYST') t5,
(select ename, rank() over (order by ename) as n from emp where job =
(select job ... (C)
from emp
group by job
having count(*) = (
select max(count(*))
from emp
group by job))
) t6
where t6.n = t1.n(+)
and t6.n = t2.n(+)
and t6.n = t3.n(+)
and t6.n = t4.n(+)
and t6.n = t5.n(+)
order by t6.n;
output: ORA-00937: not a single-group group function ... (B)
Но вывод (B), так как есть два задания (CLERK, SALESMAN), оба из которых имеют значение из 4, как вы можете видеть из (A).
Вопрос)
Есть ли способ изменить (C), чтобы он выбрал только одну работу, несмотря на (A), так как я хочу только создать достаточное количество столбцов для всех заданий после поворота?
Надеюсь, я все прояснил.
* Завершенный запрос *
select t1.ename "Clerk", t2.ename "Salesman", t3.ename "President", t4.ename "Manager", t5.ename "Analyst"
from
(select ename, rank() over (order by ename) as n from emp where job = 'CLERK') t1,
(select ename, rank() over (order by ename) as n from emp where job = 'SALESMAN') t2,
(select ename, rank() over (order by ename) as n from emp where job = 'PRESIDENT') t3,
(select ename, rank() over (order by ename) as n from emp where job = 'MANAGER') t4,
(select ename, rank() over (order by ename) as n from emp where job = 'ANALYST') t5,
(select ename, rank() over (order by ename) as n from emp where job =
(select job
from
(select *
from (
select job, count(job)
from emp
group by job
order by count(job) desc)
where rownum = 1))) t6
where t6.n = t1.n(+)
and t6.n = t2.n(+)
and t6.n = t3.n(+)
and t6.n = t4.n(+)
and t6.n = t5.n(+)
order by t6.n;