Skip to content
Advertisement

SQL, operation between paired rows

I have a table t1

I need the output

I have been able to do it in a far from efficient way and would like a more satisfactory way. Thank you.

Advertisement

Answer

If you don’t want a floating point result, and you can have more than 2 b values for each a value, you can use a recursive CTE to generate the results:

Output:

Demo on dbfiddle

Note that this could be simplified if there was a column by which you could order the rows, that would remove the need to have the maxrows CTE.

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