If your hierarchy is only one level deep as in the sample data:
WITH(CTE_TableA) AS
(
SELECT JoinKey = COALESCE(ParentID,ID)
FROM tabA
)
SELECT ID = A.JoinKey, Value = SUM(B.Value)
FROM
CTE_TableA A
INNER JOIN
tabB B
ON A.JoinKey = B.ID
GROUP BY A.JoinKey
If you have more levels in your hierarchy, you can use a recursive CTE to flatten the hierarchy and then do the aggregation.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP