I'm having a question how to resolve my problem.
I have procedure where inside i'm having this SELECT inside LEFT OUTER JOIN witch are returning me some values:
SELECT * FROM database1 data1
JOIN database2 data2 ON data2.id = data1.attr_id
JOIN database3 data3 ON data3.attr_id = data2.id
JOIN database4 data4 ON data4.objt_attr_id = data3.id
JOIN database5 data5 ON data5.stya_id = data4.id
AND data5.value = 1
JOIN database6 data6 ON data6.id = data5.sero_id
JOIN database7 data7 ON srv.id = data6.srv_id
JOIN database8 data8 ON data8.code IN ('CALC1','CALC2')
WHERE data1.ordet_id IN (data8.id)
As you can see he is looking for CALC1 and CALC2 And then search by their ID's in data1.ordet_id.
Naturally, he return me 2 entries witch is not correct.
How to make the check. When script will find record with CALC1, then he skips the check on CALC2, so the script is returning only 1 record by CALC1(not like now for both). And vice versa, if by CALC1 record wasn't found, he check the CALC2
I think you can take advantage of the analytical function
row_numberas following:-- Update
As per your below comment, You can also use
NOT EXISTSand combination ofCASE..WHENas following:Cheers!!