with a as (
select
1 as id,
1 as value
),
b as (
select
1 as id,
1 as a_id,
1 as value
union all select 2, 1, 2
union all select 3, 1, 3
union all select 4, 1, 3
),
d as (
select
1 as id,
1 as b_id,
1 as value
union all select 2, 1, 2
union all select 3, 1, 3
)
select
a.id,
b.id,
b.value as b_value,
d.id
from a
left join b on a.id = b.a_id
left join d on b.id = d.b_id;
Is it possible to calculate sum(b.value)
as 9 instead of 11?
I've been trying to do it like this, but with no success.
sum(b.value) * (count(distinct b.id)::decimal / count(*))
will give 7.3 instead of 9.