oracle with 语句实现递归查询
时间:2022-03-15 09:11
Oracle with 语句可以实现如同connect by 语句一样的序列:
使用level实现1到10的序列。
select level from dual connect by level<=10;
更多connect by 用法参考:
查询当前时间往前的12周的开始时间、结束时间、第多少周:
select sysdate - (to_number(to_char(sysdate - 1, 'd')) - 1) - (rownum - 1) * 7 as startDate, sysdate + (7 - to_number(to_char(sysdate - 1, 'd'))) - (rownum - 1) * 7 as endDate, to_number(to_char(sysdate, 'iw')) - rownum + 1 as weekIndex from dualconnect by level<= 12;--将level改成rownum可以实现同样的效果
d 表示一星期中的第几天
iw 表示一年中的第几周
字符串分割,由一行变为多行:
比如说分割01|02|03|04这种有规律的字符串
select REGEXP_SUBSTR('01|02|03|04', '[^|]+', 1, rownum) as newport from dual connect by rownum <= REGEXP_COUNT('01|02|03|04', '[^|]+');