一:oracle
①select deptno,ename,sal,sum(sal) over (partition by deptno order by sal) dept_sum,--根据部门分组,再按部门内的个人薪水排序,逐个累加。
sum(sal) over (order by deptno,sal) sum --按部门排序,将薪水逐个累加。
from emp;
②查询类及其子类(id=4的类及其子类):
select * from tb_common_goodsclass t connect by prior t.classid=t.superclassid start with t.classid=4
③select *
from (select ltrim(SYS_CONNECT_BY_PATH(classname, '>'), '>') firstclassname,
t.classname secondclassname,
t.classid secondclassid,
t.superclassid
from tb_common_goodsclass t
WHERE CONNECT_BY_ISLEAF = 1
connect by prior t.classid = t.superclassid
start with t.superclassid = 0)
执行结果:
注:--SYS_CONNECT_BY_PATH一定要和connect by子句合用,第一个参数是形成树形式的字段,第二个参数是父级和其子级分隔显示用的分隔符
--level 就是这个数据属于 哪一个等级
--connect_by_isleaf就是树的最末端的值,或者说这个树枝下已经没有树叶了
--connect_by_iscycle 导致出现死循环的那个树枝