出于某种原因,我无法提出一些简单的代码。
我想在下面的代码中将'wins'除以'playing',以便在我的输出中如果你赢了4场比赛中的2场,它会显示为.500?
当然这很简单,一个位置可能很关键,但我无法理解它的生命!!
$result = mysql_query("
select team,
count(*) played,
count(case when HomeScore > AwayScore then 1 end) wins,
count(case when AwayScore > HomeScore then 1 end) lost,
count(case when HomeScore = AwayScore then 1 end) draws,
sum(HomeScore) ptsfor,
sum(AwayScore) ptsagainst,
sum(HomeScore) - sum(AwayScore) goal_diff,
sum(case when HomeScore > AwayScore then 3 else 0 end + case
when HomeScore = AwayScore then 1 else 0 end) score
from (select
HomeTeam team,
HomeScore,
AwayScore
from Game
union all
select AwayTeam,
AwayScore,
HomeScore
from Game) a
group by team
order by wins desc,
draws desc,
lost asc,
goal_diff desc;");
由于
答案 0 :(得分:0)
只需在查询中添加另一个计算列,如下所示:
select team,
count(*) played,
count(case when HomeScore > AwayScore then 1 end) wins,
(cast(count(case when HomeScore > AwayScore then 1 end) as decimal(8,2)/cast(count(*) as decimal(8,2)) percentage,
.... -> Rest of the query goes here
由于我假设分数是整数,因此完成了转换。如果没有铸造,将会失去准确性,即2/4将在没有铸造的情况下返回0。
答案 1 :(得分:0)
尝试以下查询。这可以利用MySQL的行为自动将boolean
表达式转换为int
。这意味着没有CASE
语句(以可移植性为代价的漂亮查询)。此外,像SUM(3*win + draw) score
这样的部分在视觉上看起来更像是你的得分计算公式。没问题计算pct
。
SELECT team
, COUNT(*) played
, SUM(win) wins
, SUM(loss) lost
, SUM(win)/count(*) pctWon
, SUM(draw) draws
, SUM(SelfScore) ptsfor
, SUM(OpponentScore) ptsagainst
, SUM(SelfScore) - SUM(OpponentScore) goal_diff
, SUM(3*win + draw) score
FROM (
SELECT team
, SelfScore
, OpponentScore
, SelfScore < OpponentScore win
, SelfScore > OpponentScore loss
, SelfScore = OpponentScore draw
FROM (
SELECT HomeTeam team, HomeScore SelfScore, AwayScore OpponentScore
FROM Game
union all select AwayTeam, AwayScore, HomeScore
FROM Game
) a
) b
GROUP BY team
ORDER BY wins DESC, draws DESC, lost ASC, goal_diff DESC;