得到每个负载的最大数量#并且只有前20个

时间:2011-09-30 21:51:09

标签: sql sql-server

使用以下代码:

SELECT        SUM(LD_NUM) AS Expr1, LD_NUM
FROM            Bale
GROUP BY LD_NUM

returns Expr1 = 74987 and LD_NUM = 4411
returns Expr1 = 61768 and LD_NUM = 4412
returns Expr1 = 75021 and LD_NUM = 4413

等..

如果我74987/4411 = 17,这给了我每LD_NUM的计数

有没有办法返回关系(17,4411),(14,1412),(17,4413) 并获得或命令'Expr1'进入前20名?

希望这可以自那以后。

3 个答案:

答案 0 :(得分:1)

SELECT TOP 20 SUM(LD_NUM) AS Expr1, LD_NUM, COUNT(LD_NUM) AS RecordCount
    FROM Bale
    GROUP BY LD_NUM
    ORDER BY Expr1 DESC

不确定您是否需要SUM用于任何其他目的。它可以很简单:

SELECT TOP 20 LD_NUM, COUNT(LD_NUM) AS RecordCount
    FROM Bale
    GROUP BY LD_NUM
    ORDER BY RecordCount DESC

答案 1 :(得分:1)

您不需要进行任何计算来计算,

count(LD_NUM)

就是所需要的

答案 2 :(得分:0)

试试这个

SELECT TOP 20 Expr1/LD_NUM,LD_NUM
FROM
(
   SELECT        SUM(LD_NUM) AS Expr1, LD_NUM
     FROM            Bale
    GROUP BY LD_NUM
) xx
ORDER BY xx.expr1