Evgeniy Kleban Evgeniy Kleban - 1 month ago 5
SQL Question

SQL query combine select statements

I have SQL query like follow:

SELECT *r1.val, r2.prevVal
FROM (Select statement) AS r1,
(Select statement) AS r2
WHERE
r1.object_id = r2.object_id


It work, problem is, when
r2
select statement return nothing, therefore, whole statement return nothing, because it can't execute
r1.object_id = r2.object_id
.

How could I reproduce the same statement, which will output values, if there are values in select statement
r1
, and no values in select statement
r2
? So I can access value
r1.val
.

Answer

Use a LEFT Join like:

SELECT *r1.val, r2.prevVal
FROM (Select statement) AS r1
LEFT JOIN (Select statement) AS r2 ON r1.object_id = r2.object_id    ---------------------Formatted as code