根据文本选择连接字段值 Oracle SQL

Concatenate values of field depending on text selection Oracle SQL

我有一个 table a_x 包含 A 列和 B 列,A 列是 CLOB 格式,B 列编号 (10):

A                            | B                                    
-----------------------------|-------
atveroeosipsumloremipsumdolor| 1
stetclitakasdtest            | 2
noseatakimataatveroeosipsum  | 3
loremipsumdolor              | 4
consetetursadipscingelitr    | 5

我想生成这个 table,以便找出哪个 ID 与某些子字符串相关:

A                            | IDs                                    
-----------------------------|-------
atveroeosipsum               | 1,3
test                         | 2
stetclitakasd                | 2
noseatakimata                | 3
loremipsumdolor              | 1,4
consetetursadipscingelitr    | 5

我尝试了以下代码:

create table a_y 
as
select a 
  from a_x where a contains('atveroeosipsum', 'test'
                  , 'stetclitakasd', 'noseatakimata'
                  , 'loremipsumdolor', 'consetetursadipscingelitr')

alter table a_y
add ids varchar2(2000); 

由于 00920. 00000 - "invalid relational operator",代码无法正常工作。 我认为不可能搜索 CLOB 格式的文本。当 A 列为 varchar 格式时,如何生成第二个 table?

更新:mathguy 的代码有效。我想使用一个名为 table_expressions 的 table,它包含所需的表达式。我创建了这个 table,它只包含一列("result table" 的第 A 列)。

mathguy修改后的代码:

create table a_y 
  as 
  with 
  input_strings ( a ) as ( 
select column_value from table_expressions 
  ) 
select t2.a, listagg(t1.b, ',') within group (order by t1.b) 
 as ids from a_x t1 join input_strings t2 on t1.a like '%' || t2.a || '%' 
 group by t2.a 

另见问题

字符串比较的正确运算符是 LIKE。请注意,它适用于 CLOB,而不仅仅是 VARCHAR2。

在下面的示例中,我使用一种特定方法即时创建了 table 输入字符串。还有其他几种方法 - 使用您熟悉的任何一种。

with
     a_x ( a, b ) as (
       select to_clob('atveroeosipsumloremipsumdolor'), 1 from dual union all
       select to_clob('stetclitakasdtest')            , 2 from dual union all
       select to_clob('noseatakimataatveroeosipsum')  , 3 from dual union all
       select to_clob('loremipsumdolor')              , 4 from dual union all
       select to_clob('consetetursadipscingelitr')    , 5 from dual
     ),
     input_strings ( str ) as (
       select column_value
       from   table ( sys.odcivarchar2list ( 'atveroeosipsum', 'test', 'stetclitakasd',
                                             'noseatakimata', 'loremipsumdolor',
                                             'consetetursadipscingelitr'
                                           )
                    )                    
     )
select   t2.str, listagg(t1.b, ',') within group (order by t1.b) as ids
from     a_x t1 
         join input_strings t2 on t1.a like '%' || t2.str || '%'
group by t2.str
;

STR                        IDS
-------------------------  ---
atveroeosipsum             1,3
consetetursadipscingelitr  5
loremipsumdolor            1,4
noseatakimata              3
stetclitakasd              2
test                       2