请大大帮忙看看一个筛选重复数据的问题

渴望私人航母 2013-11-20 09:21:12
表:
c1 c2
1 a
2 b
3 a
4 c
5 b
6 d
要获得如下结果:
c1 c2
1 a
3 a
2 b
5 b
应该怎么写呢?感激不尽~
...全文
120 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
秃驴子 2013-11-20
  • 打赏
  • 举报
回复


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
xxfvba 2013-11-20
  • 打赏
  • 举报
回复
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
秃驴子 2013-11-20
  • 打赏
  • 举报
回复
;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
唐诗三百首 2013-11-20
  • 打赏
  • 举报
回复

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

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧