첫째로, 이것은 숙제인데, 나는 어떤 방법 으로든 답을 찾지 않을 것입니다. 나는 데이터베이스 클래스에 있고 미국 야구 리그 (팀, 각 사람의 통계 등)에 통계가있는 데이터베이스가 주어졌습니다. 각 테이블의 스키마 정의는 질문 아래에 있습니다. 필자가 작성해야하는 쿼리는 다음과 같습니다.팀당 가장 높은 홈런을 가진 사람을 찾으십시오. (DERBY/SQL)
같은 팀의 한 쌍의 타자가 가장 많은 수의 홈런을 한 쌍으로 갖고 있습니까? 귀하의 질문은 각 타자의 첫 번째와 마지막 이름을 각 타격 홈런 수와 함께 제공해야합니다.
기본적으로 두 팀 중 가장 높은 홈런을 취하고 어느 팀이 최고가되는지 확인하지만 팀당 홈런의 점수가 가장 높은 사람을 생성하는 쿼리를 작성하는 데 문제가 있습니다. . 내가 지금까지 가지고있는 것은 이것과 같은 것이다. 정확한 팀 이름을 표시
select
nameFirst, nameLast, name, HR
from
Players, Teams, Batting
where
HR >= ALL(select HR from Batting)
and Players.playerID = Batting.playerID;
(그리고 30 개 튜플을 표시 때문에 나는 이것을 알고, 그리고는 DB에있는 팀의 수의), 그러나 홈런의 양 플레이어의 이름과 성이 전부입니다 똑같다. (이 데이터베이스에서는 데이터베이스에서 가장 많은 홈런을 기록했기 때문에 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)
);
저는 SQL 전문가가 아니지만, 당신이 mathy-expression을하고 싶다면 HAVING 절을 사용할 수 있다는 인상하에 있습니다. HAVING과 MAX (somefield)를 합치면 더 잘 작동합니다. – Kristian
흠, 나는 성명서를 가지고 놀아 보려고했으나 그것을 얻을 수 없었습니다. 그래서 가장 많은 양의 홈런을 가진 사람 이상을 줄 수있었습니다. DB에. – joebro