Skip to content
Advertisement

Query to mark if a value falls between a set of values

I have two tables. And I want to check if a value from one, falls between any two numbers in the other table. For example the tables are:

From this I want to get the closest value above and below the number in table_b that exists in table_a, and then show this as a “match” and then also display the times of the closest value above and below this number. So the results should look something similar to:

If this doesn’t match, then it will show this in the results as not matching and the times will just be blank.

Thanks

Advertisement

Answer

You can do this by generating a table of before and after values for count and time in table_a, and then JOINing that to table_b such that table_b.count is between count_before and count_after:

Output:

Demo on dbfiddle

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement