当前位置: 代码迷 >> Sql Server >> 一个显示重复数据的SQL解决办法
  详细解决方案

一个显示重复数据的SQL解决办法

热度:43   发布时间:2016-04-27 12:41:19.0
一个显示重复数据的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 twhere (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 tbgocreate table tb(    id int,    col1 varchar(5),    col2 varchar(5),    col3 varchar(5))goinsert into tbselect '1','A','B','C' union allselect '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'gowith 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,     col3from ctewhere v_count>1--ID Col1 col2 Col3--1    A    B    C--2    A    B    C--3    D    E    F--4    D    E    F
  相关解决方案