HIVE尝试从另一个select语句字段中加入和减去一个字段

时间:2016-04-24 03:04:56

标签: sql hive

enter image description here

我正在尝试在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';

1 个答案:

答案 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;