Oracle SQL:: 子查询中带有 orderby 的 Rownum 抛出缺少括号
Posted
技术标签:
【中文标题】Oracle SQL:: 子查询中带有 orderby 的 Rownum 抛出缺少括号【英文标题】:Oracle SQL:: Rownum with orderby in a subquery throws missing parenthesis 【发布时间】:2019-02-20 01:27:49 【问题描述】:我的 sql 如下所示。它为具有 orderby 子句的行抛出缺少括号。如何重写此查询以克服此错误?
update MY_TABLE1 a
set (my_addr)=
(select my_addr
from MY_TABLE1 b
where b.code1=a.code1
and b.code2=a.code2
and b.my_addr is not null
and rownum = 1
order by LAST_UPDTD_TMSTMP DESC)
where a.my_addr is null
and exists (select 1
from MY_TABLE1 b
where b.code1=a.code1
and b.code2=a.code2
and b.my_addr is not null)
如果我尝试再创建一个嵌套子查询,对别名“a”的引用就会消失。
update MY_TABLE1 a
set (my_addr)=
(select my_addr from (select my_addr
from MY_TABLE1 b
where b.code1=a.code1
and b.code2=a.code2
and b.my_addr is not null
order by LAST_UPDTD_TMSTMP DESC) where rownum = 1)
where a.my_addr is null
and exists (select 1
from MY_TABLE1 b
where b.code1=a.code1
and b.code2=a.code2
and b.my_addr is not null)
非常感谢任何指针。
【问题讨论】:
如果您能提供示例数据,那将很有帮助 【参考方案1】:您可以使用keep
来获取您想要的值:
update MY_TABLE1 a
set my_addr = (select max(my_addr) keep (dense_rank first order by LAST_UPDTD_TMSTMP DESC)
from MY_TABLE1 b
where b.code1 = a.code1 and
b.code2 = a.code2 and
b.my_addr is not null
)
where a.my_addr is null and
exists (select 1
from MY_TABLE1 b
where b.code1 = a.code1 and
b.code2 = a.code2 and
b.my_addr is not null
);
【讨论】:
感谢您的解决方案。这有效!非常感谢。以上是关于Oracle SQL:: 子查询中带有 orderby 的 Rownum 抛出缺少括号的主要内容,如果未能解决你的问题,请参考以下文章