hive> select * from app_data_stats_historical where os='1' group by dt limit 100;
出现结果如下:
2014-01-01
2014-01-06
......
2014-02-07
2014-02-10
2014-02-14
2014-02-17
2014-02-24
(只返回了一列日期。说明* 不起作用。不过这样,可以查看总共哪些日期,有效,存在数据)
加上having命令,having只作用于group by中的字段,非group的字段不行:
select * from app_data_stats_historical where os='1' group by dt having dt>' limit 1002014-02-01' limit 100;
2014-02-03
2014-02-04
2014-02-07
2014-02-10
2014-02-14
2014-02-17
2014-02-24
(可以用日期进行比较。)
 
hive> select * from app_data_stats_historical where os='1' order by dt desc limit 100;
字段是全部返回的。降序。
 group by 多个字段:2周内分联盟(5是多盟)分平台(分平台指的是分ios和android),分时段的曝光及点击。
select substr(createtime,12,2)hour,logtype,os_id,count(distinct logtype)
from wizad_mdm_raw_hdfs
where ad_id in('19057','19058','812b4ba287f5ee0bc9d43bbf5bbe87fb') and day >= '2014-12-01'and adn=5
group by substr(createtime,12,2), logtype, os_id;
注意,时段hour提取函数substr和substring 是通用的!
 
或者分步做:将group的字段按取值加到where中去(os_id取值1是android,2是iOS):
select substr(createtime,12,2)hour,logtype,count(*)
from wizad_mdm_raw_hdfs
where ad_id in('19057','19058','812b4ba287f5ee0bc9d43bbf5bbe87fb') and day >= '2014-12-01'and os_id='2' and adn=5
group by substr(createtime,12,2), logtype;
以及:
select substr(createtime,12,2),logtype,count(*)
from wizad_mdm_raw_hdfs
where ad_id in('19057','19058','812b4ba287f5ee0bc9d43bbf5bbe87fb') and day >= '2014-12-01'and os_id='1' and adn=5
group by substr(createtime,12,2), logtype;
 
substr(createtime,12,2)提取小时字段,起hour别名,group by 不支持。
group 里面不能起别名hour,直接用或引用都不行。
(1)Group by定义别名hour,报错,解析错误
selectsubstr(createtime,12,2),logtype,count(*)
from wizad_mdm_raw_hdfs
where day >= '2014-12-01'
group by substr(createtime,12,2) hour,logtype;
 
(2)引用定义的hour别名也不行: Line 4:9 Invalid table alias or column reference 'hour'
select substr(createtime,12,2)hour,logtype,count(*)
from wizad_mdm_raw_hdfs
where day >= '2014-12-01'
group by hour, logtype;
 
 
 
hive wiki上说group by有两种使用情况限制
(1)group by只有一列,则distinct只能作用一列(可以多次作用同一列)
 INSERT OVERWRITE TABLE pv_gender_sum
 SELECT pv_users.gender, count (DISTINCT pv_users.userid)
 FROM pv_users
 GROUP BY pv_users.gender;
 
 像这样 SELECTpv_users.gender, count(DISTINCT pv_users.userid), count(*), sum(DISTINCTpv_users.userid)
 只distinct一列,但出现多次是可以的。
下面的查询错误,不允许DISTINCT多个列
 INSERT OVERWRITE TABLE pv_gender_agg
 SELECT pv_users.gender, count(DISTINCT pv_users.userid), count(DISTINCTpv_users.ip)
 FROM pv_users
 GROUP BY pv_users.gender;
但我测试发现是可以的。有大神可以给我解释么?
 

group by统计去重distinct个数
select substr(createtime,12,2)hour,logtype,os_id,count(distinct logtype), count(distinct os_id)
from wizad_mdm_raw_hdfs
where ad_id in('19057','19058','812b4ba287f5ee0bc9d43bbf5bbe87fb') and day >= '2014-12-01'and adn=5
group by substr(createtime,12,2), logtype, os_id;
 
 
或者一些测试脚本都说明了,我们
select logtype, count(distinct os_id),count(distinctip),count(distinct id)
from wizad_mdm_raw_hdfs
where ad_id in('19057','19058','812b4ba287f5ee0bc9d43bbf5bbe87fb') and day >= '2014-12-01'
group by logtype;
结果:
1      2       8493314 77579300
2      2       267685  211119
 
select substr(createtime,12,2) hour, count(distinctid),count(distinct ip)
from wizad_mdm_raw_hdfs
where ad_id in('19057','19058','812b4ba287f5ee0bc9d43bbf5bbe87fb') and day >= '2014-12-01'and adn=5
group by substr(createtime,12,2);
                                                     
部分结果如下:
00     1598136 154053
04     989745  51201
……
18     1711493 201436
21     3644241 374243
 
 
(2)另一个wiki限制说明:group by后,除了作用列和聚合函数统计项,多余列不能存在。
这个我验证过确实是不行的。
SELECT a,sum(b)
FROM t1
GROUP BY a;
是正确的
 
下面是错误的。
SELECT a,b
FROM t1
GROUP BY a;
因为有多余列b,其不在group by的字段属性,(且不是聚合函数).
查询结果会是这样
a   b
------
100 1
100 2
100 3
gourp by a后,b不能成为集合{1,2,3}返回,你可以count,但不能直接返回b。b是多值的。hive摒弃了这种猜测无效的SQL(HQL,要准确):有一列在select子句中,却不包含在GROUPBY子句中。
pig是可以构成集合返回的。
 
 

hive:(group by, having;order by)的使用;group by+多个字段,以及wiki说的group by两种使用限制验证的更多相关文章

  1. SQL的GROUP BY 与 Order By

    1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...

  2. hive的高级查询(group by、 order by、 join 、 distribute by、sort by、 clusrer by、 union all等)

    查询操作 group by. order by. join . distribute by. sort by. clusrer by. union all 底层的实现 mapreduce 常见的聚合操 ...

  3. hive的strict模式;where,group by,having,order by同时使用的执行顺序

    主要限制三种情况 (1) 有partition的表查询需要加上where子句,筛选部分数据实现分区裁剪,即不允许全表全分区扫描,防止数据过大 (2) order by 执行时只产生一个reduce,必 ...

  4. 深度分析mysql GROUP BY 与 ORDER BY

    鉴于项目的需要,就从网上找到该文章,文章分析得很详细也很易懂,在android里,(不知道是不是现在水平的限制,总之我还没找到在用ContentProvider时可以使用子查询),主要方法是用SQLi ...

  5. mysql GROUP BY 与 ORDER BY 查询不是最新记录

    转载:http://blog.csdn.net/qvbfndcwy/article/details/7200910 鉴于项目的需要,就从网上找到该文章,文章分析得很详细也很易懂,在android里,( ...

  6. mysql “group by ”与"order by"的研究--分类中最新的内容

    这两天让一个数据查询难了.主要是对group by 理解的不够深入.才出现这样的情况这种需求,我想很多人都遇到过.下面是我模拟我的内容表我现在需要取出每个分类中最新的内容 select * from ...

  7. select的5中子句where,group by, havaing, order by, limit的使用顺序及实例

    -- 语法: SELECT select_list FROM table_name [ WHERE search_condition ] [ GROUP BY group_by_expression ...

  8. GROUP BY和ORDER BY共用

    SELECT BatchNumber,MAX(Id) FROM dbo.SceneryOrder AND BatchNumber<>'' GROUP BY BatchNumber DESC

  9. 查询语句中select from where group by having order by的执行顺序

    查询语句中select from where group by having order by的执行顺序   1.查询中用到的关键词主要包含六个,并且他们的顺序依次为  select--from--w ...

随机推荐

  1. Sencha EXTJS6的 Eclipse 插件安装指南

    Sencha EXTJS的 Eclipse 插件安装指南 (翻译:苏生米沿) 本文地址:http://blog.csdn.net/sushengmiyan/article/details/52566 ...

  2. Dynamics CRM2016 Set Values of all Data Types using Web API

    之前的博客里有谈到了web api的增删改查,里面会涉及到各种类型字段的赋值,因为时间和精力关系,没有对所有的字段类型一一测试,这篇博文中给出了全部的 http://inogic.com/blog/2 ...

  3. Web自动化框架LazyUI使用手册(3)--单个xpath抓取插件详解(selenium元素抓取,有此插件,便再无所求!)

    概述 前面的一篇博文粗略介绍了基于lazyUI的第一个demo,本文将详细描述此工具的设计和使用. 元素获取插件:LazyUI Elements Extractor,作为Chrome插件,用于抓取页面 ...

  4. Dynamics CRM2013 6.1.1.1143版本插件注册器的一个bug

    最近在做的项目客户用的是CRM2013sp1版本,所以插件注册器使用的也是与之对应的6.1.1.1143,悲剧的事情也因此而开始. 在插件中注册step时,工具里有个run in user's con ...

  5. For oracle databases, if the top showing the oracle database, then oracle process is using the top c

    Note 805586.1   Troubleshooting Session Administration (Doc ID 805586.1)Note 822527.1   How To Find ...

  6. Android ListView中Item点击事件失效解决方案

    欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 在平常的开发过程中,我们的ListView可能不只是简单的显示下文本或者按钮,更多的是显示复杂的布局,这样的话,我们就得自己写布 ...

  7. java操作properties配置文件

    Java中有个类Properties(Java.util.Properties),主要用于读取Java的配置文件,将一些可能需要变化的值存放在properties中进行配置,通常为为.properti ...

  8. 为你的MacOS App添加开机自启动(Swift)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/52104828 ...

  9. Windows下多线程数据同步互斥的有关知识

     对于操作系统而言,在并行程序设计中难免会遇到数据同步和共享的问题,本文针对这个问题,以windows系统为例回顾一下资源同步的相关问题.要点如下: 1.同步和数据共享  数据征用 2.同步原语 ...

  10. ajax核心技术1---XMLHttpRequset对象的使用

    AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术.AJAX = 异步 Ja ...