日期:2014-05-17 浏览次数:20448 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-11-13 16:01:28
-- 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,[MDM_ID] int,[DEPT_ID] int,[HCPNAME] varchar(3))
insert [huang]
select 1,12345,1,'aaa' union all
select 2,12345,2,'aaa' union all
select 3,12345,3,'bbb' union all
select 4,12345,4,'bbb' union all
select 5,12345,5,'ccc' union all
select 6,12345,6,'eee'
--------------开始查询--------------------------
SELECT *
FROM HUANG A
WHERE EXISTS (SELECT 1 FROM (
select MDM_ID,HCPNAME
from [huang]
GROUP BY MDM_ID,HCPNAME
HAVING COUNT(HCPNAME)>1)B WHERE A.MDM_ID=b.MDM_ID AND a.HCPNAME=b.HCPNAME)
----------------结果----------------------------
/*
ID MDM_ID DEPT_ID HCPNAME
----------- ----------- ----------- -------
1 12345 1 aaa
2 12345 2 aaa
3 12345 3 bbb
4 12345&nb