日期:2014-05-18  浏览次数:20603 次

一个显示重复数据的SQL
表TEST四个字段ID,A,B,C如下显示数据
ID A B C
1 A B C
2 A B C
3 D E F
4 D E F
5 G H I
6 J K L
怎么样一条SQL显示出A,B,C这三列重复数据
ID A B C
1 A B C
2 A B C
3 D E F
4 D E F


------解决方案--------------------
SQL code
select * from test t
where (select count(1) from test where a=t.a and b=t.b and c=t.c)>1

------解决方案--------------------
SQL code

if OBJECT_ID('tb','U') is not null drop table tb
go
create table tb
(
    id int,
    col1 varchar(5),
    col2 varchar(5),
    col3 varchar(5)
)
go
insert into tb
select '1','A','B','C' union all
select '2','A','B','C' union all 
select '3','D','E','F' union all 
select '4','D','E','F' union all 
select '5','G','H','I' union all 
select '6','J','K','L'
go
with cte as 
(
    select 
        ID,
        col1,
        col2,
        col3,
        COUNT(1) over(partition by col1,col2,col3) as v_count
    from tb A
) 
select 
     id,
     col1,
     col2,
     col3
from cte
where v_count>1

--ID Col1 col2 Col3
--1    A    B    C
--2    A    B    C
--3    D    E    F
--4    D    E    F