34,576
社区成员
发帖
与我相关
我的任务
分享
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
;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
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)
*/