我在MySQL中正在处理查询的以下部分。
SELECT
MAX(CAST(MatchPlayerBatting.BatRuns AS SIGNED)) AS HighestScore
FROM
MatchPlayerBatting
它返回正确的结果。但是,还有另一列我需要它来解决。
也就是说,如果找到的最大值在“ BatHowOut”内的值也为“ not out”,则其结果应显示为例如96 *而不是96 *。
怎么办呢?
为了帮助使数据具体化,请考虑以下两种情况:
BatRuns BatHowOut
96 not out
96 lbw
BatRuns BatHowOut
96 not out
102 lbw
对于第一个数据,答案应为'96*'
; 第二,'102'
。
如何按降序排列分数并仅选择第一条记录呢?
select concat(BatRuns , case when BatHowOut = 'not out' then '*' else '' end)
from mytable
order by cast(BatRuns as signed) desc,
(case when BatHowOut = 'not out' then 1 else 2 end)
limit 1;
样品在这里。
如果您想为每个玩家找到最高的分数,这是一个可能并不优雅但非常有效的解决方案。
select PlayerID,
case when runs != round(runs)
then concat(round(runs),'*')
else
round(runs)
end highest_score
from (select PlayerID,
max(cast(BatRuns as decimal) +
case when BatHowOut = 'not out' then 0.1 else 0 end
) runs
from MatchPlayerBatting
group by PlayerID) max_runs;
这利用了一个事实,即运行永远不能是分数,只能是整数。当最高分并列且其中一个未败时,在未败分上加0.1将使它成为最高分。以后可以将其删除并与*串联。
样品在这里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句