开发者

SQL Get the 10 biggest numbers

I'm trying to retrive the 10 biggest numbers from a table called bonus.

B开发者_JS百科ut I'm getting this error message:

Incorrect syntax near 'LIMIT'

My code:

SELECT cust_nr, period1_bonus FROM bonus ORDER BY period1_bonus DESC LIMIT 10


Try this instead:

SELECT TOP 10 cust_nr, period1_bonus FROM bonus ORDER BY period1_bonus DESC

LIMIT <x> is a mySQL construct, not an MSSQL construct. TOP should work for you here.


Try SELECT TOP

SELECT TOP 10 cust_nr, period1_bonus 
FROM bonus 
ORDER BY period1_bonus DESC


I am not sure, but will

SELECT TOP 10 cust_nr, period1_bonus FROM bonus ORDER BY period1_bonus DESC

be working?

Edit: lol, I think i was right (now seeing the other answers suddenly) +1 for @Martin for asking for the rdbms:)


You seem to be using SQL Server.

In TSQL, the syntax is this:

SELECT  TOP 10 cust_nr, period1_bonus
FROM    bonus
ORDER BY
        period1_bonus DESC


Since this question is also tagged w/ c#, I'm assuming you're also interested in executing this in c#. Here is the linq-to-sql equivalent of the SQL code.

public IEnumerable<Bonus> GetHighestValues()
{
    var query = (from b in _context.bonus
                 take 10                     
                 orderby b.period1_bonus descending
                 select b);
    return query;
}

Edit - I see that the c# tag has now been removed from the question. Still, my answer might help you (or others).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜