Product of two querysets with a parameter in where clause of second result
Here is the query that can be used to get desired result

select ta.id,ta.related_a_id,tc.c_field1 as cf1,sum(td.c_field3) as cf3
from tableA ta
inner join tablec tc on tc.a_id in (ta.id,ta.related_a_id)
inner join tabled td on td.id = tc.d_id
group by cf1, ta.id order by ta.id;