hive sql例子

例子1: 

select * from business;
jack	2017-01-01	10
jack	2017-01-02	10
tom	    2017-01-01	10
tom	    2017-01-02	10
tom	    2017-01-03	10
andy	2017-01-01	10
andy	2017-01-02	10
andy	2017-01-03	10
lucy	2017-01-01	10
lucy	2017-01-02	10
lucy	2017-01-03	10
jack	2017-01-03	10
jack	2017-02-01	10
jack	2017-02-02	10
tom	    2017-02-01	10
tom	    2017-02-02	10
tom	    2017-02-03	10
andy	2017-02-01	10

求二月份購買的顧客以及總人數:
方法1: 注意無條件join的方法1=1
select t1.name, t2.count
from
(select name from business where substr(orderdate,1,7)="2017-02" group by name) t1
left join 
(select count(distinct name) count from business where substr(orderdate,1,7)="2017-02") t2
on 1 = 1;
方法二:
select name, count(*) over() from business where substr(orderdate,1,7)="2017-02" group by name;

最終輸出:
OK
andy	3
jack	3
tom	3
Time taken: 19.661 seconds, Fetched: 3 row(s)

例子2

hive> select * from person_info1;

悟空	A	男
大海	A	男
宋宋	B	男
鳳姐	A	女
婷姐	B	女
婷婷	B	女

hive> desc person_info1;
OK
name                	string              	                    
type                	string              	                    
sex                 	string  

需求:需要將type 的男女各個數量區分開來如下。
select type,
sum(case when sex = '男' then 1 else 0 end) male_count,
sum(case when sex = '女' then 1 else 0 end) female_count
from person_info1
group by type;

type  男數量  女數量
A	    2      1
B	    1      2

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章