MAX有额外的标准,包括分组

时间:2013-08-21 10:39:55

标签: mysql

继续这个问题(MAX with extra criteria),我被告知我需要打开一个新问题以获取更多信息。

上述问题涉及获得玩家的最高分,如果最高分在“BatHowOut”字段中的值为“not out”,则应显示为96 *而不是96。

我需要能够通过PlayerID分组获得最高分,这就是这个问题的内容。

SELECT
   PlayerID,
   MAX(CAST(MatchPlayerBatting.BatRuns AS SIGNED)) AS HighestScore
FROM
   MatchPlayerBatting
GROUP BY
   PlayerID

根据上一个问题,请考虑:

BatRuns   BatHowOut
96        not out
96        lbw

BatRuns   BatHowOut
96        not out
102       lbw

1 个答案:

答案 0 :(得分:0)

尝试做这样的事情:

<强> EDITED ..

select playerID,
max(concat((BatRuns),
   case when BatRuns = (Select max(BatRuns) from mytable where 
                        outornot = 'no') then '*' else '' end))
from mytable
group by playerID
order by cast(BatRuns as signed) desc,
(CASE WHEN outornot = 'no' then 1 else 2 end);

<强> SQL Fiddle