做项目经常会碰到重复数据处理的需求,当需求是标记相邻重复的数据,整理以下两种方式
--方法1:链接,分组出有重复的资料,再次链接原表中数据,下面是语法
select * from t a
left join (
select t.t1,t.t2
from t
group by t.t1,t.t2
having COUNT(1)>1
) b on a.t1=b.t1 and a.t2=b.t2
--方法2:利用函数
select *,count(1) over(partition by t.t1,t.t2)
from t
--下面展示例子
--首先创建模拟临时数据表
if OBJECT_ID('tempdb..#123')is not null drop table #123
create table #123(
staff varchar(8),
cardtime datetime
)
insert into #123
(staff,cardtime)
values
('a','2016-03-09 08:00'),
('b','2016-03-09 08:00'),
('a','2016-03-09 08:00