The expected result can be achieved by a "self-join" through the ChildID column, like this:
select
f.ParentID
, count(distinct o.ParentID) as other_parents
from mytable as f
left join mytable as o on f.ChildID = o.childid
and f.ParentId <> o.ParentID
group by
f.ParentID
;
See
http://sqlfiddle.com/#!18/08916/1"
This can be taken a little further by including string_agg (version dependent) so that you list out the "other" parents in a comma separated list:
select
f.ParentID
, count(distinct o.ParentID) as count_parents
, STRING_AGG(o.ParentID,',') AS other_parents
from mytable as f
left join mytable as o on f.ChildID = o.childid
and f.ParentId <> o.ParentID
group by
f.ParentID
;
see:
SQL Fiddle[
^]
ParentID count_parents other_parents
1001 1 1003
1002 0 (null)
1003 1 1001