这不是要求如何使用SUM()和GROUP BY
我在tableA中有两行
ID VALUE
1 100
1 200
我想要tableA:
ID VALUE
1 300
请注意,我想
这与merge
功能有关吗?
我只想在tableA上工作,不想创建任何新表。
答案 0 :(得分:2)
MERGE
:
;with cte as (
select t.*,
row_number() over (partition by id order by id) as rn,
sum(value) over (partition by id) as total_value
from your_table t
)
merge into cte as t
using cte as t2
on (
t.id = t2.id
and t.rn = t2.rn
and t.rn = 1
)
when matched then update set t.value = t2.total_value
when not matched by source then delete;