create table 表
(c1 int,c2 varchar(10))insert into 表
 select 1,'a' union all
 select 2,'b' union all
 select 3,'a' union all
 select 4,'c' union all
 select 5,'b' union all
 select 6,'d'
select c1,c2
 from 表 a
 where exists(select 1 from 表 b 
              where b.c1<>a.c1 and b.c2=a.c2)
 order by c2,c1/*
c1          c2
----------- ----------
1           a
3           a
2           b
5           b(4 row(s) affected)
*/

解决方案 »

  1.   

    ;with testtb(c1,c2)
    as
    (
    select 1,'a'
    union all
    select 2,'b'
    union all
    select 3,'a'
    union all
    select 4,'c'
    union all
    select 5,'b'
    union all
    select 6,'d'
    )
    select * from testtb
    order by c2,c1
      

  2.   

    select a.* from tb a,
    (select  c2,count(*) a from tb group by c2 having count(*)>1) b
    where a.c2=b.c2
    order by a.c2,c1
      

  3.   

    select * from testtb where c2 in
    (
    select c2 from(
    select  c1,c2,
    ROW_NUMBER()OVER(partition by c2 order by getdate()) 'sn'
    from testtb) t
    where t.sn>1)
    order by c1