--查询指定条件下的重复次数
--测试数据
with tabname(id,name) as (
select 1,'name1' union all
select 1,'name1' union all
select 1,'name1' union all
select 1,'name2' union all
select 1,'name2' union all
select 1,'name3' union all
select 2,'name1' union all
select 2,'name1' union all
select 2,'name2' union all
select 2,'name3' union all
select 3,'name1')
select id,(name1+name2+name3) as 重复次数,name1,name2,name3 from(
select id,name from tabname
) as a
pivot(
count(name)
for
name in (name1,name2,name3)
) as b
结果:
select id,count(*) -1, count(name1) as name1, count(name2) as name2, count(name3) as name3 from tabname
group by id
having count(*) > 1