HiveSQL面试题47:京东面试题
Posted 莫叫石榴姐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HiveSQL面试题47:京东面试题相关的知识,希望对你有一定的参考价值。
目录
0 需求
1 数据准备
数据
1 | aa |
2 | aa |
3 | aa |
4 | d |
5 | c |
6 | aa |
7 | aa |
8 | e |
9 | f |
10 | g |
建表
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 )).
以上是关于HiveSQL面试题47:京东面试题的主要内容,如果未能解决你的问题,请参考以下文章