美文网首页
hive sql 常用场景一

hive sql 常用场景一

作者: 在路上很久了 | 来源:发表于2019-05-09 16:09 被阅读0次

    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 常用场景一

          本文链接:https://www.haomeiwen.com/subject/btzwoqtx.html