SUM over distinct rows with multiple joins