我认为家庭是购物者的等级制度。因此,我建议使用group by
和窗口函数:
select family_id, shopper_id, product_id,
sum(quantity) as total_quantity_shopper,
sum(sum(quantity)) over (partition by family_id, product_id) as total_quantity_family
from products
group by family_id, shopper_id, product_id