我是Oracle新手,我不知道在Oracle SQL(而不是PL / SQL)中使用for循环。
我查询并执行如下语句:
我有以下数据
ID NAME DATE SALARY
101 5A101 100411041204 12000
我需要以下结果:
ID NAME DATE SALARY
101 5A101 1004 12000
101 5A101 1104 12000
101 5A101 1204 12000
如何为该结果创建查询?
答案 0 :(得分:0)
例如,您可以使用递归CTE:
<强> SQLFiddle Demo 强>
with cte (id, name, date_, salary, dt, rn) as (
select id, name, date_, salary, substr(date_, 1, 4), 1 from t
union all
select id, name, date_, salary,
substr(date_, rn * 4 + 1, 4), rn + 1
from cte where substr(date_, rn * 4 + 1, 4) is not null )
select id, name, dt, salary from cte
order by id, rn
答案 1 :(得分:0)
忽略奇怪的数据结构,如果你想要答案,可能是
WITH test
AS (SELECT 101 id,'5A101' name,'100411041204' date1,12000 salary FROM DUAL UNION ALL
SELECT 1012 id,'5A102' name,'10041104' date1,12000 salary FROM DUAL)
SELECT DISTINCT id,
name,
SUBSTR (date1, (LEVEL - 1) * 4 + 1, 4) date1
FROM test
CONNECT BY LEVEL <= LENGTH (date1) / 4
ORDER BY id, name, date1
此处日期列的数据长度应为4。