Sum values from multiple rows into one row

前端 未结 2 793
野的像风
野的像风 2021-01-11 10:26

In SQL Server 2012, I have a table my_table that has columns state, month, ID, and sales.

My goal is to merge different rows

2条回答
  •  迷失自我
    2021-01-11 10:36

    Considering there should be an index on column id, this query would be a better solution:

    select state, month, id, sum(sales) Total
    from yourtable
    group by id, state, month
    order by id
    

提交回复
热议问题