Skip to content
Advertisement

I have 3 table and need exclusive left join

Table_1:

Table_2:

Table_3:

Using 2 left joins I got this:

But I do group over the first column and 8 is accumulated twice, I wish to get:

I also wish to avoid UNION ALL because my first table is actually a complex query, I wish to avoid its recalculation.

https://dbfiddle.uk/?rdbms=postgres_12&fiddle=d6012e281e3ba43487b1bfec85fd886c

Advertisement

Answer

I am not sure if that works without UNION ALL. For example, all answers here (SQL left join two tables independently) work with it.

So, my solution is:

demo:db<>fiddle


Edit: Found a way using GROUPING SETS:

demo:db<>fiddle

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement