Hi,
I've got a dynamic table called @v_filter_table that is populated with some rows, ala:
Now, I have a query that executes, and one of the filter criteria in the query filters on the contents of this table - ala:
The problem is that I want the query to use it's own filter criteria if it can't match any rows in @v_filter_table with the location_id of 102 - since by making the filter criterion equal itself - mytable.row_id = mytable.row_id - it effectively removes itself from the WHERE clause - which is exactly what I want. E.g.
The problem is, the above doesn't work if there is more then one matching row in the @v_filter_table . E.g. If I removed one of the rows with a location_id of 102, it'll work - but otherwise it won't. The return of the COALESCE statement evidently cannot be more then one row.
How would I be able to refactor this query to acheive the result I need? Any help greatly appericated!
I've got a dynamic table called @v_filter_table that is populated with some rows, ala:
Code:
row_id location_id 1 102 2 102 3 104
Code:
SELECT {...} FROM {...} WHERE mytable.row_id IN (SELECT row_id FROM @v_filter_table WHERE location_id = 102)
Code:
SELECT {...} FROM {...} WHERE mytable.row_id IN (COALESCE((SELECT row_id FROM @v_filter_table WHERE location_id = 102),mytable.row_id))
How would I be able to refactor this query to acheive the result I need? Any help greatly appericated!
Comment