27,579
社区成员
发帖
与我相关
我的任务
分享
select case when number=2 then 2 else min(number)over() end
,case when number=2 then 2 else min(NULLIF(number,2))over() end
, *
from master.dbo.spt_values
where type='P'
and ((number>rand()*10 and number<100) or number=2)
create table tabletest (state int)
insert into tabletest
select 0 union all
select 1 union all
select 2 union all
select 3 union all
select 2
select case state when 2 then 2
Else MIN(state)
end as state
from tabletest
group by
你的案例不全,在最后一句group by中加你的分组字段就可以实现了SELECT [State]
FROM tablename
WHERE State = 2
OR ( NOT EXISTS ( SELECT *
FROM tablename
WHERE State = 2 )
AND tablename.State = ( SELECT MIN(State)
FROM tablename
)
);