一、topN分析問題?
加偽列rownum行號(hào)的問題:
1:永遠(yuǎn)按照默認(rèn)的順序生成----在原表的順序下,跟排序無關(guān)
原因:臨時(shí)表的順序 create global temporary table****
特點(diǎn):當(dāng)會(huì)話或者事務(wù)結(jié)束的時(shí)候,臨時(shí)表中的數(shù)據(jù)自動(dòng)刪除
2:rownum只能使用<,<=,不能使用>,>=
select? rownum2, empno, ename, sal from (select rownum,empno,ename,sal from emp order by sal ?desc)where rownum2<=3;
二、查詢薪水高于本部門平均工資的員工
select e.empno, e.ename,e.sal ,d.AVGSAL from?
emp e, (select deptno,avg(sal) avgsal from emp group by deptno )d
WHERE E.deptno=d.deptno
and e.sal>d.avgsal;
相關(guān)子查詢:把主查詢中的某些值作為參數(shù)傳遞給子查詢
select e.empno,e.name,e.sal,(select avg(sal) from emp where deptno=e.deptno )avgsal
from emp e
where e.sal> (select avg(sal) from emp where deptno=e.deptno )
三、統(tǒng)計(jì)每年入職的員工人數(shù)
select count(*) total ,
sum(if to_year(to_date(hiredate))='1981' ?then i=i+1 else )"1981",
from emp
行轉(zhuǎn)列:
wm_concat(varchar2)-->組函數(shù)
例:select deptno,wm_concat(ename) namelist
from emp
group by deptno;