Oracle SQL比较表中的记录

穆罕默德·伊克萨斯(Mohamed Iqzas)

我有一个如下表:

S.No | Item_ID | Item_Revision | Code |
-----+---------+---------------+-------
1.   | item1   | 0             | xyz  |
2.   | item2   | 0             | xyz  |
3.   | item3   | 0             | xyz  |
4.   | item1   | 1             |      |
5.   | item2   | 1             | abc  |
6.   | item3   | 1             | xyz  |

我需要比较表中的记录,以找到项目不同版本中代码的差异。

我希望结果集如下:

 | Item_ID | Code_Revision_0 | Code_Revision_1 |
 | item1   | xyz             |                 |
 | item2   | xyz             | abc             |

我无法为此目的制定一个oracle查询。

提前致谢!

戈登·利诺夫(Gordon Linoff)

一种基本想法是使用join

select t0.item_id, t0.code as code_0, t1.code as code_1
from t t0 join
     t t1
     on t0.item_id = t1.item_id and
        t0.item_revision = 0 and
        t1.item_revision = 1
where t0.code <> t1.code;

但是,如果code值为NULL(或空字符串),则需要格外小心:

where t0.code <> t1.code or (t0.code is null and t1.code is not null) or
      (t0.code is not null and t1.code is null) 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章