0

我正在使用 MYSQL 并尝试在 MYSQL 的能力范围内工作以进行排名。

我的查询是:

    SELECT petz.s_name,
       petz.breed,
       a.num,
       sum(a.rank) AS rank
FROM wins_conf a
JOIN
  (SELECT DISTINCT rank
   FROM wins_conf
   ORDER BY rank DESC LIMIT 10) b ON a.rank = b.rank
JOIN petz ON a.num=petz.num
GROUP BY petz.num
ORDER BY petz.breed,
         rank DESC

返回结果:

                                                                  sum(Rank)
INSANITY'S ACE OF SPADES           Collie          1026           58
INSANITY'S SAVE ME                 Collie          1000           31
STAR GAZER'S BEAUTIFUL LIES        Collie          1039           24
BANYON'S ALL IS FORGIVEN           Collie          1009           19
FELIXTOWE CHERRY BLOSSOM           Collie          1214           18
KE'S PRICELESS FIGUREINE           Collie          1004           13
NOVABLUE'S LOVES UNENDING LEGACY   Collie          1211           12
STAR GAZER'S WARRIOR OF MY HEART   Collie          1059            9
INSANITY'S BE MINE                 Collie          1028            9
STAR GAZER'S A WILDCAT'S REVENGE   Collie          1040            5
KE'S TRICKS OF THE TRADE           Collie          1005            5

记录 1059 (STAR GAZER'S WARRIOR OF MY HEART) 返回 9 作为排名,但是根据数据库中的 sum() 记录,它应该是 12

                                                       Rank
conf    33    13    1059    Best of Breed    0    0    5    0   2
conf    78    3139  1059    Best of Breed    0    0    4    0   2
conf    82    2518  1059    Best of Breed    0    0    1    0   2
conf    81    13    1059    Best in Specialty0    0    1    0   2
conf    79    13    1059    Best of Breed    0    0    1    0   2

通过一些调查,我发现如果 1 大于或等于 4,它只会看到排名列的 sum() 的最后 3 条记录

有关如何纠正此问题的任何建议?


编辑/更新回复 AgRizzo 我刚刚删除了全名和品种以便于阅读,这就是我想要的,排名明智。我想显示排名,重复但只有 10 个(包括它们的重复项)。

     num          rank
1    1026         58
2    1000         31
3    1039         24
4    1009         19
4    1214         19
5    1004         13
6    1211         12
6    1059         12
7    1028          9
8    1005          5
8    1040          5
9    1010          3
10    1276          1

我在这里设置了一些基本数据:http ://sqlfiddle.com/#! 2/7e2992 如上所示,它缺少一些绒毛内容,但排名中不需要这些内容。

4

3 回答 3

0

这是一个排名的变化

SELECT s_name
    , breed
    , num
    , @denserank := IF(@prevrank = rank, @denserank, @denserank + 1 ) as DenseRank
    , @prevrank := rank AS rank
FROM (
  SELECT petz.s_name AS s_name
      , petz.breed AS breed
      , a.num AS num
      , sum(a.rank) as rank
  FROM wins_conf a
  JOIN petz 
    ON a.num=petz.num
  WHERE petz.breed = 'Collie' 
  GROUP BY petz.s_name, petz.breed, a.num
  ORDER BY petz.breed, rank DESC) AS temp1
JOIN (SELECT @prevscore := NULL, @denserank := 0) AS dummy
WHERE @denserank < 5

它在 SQLFiddle http://sqlfiddle.com/#!2/7e2992/7上。由于您的数据有限,此示例列出了前 5 个,否则将选择所有记录。更改 WHERE 子句以列出您网站中的前 10 个

于 2013-10-11T12:46:15.870 回答
0

尝试这个

   select petz.s_name, petz.breed, a.num, sum(a.rank) as rank
   from wins_conf a
   JOIN petz ON a.num=petz.num 
   GROUP BY petz.num 
   ORDER BY petz.breed, rank DESC LIMIT 10
于 2013-10-09T18:05:17.640 回答
0

在其他 RDBMS 中,您可能会使用 CTE 来计算每个petz' 总分(您的SUM(rank)),然后使用 DENSE_RANK 函数根据这些分数对它们进行排名。

由于 MySQL 缺乏这些便利,我们可以使用 VIEW 或子查询来代替 CTE。DENSE_RANK 可以使用会话变量来计算,如@AgRizzo 的答案,或者只是一 (1) 加上比特定分数“更好”的不同分数的计数。

我将使用 VIEWs 而不是子查询来组装这一切,因为我认为它使查询的逻辑显而易见:

SET SESSION sql_mode='ANSI';

-- Compute each petz' score
CREATE OR REPLACE VIEW scorez AS
  SELECT "num", SUM("rank") AS "score"
    FROM wins_conf
GROUP BY 1;

-- Compute each scored petz' DENSE_RANK
CREATE OR REPLACE VIEW standingz AS
   SELECT my."num", 
          my."score",
          COUNT(DISTINCT their."score") + 1 AS "rank" -- DENSE_RANK
     FROM scores my
LEFT JOIN scores their
          ON their.score > my.score
 GROUP BY 1, 2;

-- Now fetch the full result set
    SELECT standingz.rank, petz.*
      FROM petz
INNER JOIN standingz
           ON petz.num = standingz.num
  ORDER BY standingz.rank ASC;
于 2013-10-11T14:00:08.273 回答