-1

I have a table SalesDetails with the following schema:

CustomerID TotalDue ShippingAdress
1          100      aaa
1          200      aaa
1          300      bbb
2          100      ccc
2          400      ddd
2          700      ccc         

And I I need a query that will surmise the above table into a new table with the following schema:

CustomerID SumDue ShippingAdress
1          300      aaa
1          300      bbb
2          800      ccc
2          400      ddd

Currently I am using a 'Group By' on the CustomerID and SUM 'TotalDue'. But I do not know how to conditionally sum the TotalDue based on both CustomerID and ShippingAddress.

Any help would be much appreciated.

Kay
  • 175
  • 9
  • 1
    Do GROUP BY on *both* `CustomerID` and `ShippingAdress`. – Andreas Mar 14 '20 at 22:52
  • Does this answer your question? [Is it possible to GROUP BY multiple columns using MySQL?](https://stackoverflow.com/questions/1841426/is-it-possible-to-group-by-multiple-columns-using-mysql) – Andreas Mar 14 '20 at 22:54
  • Does this answer your question? [Using group by on two fields and count in SQL](https://stackoverflow.com/q/10380990/5221149) – Andreas Mar 14 '20 at 22:56

1 Answers1

1

You are looking for a simple GROUP BY:

select CustomerId, sum(TotalDue) as sumDue, ShippingAdress
from t
group by CustomerId, ShippingAdress
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786