PROBLEM
I was just wondering on how create a query that in such a way it will check if the column is in between in a reference table.
such as
SELECT *
FROM Table1 WHERE Column1 BETWEEN ( SELECT Column1 , Column2 FROM TABLE2 )I just don't know how to implement it in a correct way.
SOLUTION
If you can have overlapping ranges in
Table2, and all you want are (unique) Table1 records that are in any range in Table2, then this query will do it.SELECT *
FROM Table1
WHERE EXISTS (
SELECT *
FROM Table2
Where Table1.Column1 BETWEEN Table2.Column1 and Table2.Column2)You can also solve this using JOINs, if the ranges in Table2 are not overlapping, otherwise you will need to use either
DISTINCT or ROW_NUMBER() to pare them down to unique Table1 records.
No comments:
Post a Comment