select row_number()[rank(),dense_rank()] OVER (PARTITION BY 分组字段1,分组字段2 ORDER BY 排序字段1) from table;注意:此处不可以用group by ,因为group by 是分组进行汇总功能。
select sno,cno,degree, row_number()over(partition by cno order by degree desc) mm from scorerank示例:
SELECT * FROM (select sno,cno,degree, rank()over(partition by cno order by degree desc) mm from score) where mm = 1;rank和row_number的区别
[ START WITH CONDITION1 ] CONNECT BY [ NOCYCLE ] CONDITION2 [ NOCYCLE ]start with 子句为可选项,用来标识哪行作为查找树型结构的第一行(即根节点,可指定多个根节点)。若该子句被省略,则表示所有满足查询条件的行作为根节点。2.关于PRIOR PRIOR置于运算符前后的位置,决定着查询时的检索顺序。
select empno, mgr, level as lv from scott.emp a start with mgr is null connect by (prior empno) = mgr order by level;
层次查询执行逻辑:
.确定上一行(相对于步骤b中的当前行),若start with 子句存在,则以该语句确定的行为上一行,若不存在则将所有的数据行视为上一行。
.从上一行出发,扫描除该行之外所有数据行。select empno, mgr, level as lv from scott.emp a start with empno = 7876 connect by (prior mgr ) = empno order by level;层次查询执行逻辑:
start with child_id = 10 connect by (prior child_id) = parent_idprior 和 子列在一起,表示寻找它的子孙,即自顶向下,和父列在一起,表示开始寻找它的爸爸,即自下向上。
LISTAGG( [,]) WITHIN GROUP (ORDER BY ) [OVER (PARTITION BY )]listagg虽然是聚合函数,但可以提供分析功能(比如可选的OVER()子句)。使用listagg中,下列中的元素是必须的:
SELECT deptno,LISTAGG(ename, ',') WITHIN GROUP (ORDER BY deptno) AS employees FROM emp GROUP BY deptno;
function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)参数说明:
<!-- 堆代码 duidaima.com --> select regexp_substr('1,2,3','[^,]+',1,1) result from dual; select regexp_substr('1,2,3','[^,]+',1,2) result from dual;可以通过connect by可以构造连续的值。如下所示:
select rownum from dual connect by rownum<=7;结合REGEXP_SUBSTR 及 connect by 即可实现拆分字符串为多行的需求,最终的语句为:
SELECT REGEXP_SUBSTR ('1,2,3', '[^,]+', 1,rownum) from dual connect by rownum<=LENGTH ('1,2,3') - LENGTH (regexp_replace('1,2,3', ',', ''))+1;
MERGE INTO [target-table] A USING [source-table sql] B ON([conditional expression] and [...]...) WHEN MATCHED THEN [UPDATE sql] WHEN NOT MATCHED THEN [INSERT sql]merge into作用:
merge into score a using (select std_no, c.dept_no from student c where c.std_no in (select std_no from tmp_20210809)) b on (a.std_no = b.std_no and a.balb_type = '01') when matched then update set a.pre_bal = nvl(a.pre_bal, 0) + 5.8 WHEN NOT MATCHED THEN insert (a.bal_id, a.std_no, a.balb_type, a.pre_bal, a.dept_no) values (序列, b.std_no, '01', 5.8, b.dept_no);总结