這是一個解決方案,不會對評論的長度做任何事先假設。我添加了兩個字符串,一個短(少於5個字符),一個長於10個字符,另一個使用NULL註釋以確保行不會丟失,從而徹底測試解決方案。
我假設(empid, dt)
是基表中的唯一組合(可能是主鍵)。順便說一句,我希望你實際上沒有在模式中使用Date
作爲列名。
該解決方案不包含WITH子句;它始於select empid...
也可能不需要ORDER BY子句。
with
test_data (empid, empname, servicenumber, dt, comments) as (
select 1, 'a', 123, to_date('23-03-1990', 'dd-mm-yyyy'), 'wednesday' from dual union all
select 1, 'a', 1234, to_date('24-04-1990', 'dd-mm-yyyy'), 'Test12' from dual union all
select 2, 'b', 234, to_date('24-05-2016', 'dd-mm-yyyy'), 'Todayis' from dual union all
select 2, 'b', 235, to_date('25-05-2016', 'dd-mm-yyyy'), 'Joe' from dual union all
select 3, 'c', 238, to_date('25-05-2016', 'dd-mm-yyyy'), '' from dual union all
select 4, 'c', 2238, to_date('25-05-2016', 'dd-mm-yyyy'), 'longer string' from dual
)
select empid, empname, servicenumber, dt, level as comno,
substr(comments, 5 * level - 4, 5) as comments
from test_data
connect by level <= 1 + length(comments)/5
and prior empid = empid
and prior dt = dt
and prior sys_guid() is not null
order by empid, dt
;
EMPID E SERVICENUMBER DT COMNO COMMENTS
---------- - ------------- ---------- ---------- --------------------
1 a 123 1990-03-23 1 wedne
1 a 123 1990-03-23 2 sday
1 a 1234 1990-04-24 1 Test1
1 a 1234 1990-04-24 2 2
2 b 234 2016-05-24 1 Today
2 b 234 2016-05-24 2 is
2 b 235 2016-05-25 1 Joe
3 c 238 2016-05-25 1
4 c 2238 2016-05-25 1 longe
4 c 2238 2016-05-25 2 r str
4 c 2238 2016-05-25 3 ing
新增:如果你的數據是CLOB數據類型,最好是使用dbms_lob
版本的substr
。另外,如果您必須將數據切分爲75個字符,則必須調整多個數字。下面是相同的解決方案,這兩個更改沒有ORDER BY子句(如果用於將數據遷移到另一個db產品,則不需要)。注意:與通常的substr()
函數相比,dbms_lob.substr()
的「金額」和「偏移」(第二個和第三個參數)反轉;注意當你比較解決方案時。
select empid, empname, servicenumber, dt, level as comno,
dbms_lob.substr(comments, 75, 75 * level - 74) as comments
from test_data
connect by level <= 1 + length(comments)/75
and prior empid = empid
and prior dt = dt
and prior sys_guid() is not null
;
謝謝:)它的工作原理 –