我有一个查询,它从几个表中检索,但有2个表具有相同的字段值,
查询
SELECT A.NUM_RIF, B.NDG, D.NDG FROM ICGR1100 A
LEFT JOIN (ICGR1105 E INNER JOIN ICGR1101 D ON E.C_IST=D.C_IST AND E.DT_DOM_NDG=D.DT_DOM_NDG AND E.N_DOM=D.N_DOM) ON A.C_IST=E.C_IST AND A.NUM_RIF=E.NUM_RIF
LEFT JOIN ICGR1103 B ON A.C_IST=B.C_IST AND A.NUM_RIF=B.NUM_RIF
结果是:
NUM_RIF NDG NDG
---------------- ---------------- ----------------
0000000000000981 (null) 0000000000002490
0000000000000985 (null) 0000000000002490
0000009000004523 0000000000001775 (null)
0000009000004521 0000000000001775 (null)
0000009000004218 0000000000001775 (null)
0000000000000980 (null) (null)
0000009000004914 0000000000002490
0000009000004917
0000009000004923 0000000000002490
所以我的期望结果如下:
NUM_RIF NDG
---------------- ----------------
0000000000000981 0000000000002490
0000000000000985 0000000000002490
0000009000004523 0000000000001775
0000009000004521 0000000000001775
0000009000004218 0000000000001775
0000000000000980 (null)
0000009000004914 0000000000002490
0000009000004914
0000009000004923 0000000000002490
情况是两个字段都可能有空值,或者为null,或者任何一个都是空/ null,但到目前为止两个字段都没有值,这意味着只有一个字段有值
答案 0 :(得分:1)
你试过吗
SELECT A.NUM_RIF, COALESCE(B.NDG, D.NDG) FROM ICGR1100 A
...
答案 1 :(得分:1)
大多数dbmss都有类似NVL的功能:
NVL(B.NDG, D.NDG) as NDG
如果不为空,将返回B.NDG,否则为D.NDG
NVL适用于Oracle,但可能与您的dbms等效。