栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

hive 面试(测试sql面试题)

hive 面试(测试sql面试题)

目录

0 需求

1 数据准备

2 求解

3 小结


0 需求

1 数据准备

数据

1aa
2aa
3aa
4d
5c
6aa
7aa
8e
9f
10g

建表

create table a(
    id string,
    name string
)
ROW format delimited FIELDS TERMINATED BY ",";

加载数据

load data local inpath "/home/dandan/a.txt" into table a;

2 求解

A->B

A->B
select flg as id
      ,concat_ws('|',collect_list(name)) as name
from(
    select id
          ,name
          ,first_value(id) over(partition by name order by cast(id as int) desc) as flg
    from a
) t
group by flg

 A->C

select max(id) as id
      ,concat_ws('|',collect_list(name)) as name
from(
    select id
          ,name
          ,sum(if(name!=lag_name,1,0)) over(order by cast(id as int)) as flg 
    from(
        select id
              ,name
              ,lag(name,1,name) over(order by cast(id as int)) as lag_name
        from a
    ) m
) n
group by flg

3 小结

本题还是利用开窗函数打标签进行辅助计算,并利用了分类重分组的思想(sum() over(order by )).

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/771426.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号