当前位置: 首页 > 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;

相关文章:

  • Attention Is All Your Need论文翻译
  • 详解CC++内存管理(new和delete)
  • C#面:在.NET中 类 System.Web.UI.Page 可以被继承吗?
  • 解决‘vue‘ 不是内部或外部命令,也不是可运行的程序(设置全局变量)
  • Lua 教程
  • 3 scala集合-Set
  • Hive3.1.2——企业级调优
  • 如何用 npm 运行本地 js 文件
  • 山脉的个数/攀登者
  • 二叉树-------前,中,后序遍历 + 前,中,后序查找+删除节点 (java详解)
  • Stream流学习笔记
  • openJudge | 过滤多余的空格 C语言
  • Leetcode29:两数相除
  • 【python之美】减少人工成本之批量拿取文件名保存_4
  • Rust的Match语句:强大的控制流运算符
  • 【许晓笛】 EOS 智能合约案例解析(3)
  • CAP 一致性协议及应用解析
  • CSS3 变换
  • Git的一些常用操作
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • NSTimer学习笔记
  • python学习笔记-类对象的信息
  • Ruby 2.x 源代码分析:扩展 概述
  • 成为一名优秀的Developer的书单
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 多线程 start 和 run 方法到底有什么区别?
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 函数式编程与面向对象编程[4]:Scala的类型关联Type Alias
  • 码农张的Bug人生 - 见面之礼
  • 删除表内多余的重复数据
  • 适配mpvue平台的的微信小程序日历组件mpvue-calendar
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 网页视频流m3u8/ts视频下载
  • 源码安装memcached和php memcache扩展
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • Spring第一个helloWorld
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • "无招胜有招"nbsp;史上最全的互…
  • #前后端分离# 头条发布系统
  • (1)STL算法之遍历容器
  • (20050108)又读《平凡的世界》
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (数据结构)顺序表的定义
  • (一)WLAN定义和基本架构转
  • (转)树状数组
  • (转)重识new
  • .net core控制台应用程序初识
  • .net 托管代码与非托管代码
  • .net6 webapi log4net完整配置使用流程
  • .Net转Java自学之路—基础巩固篇十三(集合)