有两个结构相同的表,第一个表是主数据,另一个表是最近更新的数据。为了。例如
表A——
col1 col2 col3
abc xyz yyz
表B--
col1 col2 col3
abc pqr yyz
当我更新 tableB 中 col2 的值时,现在我想要查询将给出表 A 中的旧值和表 B 中的新值。即
oldValue NewValuwe
xyz pqr
使用您发布的示例数据,我假设两个表中的 col1 都不会更新,因此可用于链接两个表
Select
a.col1,
CASE
WHEN NVL(a.col2, '') <> NVL(b.col2, '') THEN a.col2
WHEN NVL(a.col3, '') <> NVL(b.col3, '') THEN a.col3
-- and so on
ELSE 'None'
END
AS oldValue,
CASE
WHEN NVL(a.col2, '') <> NVL(b.col2, '') THEN b.col2
WHEN NVL(a.col3, '') <> NVL(b.col3, '') THEN b.col3
-- and so on
ELSE 'None'
END
AS NewValue
from
tableA a, tableB b
where
a.col1 = b.col1
and
(
NVL(a.col2, '') <> NVL(b.col2, '')
OR NVL(a.col3, '') <> NVL(b.col3, '') -- assuming you want to track changes of col3 as well
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句