我正在尝试在id和playerID以及年份和年份ID(下面的架构)上加入这两个select语句。也减去别名HAB - EG
SELECT playerID, yearID, (E/ G) AS EG FROM fielding
WHERE yearID > 2005 AND yearID < 2009 AND G > 20;
SELECT id, year, (hits/ab) AS HAB FROM batting
WHERE year > 2005 AND year < 2009 AND ab > 40;
JUST SCHEMA
CREATE EXTERNAL TABLE IF NOT EXISTS fielding
(playerID STRING ,yearID INT ,teamID STRING ,lgID STRING ,
POS STRING ,G INT ,GS INT , InnOuts INT , PO INT,A INT, E INT,
DP INT , PB INT , WP INT ,SB INT ,CS INT , ZR INT ) ROW
FORMAT DELIMITED FIELDS TERMINATED BY ',' LOCATION '/home/hduser/hivetest/fielding';
JUST THE SCHEMA
CREATE EXTERNAL TABLE IF NOT EXISTS batting(id STRING, year INT, team STRING,
league STRING, games INT, ab INT, runs INT, hits INT, doubles INT, triples
INT, homeruns INT, rbi INT, sb INT, cs INT, walks INT, strikeouts INT, ibb
INT, hbp INT, sh INT, sf INT, gidp INT) ROW FORMAT DELIMITED FIELDS
TERMINATED BY ',' LOCATION '/home/hduser/hivetest/batting';
答案 0 :(得分:1)
试试这个:
SELECT
a.playerID AS ID,
a.yearID AS yearID,
(b.HAB - a.EG) AS HAB-EG
FROM
(SELECT
playerID,
yearID,
(E/G) AS EG
FROM fielding
WHERE (
yearID > 2005
AND yearID < 2009
AND G > 20
)
) AS a
JOIN
(SELECT
id,
year,
(hits/ab) AS HAB
FROM batting
WHERE(
year > 2005
AND year < 2009
AND ab > 40
)
) AS b ON a.playerID = b.id AND a.yearID = b.year;