日期:2014-05-17 浏览次数:20535 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-05-23 16:50:15
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([CTYPE] int,[CCOMMEN] int,[CFIELD] int,[CFIELDID] int,[COFFSE] int)
insert [huang]
select 1,1,1,1,1
UNION ALL
select 1,1,1,1,1
UNION ALL
select 2,1,1,1,1
UNION ALL
select 1,2,1,1,1
--------------开始查询--------------------------
SELECT DISTINCT * FROM [huang] a
WHERE EXISTS (SELECT 1 FROM (
select [CTYPE],[CCOMMEN],ROW_NUMBER()OVER(PARTITION BY CTYPE,CCOMMEN,CFIELD,CFIELDID,COFFSE ORDER BY CTYPE,CCOMMEN)[count]
from [huang])b WHERE a.[CTYPE]=b.[CTYPE] AND a.[CCOMMEN]=b.[CCOMMEN] AND b.[count]>1)
----------------结果----------------------------
/*
CTYPE CCOMMEN CFIELD CFIELDID COFFSE
----------- ----------- ----------- ----------- -----------
1 1 1 1 1
*/
select * from tb t
where exists(select * from tb
where CTYPE=t.CTYPE and CCOMMEN=t.CCOMMEN
and (CFIELD<>t.CFIELD or CFIELDID<>t.CFIELDID or COFFSE<>t.COFFSE))