1.连接某个字段
对于表A 我们有B和C两个字段
有时候,我们想B相同的,C字段相连接的值,可以用WM_CONCAT和LISTAGG两种方法。其中LISTAGG为11g中的新特性。
两种用法如下:
SELECT WM_CONCAT(V.USER_NAME) AS USER_NAME, ITEM_ID FROM USER_ITEM_VIEW V GROUP BY ITEM_ID HAVING COUNT(1) > 1查询结果:
SELECT LISTAGG(V.USER_NAME, ',') WITHIN GROUP(ORDER BY V.USER_NAME) AS USER_NAME, V.ITEM_ID FROM USER_ITEM_VIEW V GROUP BY V.ITEM_ID HAVING COUNT(1) > 1;LISTAGG可以自定义分隔符,且速度更快。
分组后取得某个字段最大或最小的记录:
SELECT LASTM_DUMMY, ZONE_ID, CREATED_ON FROM (SELECT A.*, ROW_NUMBER() OVER(PARTITION BY A.ZONE_ID ORDER BY CREATED_ON DESC) RN FROM A) B WHERE B.RN = 1
ORACLE实现九九乘法表:
SELECT REVERSE(sys_connect_by_path(REVERSE(rpad(rownum * lv, 2)) || '=' || rownum || '*' || lv, ' ')) FROM (SELECT LEVEL lv FROM dual CONNECT BY LEVEL < 10) t WHERE lv = 1 CONNECT BY lv + 1 = PRIOR lv;