首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何写这样的sql语句或者函数,返回极端值

2012-03-04 
怎么写这样的sql语句或者函数,返回极端值表里有若干记录,有一列叫statestate包含F,E,I等级别,重要性依次递

怎么写这样的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 行)

热点排行
Bad Request.