這應該可以做到。
select *
from (select substr(can, 1, instr(can, '|', 1, 1) - 1) as can,
case
when level = 1 then
replace(substr(can, 1, instr(can, '|', 1, 1)), '|', '')
else
replace(substr(can,
instr(can, '|', 1, level),
length(can) - instr(can, '|', 1, level)),
'|',
'')
end as drum
from canon
connect by level <= (length(can) - length(replace(can, '|')))
and prior sys_guid() is not null
and prior can = can) x
where not exists (select 'x' from drum y where y.dr = x.drum)
order by 1, 2
SQL小提琴演示:http://sqlfiddle.com/#!4/59a7b/1/0
注:我加在每個2代表的第二行用於測試目的,因爲我想確保它爲2+行,因爲邏輯對此很敏感。
作爲第二個回答你的問題,如果你想要在同一行的數據(雖然我認爲我的第一個查詢更有意義),這會給你你要求的確切結果(把所有的值都放到CAN的每個值都有相同的行):
select can||'|'||listagg(drum,'|') within group (order by drum) as listed
from (select substr(can, 1, instr(can, '|', 1, 1) - 1) as can,
case
when level = 1 then
replace(substr(can, 1, instr(can, '|', 1, 1)), '|', '')
else
replace(substr(can,
instr(can, '|', 1, level),
length(can) - instr(can, '|', 1, level)),
'|',
'')
end as drum
from canon
connect by level <= (length(can) - length(replace(can, '|')))
and prior sys_guid() is not null
and prior can = can) x
where not exists (select 'x' from drum y where y.dr = x.drum)
group by can
order by 1
您是否一直在尋找CANON中的內容,但不是DRUM中的內容? (而不是相反)也只是爲了更好地理解這一點,是相機的第一個標識符('18102-75'),接下來的2個是兼容的鼓?它會在CANON的所有行上遵循相同的格式嗎?第一個ID是相機,然後是每個兼容的鼓由管道分開? –
是的,我想從CANON表中獲取數據。有些時候佳能只有一位數的CAN號。 – user3095575