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

SQL Server怎么查询前3行数据的最大值

2012-05-11 
SQL Server如何查询前3行数据的最大值?数据结构如下ID值112435465768查询出前三行数据的最大值,也就是5,SQ

SQL Server如何查询前3行数据的最大值?
数据结构如下
ID 值
1 1
2 4
3 5
4 6
5 7
6 8

查询出前三行数据的最大值,也就是5,SQL语句该怎么写?

[解决办法]

SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([ID] int,[值] int)insert [test]select 1,1 union allselect 2,4 union allselect 3,5 union allselect 4,6 union allselect 5,7 union allselect 6,8select top 1 * from(select top 3 * from test order by GETDATE())torder by [值] desc/*ID    值3    5*/
[解决办法]
SQL code
select max([值]) from (    select top 3 * from yourtable order by id asc) a 

热点排行