Oracle - 引用子查询别名时的未知标识符

时间:2014-12-09 16:24:52

标签: oracle subquery alias

我有以下查询:

select "Title_ID","Text",
(SELECT LISTAGG("Genre",',') WITHIN GROUP (ORDER BY "Genre") from (SELECT DISTINCT "g"."Genre" from "RegionBroadcasts" left join "Genres" "g" on "RegionBroadcasts"."Genre" = "g".PK_GENRE where "RegionBroadcasts"."Region"=24 and "RegionBroadcasts"."Title_ID"="IDs"."Title_ID")) as "SourceGenres",
(SELECT LISTAGG("Genre",',') WITHIN GROUP (ORDER BY "Genre") from (SELECT DISTINCT "g"."Genre" from "RegionBroadcasts" left join "Genres" "g" on "RegionBroadcasts"."Genre" = "g".PK_GENRE where "RegionBroadcasts"."Region"=1 and "RegionBroadcasts"."Title_ID"="IDs"."Title_ID")) as "TargetGenres",
(SELECT LISTAGG("Channel_Name",',') WITHIN GROUP (ORDER BY "Channel_Name") from (SELECT DISTINCT "Channel_Name" from "ProgrammeChannels" left join "ChannelNames" on "ProgrammeChannels"."Channel"="ChannelNames"."Channel_ID" where "ProgrammeChannels"."Region"=24 and "ProgrammeChannels"."Title_ID"="IDs"."Title_ID")) as "SourceChannels",
(SELECT LISTAGG("Channel_Name",',') WITHIN GROUP (ORDER BY "Channel_Name") from (SELECT DISTINCT "Channel_Name" from "ProgrammeChannels" left join "ChannelNames" on "ProgrammeChannels"."Channel"="ChannelNames"."Channel_ID" where "ProgrammeChannels"."Region"=1 and "ProgrammeChannels"."Title_ID"="IDs"."Title_ID")) as "TargetChannels"

from
((
select "LocalTitle" as "Title_ID" from "RegionBroadcasts2" where "Region" = 24
union
select "GlobalTitle" as "Title_ID" from "RegionBroadcasts2" where "Region" = 24)
intersect
(select "LocalTitle" as "Title_ID" from "RegionBroadcasts2" where "Region" = 1
union
select "GlobalTitle" as "Title_ID" from "RegionBroadcasts2" where "Region" = 1
)) "IDs"

left join "ProgrammeTitles" on "IDs"."Title_ID" = "ProgrammeTitles"."Text_ID"
where "IDs"."Title_ID" <> 1
and "IDs"."Title_ID" is not null
order by "Text";

出于某种原因,我有类型&#34;无效标识符&#34;在第一个&#34;选择listagg&#34;我链接&#34; RegionBroadcasts&#34;。&#34; Title_ID&#34;使用&#34; ID&#34;。&#34; Title_ID&#34;

就像我没有被授权使用子查询&#34; ID&#34;。

我想要准确一点,如果我要更换&#34; ID&#34;。&#34; Title_ID&#34;通过一个固定的整数值,我会得到一些结果

1 个答案:

答案 0 :(得分:1)

Oracle不允许在子查询中使用父列,这些子列的深度超过1级。

解决这个问题的最简单方法是将where移到一个上层:

(SELECT LISTAGG("Genre",',') 
        WITHIN GROUP (ORDER BY "Genre") 
 from (SELECT DISTINCT "g"."Genre", "RegionBroadcasts"."Title_ID" tmp_title_id 
       from "RegionBroadcasts" left join "Genres" "g" on "RegionBroadcasts"."Genre" = "g".PK_GENRE where "RegionBroadcasts"."Region"=24
      ) 
 where tmp_title_id = "IDs"."Title_ID") as "SourceGenres"

它可能会对性能产生负面影响,但我不确定它在这里是否重要。