表A具有这样的数据
JID P_ID P_CODE COMM MDATE
--- ---- ------ ---- ---------
1 112 WXCVA null 2-feb-19
1 112 UCXVA WXCVA 09-sep-19
2 222 DCVA null 08-aug-18
2 222 UCVA DCVA 09-dec-09
有200条这样的记录。现在,我需要阅读这些记录并更新辅助代码的日期。结果表应该看起来像这样
JID P_ID P_CODE COMM MDATE
--- ---- ------ ---- ---------
1 112 WXCVA null 2-feb-19
1 112 UCXVA WXCVA 2-feb-19
2 222 DCVA null 08-aug-18
2 222 UCVA DCVA 08-aug-18
如何实现?
答案 0 :(得分:1)
create table mytab(p_id number, p_code varchar2(10), comm varchar2(10), mdate date);
insert into mytab values(112, 'WXCVA', null, to_date('20190202','yyyymmdd'));
insert into mytab values(112, 'UCXVA', 'WXCVA', to_date('20190909','yyyymmdd'));
insert into mytab values(222, 'DCVA', null, to_date('20180808','yyyymmdd'));
insert into mytab values(222, 'UCVA', 'DCVA', to_date('20091209','yyyymmdd'));
COMMIT;
select * from mytab;
P_ID P_CODE COMM MDATE
112 WXCVA NULL 02-FEB-19
112 UCXVA WXCVA 09-SEP-19
222 DCVA NULL 08-AUG-18
222 UCVA DCVA 09-DEC-09
合并语句以执行所需的更新。
MERGE INTO MYTAB C
USING (SELECT A.P_ID, B.P_CODE, B.COMM, A.MDATE
FROM MYTAB A
INNER JOIN
MYTAB B
ON A.P_ID = B.P_ID
AND A.P_CODE = B.COMM) D
ON ( C.P_ID = D.P_ID
AND C.P_CODE = D.P_CODE)
WHEN MATCHED
THEN UPDATE SET C.MDATE = D.MDATE;
select * from mytab;
P_ID P_CODE COMM MDATE
112 WXCVA NULL 02-FEB-19
112 UCXVA WXCVA 02-FEB-19
222 DCVA NULL 08-AUG-18
222 UCVA DCVA 08-AUG-18
答案 1 :(得分:0)
这是您想要的吗?
update tab
set mdate = (select t2.mdate
from tab t2
where t2.p_code = tab.comm
)
where tab.comm is not null;
答案 2 :(得分:0)
对于您发布的示例数据,此更新声明应该有效:
update tablename t
set t.mdate = (
select mdate
from tablename
where p_id = t.p_id and comm is null
)
where t.comm is not null;
请参见demo。
结果:
P_ID | P_CODE | COMM | MDATE
---: | :----- | :---- | :--------
112 | WXCVA | null | 02-FEB-19
112 | UCXVA | WXCVA | 02-FEB-19
222 | DCVA | null | 08-AUG-18
222 | UCVA | DCVA | 08-AUG-18