Problemet er, at store
med id 1 har to tilsvarende poster i store_sub
tabel, hvilket betyder, at qty
feltet vises to gange for butik 1. Du skal summere de 2 tabeller separat i underforespørgsler og sammenføje de summerede versioner:
select t1.department, t1.sqty, t2.ssubqty
from
(select department, sum(qty) as sqty from store group by department) t1
left join
(select s2.department, s1.sum(sub_qty) as ssubqty
from store_sub s1
inner join store s2 on s1.parent_id=s2.id
group by s2.department) t2 on t1.department=t2.department