如何创建一个标识SQL Server 2016中重复变量的变量?

时间:2018-05-03 18:48:31

标签: sql sql-server

我有以下A列和B列,并希望添加一个额外的列C,以便将A列中的重复值标识为' DUP'。

2 个答案:

答案 0 :(得分:2)

使用带有case表达式的窗口函数

select *,
       (case when count(*) over (partition by colA) > 1
             then 'Duplicate' else 'Single' end) as Dup   
from table t; 

答案 1 :(得分:0)

首先,您创建新列c

alter table my_table add c varchar(10);

然后,仅更新重复行的值:

update my_table set c = 'DUP'
  where a in (
    select a from my_table group by a having count(*) > 1
    );