更新:重新格式化整个问题
我有一个包含个人F1比赛结果的数据库。每个驾驶员根据他们完成的位置获得积分。现在我想建立一个包含所有赛车的所有组合点的桌子。所以基本上是一个"驱动程序排名"表
我当然可以很容易地得到总结点,但是当多个驾驶员具有完全相同的总分时,我遇到的问题就出现了。在这些情况下,我需要检查哪一个驱动程序具有最佳(或更多)单个竞赛结果,因为他应该在结束表上方。
哦,在放置栏上" 0"意味着没有完成,这是最糟糕的结果。
简单示例:
Event | Driver | Placement | Points
--------------------------------------
Race 1 | Driver A | 1 | 3
Race 1 | Driver B | 2 | 2
Race 1 | Driver C | 3 | 1
Race 2 | Driver A | 3 | 3
Race 2 | Driver B | 2 | 2
Race 2 | Driver C | 1 | 1
Race 3 | Driver A | 0 | 0
Race 3 | Driver B | 0 | 0
Race 3 | Driver C | 2 | 2
并要求完成表格
Driver | Points
------------------
Driver C | 4
Driver A | 4
Driver B | 4
订单是因为Driver C有一个第一名并且第二名完成。驾驶员A有一个第一名和第三名完成,其中驾驶员B只有第二名完成。 (假设我的头很有效,那已经很晚了。)
简单示例http://sqlfiddle.com/#!9/16f9c5
期望的结果
| Driver | Points |
|------------------------|
| N. Hulkenberg | 43 |
| F. Massa | 43 |
| P. Wehrlein | 5 |
| D. Kvyat | 5 |
| M. Ericsson | 0 |
| P. Gasly | 0 |
| A. Giovinazzi | 0 |
| B. Hartley | 0 |
从小提琴数据中我删除了除问题案例之外的所有内容。每个人都有至少一个相同的积分,并且领带破坏者从简单的最佳完成比赛到最佳结束次数(四个第六名胜利优于第二名胜利,如Hulkenberg和Massa的情况)。< / p>
结果表是因为:
1. N. Hulkenberg - has four 6th place finishes
2. F. Massa - has two 6th place finishes
3. P. Wehrlein - has 8th place finish
4. D. Kvyat - has 9th place finish
5. M. Ericsson - has 11th place finish
6. P. Gasly - has 12th and 13th place finishes
7. A. Giovinazzi - has only one 12th place finish and nothing else
8. B. Hartley - has 13th place finish
所以真正有问题的领带破坏者是[Hulkenberg vs Massa]和[Gasly vs Giovinazzi]
任何帮助表示感谢。
答案 0 :(得分:2)
这应该可以做到没有太多模糊的伎俩。
SELECT
driver
,SUM(points) AS points
,CONCAT(GROUP_CONCAT(IF(placement = 0, 99, LPAD(placement,2,0)) ORDER BY IF(placement = 0, 99, placement) SEPARATOR ','), ',99') AS best
FROM
f1
GROUP BY
driver
ORDER BY
points DESC,
best
或者,如果您想要隐藏展示位置排序数据列:
SELECT
driver
,SUM(points) AS points
FROM
f1
GROUP BY
driver
ORDER BY
points DESC
,CONCAT(GROUP_CONCAT(IF(placement = 0, 99, LPAD(placement,2,0)) ORDER BY IF(placement = 0, 99, placement) SEPARATOR ','), ',99')
它只是利用字母数字排序来确定在点数相关的情况下的最佳位置。
答案 1 :(得分:0)
尝试自我加入。
select t1.driver, points, min_placement
from (select driver, sum(points) points from f1 group by driver) t1 inner join
(select driver, min(placement) min_placement from f1 group by driver) t2
on t1.driver = t2.driver
order by points desc, min_placement asc
在您的测试数据中,大多数司机的展示位置最低为零,因此您无法看到您正在寻找的结果。
答案 2 :(得分:0)
我认为你在最高成绩后至少需要第二次抢七:
SELECT
driver,
SUM(points) as points,
CASE WHEN MAX(placement) = 0 THEN 99 ELSE MAX(placement) END AS highest_finish,
-- or maybe this would work?
SUM(CASE WHEN placement <> 0
THEN POWER(20, placement) ELSE 0 END) AS weighted_finish
FROM f1
GROUP BY driver
ORDER BY points DESC, highest_finish
你可能也会幸运得到这样的表达式:
SELECT f1.driver,
MAX((100 - f1.placement) * (
SELECT count(*) FROM f1 f2
WHERE f2.driver = f1.driver
AND f2.placement = f1.placement
AND f2.placement > 0
)) AS highest_placement_weight
FROM f1
GROUP BY f1.driver