hive嵌套查询 您所在的位置:网站首页 咒术回战七海的话 hive嵌套查询

hive嵌套查询

2023-12-04 17:27| 来源: 网络整理| 查看: 265

任务描述

本关任务:使用嵌套查询语句完成指定操作。

相关知识

为了完成本关任务,你需要掌握:1.如何进行嵌套查询。

嵌套子查询:执行不依赖于外部的查询

嵌套子查询执行顺序先是子查询然后在是主查询。子查询的结果不会被显示,会将其传递给外部查,作为外部查询的条件来输出。

例子:查询所有价格高于平均价格的商品信息。

select * from product where price > (select avg(price) from product);

相关子查询:执行依赖于外部查询。

执行顺序:先是主查询然后在是子查询。

执行过程:

从主查询中取出一个元组(表中的行),将元组相关的列的值传递给子查询;子查询将列的值作为条件得到结果;主查询根据子查询得到的结果或者结果集得到满足条件的行,selece 控制显示;然后主查询取出下一个元组继续 1-3 步骤,直到所有元组全部处理完毕。

例:取出不同 num 中的最高价格的商品信息 select * from product p where p.price = (select max(price) from product where num=p.num);

案例

hive> select * from product;+----+------+-------+-----+| id | name | price | num |+----+------+-------+-----+| 1 | 伊利 | 68 | 1 || 2 | 蒙牛 | 88 | 1 || 3 | nike | 888 | 2 || 4 | 阿迪 | 688 | 2 || 5 | kris | 1888 | 3 || 6 | tom | 2888 | 3 || 7 | sam | 1688 | 3 |+----+------+-------+-----+hive> select * from product where price > (select avg(price) from product);+----+------+-------+-----+| id | name | price | num |+----+------+-------+-----+| 5 | kris | 1888 | 3 || 6 | tom | 2888 | 3 || 7 | sam | 1688 | 3 |+----+------+-------+-----+hive> select * from product p where p.price = (select max(price) from product where num=p.num);+----+------+-------+-----+| id | name | price | num |+----+------+-------+-----+| 2 | 蒙牛 | 88 | 1 || 3 | nike | 888 | 2 || 6 | tom | 2888 | 3 |+----+------+-------+-----+

编程要求

根据提示,在右侧编辑器补充代码。

平台中有一个表 employee, 建表的语法如下:

CREATE TABLE IF NOT EXISTS employee(Id int,Name string,Salary int,Designation string,Dept string)row format delimited fields terminated by',' lines terminated by'\n'stored as textfile;

你需要在右边的代码框中编写 Hive SQL 语句,实现选出每个部门 Dept 的工资 Salary 最高者的全部信息。

测试说明

平台已经建好需要的表,你只需要完善 Hive SQL 语句,平台会对你编写的代码进行测试。

代码如下

-- BEGIN select * from employee e where e.salary = (select max(salary) from employee where Dept=e.Dept); -- END


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有