select * from bw bw left outer join az az on az.az_aa = case when bw_anr != 0 AND bw_anr_az != 0 then bw_anr_aa else 0 end and az.az_anr = bw.bw_anr and az.az_pos = bw.bw_anr_az