Home > Back-end >  PostgresSQL avoiding cartesian product
PostgresSQL avoiding cartesian product

Time:02-01

I am trying to add a column to an existing table. Basically this is how it looks:

Table 1
value1
value2
value3
value4

Tab2
max1 min1 output1
max2 min2 output2
max3 min3 outpu3

So in function on the interval (min, max) in which value is present, there is an output.

My current code is the following

Select Table1.*, 
     case when value < max and value > min then output
     end new_column
 from Table1, Table2

But I end up getting several times the same row (exactly the same). How can I avoid this, or what I am doing wrong ?

This is what I expect to obtain at the end:

Table
value1 output1
value2 output4
value3 output1
...

Thank you

CodePudding user response:

Using a join

 select t1.*, t2.output
 from Table1 t1
 join Table2 t2 on t1.value < t2.max and t1.value > t2.min
  • Related