I have 2 tables with flex one-to-many relationship, meaning the parent side may not have any child:
Parent_table
id dim1 price
1 "abc" 1.00
2 "abc" 2.00
3 "def" 1.00
child_table:
id parent_id
1 1
2 1
The parent query is very straight forward:
select dim1, count(*), sum(price) from parent_table group by dim1
Now, I want to also add another measure call "child_count" to the above query. By doing this, I get a very good performant query but naturally, it double counts the parent columns due to the join:
select p.dim1, count(*), sum(p.price), count(distinct c.id) as child_count
from parent_table p
left outer join child_table c on c.parent_id = p.id
group by dim1
I believe the below query is exactly what I want, but these tables are extremely large and I cannot finish the query to verify:
select p.dim1, sum(*), sum(p.price), sum(child_count) as child_count
from parent_table p
left outer join (select c.parent_id, count(c.id) child_count from child_table c) child on p.id = child.parent_id
group by dim1
Do you have or do you know if Postgres has any smart way to achieve the query I wanted without doing a nested query to count the child records for each parent?
Edit: I made some fix to my mistake on the existing question to move the focus away from it. The changes are:
add 2 more rows to parent table
delete quantity column
change count(quantity) --> count(*)
Edit 2: hmm, I don't know why I forgot it exists, but this seems to work a lot better in Postgresql compared to joining once:
select p.dim1, count(*), sum(p.price),
sum(coalesce((select count(distinct id) from child_table where parent_id = p.id), 0) as child_count
from parent_table p
group by dim1
What do you think of it?