【问题标题】:get the value from subquery in hive从 hive 中的子查询中获取值
【发布时间】:2019-06-04 16:30:50
【问题描述】:

我试图参数化 hive 中的值,而不是在查询中对其进行硬编码。以下是查询。

select * from employee where sal >30000

但不是使用硬编码的 30000 值,我需要它来自如下相同的查询。但我遇到了问题:

select * from employee where sal > (select max(sal) from employee)

感谢任何帮助。

谢谢

【问题讨论】:

  • 什么问题,收到任何错误或不返回任何数据?
  • 接收到不支持的错误
  • 试试表别名select E.* from employee E where E.sal > (select max(S.sal) AS MaxSal from employee S)
  • 谢谢,但给出错误:无法识别附近的选择(

标签: hive hiveql


【解决方案1】:

您可以尝试使用这种形式的 Hive 查询。这将使员工的薪水等于最高薪水。

SELECT e1.* FROM employee e1 
JOIN
(SELECT MAX(sal) as max_sal FROM employee) e2
ON e1.sal = e2.max_sal;

例子:

Table: employee
id  fname   sal
1   AAA     15000
2   BBB     35000
3   CCC     12000
4   DDD     35000
5   EEE     9000

查询执行输出:

2   BBB     35000
4   DDD     35000

【讨论】:

    【解决方案2】:

    Hive 不支持这样的子查询,也不允许计算变量,Hive 中的变量是简单的文本替换,无需计算。您可以在 shell 中计算谓词并传递给您的配置单元脚本,如下所示:https://stackoverflow.com/a/37821218/2700344

    如果你想在同一个 hive 查询中做这件事,计算子查询并用它的结果做一个交叉连接没有错,然后过滤。子查询将首先被计算,然后将其结果放置在分布式缓存中并应用于读取表的每个映射器中的过滤器:

    with sub as(--this is example only and makes no sense
                --replace with real query
                --of course there is no rows with sal>max sal in the same table
    select max(S.sal) AS MaxSal from employee S  
    )
    
    select * 
      from employee e 
           cross join sub s  
    where e.sal>s.MaxSal 
    

    如果不写CROSS JOIN,直接写from employee e, sub s,或者不加条件的JOIN,还是一样的cross join,better write it explicitly using cross join

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多