课程表

Impala 基础

Table 特定语句

Impala 子句

工具箱
速查手册

impala with子句

当前位置:免费教程 » 大数据/云 » Impala

如果查询太复杂,我们可以为复杂部分定义别名,并使用Impala的with子句将它们包含在查询中。

语法

以下是Impala中的with子句的语法。

  1. with x as (select 1), y as (select 2) (select * from x union y);

假设我们在数据库my_db中有一个名为customers的表,其内容如下 -

  1. [quickstart.cloudera:21000] > select * from customers;
  2. Query: select * from customers
  3. +----+----------+-----+-----------+--------+
  4. | id | name | age | address | salary |
  5. +----+----------+-----+-----------+--------+
  6. | 1 | Ramesh | 32 | Ahmedabad | 20000 |
  7. | 9 | robert | 23 | banglore | 28000 |
  8. | 2 | Khilan | 25 | Delhi | 15000 |
  9. | 4 | Chaitali | 25 | Mumbai | 35000 |
  10. | 7 | ram | 25 | chennai | 23000 |
  11. | 6 | Komal | 22 | MP | 32000 |
  12. | 8 | ram | 22 | vizag | 31000 |
  13. | 5 | Hardik | 27 | Bhopal | 40000 |
  14. | 3 | kaushik | 23 | Kota | 30000 |
  15. +----+----------+-----+-----------+--------+
  16. Fetched 9 row(s) in 0.59s

同样,假设我们有另一个名为employee的表,其内容如下 -

  1. [quickstart.cloudera:21000] > select * from employee;
  2. Query: select * from employee
  3. +----+---------+-----+---------+--------+
  4. | id | name | age | address | salary |
  5. +----+---------+-----+---------+--------+
  6. | 3 | mahesh | 54 | Chennai | 55000 |
  7. | 2 | ramesh | 44 | Chennai | 50000 |
  8. | 4 | Rupesh | 64 | Delhi | 60000 |
  9. | 1 | subhash | 34 | Delhi | 40000 |
  10. +----+---------+-----+---------+--------+
  11. Fetched 4 row(s) in 0.59s

以下是Impala中的with子句的示例。 在本示例中,我们使用with子句显示年龄大于25的员工和客户的记录。

  1. [quickstart.cloudera:21000] >
  2. with t1 as (select * from customers where age>25),
  3. t2 as (select * from employee where age>25)
  4. (select * from t1 union select * from t2);

执行时,上述查询给出以下输出。

  1. Query: with t1 as (select * from customers where age>25), t2 as (select * from employee where age>25)
  2. (select * from t1 union select * from t2)
  3. +----+---------+-----+-----------+--------+
  4. | id | name | age | address | salary |
  5. +----+---------+-----+-----------+--------+
  6. | 3 | mahesh | 54 | Chennai | 55000 |
  7. | 1 | subhash | 34 | Delhi | 40000 |
  8. | 2 | ramesh | 44 | Chennai | 50000 |
  9. | 5 | Hardik | 27 | Bhopal | 40000 |
  10. | 4 | Rupesh | 64 | Delhi | 60000 |
  11. | 1 | Ramesh | 32 | Ahmedabad | 20000 |
  12. +----+---------+-----+-----------+--------+
  13. Fetched 6 row(s) in 1.73s
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号