Hive Query中的语法错误

时间:2016-04-16 09:30:14

标签: sql hadoop hiveql

enter image description here

我想回答这个问题

Of  the right-handed    batters who were    born    in  October and died in 
2011,   which   one had the most    hits    in  his career?

我尝试获取查询,请忽略总数,它应该是b.hits的总和,不知道如何别名。

SELECT n.id, n.bmonth, n.dyear,n.bats, SUM(b.hits) FROM master n
JOIN (SELECT b.id , b.hits FROM batting GROUP BY id) o
WHERE n.bmonth == 10 AND n.dyear == 2011) x
ON x.id=n.id 
ORDER BY total DESC;

任何人都需要使用两个表的架构,如下所示。

INSERT OVERWRITE DIRECTORY '/home/hduser/hivetest/answer4' 
SELECT n.id, n.bmonth, n.dyear,n.bats, SUM(b.hits) FROM master n
JOIN (SELECT b.id , b.hits FROM batting GROUP BY id) o
WHERE n.bmonth == 10 AND n.dyear == 2011) x
ON x.id=n.id 
ORDER BY total DESC;

1 个答案:

答案 0 :(得分:1)

首先,虽然Hive接受==,但这并不意味着您应该使用它。标准SQL相等运算符只是=。没有理由使用同义词。

我怀疑问题有几个:

  • 缺少group by
  • 误用聚合函数。
  • 缺少别名
  • SQL查询子句的顺序正确
  • 不平衡的括号

换句话说,查询只是一团糟。您需要查看查询语法的基础知识。这有用吗?

SELECT m.id, m.bmonth, m.dyear, m.bats, b.hits as total
FROM master m JOIN
     (SELECT b.id, SUM(b.hits) as hits
      FROM batting b
      GROUP BY id
     ) b
     ON b.id = m.id 
WHERE m.bmonth = 10 AND m.dyear = 2011
ORDER BY total DESC;