首先,这是家庭作业,但我不是以任何方式寻找答案。我在数据库类中,我们获得了一个数据库,其中包含美国棒球联盟的统计数据(团队,每个人的统计数据等),每个表的模式定义都在问题的下方。我需要写的查询是:
同一支队伍中的哪一对击球手最多成为一对?您的查询应该提供对中每个击球手的名字和姓氏以及每个击球次数。
所以基本上,只需要按照每个团队的两个最高本垒打,看看哪个团队的金额最高,但我在编写一个查询时遇到问题,这个查询将产生每个团队中得分最高的人(就本垒打而言)。到目前为止我所拥有的是这样的。
select
nameFirst, nameLast, name, HR
from
Players, Teams, Batting
where
HR >= ALL(select HR from Batting)
and Players.playerID = Batting.playerID;
显示正确的团队名称(我知道这是因为它显示了30个元组,这就是数据库中的团队数量),但本垒打量和玩家的名字和姓氏都是相同的。 (在这个数据库中,它是Brewers的Prince Fielders,因为他拥有数据库中最多的本垒打。)关于如何实现的提示是如此显示正确的人的名字和姓,非常感谢!
CREATE TABLE Players
(
playerID VARCHAR(10), --A unique code asssigned to each player. The playerID
--links the data in this file with records in the other files.
nameFirst VARCHAR(50), --First name
nameLast VARCHAR(50), --Last name
bats CHAR(1), --Player's batting hand (left, right, or both)
throws CHAR(1), --Player's throwing hand (left or right)
PRIMARY KEY (playerID)
);
CREATE TABLE Teams
(
teamID CHAR(3), --Three-letter team code
lgID CHAR(2), --Two-letter league code
divID CHAR(1), --One letter code for the division the team player in
Rank SMALLINT, --Position in final standings in that division
G SMALLINT, --Games played
W SMALLINT, --Games won
L SMALLINT, --Games lost
DivWin CHAR(1), --Division winner (Y or N)
WCWin CHAR(1), --Wild card winner (Y or N)
LgWin CHAR(1), --League champion (Y or N)
WSWin CHAR(1), --World series winner
name VARCHAR(50), --Team's full name
park VARCHAR(255), --Name of team's home ballpark
PRIMARY KEY (teamID)
);
CREATE TABLE Batting
(
playerID VARCHAR(10), --Player ID code
yearID SMALLINT, --Will always be 2011 in data for this assignment
stint SMALLINT, --Used to identify a particular continuous period that
--a player played for one team during the season. For example, a player
--who played during May for the Brewers, was then sent down to the
--minors and came back to play again for the Brewers in August would
--have two stints -- numbered 1 and 2
teamID CHAR(3), --Three-letter team ID
lgID CHAR(2), --Two letter league ID -- NL or AL
G SMALLINT, --Number of games appeared in during this stint
G_batting SMALLINT, --Number of games appeared in as a batter during this stint
AB SMALLINT, --Number of at bats
R SMALLINT, --Number of runs
H SMALLINT, --Number of hits
doubles SMALLINT, --Number of doubles
triples SMALLINT, --Number of triples
HR SMALLINT, --Number of home runs
RBI SMALLINT, --Number of runs batted in
SB SMALLINT, --Number of stolen bases
CS SMALLINT, --Number of times caught trying to steal a base
BB SMALLINT, --Number of base on balls (walks)
SO SMALLINT, --Number of time player struck out
IBB SMALLINT, --Number of intentional walks received
HBP SMALLINT, --Number of time hit by pitch
SF SMALLINT, --Number of sacrifice flied
GIDP SMALLINT, --Number of times grounded into double play
PRIMARY KEY (playerID, stint)
);
答案 0 :(得分:0)
我假设HR在Batter表中,即使它似乎不在表定义中。但你需要做的是将击球手表连接到自己身上。您可以在同一查询中多次从同一个表中进行查询,只需为每个表实例提供不同的别名即可。
通过将Batter表连接到自身,您正在创建所谓的“笛卡尔积”,它基本上是该表中每两个击球员的组合。从那里,您可以计算HR的总和,然后相应地按该值排序。
如果你愿意,我会为你提供查询,但我知道你说这是家庭作业,所以如果你想根据我的解释想出来,你可以发布任何问题,我可以从那里得到帮助:)< / p> 祝你好运!
答案 1 :(得分:0)
select t.teamname, p1.HR + p2.HR as [Sum], p1.playerid, p2.playerid
from player p1
join player p2
on p1.teamid = p2.teamid
and p1.playerid <> p2.playerid
join team t
on p1.teamid = t.teamid
and p2.teamid = t.teamid
这将使你走上正轨,希望如此。