MySql十进制问题输出
MySql Decimal Issue Output
晚上
我有一项运动 table,它按成功百分比排序((获胜的游戏 +(游戏 drawn/2)/玩过的游戏),我在输出之后如果 100%游戏赢了,然后它读作 1.000、75%,然后是 .750、50% 为 .500 等(前导 0 被删除)。
目前我可以让它显示 0.000 到 0.950,如果球队只打了 1 场比赛并赢了,那么输出显示 0.100。
我怎样才能:
a) 让它显示 1 场比赛 played/won 为 1.000
b) 以百分比形式显示和其他组合,例如 .500
有什么想法吗?
谢谢
SELECT team
, COUNT(*) as played
, SUM(win) as wins
, SUM(loss) as lost
, SUM(draw) as draws
, SUM(SelfScore) as ptsfor
, SUM(OpponentScore) as ptsagainst
, SUM((win*2 + draw)- loss) as score
, CAST(SUM(win + (draw/2))/(count(*)) as decimal(3,3)) pctWon
FROM (
SELECT team
, SelfScore
, OpponentScore
, SelfScore > OpponentScore win
, SelfScore < OpponentScore loss
, SelfScore = OpponentScore draw
FROM (
SELECT HomeTeam team, HomeScore SelfScore, AwayScore OpponentScore
FROM Game
WHERE Season = '2015' and Type = 'League'
UNION ALL
SELECT AwayTeam, AwayScore, HomeScore
FROM Game
WHERE Season = '2015' and Type = 'League'
) a
) b
GROUP BY team
ORDER BY pctWon DESC, score DESC, ptsagainst DESC, team ASC;
");
这会产生正确的外观 table,除了前 2 个显示的 PCT 不正确。
Team W L T PF PA PCT
T1 1 0 0 54 0 0.100
T2 1 0 0 44 0 0.100
T3 0 1 0 0 54 0.000
T4 0 1 0 0 44 0.000
T5 6 2 2 220 122 0.700
T6 7 3 0 247 139 0.700
T7 6 4 0 191 191 0.600
T8 4 5 1 167 201 0.450
T9 3 6 1 142 202 0.350
T10 2 8 0 193 305 0.200
谢谢
DECIMAL(3,3)
小数点左边没有 space。你需要 DECIMAL(4,3)
.
您使用的 MySQL 是什么版本? 0.100
听起来像个错误。但我无法在 5.6.12 上使用
重现它
mysql> SELECT CAST(((1+0/2)/1) AS DECIMAL(3,3));
+-----------------------------------+
| CAST(((1+0/2)/1) AS DECIMAL(3,3)) |
+-----------------------------------+
| 0.999 |
+-----------------------------------+
请注意 (4,3) 可以满足您的需求:
mysql> SELECT CAST(((1+0/2)/1) AS DECIMAL(4,3));
+-----------------------------------+
| CAST(((1+0/2)/1) AS DECIMAL(4,3)) |
+-----------------------------------+
| 1.000 |
+-----------------------------------+
我终于解决了这个问题!
问题似乎是在 CAST 中玩的游戏的计数。
而不是:
, CAST(SUM(win + (draw/2))/(count(*)) as decimal(3,3)) pctWon
以下给出了正确的输出:
, CAST(SUM(win + (draw/2))/SUM(win + loss + draw) as decimal(4,3)) as pctWon
晚上
我有一项运动 table,它按成功百分比排序((获胜的游戏 +(游戏 drawn/2)/玩过的游戏),我在输出之后如果 100%游戏赢了,然后它读作 1.000、75%,然后是 .750、50% 为 .500 等(前导 0 被删除)。
目前我可以让它显示 0.000 到 0.950,如果球队只打了 1 场比赛并赢了,那么输出显示 0.100。
我怎样才能: a) 让它显示 1 场比赛 played/won 为 1.000 b) 以百分比形式显示和其他组合,例如 .500
有什么想法吗?
谢谢
SELECT team
, COUNT(*) as played
, SUM(win) as wins
, SUM(loss) as lost
, SUM(draw) as draws
, SUM(SelfScore) as ptsfor
, SUM(OpponentScore) as ptsagainst
, SUM((win*2 + draw)- loss) as score
, CAST(SUM(win + (draw/2))/(count(*)) as decimal(3,3)) pctWon
FROM (
SELECT team
, SelfScore
, OpponentScore
, SelfScore > OpponentScore win
, SelfScore < OpponentScore loss
, SelfScore = OpponentScore draw
FROM (
SELECT HomeTeam team, HomeScore SelfScore, AwayScore OpponentScore
FROM Game
WHERE Season = '2015' and Type = 'League'
UNION ALL
SELECT AwayTeam, AwayScore, HomeScore
FROM Game
WHERE Season = '2015' and Type = 'League'
) a
) b
GROUP BY team
ORDER BY pctWon DESC, score DESC, ptsagainst DESC, team ASC;
");
这会产生正确的外观 table,除了前 2 个显示的 PCT 不正确。
Team W L T PF PA PCT
T1 1 0 0 54 0 0.100
T2 1 0 0 44 0 0.100
T3 0 1 0 0 54 0.000
T4 0 1 0 0 44 0.000
T5 6 2 2 220 122 0.700
T6 7 3 0 247 139 0.700
T7 6 4 0 191 191 0.600
T8 4 5 1 167 201 0.450
T9 3 6 1 142 202 0.350
T10 2 8 0 193 305 0.200
谢谢
DECIMAL(3,3)
小数点左边没有 space。你需要 DECIMAL(4,3)
.
您使用的 MySQL 是什么版本? 0.100
听起来像个错误。但我无法在 5.6.12 上使用
mysql> SELECT CAST(((1+0/2)/1) AS DECIMAL(3,3));
+-----------------------------------+
| CAST(((1+0/2)/1) AS DECIMAL(3,3)) |
+-----------------------------------+
| 0.999 |
+-----------------------------------+
请注意 (4,3) 可以满足您的需求:
mysql> SELECT CAST(((1+0/2)/1) AS DECIMAL(4,3));
+-----------------------------------+
| CAST(((1+0/2)/1) AS DECIMAL(4,3)) |
+-----------------------------------+
| 1.000 |
+-----------------------------------+
我终于解决了这个问题!
问题似乎是在 CAST 中玩的游戏的计数。
而不是:
, CAST(SUM(win + (draw/2))/(count(*)) as decimal(3,3)) pctWon
以下给出了正确的输出:
, CAST(SUM(win + (draw/2))/SUM(win + loss + draw) as decimal(4,3)) as pctWon