22,209
社区成员
发帖
与我相关
我的任务
分享
select a.* from #t a
(select C,F,H from #t group by C,F,H having count(C)>1) v
where a.C=b.C
and a.F=b.F
and a.H=b.H
select * from (select *,count(*) (partition by c,f,h) as num from tb) a
where num>1
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-03-18 11:36:20
-- Verstion:
-- Microsoft SQL Server 2000 - 8.00.2055 (Intel X86)
-- Dec 16 2008 19:46:53
-- Copyright (c) 1988-2003 Microsoft Corporation
-- Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] int,[b] int,[c] int)
insert [tb]
select 1,2,3 union all
select 2,3,4 union all
select 3,3,4 union all
select 4,2,3 union all
select 5,1,2 union all
select 6,2,2
--------------开始查询--------------------------
select
*
from
tb t
where
exists(select 1 from tb where c=t.c and b=t.b and a<>t.a)
----------------结果----------------------------
/* a b c
----------- ----------- -----------
1 2 3
2 3 4
3 3 4
4 2 3
(所影响的行数为 4 行)
*/
create table #A
(
A int,
B int,
C int,
D int,
E int,
F int,
G int,
H int
)
--C列、F列、H列
insert into #A select 1,2,3,44,5,6,7,81
insert into #A select 11,2,3,41,5,6,7,81
insert into #A select 11,2,45,40,5,6,7,81
insert into #A select 12,2,53,44,5,6,7,81
insert into #A select 13,2,3,46,5,6,7,81
insert into #A select 14,2,45,48,5,6,7,8
insert into #A select 15,2,38,49,5,6,7,8
insert into #A select 16,2,30,40,5,6,7,8
select A1.* from #A A1 join
(
select C,F,H from #A group by C,F,H having count(C)>1
) A2
on A1.C=A2.C
and A1.F=A2.F
and A1.H=A2.H
A B C D E F G H
----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 2 3 44 5 6 7 81
11 2 3 41 5 6 7 81
13 2 3 46 5 6 7 81
(3 行受影响)
select
*
from
(结果集表) t
where
exists(select 1 from tb where c=t.c and f=t.f and h=t.h and a<>t.a)
select * from tb group by C,F,H having count(C)>1 and count(F))>1 and count(H)>1
select * from tb having(count(C))>1 and having(count(F))>1 and having(count(H))>1