Question

I have a main table that I must get data from. I have a left outer join where the fields will match 40% of the time. And then I have another join where I need to match the data from table A with.

This is the SQL in pseudo code. This query won't work.

-- This is the part I want to do but doesn't work. AND H.COL3 = A.STATE????

I am working with IBM DB2.

SELECT DISTINCT
  APP_NO as app_no,
  A.STATE as state
  ...
  ... Fields
  ...
FROM 
  TABLE_A A
LEFT OUTER JOIN  
  TABLE_B HIST
ON
  HIST.COL1 = A.COL1
, TABLE_C B  
LEFT OUTER JOIN
  TABLE_D H  
ON
  H.COL2 = B.COL2
-- This is the part I want to do but doesn't work.
AND
  H.COL3 = A.STATE????
WHERE
  A.BRANCH = 'Data'
Was it helpful?

Solution

I think you could re-write it like this (but I could be reading your statement wrong)

FROM 
  TABLE_A A LEFT OUTER JOIN TABLE_B HIST ON
      HIST.COL1 = A.COL1
  LEFT OUTER JOIN TABLE_D H ON 
      H.COL3 = A.STATE
  LEFT OUTER JOIN TABLE_C B ON H.COL2 = B.COL2
WHERE
  A.BRANCH = 'Data'

Also, the IBM doco on this error states:

An ON clause associated with a JOIN operator or in a MERGE statement is not valid. Explanation:

Column references in an ON clause must only reference columns of tables that are in the scope of the ON clause.

So I could be mistaken, it just looks like the erronous ON clause when outer joining "H.COL3 = A.STATE" is out of scope of the On clause because the table A is not in that scope.

OTHER TIPS

What happens if you put the "AND H.COL3 = A.STATE" in your WHERE clause?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top