oracle - 即使列有前缀,“列定义不明确的错误”?
问题描述
我有以下 sql,它工作正常(Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production)。
with n as (
select ntile(cnt) over ( partition by cnt order by Id ) NT, Id
from t
)
select n.NT, high.Id
from (select distinct NT from n) n
outer apply (
select *
from n x where x.NT = n.NT and rownum = 1 order by Id
) low
outer apply (
select *
from (select * from n x where x.NT = n.NT order by Id desc) d where rownum = 1
) high
现在我将其更改为以下内容,
with n as (
select ntile(cnt) over ( partition by cnt order by Id ) NT, Id
from t
)
select n.NT, phigh.Id -- Error - phigh.Id
from (select distinct NT from n) n
outer apply (
select *
from n x where x.NT = n.NT and rownum = 1 order by Id
) low
outer apply (
select *
from (select * from n x where x.NT = n.NT order by Id desc) d where rownum = 1
) high
outer apply (
select *
from (select * from n x where x.NT = n.NT-1 order by Id desc) d where rownum = 1
) phigh
现在它收到以下错误phigh.Id
:
ORA-00918: 列定义不明确
- 00000 - “列定义不明确”
如果我更改phigh.Id
为,第二个查询有效phigh.*
?换phigh.Id
工作high.Id
也行。
更新:
尝试使用与 CTE 不同的别名。仍然有同样的错误
with n as (
select ntile(cnt) over ( partition by cnt order by Id ) NT, Id
from t
)
select n.NT, phigh.Id -- Error - phigh.Id
from (select distinct NT from n) nt
outer apply (
select *
from n x where x.NT = nt.NT and rownum = 1 order by Id
) low
outer apply (
select *
from (select * from n x where x.NT = nt.NT order by Id desc) d where rownum = 1
) high
outer apply (
select *
from (select * from n x where x.NT = nt.NT-1 order by Id desc) d where rownum = 1
) phigh
更新2:
以下可测试查询出现错误。
with t as (
select 10 as cnt, 1 as Id from dual
),
x as (
select ntile(cnt) over ( partition by cnt order by Id ) NT, Id
from t
)
select n.NT,
high.Id a,
phigh.Id
from (select distinct NT from x) n
outer apply (select * from x where x.NT = n.NT and rownum = 1 order by Id) low
outer apply (select * from (select * from x where x.NT = n.NT order by Id desc) d where rownum = 1) high
outer apply (select * from (select * from x where x.NT = n.NT - 1 order by Id desc) d where rownum = 1) phigh
以下代码有效。
with t as (
select 10 as cnt, 1 as Id from dual
),
x as (
select ntile(cnt) over ( partition by cnt order by Id ) NT, Id
from t
)
select n.NT,
--high.Id a,
phigh.Id
from (select distinct NT from x) n
outer apply (select * from x where x.NT = n.NT and rownum = 1 order by Id) low
--outer apply (select * from (select * from x where x.NT = n.NT order by Id desc) d where rownum = 1) high
outer apply (select * from (select * from x where x.NT = n.NT - 1 order by Id desc) d where rownum = 1) phigh
解决方案
我发现重命名共享的列名high
并且“phigh”有效。
with t as (
select 10 as cnt, 1 as Id from dual
),
x as (
select ntile(cnt) over ( partition by cnt order by Id ) NT, Id
from t
)
select n.NT,
high.Id a,
phigh.Id
from (select distinct NT from x) n
outer apply (select * from x where x.NT = n.NT and rownum = 1 order by Id) low
outer apply (select Id z from (select * from x where x.NT = n.NT order by Id desc) d where rownum = 1) high
outer apply (select * from (select * from x where x.NT = n.NT - 1 order by Id desc) d where rownum = 1) phigh
但这对我(问题所有者)来说真的不是一个解决方案。代码会动态生成,会有很多列。
当多个子查询返回具有相同名称的列并且这些列位于 select 子句中时,Oracle 似乎存在问题。
解决方案2:
更改outer apply (select * from x where x.NT = n.NT and rownum = 1 order by Id) low
为outer apply (select * from x where x.NT = 1 and rownum = 1 order by Id) low
也将摆脱错误。
推荐阅读
- c# - Asp.Net Core 6 将 Bootstrap 添加到脚手架 Identity _Layout.cshtml
- graphql - 如何修复“未捕获的错误:查询没有更多的模拟响应”
- visual-studio-2019 - Task Runner Explorer 键盘快捷键在 VS 2019 中停止工作
- python - 如何比较字典值中的多个数组,并将每个数组元素的字典键映射到新数组/列表中
- python - 对所有值进行假设检验
- android - 如何在 android 中将 ListUpdateCallback 添加到 ListAdapter
- xamarin - Xamarin Android 应用程序在加载 jar 引用的 .so 库时崩溃
- regex - 正则表达式匹配某个域和 URL 的最后一部分
- azure - 如何为 Azure 应用程序网关 403 页面设置自定义错误页面
- javascript - 如何在 HTML、CSS 和 JavaScript 中滑动和向下滚动