当前位置: 首页 > news >正文

(09)Hive——CTE 公共表达式

目录

1.语法

 2. 使用场景

select语句

chaining CTEs 链式

union语句

insert into 语句

create table as 语句

前言

   Common Table Expressions(CTE):公共表达式是一个临时的结果集,该结果集是从with子句中指定的查询派生而来的,紧跟在select 或 insert关键字之前。CTE可以在 select,insert,  create table as select 等语句中使用。

1.语法

[wtih CommonTableExpression]
selectcolumn1,column2, ...
from table 
[where 条件] 
[group by column]
[order by column] 
[cluster by column| [distribute by column] [sort by column] 
[limit [offset,] rows];

 2. 使用场景

  • select语句

with tmp as (selectoid,uid,otime,date_format(otime, 'yyyy-MM') as dt,oamount,---计算rk的目的是为了获取记录中的第一条row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) rkfrom t_order
)selectuid,--每个用户一月份的订单数sum(if(dt = '2018-01', 1, 0)) as  m1_count,--每个用户二月份的订单数sum(if(dt = '2018-02', 1, 0)) as  m2_count
from tmpgroup by uidhaving m1_count >0 and m2_count=0;
  • chaining CTEs 链式


with tmp1 as (selectoid,uid,otime,date_format(otime, 'yyyy-MM') as dt,oamount,---计算rk的目的是为了获取记录中的第一条row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) as rkfrom t_order
),tmp2 as(selectuid,--每个用户一月份的订单数sum(if(dt = '2018-01', 1, 0)) as m1_count,--每个用户二月份的订单数sum(if(dt = '2018-02', 1, 0)) as m2_countfrom tmp1group by uidhaving m1_count > 0and m2_count = 0)
select * from tmp2 limit 1;
  • union语句

with q1 as (select * from student where num = 95002),q2 as (select * from student where num = 95004)
select * from q1 union all select * from q2;
  • insert into 语句

with tmp1 as (selectoid,uid,otime,date_format(otime, 'yyyy-MM') as dt,oamount,---计算rk的目的是为了获取记录中的第一条row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) as rkfrom t_order
),tmp2 as(selectuid,--每个用户一月份的订单数sum(if(dt = '2018-01', 1, 0)) as m1_count,--每个用户二月份的订单数sum(if(dt = '2018-02', 1, 0)) as m2_countfrom tmp1group by uidhaving m1_count > 0and m2_count = 0)insert into tmp3
select * from tmp2 limit 10;
  • create table as 语句

--- 从tmp2 表中取10条数据,基于此创建表tmp3 
create table tmp3 as 
with tmp1 as (selectoid,uid,otime,date_format(otime, 'yyyy-MM') as dt,oamount,---计算rk的目的是为了获取记录中的第一条row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) as rkfrom t_order
),tmp2 as(selectuid,--每个用户一月份的订单数sum(if(dt = '2018-01', 1, 0)) as m1_count,--每个用户二月份的订单数sum(if(dt = '2018-02', 1, 0)) as m2_countfrom tmp1group by uidhaving m1_count > 0and m2_count = 0)
select * from tmp2 limit 10;

http://www.lryc.cn/news/301468.html

相关文章:

  • Spring 用法学习总结(四)之 JdbcTemplate 连接数据库
  • 第 385 场 LeetCode 周赛题解
  • 什么是RabbitMQ?
  • JWT登录验证前后端设计与实现笔记
  • 自定义类型详解 ----结构体,位段,枚举,联合
  • VueCLI核心知识综合案例TodoList
  • 关于cuda路径问题
  • 六、Spring/Spring Boot整合ActiveMQ
  • 树莓派4B(Raspberry Pi 4B)使用docker搭建springBoot/springCloud服务
  • 数据库设计、JDBC、数据库连接池
  • SpringBoot实现OneDrive文件上传
  • C++初阶:容器适配器介绍、stack和queue常用接口详解及模拟实现
  • GRUB and the Boot Process on UEFI-based x86 Systems
  • 2.C语言——输入输出
  • MySQL篇之SQL优化
  • QGis —— 1、Windows10下载安装QGis及插件
  • 【打工日常】使用docker部署Dashdot工具箱
  • 使用client-only 解决组件不兼容SSR问题
  • 基于Java SSM框架实现网上报名系统项目【项目源码+论文说明】
  • 7.1 Qt 中输入行与按钮
  • 云计算基础-网络虚拟化
  • 166基于matlab的通过峭度指标与互相关系数筛选IMF进行SVD分解去噪
  • 第六十三天 服务攻防-框架安全CVE复现DjangoFlaskNode.JSJQuery
  • 最大子序和+旅行问题——单调队列
  • Unity设备分级策略
  • 自己在开发AI应用的过程总结的 Prompt - 持续更新
  • STM32——OLED菜单
  • Open CASCADE学习|布尔运算后消除内部拓扑
  • 【数据仓库】主题域和数据域
  • C#,二分法(Bisection Method)求解方程的算法与源代码