mySQL“得到TOP 100得分”的查询让我发疯了

时间:2016-10-03 14:07:15

标签: mysql greatest-n-per-group

周五我问过this question,从那以后我一直在努力尝试一切没有成功的事情

我无法让这个工作

SELECT * FROM WorldFlowers_table GROUP BY device_id ORDER BY score DESC LIMIT 100 

然后回复我:

  • 排名前100位的100行

  • 已过滤,因此只显示每个device_id的最高得分

  • ORDER BY得分DESC

@ 1000111和Giorgos Betsos

一个人走进一个房间说,“请帮助,我疯狂地试图用我所有的力量解决一个简单的问题,任何人都可以在眨眼之间解决这个问题”

2个家伙转过身,射击脸上的那个人说“重复”

我筋疲力尽:(

///////// 来评论草莓评论

我尝试在SQLFiddle中构建它

CREATE TABLE WorldFlowers_table 
    (
     id int identity primary key, 
     timestamp varchar(20), 
     name varchar(30),
     score int,
     color varchar(30),
     flower varchar(30),
     device_id varchar(30),
    );

INSERT INTO WorldFlowers_table
(timestamp, name,score, color, flower, device_id  )
VALUES
('1475151826', 'RI-RI', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RO-RO', 46, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'ABC' ),
('1475151826', 'RI-RI', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RA-RA', 45, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'ABC' ),
('1475151826', 'RU-RU', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RE-RE', 44, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'DEF' ),
('1475151826', 'RY-RY', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RX-RX', 43, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RA-RA', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'DEF' );

但是只会在2中构建一次。

我正在寻找的回报将是

+------+------------+--------------------------------------------+
| id   | timestamp  | name  | score | color | flower | device_id |                               
+------+------------+--------------------------------------------+
| 2    | blabla     | RO-RO |  46   |  ...  |   ...  |   ABC     | 
| 5    | blabla     | RE-RE |  44   |  ...  |   ...  |   DEF     |
| 7    | blabla     | RX-RX |  43   |  ...  |   ...  |   XYZ     |
+------+-------+-------------------------------------------------+

。每次返回每个设备ID只有1个

。每个设备ID得分最高

。并且结果在ORDER BY得分DESC中给出

更新:来自scaisEdge的这个似乎工作正常

SELECT * FROM分数   WHERE(名称,分数)IN(SELECT name,MAX(score)                                   来自分数                                   GROUP BY名称
                                  ORDER BY得分DESC                                 )   ORDER BY得分DESC   限制100;

3 个答案:

答案 0 :(得分:0)

如果我不正确你想要前100名设备得分

for sha in $(git log --format=%H); do
  author="$(git show --format=%an $sha | head -n1)"

  removed_if_statements=$(git show $sha | grep -E '^\-(.* )?if \(' | wc -l)
  added_if_statements=$(git show $sha | grep -E '^\+(.* )?if \(' | wc -l)

  delta=$(( $added_if_statements - $removed_if_statements ))

  if [ $delta -gt 0 ]; then
    for x in $(seq 1 $delta); do
      echo $author
    done
  fi
done | sort | uniq -c | sort -rg

答案 1 :(得分:0)

MySQL在某些子查询运算符的子查询中不支持LIMIT:

[Branches.Branch_Employee].[endDate] IS NULL

https://dev.mysql.com/doc/mysql-reslimits-excerpt/5.6/en/subquery-restrictions.html

看看这里:

Problem with LIMIT & IN/ALL/ANY/SOME subquery

答案 2 :(得分:0)

SELECT id, timestamp, name, score, color, flower, device_id
    FROM WorldFlowers_table 
    WHERE (device_id, score) IN 
    (SELECT device_id, MAX(score) FROM WorldFlowers_table GROUP BY device_id)
ORDER BY score DESC LIMIT 100;