我在德比中形成了以下查询来计算所有棒球运动员的击打百分比......
SELECT p1.nameFirst as "First",
p1.nameLast as "Last",
(b1.H + (2 * b1.doubles) + (3 * b1.triples) + (4 * b1.HR)) / (b1.AB * 1.0) as "SLUGGING"
FROM Batting b1 join Players p1 on b1.playerID = p1.playerID
WHERE AB >= 400;
它生产所有击球时有400或以上的击球手,并计算他们的击打率。我的问题是我只需要选择一个具有最大段塞百分比的玩家。我尝试用SELECT MAX包围数学块,但无济于事。我想要的输出是......
Jose Bautista 0.7426900584795321637426900
另外,有没有办法将该十进制格式化为一定数量的数字?任何建议都表示赞赏。
答案 0 :(得分:2)
将ORDER BY SLUGGING DESC LIMIT 1
添加到您的查询中,您应该获得最佳结果。
编辑: 似乎Derby不支持限制返回的行数。那么最好的方法可能就是创建一个带有最大段塞结果的子选择,并加入它以使playerId打印出来。
答案 1 :(得分:0)
我开始怀疑自己是在帮助你为MBA写点什么,还是做计算机科学作业。 : - )
SELECT
p1.nameFirst as "First",
p1.nameLast as "Last",
b.s AS "SLUGGING"
FROM
(
SELECT
MAX(
(b1.H + (2 * b1.doubles) + (3 * b1.triples) + (4 * b1.HR)) / (b1.AB * 1.0)
) AS s
FROM
Batting b1
WHERE
b1.AB >= 400
) m1
JOIN
Batting b
ON
m1.s = (b.H + (2 * b.doubles) + (3 * b.triples) + (4 * b.HR)) / (b.AB * 1.0)
JOIN
Players p1
ON
b1.playerID = p1.playerID
WHERE
b1.AB >= 400
OR
*不确定是否支持
SELECT
TOP 1
p1.nameFirst as "First"
, p1.nameLast as "Last"
, (b1.H + (2 * b1.doubles) + (3 * b1.triples) + (4 * b1.HR)) / (b1.AB * 1.0) AS "SLUGGING"
FROM
Batting b1
JOIN
Players p1 on b1.playerID = p1.playerID
WHERE
b1.AB >= 400
ORDER BY
(b1.H + (2 * b1.doubles) + (3 * b1.triples) + (4 * b1.HR)) / (b1.AB * 1.0) DESC
OR
*我认为这个会起作用
SELECT * FROM (
SELECT
ROW_NUMBER() OVER(
ORDER BY
(b1.H + (2 * b1.doubles) + (3 * b1.triples) + (4 * b1.HR)) / (b1.AB * 1.0) DESC
) as rownum
, p1.nameFirst as "First"
, p1.nameLast as "Last"
, (b1.H + (2 * b1.doubles) + (3 * b1.triples) + (4 * b1.HR)) / (b1.AB * 1.0) AS "SLUGGING"
FROM
Batting b1
JOIN
Players p1 on b1.playerID = p1.playerID
WHERE
b1.AB >= 400
) AS r
WHERE r.rownum = 1