34,838
社区成员




create table tb(id int, value int)
insert into tb values(1, 20 )
insert into tb values(2, 20 )
insert into tb values(3, 25 )
insert into tb values(4, 25 )
insert into tb values(5, 30 )
insert into tb values(6, 35 )
insert into tb values(7, 40 )
select top 1 *
from tb a
where exists
(
select value from tb where value = a.value group by value having count(*) = 1
)
drop table tb
/*
id value
----------- -----------
5 30
(所影响的行数为 1 行)
*/
create table tb(id int, value int)
insert into tb values(1, 20 )
insert into tb values(2, 20 )
insert into tb values(3, 25 )
insert into tb values(4, 25 )
insert into tb values(5, 30 )
insert into tb values(6, 35 )
insert into tb values(7, 40 )
select a.* from tb a where value in
(select top 1 value from tb group by value having count(*) = 1 order by value)
drop table tb
/*
id value
----------- -----------
5 30
(所影响的行数为 1 行)
*/
select top 1 value from (select value from @t group by value having count(*)=1) t
declare @tb table (id int,[value] int)
insert into @tb select 1,20
insert into @tb select 2,20
insert into @tb select 3,25
insert into @tb select 4,25
insert into @tb select 5,30
insert into @tb select 6,35
insert into @tb select 7,40
select min(value) from @tb a
where not exists(select 1 from @tb where value=a.value group by value having count(1)>1)
create table tb(id int, value int)
insert into tb values(1, 20 )
insert into tb values(2, 20 )
insert into tb values(3, 25 )
insert into tb values(4, 25 )
insert into tb values(5, 30 )
insert into tb values(6, 35 )
insert into tb values(7, 40 )
select a.* from tb a where value in
(
select min(value) from
(
select value from tb group by value having count(*) = 1
) t
)
drop table tb
/*
id value
----------- -----------
5 30
(所影响的行数为 1 行)
*/
declare @t table(id int,value int)
insert @t select 1, 20
insert @t select 2 , 20
insert @t select 3 , 25
insert @t select 4 , 25
insert @t select 5 , 30
insert @t select 6 , 35
insert @t select 7 , 40
select top 1 id,value from @t where value in(select value from @t group by value having count(*)=1)
order by value asc
/*
id value
----------- -----------
5 30
(所影响的行数为 1 行)
*/
create table tb(id int, value int)
insert into tb values(1, 20 )
insert into tb values(2, 20 )
insert into tb values(3, 25 )
insert into tb values(4, 25 )
insert into tb values(5, 30 )
insert into tb values(6, 35 )
insert into tb values(7, 40 )
select min(value) from
(
select value from tb group by value having count(*) = 1
) t
drop table tb
/*
-----------
30
(所影响的行数为 1 行)
*/
select min(value) from
(
select value from tb group by value having count(*) = 1
) t