多表中的HIVE组

时间:2018-02-28 03:39:45

标签: sql hive hql

两张桌子 - '薪水'和'master1'

薪水是按年计算的,我可以将它们分组,以便使用

获得每位玩家的总和
SELECT playerID, sum(salary) as sal 
FROM salaries 
GROUP BY playerID ORDER BY sal DESC LIMIT 10; 

这将返回playerID和工资总和,但我需要'name1'和'nameLast'列下'master1'表中的玩家姓名。他们在'master1'和'薪水'中都有'playerID'的公共列,但是当我尝试运行时

SELECT master1.nameFirst, master1.nameLast, sum(salary) as sal 
FROM salaries, master1 
GROUP BY salaries.playerID ORDER BY sal DESC LIMIT 10; 

我收到错误 表达式不在GROUP BY键'nameFirst'

我尝试过修补它以继续收到错误。

谢谢!

2 个答案:

答案 0 :(得分:0)

需要在nameFirst中包含nameLastgroup by

SELECT 
master1.nameFirst, 
master1.nameLast, 
sum(salary) as sal 
FROM salaries JOIN master1 ON salaries.playID = master1.playerID
GROUP BY master1.nameFirst, master1.nameLast, salaries.playerID 
ORDER BY sal DESC LIMIT 10;

答案 1 :(得分:0)

首先,您需要使用正确的显式JOIN语法

SELECT  
        MAX(m.nameFirst) FirstName,
        MAX(m.nameLast) LastName,
        SUM(s.salary) Salary
FROM master1 m
INNER JOIN salaries s ON m.playerID = s.playerID 
GROUP BY m.playerID 

使用master1表获取 FirstName LastName ,然后执行JOIN salaries表来获取每个玩家的总薪水。

对于使用GROUP BY子句时的当前查询异常,请确保SELECT语句中的 columns / expressions 需要聚合。