I want to get a row that matches the first matching "when" in a case expression in the 'on' clause of a left outer join, but instead I get rows from EVERY when that is matched.
The internet tells me this is impossible, that a case will always stop at the first matching when.
SELECT MILL_ORDER_NUMBER
,SHORTY_NAME
,PRIMARY_DEST
,ALT_DESTINATION
,CB.CDE_CNSUM_LOC as CB4V_CNSUM_LOC
,CB.CDE_DEST
,CB.NAM_CUST_SHTY
FROM HLFOR01A OA
left outer join (select CDE_CNSUM_LOC, CDE_DEST, NAM_CUST_SHTY from CSAR_CB4V0023) CB
on case
when ((OA.SHORTY_NAME = CB.NAM_CUST_SHTY) and (substring(OA.PRIMARY_DEST,1,1) < 'A') and (OA.PRIMARY_DEST = CB.CDE_DEST)) then 1
when ((OA.SHORTY_NAME = CB.NAM_CUST_SHTY) and (CB.CDE_DEST = (select min(dd.CDE_DEST) from CSAR_CB4V0023 dd where dd.NAM_CUST_SHTY = OA.SHORTY_NAME))) then 1
else 0 end = 1
where MILL_ORDER_NUMBER = '84220631'
If both when clauses exist, I get
MILL_ORDER_NUMBER SHORTY_NAME PRIMARY_DEST ALT_DESTINATION CB4V_CNSUM_LOC CDE_DEST NAM_CUST_SHTY
84220631 CMPNY1 5U 1641 00 CMPNY1 <-- matches 2nd when clause
84220631 CMPNY1 5U 1627 5U CMPNY1 <-- matches 1st when clause
If I comment out the 1st when clause I only get the first row.
If I comment out the 2nd when clause, I only get the 2nd row.
I don't understand why it doesn't stop at the first when clause that matches?