Hive使用--常用普通函數(shù)&窗口函數(shù)

1. 常用函數(shù)

數(shù)據(jù)本體:

1,zhangsan,56.7

2,lisi,78.9

3,wagnwu,90.8

4,趙六,100

1.1? case when

select id, name, score, case when score < 60 then 'bujige'

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? when score>60 and score<80 then 'youliang'

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else 'youxiu' end as? pingding

from student;


1.2 if語(yǔ)句

select id, name, score,

if (score<60,"Bujige", "Jige")? as PingDing from student;

1.3 插入數(shù)據(jù)

先要插入數(shù)據(jù),兩種方法,一種是直接values,一種是select另外一個(gè)子表作為

insert into table student values(5, "laoxing",null);?

insert into table student? ? (select 6, "guyizaodekong", null);



1.4 nvl函數(shù):控制轉(zhuǎn)換函數(shù)

select id, score, nvl(score, 0) from student;

1.5 其他常用函數(shù)

https://www.cnblogs.com/MOBIN/p/5618747.html

2. 窗口函數(shù)

2.0 數(shù)據(jù)本體

數(shù)據(jù)內(nèi)容

1,18,a,male

2,19,a,male

3,22,a,female

4,16,b,female

5,30,b,male

6,26,b,female

創(chuàng)建table

use frog_db;

drop table userinfo;

create table userinfo(

? id string,

? age int,

? title string,

? sex string)

row format delimited fields terminated by ','

lines terminated by '\n'

stored as textfile;

加載數(shù)據(jù):

load data local inpath "" into table userinfo;


2.1 row_number() over()

得到每個(gè)性別中年齡最大的2條數(shù)據(jù)。

select?

*,

row_number() over(partition by sex order by age desc) rn

from userinfo;


select * from aaaa where rn <3;


可以用來(lái)去重。

2.2 sum() over()

得到每個(gè)月的累加和。

數(shù)據(jù):

A,2012-01,1000

A,2012-02,2030

A,2012-03,3600

A,2012-04,6008

A,2012-05,3000

B,2012-01,2000

B,2012-02,2300

B,2012-03,1800

B,2012-04,2000

B,2012-05,1300

B,2012-06,1600

B,2012-07,5000

C,2012-01,1020

C,2012-02,2000

C,2012-03,3200

C,2012-04,6000

C,2012-05,5300

C,2012-06,8800

C,2012-07,9000

建表的語(yǔ)句:

drop table saleinfo;

create table saleinfo ( product_name? string,

month string,?

?money string)

row format delimited fields terminated by ',';

導(dǎo)入語(yǔ)句:

load data local inpath '/home/froghd/data1.txt' into table saleinfo;


累加怎么算?

select? *,

sum(money) over(partition by product_name order by month) as money_cumsum from saleinfo;


分組求和:

select product_name, sum(money) from saleinfo group by product_name;


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本篇文章長(zhǎng)更,歡迎大家收藏和喜歡 2018.12.25 字符串函數(shù)2 1.正則表達(dá)式解析函數(shù):regexp_ext...
    清聽(tīng)閱讀 4,848評(píng)論 0 1
  • 1、系統(tǒng)內(nèi)置函數(shù)1.查看系統(tǒng)自帶的函數(shù)hive> show functions;2.顯示自帶的函數(shù)的用法hive>...
    我還不夠強(qiáng)閱讀 982評(píng)論 0 0
  • Select 窗口函數(shù) over():指定分析函數(shù)的數(shù)據(jù)窗口大小,這個(gè)數(shù)據(jù)窗口大小隨行的變化而變化。CURRENT...
    歌哥居士閱讀 1,336評(píng)論 0 0
  • 1.Hive的概述 √ 意義:在于大幅度降低工程師學(xué)習(xí)MapReduce的學(xué)習(xí)成本,讓好用(計(jì)算速度快)的MapR...
    C8H11O2N_4cd4閱讀 385評(píng)論 0 0
  • 久違的晴天,家長(zhǎng)會(huì)。 家長(zhǎng)大會(huì)開(kāi)好到教室時(shí),離放學(xué)已經(jīng)沒(méi)多少時(shí)間了。班主任說(shuō)已經(jīng)安排了三個(gè)家長(zhǎng)分享經(jīng)驗(yàn)。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,819評(píng)論 16 22

友情鏈接更多精彩內(nèi)容