日期:2014-05-19  浏览次数:20498 次

怎么写这样的sql语句或者函数,返回极端值
表里有若干记录,有一列叫state
state包含F,E,I等级别,重要性依次递减
现在我要返回重要性级别最高的一条记录
请问该怎么写?

------解决方案--------------------
create table tmp(id int,state varchar(10))
insert tmp select 1, 'D '
union all select 2, 'E '
union all select 3, 'F '
union all select 4, 'C '
union all select 5, 'D '
union all select 6, 'C '
union all select 7, 'B '
union all select 8, 'E '

select top 1 * from tmp
where state in ( 'F ', 'E ', 'D ', 'C ', 'B ')
order by case state when 'F ' then 1
when 'E ' then 2
when 'D ' then 3
when 'C ' then 4
when 'B ' then 5 end

drop table tmp

id state
----------- ----------
3 F

(所影响的行数为 1 行)