我有一个表REVERSE
,列为
ID ORG_ID FULL_ID DATE_REV
-- ------ ------- --------
X1 11% NULL
X2 22% NULL
X3 33% NULL
X4 44% NULL
X5 55% NULL
我还有另一个表MASTER
,列为
FULL_ORG DATE
-------- --------
11ABC 20190101
22DEF 20190101
33GHI 20190101
44XYZ 20190101
55MNO 20190101
如何使用MASTER
中的ORG_ID从REVERSE
中找到FULL_ORG和相应的DATE值,并相应地在FULL_ID字段和DATE_REV字段中进行更新。
请帮助。
结果应如下所示。
TXN ORG_TXN FULL_ORG DATE
--- ------- -------- --------
1 11% 11ABC 20190101
2 22% 22DEF 20190101
3 33% 33GHI 20190101
4 44% 44XYZ 20190101
5 55% 55MNO 20190101
答案 0 :(得分:0)
您可能会考虑以下一种选择:
SQL> with
2 rev (id, org_id) as
3 (select 'X1', '11%' from dual union all
4 select 'X2', '22%' from dual union all
5 select 'X3', '33%' from dual union all
6 select 'X4', '44%' from dual union all
7 select 'X5', '55%' from dual
8 ),
9 mas (full_org, c_date) as
10 (select '11ABC', 20190101 from dual union all
11 select '22DEF', 20190101 from dual union all
12 select '33GHI', 20190101 from dual union all
13 select '44XYZ', 20190101 from dual union all
14 select '55MNO', 20190101 from dual
15 )
16 select regexp_substr(r.id, '\d+$') txn,
17 r.org_id,
18 m.full_org,
19 m.c_date
20 from rev r join mas m on regexp_substr(r.org_id, '^\d+') = regexp_substr(m.full_org, '^\d+');
TXN ORG FULL_ C_DATE
-------- --- ----- ----------
1 11% 11ABC 20190101
2 22% 22DEF 20190101
3 33% 33GHI 20190101
4 44% 44XYZ 20190101
5 55% 55MNO 20190101
SQL>
答案 1 :(得分:0)
您可以使用以下查询来更新REVERSE表中的数据:
MERGE INTO REVERSE R
USING MASTER M
ON ( REGEXP_SUBSTR(R.ORG_ID, '^\d+') = REGEXP_SUBSTR(M.FULL_ORG, '^\d+') )
WHEN MATCHED THEN
UPDATE
SET R.FULL_ID = M.FULL_ORG,
R."DATE" = M."DATE"
干杯!
答案 2 :(得分:0)
如果您的ORG_ID
具有固定长度(例如两位数字和一个%
字符),则可以简单地将substr
键的键连接起来
select r.org_id,
m.full_org,
m.c_date
from reverse r join master m
on substr(r.org_id,1,2) = substr(m.full_org,1,2)
这是性能最高的 方法,但如果密钥长度发生变化,则失败