Make use of CROSS JOIN
, LEFT [OUTER] JOIN
and COALESCE
:
If you want a CROSS JOIN
, which is pretty unusual and may produce a lot of rows, it could look like this.
Even more aggressive after update: b
and d
are also optional now.
SELECT a.name AS a_name, e.name AS e_name, f.name AS f_name
,COALESCE(sum(c.quantity), 0) As sum_quantity
FROM a
CROSS JOIN e
CROSS JOIN f
LEFT JOIN b ON b.a_id = a.id
LEFT JOIN d ON d.e_id = e.id
AND d.f_id = f.id
LEFT JOIN c ON c.b_id = b.id
AND c.d_id = d.id
GROUP BY 1,2,3;
For lack of specification I join c
only if both b_id
and d_id
have a match.
Additional Q in comment
In your last answer you put a parenthesis after cross join, what does that do?
I quote the manual here:
Use parentheses if necessary to determine the order of nesting. In the absence of parentheses, JOINs nest left-to-right. In any case JOIN binds more tightly than the commas separating FROM items.