Skip to content
Advertisement

Compare table column with a range of values and take the position of that value from another table

I got stuck with this problem, please help if possible.

Suppose we have two tables, Table A and Table B. Table A looks like this:

And Table B looks like this:

In table B we have around 23 values for each date, now we need to match val1 of table A to val2 of tableB for each date and return its position in the row for example 0.1 on 2021-08-16 in Table A should look for a match in table B val2 and should return its position in the row (14 in case of 0.1)

I tried multiple ways (including row_number(), etc) but none of them is working. Many thanks in advance. I need an output like this

I am trying to use row_number so, that I can create a row number on TableB and then use Case statement with Table A to get the row_number which matches with val1, I tried similar queries like below but none of them are working the sequence of the rows get changed whenever I use ranking functions, as I need the sequence of the rows exactly as shown for the table B otherwise the position of the values will change

Advertisement

Answer

You can use simple left join and row_number()

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