一、over(partition by ......)主要和聚合函数sum()、count()、avg()等结合使用,实现分组聚合的功能

示列:根据day_id日期和mac_id机器码进行聚合分组求每一天的该机器的销量和即sum_num,hive sql语句:select day_id,mac_id,mac_color,day_num,sum(day_num)over(partition by day_id,mac_id order by day_id) sum_num from test_temp_mac_id;

注:day_id,mac_id,mac_color,day_num为查询原有数据,sum_num为计算结果

day_id mac_id mac_color day_num sum_num
20171011 1292 金色 1 89
20171011 1292 金色 14 89
20171011 1292 金色 2 89
20171011 1292 金色 11 89
20171011 1292 黑色 2 89
20171011 1292 粉金 58 89
20171011 1292 金色 1 89
20171011 2013 金色 10 22
20171011 2013 金色 9 22
20171011 2013 金色 2 22
20171011 2013 金色 1 22
20171012 1292 金色 5 18
20171012 1292 金色 7 18
20171012 1292 金色 5 18
20171012 1292 粉金 1 18
20171012 2013 粉金 1 7
20171012 2013 金色 6 7
20171013 1292 黑色 1 1
20171013 2013 粉金 2 2
20171011 12460 茶花金 1 1

二、over(partition by ......)与group by 区别

如果用group by实现一中根据day_id日期和mac_id机器码进行聚合分组求每一天的该机器的销量和即sum_num,

则hive sql语句为:select day_id,mac_id,sum(day_num) sum_num from test_temp_mac_id group by day_id,mac_id order by day_id;结果如下表

注:我们可以观察到group by可以实现同样的分组聚合功能,但sql语句不能写与分组聚合无关的字段,否则会报错,即group by 与over(partition by ......)主要区别为,带上group by的hive sql语句只能显示与分组聚合相关的字段,而带上over(partition by ......)的hive sql语句能显示所有字段.。

day_id mac_id sum_num
20171011 124609 1
20171011 20130 22
20171011 12922 89
20171012 12922 18
20171012 20130 7
20171013 12922 1
20171013 20130 2

https://blog.csdn.net/qq_37325859/article/details/78222712

hive开窗函数over(partition by ......)用法的更多相关文章

  1. 1.hive开窗函数,分析函数

    http://yugouai.iteye.com/blog/1908121 分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行.开窗函数指 ...

  2. Week08_day01 (Hive开窗函数 row_number()的使用 (求出所有薪水前两名的部门))

    数据准备: 7369,SMITH,CLERK,7902,1980-12-17,800,null,20 7499,ALLEN,SALESMAN,7698,1981-02-20,1600,300,30 7 ...

  3. Hive开窗函数的理解

    1.从一个sql语句开始 select id,sum(price) over(partition by id order by price desc) from books; sum作为聚合函数的时候 ...

  4. oracle 高级用法, DECODE 排序, OVER(PARTITION BY X ORDER BY Y DESC) 开窗函数

    场景 01 (IN 语句 排序 decode() 函数): 1,我们在查询中会经常使用这样的用法, select * from table_name t where t.id in (1, 3, 7, ...

  5. over(partition by)开窗函数的使用

    开窗函数是分析函数中的一种,开窗函数与聚合函数的区别是:开窗函数是用于计算基于组的某种聚合值且每个的组的聚合计算结果可以有多行,而聚合函数每个组的聚合计算结果只有一个.使用开窗函数可以在没有group ...

  6. sql server ,OVER(PARTITION BY)函数用法,开窗函数,over子句,over开窗函数

    https://technet.microsoft.com/zh-cn/library/ms189461(v=sql.105).aspx https://social.msdn.microsoft.c ...

  7. hive常用函数 wordCount--Hive窗口函数1.1.1 聚合开窗函数聚合开窗函数实战

    第三天笔记 第三天笔记 SQL练习Hive 常用函数关系运算数值计算条件函数日期函数重点!!!字符串函数Hive 中的wordCount1.1 Hive窗口函数1.1.1 聚合开窗函数聚合开窗函数实战 ...

  8. SQLServer学习笔记<>.基础知识,一些基本命令,单表查询(null top用法,with ties附加属性,over开窗函数),排名函数

    Sqlserver基础知识 (1)创建数据库 创建数据库有两种方式,手动创建和编写sql脚本创建,在这里我采用脚本的方式创建一个名称为TSQLFundamentals2008的数据库.脚本如下:   ...

  9. hive SQL 初学者题目,实战题目 字符串函数,日期拼接,开窗函数。。。。

    sql:Hive实现按照指定格式输出每七天的消费平均数输出格式:2018-06-01~2018-06-07 12.29...2018-08-10~2018-08-16 80.67 答案:-- 1.先将 ...

随机推荐

  1. Linux-进程间通信(一): 管道

    1. 管道局限性: (1) 半双工:(若模拟全双工,可以使用两个管道,即,proc1-->proc2一条管道,proc2-->proc1一条管道) (2) 只能在具有公共祖先的进程之间使用 ...

  2. kuangbin带你飞 后缀数组 题解

    2份模板 DC3 . 空间复杂度O3N 时间复杂度On #define F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb)) #define G(x) ((x) < ...

  3. 【转载】Window 窗口层次关系

    相信在Window 下面编程的很多兄弟们都不是很清楚Window 中窗口的层次关系是怎么样的,这个东西很久已经研究过一下,后来又忘记了,今天又一次遇到了这个问题,所以便整理一下.下面就说说Window ...

  4. 团队开发中git分支的使用

    1.Github上保持两个分支:master和develop. master是主分支,对项目进行tag或发布版本等操作,都必须在该分支上进行.最好设为不可提交只能合并的. develop是开发分支,从 ...

  5. php7使用curl

    /** * @param string $url * @return mixed */ public function doGet($url) { //初始化 $ch = curl_init(); c ...

  6. 浙江省第十二届省赛 B - Team Formation

    Description For an upcoming programming contest, Edward, the headmaster of Marjar University, is for ...

  7. CF 1003B Binary String Constructing 【构造/找规律/分类讨论】

    You are given three integers a, b and x. Your task is to construct a binary string s of length n=a+b ...

  8. HDU 6208 The Dominator of Strings【AC自动机/kmp/Sunday算法】

    Problem Description Here you have a set of strings. A dominator is a string of the set dominating al ...

  9. CodeForces 672B Different is Good

    链接:http://codeforces.com/problemset/problem/672/B 本文链接:http://www.cnblogs.com/Ash-ly/p/5491176.html ...

  10. MapReduce 编程模板

    1.MapReduce 编程模型的5个步骤: 1)迭代,将输入数据解析成 key/value 对: 2)将解析的 key/value经过Map处理映射成另一组key/value对: 3)根据key进行 ...