按sum(score)排序desc返回错误的结果

时间:2012-12-03 15:17:12

标签: android sql sum sql-order-by

不知道造成这种情况的原因,但我试图获得该级别所有答案中得分最高的级别。

Cursor c = myDataBase
                .rawQuery(
                        "select level "
                                + "from answers where player ='"
                                + player
                                + "' group by level order by sum (score) desc",
                        null);

某个级别的所有答案都有分数值。它不断返回10级(10级),而实际上另一级别得分最高......如果我应用asc命令,它可以正常工作:s

3 个答案:

答案 0 :(得分:2)

如果您尝试获得的分数最高,请尝试使用having,如下所示:

            "select level "
            + "from answers where player ='"
            + player
            + "' group by level "
            + " having sum (score) = max(sum (score))";

编辑:

            "select level from (select level, max(levelscore) as maxscore from "+
                               +" (select level, sum (score) as levelscore, "
                               + " from answers where player ='"
                               + player
                               + "' group by level )"
            + " ) where levelscore = maxscore";

           "select level from "+
                   +" (select level, sum (score) as levelscore, "
                   + " from answers where player ='"
                   + player
                   + "' group by level )"
                   + "order by levelscore desc LIMIT 1";

答案 1 :(得分:1)

SELECT level, SUM( score ) AS scoresum
FROM answers
WHERE player = ?
GROUP BY level
ORDERBY scoresum DESC
LIMIT 0, 1

答案 2 :(得分:0)

为什么不这个陈述(更容易)

SELECT TOP 1 level 
FROM answers 
WHERE player = {player} 
ORDER BY score DESC
小心,如果你在“保存”玩家的第一个结果之前执行此查询,你将得不到任何结果......

塞尔