SQL Dynamic SUM Calculation with GROUP BY