日期:2014-05-16 浏览次数:20469 次
SELECT distinct a.A,a.B,a.C,a.D FROM 表名 a where Str(a.A)+Str(a.B)+Str(a.C)+Str(a.D) in (select Str(A)+Str(B)+Str(C)+Str(D) from person b group by A,B,C,D having count(*)>1)
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-02-18 07:59:30
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([ID] int,[A] int,[B] int,[C] int,[D] int,[E] int)
insert [huang]
select 1,1,1,2,4,1 union all
select 2,1,1,2,4,2 union all
select 3,1,2,2,4,3 union all
select 4,1,2,2,4,4 union all
select 5,2,1,3,5,5 union all
select 6,2,1,3,5,8 union all
select 7,2,3,3,5,9 union all
select 8,2,3,3,5,10 union all
select 9,2,3,3,5,11 union all
select 10,3,2,8,9,9 union all
select 11,3,2,8,9,10 union all
select 12,3,3,8,9,11 union all
select 13,3,3,8,9,12
--------------开始查询--------------------------
select a.A,stuff((select DISTINCT ','+CAST(B AS VARCHAR) from [huang] b
where b.A=a.A
for xml path('')),1,1,'') 'B',a.C,A.D
from [huang] a