Hive的一個面試題
需求:求出每個User截止當月總的流量
user date traffic
熊貓tv 2018-01-02 5
快手 2018-01-02 3
YY 2018-01-02 2
抖音 2018-01-02 15
熊貓tv 2018-01-03 5
快手 2018-01-03 3
YY 2018-01-03 2
抖音 2018-01-03 15
熊貓tv 2018-02-02 5
快手 2018-02-02 3
YY 2018-02-02 2
抖音 2018-02-02 15
熊貓tv 2018-02-03 5
快手 2018-02-03 3
YY 2018-02-03 2
抖音 2018-02-03 15
熊貓tv 2018-03-02 5
快手 2018-03-02 3
YY 2018-03-02 2
抖音 2018-03-02 15
熊貓tv 2018-03-03 5
快手 2018-03-03 3
YY 2018-03-03 2
抖音 2018-03-03 15
hive建立表:
create table user_traffic(user string,date string,traffic bigint) row format delimited fields terminated by '\t';
load data local inpath '/home/hadoop/data/user_traffic.txt' overwrite into table user_traffic;
先按user,月份分組求出每個月的traffic
select temp.user,temp.yearmonth,sum(temp.traffic) from(select
user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic
from user_traffic) temp group by temp.user,temp.yearmonth;
表自連線
select t1.*,t2.* from (select temp.user,temp.yearmonth,sum(temp.traffic) from
(select user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic from user_traffic)
temp group by temp.user,temp.yearmonth) t1,(select temp.user,temp.yearmonth,sum(temp.traffic) from
(select user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic from user_traffic) temp group by
temp.user,temp.yearmonth) t2 where t1.yearmonth=t2.yearmonth and t1.user = t2.user and t1.yearmonth >= t2.yearmonth;
select t.user,t.yearmonth,sum(t.total) from (select t1.user,t1.yearmonth,t1.total from (select temp.user,temp.yearmonth,sum(temp.traffic) as total from (select user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic from user_traffic) temp group by temp.user,temp.yearmonth) t1,(select temp.user,temp.yearmonth,sum(temp.traffic) as total from (select user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic from user_traffic) temp group by temp.user,temp.yearmonth) t2 where t1.user = t2.user and t1.yearmonth >= t2.yearmonth) t group by t.user,t.yearmonth;
相關文章
- Hive 面試問題Hive面試
- Apache Hive 面試問答題ApacheHive面試
- PHP引用的一個面試題PHP面試題
- 又一個有趣的面試題面試題
- 一個Promise面試題Promise面試題
- 陳皓:一個 fork 的面試題面試題
- Hive partition prune的一個BugHive
- 記一個面試題引發的思考面試題
- 一個 Handler 面試題引發的血案!!!面試題
- 關於Integer面試的一個問題面試
- 【SQL 學習】一個面試題SQL面試題
- 分享一個PHP面試題總結PHP面試題
- Hive-常見調優方式 && 兩個面試sqlHive面試SQL
- 一個考併發的面試題,怎麼答??面試題
- 一個node連結串列翻轉的面試題面試題
- 面試中一個暴露能力等級的問題面試
- 15 個 Linux ls 命令的面試問題(一)Linux面試
- 面試題,實現一個求和函式面試題函式
- 一個面試題引起的SpringBoot啟動解析面試題Spring Boot
- 一個經典程式設計面試題的“隱退”程式設計面試題
- 爆肝一個月,我做了個免費的面試刷題網面試
- 五個典型的JavaScript面試題JavaScript面試題
- 5個典型的JavaScript面試題JavaScript面試題
- 我的一個朋友過來面試引發我要說的一個小話題面試
- 面試題 | 請實現一個深拷貝面試題
- 有一個java面試題求大神解讀Java面試題
- 肖sir__ 第一個月綜合面試題面試題
- 一個面試面試
- 面試題:一個整數,它加上100後是一個完全...面試題
- 面試題之正規表示式解析一個完整的url面試題
- hive基礎總結(面試常用)Hive面試
- Java面試題(一)Java面試題
- 面試題系列一面試題
- 今天BOSS約了個面試,HR直接發我一道面試題面試題
- 關於 Promise 的 9 個面試題Promise面試題
- 10 個常問的 JS 面試題JS面試題
- 5 個很好的 Python 面試題Python面試題
- 20個必會的JavaScript面試題JavaScript面試題