且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

合并 SQL Server 中的重复记录

更新时间:2023-02-05 19:30:46

两个步骤:1. 更新位置正确的记录,2. 删除位置错误的记录.

Two steps: 1. update the records with the correct locations, 2. delete the records with the wrong locations.

update mytable
set onhand = onhand + 
(
  select coalesce(sum(wrong.onhand), 0)
  from mytable wrong
  where wrong.location like ' %'
  and trim(wrong.location) = mytable.location
)
where location not like ' %';

delete from mytable where location like ' %';