如何从包含逗号分隔条目的变量中创建(不同的)值列表?

时间:2019-06-04 09:44:22

标签: sql oracle plsql oracle-apex

我在APEX报表(保存在名为APR的表中)中有一个用户生成的变量(KEYWORDS),允许用户输入多个(逗号分隔)值。我想建立一个不同值的列表,这些值以前曾用于以后通过弹出式LOV进行选择。

我设法使用

创建了一个不同的列表
SELECT distinct regexp_substr((select LISTAGG(KEYWORDS, ',') from APR), '[^,]+', 1, LEVEL) from dual
    connect by regexp_substr((select LISTAGG(KEYWORDS, ',') from APR), '[^,]+', 1, LEVEL)
is not null 
;

其中select LISTAGG(KEYWORDS, ',') from APR是:'test,test,TEST2,test,TEST2,test,TEST2,test3'

select keywords from APR由6行组成:

test
test,TEST2
test,TEST2
test
TEST2
test3

我的问题与尝试在Edit LOV查询中实现此问题有关。

select (select distinct regexp_substr((select LISTAGG(KEYWORDS, ',') from APR), '[^,]+', 1, LEVEL) from dual
    connect by regexp_substr((select LISTAGG(KEYWORDS, ',') from APR), '[^,]+', 1, LEVEL)
is not null) as display_value, 

(select distinct regexp_substr((select LISTAGG(KEYWORDS, ',') from APR), '[^,]+', 1, LEVEL) from dual
    connect by regexp_substr((select LISTAGG(KEYWORDS, ',') from APR), '[^,]+', 1, LEVEL)
is not null) as return_value 

  from APR
 order by 1

此代码已在“编辑LOV”部分中成功验证,但弹出的LOV中未显示任何值。粘贴到SQL命令中时,我收到以下错误消息:ORA-01427:单行子查询返回多个行。

3 个答案:

答案 0 :(得分:1)

您发布的查询无效; listagg需要within group子句。

无论如何:建议您使用该查询(您可能已经正确编写)作为数据源,如下所示:

with t_data as
  (select listagg(keywords, ',') within group (order by null) col 
   from apr
  )
select col display_value,
       col return_value
from t_data
order by col

答案 1 :(得分:0)

这是预期的结果吗?

代码:-

   WITH apr_new AS (
       SELECT DISTINCT regexp_substr(keywords,'[^,]+',1,level) keywords_new
       FROM (SELECT LISTAGG(keywords,',') WITHIN GROUP(ORDER BY keywords) keywords FROM apr) apr
       CONNECT BY regexp_substr(keywords,'[^,]+',1,level) IS NOT NULL
   ) SELECT
       keywords_new AS display_value,
       keywords_new AS return_value
     FROM apr_new ORDER BY 1;

结果:-

TEST2 TEST2

TEST3 TEST3

测试测试

答案 2 :(得分:0)

感谢@Littlefoot,使用该框架

使用

with t_data as
(select distinct regexp_substr((select LISTAGG(KEYWORDS, ',') within group (order by null) from APR), '[^,]+', 1, LEVEL) col from dual 
    connect by regexp_substr((select LISTAGG(KEYWORDS, ',') within group (order by null)from APR), '[^,]+', 1, LEVEL) 
is not null )
select col display_value,
       col return_value
from t_data
order by col

with t_data as
(select distinct regexp_substr((select LISTAGG(KEYWORDS, ',') from APR), '[^,]+', 1, LEVEL) col from dual 
    connect by regexp_substr((select LISTAGG(KEYWORDS, ',') from APR), '[^,]+', 1, LEVEL) 
is not null )
select col display_value,
       col return_value
from t_data
order by col

产生相同的结果。尽管listagg显然需要在group子句中使用,后者仍然有效(有兴趣知道这是否可能导致任何问题)。