I have table:
id doc_id loc_id price
1 null 13 100
2 12 13 40
3 12 null 300
4 null null 150
I have to select from table the lowest price, but in that order:
- If there is doc_id and loc_id equals to parameters then select this price (in our example it would be 40)
- If there isn't check if there is row with only equal doc_id, if yes then take price
- if there isn't check fi there is row with only equal loc_id, if yes then take price
- If there are all nulls take price with nulls
The tables are of course bigger (there are few inner joins), but that's how final table should look like. I got suggestion to use ROW_NUMBER() with partition over doc_id and loc_id, but i am not sure how to to this.