Redian新闻
>
怎么用Update实现这个?
avatar
怎么用Update实现这个?# Database - 数据库
c*t
1
in Oracle, Table A has the following records:
F1, F2, F3
a1 x v1
a1 y v2
a2 x v1'
a2 y v2'
How to write update statement, and make the records look like:
a1, x, v1-v2
a1, y, v2
a2, x, v1'-v2'
a2, y, v2'
avatar
c*t
2
update (select x.F1, x.F2, x.F2-y.F2 as F2'
from A x, A y
where x.F1=y.F1 and x.F2='x' and y.F2='y'
)
set F2=F2'
ORA-01779: cannot modify a column which maps to a non key-preserved table

【在 c**t 的大作中提到】
: in Oracle, Table A has the following records:
: F1, F2, F3
: a1 x v1
: a1 y v2
: a2 x v1'
: a2 y v2'
: How to write update statement, and make the records look like:
: a1, x, v1-v2
: a1, y, v2
: a2, x, v1'-v2'

avatar
c*t
3
update A x
set F3 = F3 - (select y.F3 from A y where x.F1=y.F1 and y.F2='y')
where x.F2='x'
ORA-01407: cannot update ... to NULL

【在 c**t 的大作中提到】
: in Oracle, Table A has the following records:
: F1, F2, F3
: a1 x v1
: a1 y v2
: a2 x v1'
: a2 y v2'
: How to write update statement, and make the records look like:
: a1, x, v1-v2
: a1, y, v2
: a2, x, v1'-v2'

avatar
B*g
4
NND, 写错了
update A x
set F3 = F3 - NVL((select y.F3 from A y where x.F1=y.F1 and y.F2='y' AND
ROWNUM = 1), 0)
where x.F2='x'

there
AND
avatar
c*t
5
your solution is bettern than mine:
merge into A orig using
(select F1, F2, SUM(F3)
from (select F1, F2, F3 from A where F2='x'
union
select F1, F2, -1*F3 as F3 from A where F2='y')
group by F1, F2
) temp
on orig.F1=temp.F1
WHEN MATCHED THEN UPDATE
set F3=temp.F3;

【在 B*****g 的大作中提到】
: NND, 写错了
: update A x
: set F3 = F3 - NVL((select y.F3 from A y where x.F1=y.F1 and y.F2='y' AND
: ROWNUM = 1), 0)
: where x.F2='x'
:
: there
: AND

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。