まず、宿題ですが、私は決して答えを探していません。私はデータベースクラスに属していて、American Baseballリーグ(チーム、各人の統計など)の統計情報を持つデータベースを与えられました。各テーブルのスキーマ定義は質問の下にあります。私が書く必要がある質問は、次のとおりです。チームごとに最高のホームランを持つ人を見つける(DERBY/SQL)
同じチームのどんなペアの打者が一組のホーマーを持っていましたか?あなたの質問は、それぞれの打者の最初と最後の名前と、それぞれが命中したホームランの数を提供する必要があります。
基本的には、チームごとに2つの最高ホームランを取って、どのチームが最高額を持っているかを見てみましょう。しかし、チームごとに最高得点者(ホームランの点で)を生成するクエリを書くのは難しいです。これまで私が持っていたことは、このようなものです。
正しいチーム名が表示されます(そして、それは30個のタプルを表示するので、私はこれを知っている、そしてそれはDBのチームの数だ)が、ホームランの量と、プレイヤーの最初と最後の名前がすべてですselect
nameFirst, nameLast, name, HR
from
Players, Teams, Batting
where
HR >= ALL(select HR from Batting)
and Players.playerID = Batting.playerID;
同じ。 (このデータベースでは、BrewersのPrince's 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
うーん、私はHavingステートメントでうんざりしていましたが、私はそれを得ることができませんでした。それは、最高ランニングの人DBに。 – joebro