查询:
select column_one, column_two, column_three
from main_tbl_name
输出:
column_one column_two
--------------------------------------------------------------------------------
data1 SBANAR SBARTO SCBADP SCCHBA SCHPIT SCTHAB SDPCLV SDPRPN SDPSEC SERIE
data2 SRALEN SRETIN SRUNLI SSAPEL SSATAR SSCMAR SSDECA
data3 DRAP14 E05A E1 ECOMOD ENJO01 ENPC01 ESPHSA ESS
我还有另一个查询:
select column_one, column_two, column_three
from another_tbl_name
where column_four in (main_tbl_name.column_two)
我需要将main_tbl_name.column_two
的值分成例如('SBANAR','SBARTO','SCBADP','SCCHBA','SCHPIT','SCTHAB','SDPCLV',' SDPRPN','SDPSEC','SERIE')
在上面的第二个查询中,如果存在某些值,请在main_tbl_name.column_three
中附加每个迭代的值
所以我的最终输出应该是
column_one column_two column_three
-------------------------------------------------------------------------------------------------------------------------
data1 SBANAR SBARTO SCBADP SCCHBA SCHPIT SCTHAB SDPCLV SDPRPN SDPSEC SERIE SBANAR SBARTO SCCHBA SDPCLV SDPSEC
data2 SRALEN SRETIN SRUNLI SSAPEL SSATAR SSCMAR SSDECA SRALEN SSCMAR SSDECA
data3 DRAP14 E05A E1 ECOMOD ENJO01 ENPC01 ESPHSA ESS DRAP14 E05A ESPHSA ESS
答案 0 :(得分:0)
以下是一个与您的数据类似的示例:
with
-- sample data
main_tbl_name ( column_one, column_two) as (
select 'data1', 'SBANAR SBARTO SCBADP SCCHBA SCHPIT SCTHAB SDPCLV SDPRPN SDPSEC SERIE'
from dual union all
select 'data2', 'SRALEN SRETIN SRUNLI SSAPEL SSATAR SSCMAR SSDECA'
from dual union all
select 'data3', 'DRAP14 E05A E1 ECOMOD ENJO01 ENPC01 ESPHSA ESS' from dual ),
another_tbl_name(column_four) as (
select 'SDPSEC SBANAR SBARTO SCCHBA SDPCLV' from dual union all
select 'DRAP14 E05A ESPHSA ESS' from dual),
-- end of sample data
t1 as (select column_one c1, column_two c2, '"'||replace(column_two, ' ', '", "')||'"' cx
from main_tbl_name),
t2 as (select '"'||replace(column_four, ' ', '", "')||'"' cx
from another_tbl_name)
select c1, c2, listagg(b.cs, ' ') within group (order by b.cs) c3
from (select c1, c2, trim(column_value) cs from t1, xmltable(cx)) a
left join (select trim(column_value) cs from t2, xmltable(cx)) b on a.cs = b.cs
group by c1, c2
...结果:
C1 C2 C3
----- -------------------------------------------------------------------- -------------------------------
data1 SBANAR SBARTO SCBADP SCCHBA SCHPIT SCTHAB SDPCLV SDPRPN SDPSEC SERIE SBANAR SBARTO SCCHBA SDPCLV SDPSEC
data2 SRALEN SRETIN SRUNLI SSAPEL SSATAR SSCMAR SSDECA
data3 DRAP14 E05A E1 ECOMOD ENJO01 ENPC01 ESPHSA ESS DRAP14 E05A ESPHSA ESS
此解决方案基于xmltable
,左联接和listagg()
。我假设单个空格是分隔符。如果another_table
包含的数据不是列表形式,那么您甚至可以简化此查询。
基本上,您必须拆分 list 列,然后进行联接。还有其他方法可以执行此操作,例如分层查询,已经发布在SO上的PLSQL函数。使用适合您的任何方法。
答案 1 :(得分:0)
所以你想:
如果是这样,您可以执行以下操作:
WITH DATA AS
( SELECT 44 AS COLUMN_ONE ,'WORD1 WORD2 WORD3 <!-- language: lang-sql --> WORD4 WORD5 WORD6' COLUMN_TWO FROM DUAL )
SELECT COLUMN_ONE ,TRIM(REGEXP_SUBSTR(COLUMN_TWO, <!-- language: lang-sql --> '[^ ]+', 1, LEVEL)) COLUMN_TWO
FROM DATA
CONNECT BY INSTR(COLUMN_TWO, ' ', 1, LEVEL - 1) > 0;
COLUMN_ONE COLUMN_TWO
44 WORD1
44 WORD2
44 WORD3
44 WORD4
44 WORD5
44 WORD6
干杯!!! 迪伦