从数据库获取最高的5个值 [英] Getting Highest 5 values from database

查看:89
本文介绍了从数据库获取最高的5个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一张桌子,存储学生的百分比.我想从中获得最高的列表(获得最高3%的获得学习的学生).所以哪个查询会更好.实际上我已经尝试过max和top函数,但它们不适合.max仅给出一个值并且top将仅给出数据库中的前3条记录.
所以如何获得最高的3条记录.

Hello All,
I have a table which stores the percentage of students. I wabt to get the topper list from it(getting highest 3 percentage acquired students).so which query will be better for it.actually i have tried max aand top functions but they are not suitable.max will give only one value and top will give only first 3 records from database.
so how to get highest 3 records.

推荐答案

SELECT TOP 5...



????



????


尝试:
SELECT <fields> FROM <tablename> WHERE <condition> LIMIT <startnumber>,<count> ORDER BY <field> ASC



或者(我没有注意到SQL2005不支持LIMIT):



Or (I hadn''t noticed SQL2005 didn''t support LIMIT):

SELECT Rank, ID, Percent FROM
    ( SELECT Rank = ROW_NUMBER() OVER (ORDER BY Percent), ID
      FROM myTable
    ) i 
WHERE 
    Rank BETWEEN 1 and 5 
ORDER BY 
    Rank

我还没有测试过,但是应该可以.

I haven''t tested it, but it should work.


如何将TOPORDER BY结合在一起.
伪代码:
How about combining TOP with ORDER BY.
Pseudo code:
SELECT TOP 3 FROM your_table ORDER BY column_to_sort


这篇关于从数据库获取最高的5个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆