This is hard to explain but I'll do my best. So to make it simple, I have 3 tables with the following format:
Creator table
id | name
1 | jack
2 | doge
3 | cardano
roads
id | creator_id | length
1 | 1 | 3
2 | 1 | 2
3 | 3 | 9
4 | 1 | 3
Blocks
id | creator_id | length
1 | 1 | 4
2 | 1 | 4
3 | 3 | 5
4 | 1 | 3
I can't seem to pick out the rows into columns, otherwise I would be posting here but the query I use to bring the data together is:
select name, layer, sum(length)
from ( SELect a.name, 'road routes'::text as layer, length
from creator a
LEFT JOIN road_routes b on a.id = b.creator
where a.id in (73, 35, 33, 31)
group by a.name, layer, length
union
SELECT a.name, 'block routes'::text as layer, length
from creator a
LEFT JOIN block_route b on a.id = b.route_creator
where a.id in (73, 35, 33, 31)
group by a.name, layer, length) x
group by layer, name
The expected result and what I need is a table with the following output:
type | jack | doge | cardano | total
roads | 8 | 0 | 9 | 17
blocks | 11 | 0 | 5 | 16
How to sort the data like this?