【发布时间】:2022-11-17 08:39:48
【问题描述】:
让p 成为主条目表,对于每个主条目,我们要聚合详细条目的集合。明细项目有两种,来自两个不同的来源。此查询在 Oracle 11 上以 ORA-00904: "P"."NAME" invalid identifier 失败,但在 Oracle 19 上运行正常。为什么?
with people (name) as (
select 'Alice' from dual union all
select 'Bob' from dual
), apples (name, title) as (
select 'Alice', 'apple1' from dual union all
select 'Bob', 'apple2' from dual union all
select 'Bob', 'apple3' from dual
), pears (name, title) as (
select 'Alice', 'pear4' from dual union all
select 'Alice', 'pear5' from dual union all
select 'Alice', 'pear6' from dual union all
select 'Bob', 'pear7' from dual union all
select 'Bob', 'pear8' from dual
)
select p.name
, (
select listagg(u.title) within group (order by null)
from (
select x.title from apples x where x.name = p.name
union
select x.title from pears x where x.name = p.name
) u
) as unioned
from people p;
| NAME | UNIONED |
|---|---|
| Alice | apple1pear4pear5pear6 |
| Bob | apple2apple3pear7pear8 |
【问题讨论】:
标签: oracle oracle11g oracle19c correlated-subquery