tags:

views:

50

answers:

3

Hi I have 2 tables. I want to list

  1. all records in table1 which are present in table2
  2. all records in table2 which are not present in table1 with a where condition

Null rows will be returned by table1 in second condition but I am unable to get the query working correctly. It is only returning null rows

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;
A: 

Which OS are you using ?

Table aliases are case sensistive on some platforms, which is why your join condition ON a.CLMSRNO=r.CLMSRNO fails.

Try with A.CLMSRNO=r.CLMSRNO and see if that works

jfoucher
Hi I am using DB2. It seems to me its because of null rows being filtered out completely.Edit----- Ok I figured out the query is not right as I want results with combination of both tables
Popo
If you figured it out, please edit your post saying so.
Nubsis
A: 

I'm not understanding your first attempt, but here's basically what you need, I think:

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
Cade Roux
Hi problem with my query is that it is only returning correct rows from table2. But all the rows from table1 present in table 2 are not returned.Would UNION remove duplicates? Also, I have to make the number of columns same in case of UNION.
Popo
If the rows from table1 also in table2 are not returned, you have a problem in your join condition in the INNER JOIN. UNION removes duplicates, UNIONA ALL does not. If you need a different schema for the two sets, then, by definition they need to be two different sets and you cannot use UNION or get them in a single set.
Cade Roux
A: 

I think you may want to remove the subquery and put its columns into the main query e.g.

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;
onedaywhen
Thanks for the reply onedaywhen, table1 is clmclmentry while table2 is uwadvice. By your solution the first part of the query (all columns of table 1) are having nulls. The rest is correct though.
Popo
Try moving the `ISMANUAL = 1` predicate to the `WHERE` clause (I'v edited my answer accordingly).
onedaywhen