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
Question
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 id
s 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 id
s 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 value
s). 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 id
s, which is not necessary contained in the list of id
s 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.
Solution
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