SQL 从 Access 数据库中查找击球平均值的语句
SQL statement to find batting average from Access Database
我正在使用 Microsoft Access 数据库创建 Visual Basic 程序。
数据库有两个表。
- Table 1 名被称为
Players
,具有名称、命中率、atBats 和团队列
- Table 2 人被称为
Teams
,有团队和联赛列
我需要帮助编写 SQL 声明以找到特定联盟中击球率最高的球员 National 或 American联赛。
Batting Average
公式是 hits
除以 atBats
。
代码:
SELECT
name, (hits / atBats) AS bAVG
FROM
Players
INNER JOIN
Teams ON Teams.team = Players.Team
WHERE
Teams.league = 'American'
AND bAVG = (SELECT MAX(bAVG) FROM Players);
我认为问题出在别名列 bAVG
上。
联接的 table 中没有列 bAVG,因此出现错误。您可以使用类似的东西:
SELECT name from Players, (hits/atBats) as bAVG FROM Players INNER
JOIN Teams ON Teams.team = Players.Team WHERE Teams.league =
'American' AND (hits/atBats) = (select MAX((hits/atBats) from
Players));
您可以使用 order by
和 top
来做到这一点:
SELECT TOP 1 name, (hits/atBats) AS bAVG
FROM Players INNER JOIN
Teams
ON Teams.team = Players.Team
WHERE Teams.league = 'American'
ORDER BY hits/atBats DESC;
我正在使用 Microsoft Access 数据库创建 Visual Basic 程序。
数据库有两个表。
- Table 1 名被称为
Players
,具有名称、命中率、atBats 和团队列 - Table 2 人被称为
Teams
,有团队和联赛列
我需要帮助编写 SQL 声明以找到特定联盟中击球率最高的球员 National 或 American联赛。
Batting Average
公式是 hits
除以 atBats
。
代码:
SELECT
name, (hits / atBats) AS bAVG
FROM
Players
INNER JOIN
Teams ON Teams.team = Players.Team
WHERE
Teams.league = 'American'
AND bAVG = (SELECT MAX(bAVG) FROM Players);
我认为问题出在别名列 bAVG
上。
联接的 table 中没有列 bAVG,因此出现错误。您可以使用类似的东西:
SELECT name from Players, (hits/atBats) as bAVG FROM Players INNER JOIN Teams ON Teams.team = Players.Team WHERE Teams.league = 'American' AND (hits/atBats) = (select MAX((hits/atBats) from Players));
您可以使用 order by
和 top
来做到这一点:
SELECT TOP 1 name, (hits/atBats) AS bAVG
FROM Players INNER JOIN
Teams
ON Teams.team = Players.Team
WHERE Teams.league = 'American'
ORDER BY hits/atBats DESC;