在从内部查询传递的外部查询中操作结果集

Manipulate result set in outer query that passed from inner query

查询下方

SELECT CASE
     WHEN results like '%duplicate%' THEN ' xxx' || results
     ELSE results
END AS results
FROM (SELECT XMLAGG (XMLPARSE (CONTENT RESULTS || '|' WELLFORMED)
      ORDER BY RESULTS).getclobval () AS RESULTS
      FROM (SELECT DISTINCT
             ' ' || result || ' - ' || result_final AS RESULTS
           FROM myTable  WHERE ID = '123456'
));

在内部查询中,如果我应用where条件:

result not like '%duplicate%'

不会发生与“xxx”的串联,因为传递给外部查询的“结果”中没有此类数据。没有 where 结论,连接发生但是“结果”与“重复%”也显示。

是否有任何解决方案可以让外部查询通过不显示任何与“xxx”串联的“Duplicate%”来操纵“结果”?

下面是我的测试数据

SQL> DESC mytable
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 RESULT                                             VARCHAR2(1024)
 RESULT_FINAL                                       VARCHAR2(1024)
 ID                                                 CHAR(6)

SQL> SELECT * FROM mytable;

Trx Duplicate    Trx Duplicate    123456
Insufficient Bal Insufficient Bal 123456

预期结果:

 xxx Insufficient Bal-Insufficient Bal|

当前结果(带where条件)

Insufficient Bal-Insufficient Bal|

当前结果(无where条件)

xxx Trx Duplicate-Trx Duplicate|Insufficient Bal-Insufficient Bal|

我希望这对你有用。我知道这不是最酷的方法,但它可以完成工作。

SQL> desc mytable
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 RESULT                                             VARCHAR2(1024)
 RESULT_FINAL                                       VARCHAR2(1024)
 ID                                                 CHAR(6)

SQL> col result for a40
SQL> col result_final for a40
SQL> set lines 140
SQL> select * from mytable

RESULT                                   RESULT_FINAL                             ID
---------------------------------------- ---------------------------------------- ------
Trx Duplicate                            Trx Duplicate                            123456
Insufficient Bal                         Insufficient Bal                         123456

现在,得到你想要的输出

SQL> with x as
  2  (SELECT XMLAGG (XMLPARSE (CONTENT RESULTS || '|' WELLFORMED)
  3        ORDER BY RESULTS).getclobval () AS RESULTS
  4        FROM (SELECT DISTINCT ' ' || result || ' - ' || result_final AS RESULTS FROM myTable )
  5  )
  6  select case when lower(results) like '%duplicate%'
  7         then
  8             case when lower(regexp_substr(results , '[^|]+', 1, 1 )) like '%duplicate%' then 'xxx'||regexp_substr(results , '[^|]+', 1, 2 )
  9                  when lower(regexp_substr(results , '[^|]+', 1, 1 )) not like '%duplicate%' then 'xxx'||regexp_substr(results , '[^|]+', 1, 1 )
 10                  when lower(regexp_substr(results , '[^|]+', 1, 1 )) like '%duplicate%' and lower(regexp_substr(results , '[^|]+', 1, 2 )) like '%duplicate%' then null
 11            end
 12        else results
 13        end
 14        as results
 15* from x
SQL> /

RESULTS
--------------------------------------------------------------------------------
xxx Insufficient Bal - Insufficient Bal

如果想要结果末尾的|,只需要拼接即可。