親内のすべての子の値の合計を取得する SQL ステートメントを作成しようとしています。
SELECT parent, child, sum(val)
FROM table_a
GROUP BY parent, child
ORDER BY parent, child
これは私に与えています:
Parent1 Child1 123
Parent1 Child2 456
Parent2 Child1 789
Parent3 Child1 12
Parent3 Child2 345
私が望むのは、各親の親の下にあるものだけでなく、すべての子を取得し、レコードがない場合は値 0 を割り当てることです。たとえば、次のようになります。
Parent1 Child1 123
Parent1 Child2 456
Parent2 Child1 789
Parent2 Child2 0
Parent3 Child1 12
Parent3 Child2 345
GROUP BY 句でこれを行うことはできますか? または、サブクエリを使用する必要がありますか?