日期:2014-05-17  浏览次数:20402 次

请大大帮忙看看一个筛选重复数据的问题
表:
c1 c2
1  a 
2  b
3  a 
4  c
5  b
6  d
要获得如下结果:
c1 c2
1  a
3  a
2  b
5  b
应该怎么写呢?感激不尽~

------解决方案--------------------

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)
*/

------解决方案--------------------
;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