简体   繁体   中英

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.

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()

                                                       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

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. I want to display ranks, with duplicates but only 10 (including their duplicates).

     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.

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 . Because of your limited data, this example lists the top 5, otherwise all records are chosen. Change the WHERE clause to list top 10 in your site

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.

Since MySQL lacks these conveniences, we can use a VIEW or subqueries instead of CTEs. 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.

I'm going to assemble this all with VIEWs rather than subqueries, because I think it makes the logic of the query obvious:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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