createtable tree ( id number(10) notnullprimarykey, name varchar2(100) notnull, super number(10) notnull//0is root ); -- 从子到父 select*from tree start with id = ? connect by id = prior super -- 从父到子 select*from tree start with id = ? connect by prior id = suepr -- 整棵树 select*from tree start with super =0 connect by prior id = suepr
2. 分页查询
select*from ( select my_table.*, rownum my_rownum from ( select name, birthday from employee orderby birthday ) my_table where rownum <120 ) where my_rownum >=100;
3. 累加查询, 以scott.emp为例
select empno, ename, sal, sum(sal) over(orderby empno) result from emp; EMPNO ENAME SAL RESULT ---------- ---------- ---------- ---------- 7369 SMITH 800800 7499 ALLEN 16002400 7521 WARD 12503650 7566 JONES 29756625 7654 MARTIN 12507875 7698 BLAKE 285010725 7782 CLARK 245013175 7788 SCOTT 300016175 7839 KING 500021175 7844 TURNER 150022675 7876 ADAMS 110023775 7900 JAMES 95024725 7902 FORD 300027725 7934 MILLER 130029025
4. 高级group by
select decode(grouping(deptno),1,'all deptno',deptno) deptno, decode(grouping(job),1,'all job',job) job, sum(sal) sal from emp groupby ROLLUP(deptno,job); DEPTNO JOB SAL ---------------------------------------- --------- ---------- 10 CLERK 1300 10 MANAGER 2450 10 PRESIDENT 5000 10all job 8750 20 CLERK 1900 20 ANALYST 6000 20 MANAGER 2975 20all job 10875 30 CLERK 950 30 MANAGER 2850 30 SALESMAN 5600 30all job 9400 all deptno all job 29025