外部联接返回Nulls

时间:2010-07-16 05:21:56

标签: sql db2

嗨,我有2张桌子。我想列出

  1. table1中存在的所有记录 表2
  2. table2中的所有记录,其中table1中没有where条件
  3. 第二个条件中table1将返回空行,但我无法使查询正常工作。它只返回空行

    SELECT 
        A.CLMSRNO,A.CLMPLANO,A.GENCURRCODE,A.CLMNETLOSSAMT,
        A.CLMLOSSAMT,A.CLMCLAIMPRCLLOSSSHARE
    FROM 
        PAKRE.CLMCLMENTRY A 
    RIGHT OUTER JOIN (
        SELECT 
            B.CLMSRNO,B.UWADVICETYPE,B.UWADVICENO,B.UWADVPREMCURRCODE,
            B.GENSUBBUSICLASS,B.UWADVICENET,B.UWADVICEKIND,B.UWADVYEAR,
            B.UWADVQTR,B.ISMANUAL,B.UWCLMNOREFNO 
        FROM
            PAKRE.UWADVICE B 
        WHERE
            B.ISMANUAL=1
    ) r
    ON a.CLMSRNO=r.CLMSRNO
    
    ORDER BY 
        A.CLMSRNO DESC;
    

3 个答案:

答案 0 :(得分:0)

您使用的是哪种操作系统?

Table aliases are case sensistive on some platforms,这就是您的加入条件ON a.CLMSRNO=r.CLMSRNO失败的原因。

尝试A.CLMSRNO=r.CLMSRNO并查看是否有效

答案 1 :(得分:0)

我不理解你的第一次尝试,但我认为这基本上是你所需要的:

SELECT *
FROM TABLE1
INNER JOIN TABLE2
    ON joincondition

UNION ALL

SELECT *
FROM TABLE2
LEFT JOIN TABLE1
    ON joincondition
    AND TABLE1.wherecondition
WHERE TABLE1.somejoincolumn IS NULL

答案 2 :(得分:0)

我认为您可能希望删除子查询并将其列放入主查询中,例如

SELECT A.CLMSRNO, A.CLMPLANO, A.GENCURRCODE, A.CLMNETLOSSAMT, 
       A.CLMLOSSAMT, A.CLMCLAIMPRCLLOSSSHARE,  
       B.CLMSRNO, B.UWADVICETYPE, B.UWADVICENO, B.UWADVPREMCURRCODE, 
       B.GENSUBBUSICLASS, B.UWADVICENET, B.UWADVICEKIND, B.UWADVYEAR, 
       B.UWADVQTR, B.ISMANUAL, B.UWCLMNOREFNO     
  FROM PAKRE.CLMCLMENTRY A
       RIGHT OUTER JOIN PAKRE.UWADVICE B 
          ON A.CLMSRNO = B.CLMSRNO
 WHERE B.ISMANUAL = 1
 ORDER 
    BY A.CLMSRNO DESC;