我的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)
任何指针都非常感谢。
您可以使用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
);