简体   繁体   English

MYSQL领带处理未返回正确的结果

[英]MYSQL tie handling not returning proper results

I'm working with MYSQL and trying to work within the ability of MYSQL of ties for ranking purposes. 我正在与MYSQL合作,并试图在MYSQL的联系能力范围内进行排名。

my query is: 我的查询是:

    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

which returns the results: 返回结果:

                                                                  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

record 1059 (STAR GAZER'S WARRIOR OF MY HEART) returns 9 as the rank, however it should be 12 based on the records within the DB that are being sum() 记录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

With some investigating i've found that it will only see the last 3 records to sum(), of the rank column, if the 1's are great than or equal to 4 通过一些调查,我发现如果1大于或等于4,它将仅看到rank列的sum()的最后3条记录。

Any suggestions on how to correct this? 关于如何纠正这一点的任何建议?


EDIT/UPDATE in reply to AgRizzo I've just removed the full names and breed for easier reading, this is what I'm wanting, rank wise. 编辑/更新以回复AgRizzo我刚刚删除了全名,并且为了便于阅读而进行了繁殖,这是我想要的,排名明智。 I want to display ranks, with duplicates but only 10 (including their duplicates). 我想显示仅包含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

I setup some basic data here: http://sqlfiddle.com/#!2/7e2992 It's missing some of the fluff content as seen above, but that content isn't needed within the ranking. 我在此处设置了一些基本数据: http : //sqlfiddle.com/#!2 /7e2992如上所示,它缺少了一些绒毛内容,但是在排名中并不需要该内容。

try this 尝试这个

   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

Here is a variation with a ranking 这是排名的变化

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

It is here on SQLFiddle http://sqlfiddle.com/#!2/7e2992/7 . 它在SQLFiddle上http://sqlfiddle.com/#!2/7e2992/7上 Because of your limited data, this example lists the top 5, otherwise all records are chosen. 由于您的数据有限,此示例列出了前5名,否则将选择所有记录。 Change the WHERE clause to list top 10 in your site 更改WHERE子句以列出网站中的前10名

In other RDBMS, you would perhaps use a CTE to compute each petz ' total score (your SUM(rank) ) and then the DENSE_RANK function to rank them according to those scores. 在其他RDBMS中,您可能会使用CTE计算每个petz的总得分(您的SUM(rank) ),然后使用DENSE_RANK函数根据这些得分对其进行排名。

Since MySQL lacks these conveniences, we can use a VIEW or subqueries instead of CTEs. 由于MySQL缺乏这些便利,因此我们可以使用VIEW或子查询代替CTE。 DENSE_RANK may be computed with session-variables, as in @AgRizzo's answer , or as simply one (1) plus the count of distinct scores "better" than a particular score. DENSE_RANK可以使用会话变量来计算,如@AgRizzo的回答 ,或者简单地将一(1)加不同分数的计数“比特定分数好”。

I'm going to assemble this all with VIEWs rather than subqueries, because I think it makes the logic of the query obvious: 我将使用VIEW而不是子查询来组装所有这些,因为我认为这使查询的逻辑显而易见:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM