运行 Hive 查询时获取 ParseException
Getting ParseException when running Hive query
我正在尝试查找工资低于平均工资的员工人数。
我是蜂巢的新手并且有点挣扎,有人可以解释我的陈述有什么问题并帮助我吗?
我的发言-
SELECT COUNT(*) FROM(SELECT wage, AVG(wage) AS avgWage FROM emp_wages) WHERE wage < avgWage;
错误 -
ParseException line 1:82 cannot recognize input near 'where' 'wage' '<' in subquery source
感谢任何帮助!
语法错误。派生 table 应该是别名。
SELECT COUNT(*)
FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage) t --alias needed here
WHERE wage < avgWage;
明智的查询,需要更改。
select count(*)
from (SELECT wage, AVG(wage) over() AS avgWage
FROM emp_wages
) t
where wage < avgWage
问题是 AVG 是一个聚合函数。如果要映射一对多关系,需要使用交叉连接函数:
select
count(*), avg(v1.wage),
sum(case when v.wage < v2.avgwage then 1 else 0 end) below_average
from
emp_wages v cross join (select avg(wage) as avgwage from emp_wages) as v2
SELECT COUNT(*)
FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage)avg --group by needed
WHERE wage < avgWage;
正确的查询是:
select count(*) where wage <(select avg(wage) from emp_wages);
您收到解析错误,因为 wage 和 avgWage 在子查询中。
我正在尝试查找工资低于平均工资的员工人数。
我是蜂巢的新手并且有点挣扎,有人可以解释我的陈述有什么问题并帮助我吗?
我的发言-
SELECT COUNT(*) FROM(SELECT wage, AVG(wage) AS avgWage FROM emp_wages) WHERE wage < avgWage;
错误 -
ParseException line 1:82 cannot recognize input near 'where' 'wage' '<' in subquery source
感谢任何帮助!
语法错误。派生 table 应该是别名。
SELECT COUNT(*)
FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage) t --alias needed here
WHERE wage < avgWage;
明智的查询,需要更改。
select count(*)
from (SELECT wage, AVG(wage) over() AS avgWage
FROM emp_wages
) t
where wage < avgWage
问题是 AVG 是一个聚合函数。如果要映射一对多关系,需要使用交叉连接函数:
select
count(*), avg(v1.wage),
sum(case when v.wage < v2.avgwage then 1 else 0 end) below_average
from
emp_wages v cross join (select avg(wage) as avgwage from emp_wages) as v2
SELECT COUNT(*)
FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage)avg --group by needed
WHERE wage < avgWage;
正确的查询是:
select count(*) where wage <(select avg(wage) from emp_wages);
您收到解析错误,因为 wage 和 avgWage 在子查询中。