Hi,
I'm a bit surprised that this query is working in its current form. When I tried to run it i got the following:
-----------------------------------------------------------------------------------------------
SQL0338N An ON clause associated with a JOIN operator or in a MERGE statement is not valid.
Explanation:
An ON clause associated with a JOIN operator or in a MERGE
statement is not valid for one of the following reasons.
o The ON clause cannot include any subqueries.
o Column references in an ON clause must only reference columns
of tables that are in the scope of the ON clause.
o Scalar fullselects are not allowed in the expressions of an
ON clause.
o A function referenced in an ON clause of a full outer join
must be deterministic and have no external action.
o A dereference operation (->) cannot be used.
o A SQL function or SQL method cannot be used.
o The ON clause cannot include an XMLQUERY or XMLEXISTS
expression.
---------------------------------------------------------------------------------------
1. Are you sure this is a DB2 query? if not then this might be in the wrong place.
2. Assuming your tables are like that below.
-
A B
-
Seq Order Seq order
-
1 1 1 1
-
2 2 1 2
-
1 3
-
1 4
-
2 1
-
2 2
-
2 3
-
2 4
-
-
And the desired output is
-
-
Seq Order
-
1 4
-
2 4
-
-
try..
-
-------------------------------------------
-
select r2.seq, r2.ord from
-
(
-
select r1.seq, r1.ord from
-
(
-
select distinct b.seq, max(b.ord) from b
-
group by b.seq
-
) r1
-
left outer join a on r1."ord" = a."ord"
-
)r2;
-
-------------------------------------------
The advantage here is that you are not doing subselects for each row satisfying the join condition. Subselects are expensive their use should be minimized where possible.