Domanda

I have a table table and I'm trying to show all the rows with the id in a certain list. If there are no rows with this id, then I would display null values.

Obviously, if these ids I want to select were in a different table, then the solution was just a right join statement.

The problem is that such table does not exists and the user provides the list of ids as input.

I'm trying to solve this using the right join (values ..) on ..=.. statement. But I'm unable to give a column name to the nested statement (with static values). And so I'm unable to write a valid on clause.

For instance, I have the table table:

id val    
-- --
0 0.1
2 -0.5
7 1.1

Then the user dinamically select a list of ids, which is not necessary contained in the list of ids of the table. For instance, if the user select 0,1,2,3, then I should diplay:

id val    
-- --
0  0.1
1  null
2  -0.5
3  null

I'm trying to do something like

select * from table right outer join (
    values (0),(1),(2),(3)
) as static_values on table.id = static_values[1]

Obviosuly static_values[1] is wrong, but I have to name the column to perform a join and I don't know how else to do it.

È stato utile?

Soluzione

You should be able to do:

select * 
  from table 
 right outer join (values (0),(1),(2),(3)) as static_values(id) 
    on table.id = static_values.id
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top