SQL Sum Multiple rows into one

前端 未结 5 2065
有刺的猬
有刺的猬 2021-01-04 03:51

I need some help with the SUM feature. I am trying to SUM the bill amounts for the same account into one grand total, but the results I am getting show my SUM column just m

5条回答
  •  醉酒成梦
    2021-01-04 04:22

    You should group by the field you want the SUM apply to, and not include in SELECT any field other than multiple rows values, like COUNT, SUM, AVE, etc, because if you include Bill field like in this case, only the first value in the set of rows will be displayed, being almost meaningless and confusing.

    This will return the sum of bills per account number:

    SELECT SUM(Bill) FROM Table1 GROUP BY AccountNumber
    

    You could add more clauses like WHERE, ORDER BY etc as needed.

提交回复
热议问题