hive sql 常用场景一

create table default.score (

userid string,

monthid string,

score  int

);

/*

insert into default.score values('001','01',70);

insert into default.score values('001','02',50);

insert into default.score values('001','03',60);

insert into default.score values('001','04',90);

insert into default.score values('001','05',70);

*/

--累计值写法一

select a.userid,a.monthid,a.score,sum(b.score)

from default.score a  join default.score  b  on a.userid = b.userid

where    a.monthid >= b.monthid

group by a.userid,a.monthid,a.score

order by a.userid,a.monthid;

--累计值写法二

select

userid,monthid,score,

sum(score) over(partition by userid order by monthid)

from default.score ;

--排名三种函数进行对比

--row_number

select userid

      ,monthid

      ,score

      ,row_number() over(order by score) as dense_number

from default.score;

--rank:

select userid

      ,monthid

      ,score

      ,rank() over(order by score) as rank

from default.score;

--dense_number

select userid

      ,monthid

      ,score

      ,dense_rank() over( order by score) as dense_number

from default.score;

--grouping sets

select userid

      ,monthid

      ,sum(score)

from default.score

group by  userid,monthid

grouping sets ( (userid , monthid) ,(userid), (monthid));

--时间转换

select from_unixtime(unix_timestamp('20180905','yyyymmdd'),'yyyy-mm-dd');

select from_unixtime(unix_timestamp('2018-09-05','yyyy-mm-dd'),'yyyymmdd');

你可能感兴趣的:(hive sql 常用场景一)