SQL命令按最高数量的"杀死除以死亡"

时间:2013-05-11 13:42:40

标签: mysql sql sql-order-by

我有一张桌子:
ID | KILLS | DEATHS
$ratio = $kills / $deaths;

如何订购字段,以便回显具有最高比率的ID?

3 个答案:

答案 0 :(得分:6)

WITH CTE AS
(
   SELECT T.*,
       Ratio = kills / deaths, 
       Rank = DENSE_RANK()OVER(PARTITION BY Id ORDER BY kills / deaths DESC) 
   FROM dbo.TableName T
)
SELECT * FROM CTE WHERE Rank = 1

答案 1 :(得分:2)

你试过这个:

select id from table order by kills/deaths desc limit 1;

如果(kills + 0.0)/deaths没有自动提升为浮点数,请尝试使用。{/ p>

同时尝试kills/(deaths+0.0001)以防止被零除..

答案 2 :(得分:1)

尝试

SELECT TOP 1 ID, cast(Kills/Deaths as float) FROM KillsDeaths
ORDER BY (Kills/Deaths) DESC