你可以用
row_number()
. 以下假定排序规则不区分大小写(默认)
select t.col1, t.col2,
(case when count(*) over (partition by col1, col2, col3) = 1
then col1
else col3 + convert(varchar(255), row_number() over (partition by col1, col2, col3 order by col1) )
end) as new_col3
from t;
您可以轻松地将其转换为更新:
with toupdate as (
select t.*,
(case when count(*) over (partition by col1, col2, col3) = 1
then col1
else col3 + convert(varchar(255), row_number() over (partition by col1, col2, col3 order by col1) )
end) as new_col3
from t
)
update toupdate
set col3 = new_col3
where new_col3 <> col3;
您可以使用
COLLATE
,如果不是默认值。