(资料图片)
select deptno, job, count(*) over (partition by deptno) as emp_cnt, count(job) over (partition by deptno,job) as job_cnt, count(*) over () as total from empDEPTNO JOB EMP_CNT JOB_CNT TOTAL------ --------- ---------- ---------- ---------- 10 CLERK 3 1 14 10 MANAGER 3 1 14 10 PRESIDENT 3 1 14 20 ANALYST 5 2 14 20 ANALYST 5 2 14 20 CLERK 5 2 14 20 CLERK 5 2 14 20 MANAGER 5 1 14 30 CLERK 6 1 14 30 MANAGER 6 1 14 30 SALESMAN 6 4 14 30 SALESMAN 6 4 14 30 SALESMAN 6 4 14 30 SALESMAN 6 4 14select a.deptno, a.job, (select count(*) from emp b where b.deptno = a.deptno) as emp_cnt, (select count(*) from emp b where b.deptno = a.deptno and b.job = a.job) as job_cnt, (select count(*) from emp) as total from emp a order by 1,2DEPTNO JOB EMP_CNT JOB_CNT TOTAL------ --------- ---------- ---------- ---------- 10 CLERK 3 1 14 10 MANAGER 3 1 14 10 PRESIDENT 3 1 14 20 ANALYST 5 2 14 20 ANALYST 5 2 14 20 CLERK 5 2 14 20 CLERK 5 2 14 20 MANAGER 5 1 14 30 CLERK 6 1 14 30 MANAGER 6 1 14 30 SALESMAN 6 4 14 30 SALESMAN 6 4 14 30 SALESMAN 6 4 14 30 SALESMAN 6 4 14select max(case grp when 1 then rpad(ename,6) || " ("|| sal ||")" end) top_3, max(case grp when 2 then rpad(ename,6) || " ("|| sal ||")" end) next_3, max(case grp when 3 then rpad(ename,6) || " ("|| sal ||")" end) rest from (select ename, sal, rnk, case when rnk <= 3 then 1 when rnk <= 6 then 2 else 3 end grp, row_number()over ( partition by case when rnk <= 3 then 1 when rnk <= 6 then 2 else 3 end order by sal desc, ename ) grp_rnk from (select ename, sal, dense_rank()over(order by sal desc) rnk from emp ) x ) y group by grp_rnkTOP_3 NEXT_3 REST--------------- --------------- -------------KING (5000) BLAKE (2850) TURNER (1500)FORD (3000) CLARK (2450) MILLER (1300)SCOTT (3000) ALLEN (1600) MARTIN (1250)JONES (2975) WARD (1250) ADAMS (1100) JAMES (950) SMITH (800)select * from it_researchDEPTNO ENAME------ -------------------- 100 HOPKINS 100 JONES 100 TONEY 200 MORALES 200 P.WHITAKER 200 MARCIANO 200 ROBINSON 300 LACY 300 WRIGHT 300 J.TAYLORselect * from it_appsDEPTNO ENAME------ ----------------- 400 CORRALES 400 MAYWEATHER 400 CASTILLO 400 MARQUEZ 400 MOSLEY 500 GATTI 500 CALZAGHE 600 LAMOTTA 600 HAGLER 600 HEARNS 600 FRAZIER 700 GUINN 700 JUDAH 700 MARGARITORESEARCH APPS-------------------- ---------------100 400 JONES MAYWEATHER TONEY CASTILLO HOPKINS MARQUEZ200 MOSLEY P.WHITAKER CORRALES MARCIANO 500 ROBINSON CALZAGHE MORALES GATTI300 600 WRIGHT HAGLER J.TAYLOR HEARNS LACY FRAZIER LAMOTTA 700 JUDAH MARGARITO GUINNselect max(decode(flag2,0,it_dept)) research, max(decode(flag2,1,it_dept)) apps from ( select sum(flag1)over(partition by flag2 order by flag1,rownum) flag, it_dept, flag2 from ( select 1 flag1, 0 flag2, decode(rn,1,to_char(deptno)," "||ename) it_dept from ( select x.*, y.id, row_number()over(partition by x.deptno order by y.id) rn from ( select deptno, ename, count(*)over(partition by deptno) cnt from it_research ) x, (select level id from dual connect by level <= 2) y ) where rn <= cnt+1 union all select 1 flag1, 1 flag2, decode(rn,1,to_char(deptno)," "||ename) it_dept from ( select x.*, y.id, row_number()over(partition by x.deptno order by y.id) rn from ( select deptno, ename, count(*)over(partition by deptno) cnt from it_apps ) x, (select level id from dual connect by level <= 2) y ) where rn <= cnt+1 ) tmp1 ) tmp2 group by flag