【问题标题】:Unable to use LISTAGG output in a subquery. ORACLE无法在子查询中使用 LISTAGG 输出。甲骨文
【发布时间】:2018-02-04 13:31:51
【问题描述】:

我从以下查询中获取逗号分隔值
SELECT LISTAGG('''' || student_name || '''',',') WITHIN GROUP (ORDER BY student_name) FROM students;

但是,当我输入子查询时,它没有给出任何结果。
select * from students where student_name in ('A', 'B'); --give 两行

select * from students where student_name in (SELECT LISTAGG('''' || student_name || '''',',') WITHIN GROUP (ORDER BY student_name) FROM students;) -- 没有行

【问题讨论】:

  • 这是https://stackoverflow.com/questions/39854040/how-to-use-listagg-to-return-rows-prefixed-with-quotes的跟进
  • 对于那些希望能够点击超文本链接的人,这是this old StackOverflow的后续行动

标签: sql oracle string-aggregation


【解决方案1】:

你不明白in 的工作原理。它不适用于字符串。它适用于多个项目。

所以,正确的逻辑写法是:

select  *
from students
where student_name in (select student_name from students) -- no rows;

您的查询实际上是在做:

where student_name in ('aaron,beth,calvin,debbie,...')

in 列表只有一项,所以这相当于:

where student_name = 'aaron,beth,calvin,debbie,...'

并且没有学生姓名(如果您有超过一行)可以是所有学生的串联名称。

【讨论】:

  • 我的字符串在single quotes 中有多个逗号分隔的项目'A','B','C','D','E'。当我单独运行第一个查询(使用 LISTAGG)时,我可以看到它。
  • @krishnakant 。 . .不,它是一个字符串,字符串中有很多逗号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多