select * from bw bw left outer join az az on az.az_aa = case when bw_aa = 0 AND bw_ak != 0 then bw_aa else 0 end and az.az_anr = bw.bw_ak and az.az_pos = bw.bw_az