当前位置:   article > 正文

Hive行转列、列转行_impala 列转行

impala 列转行

目录

行转列

列转行


行转列

数据准备:

  1. create table stu_score(
  2. stu_id string,
  3. english bigint,
  4. math bigint,
  5. science bigint
  6. );
  7. insert into stu_score values
  8. ('甲',100,90,95),
  9. ('乙',95,90,100),
  10. ('丙',65,99,88);

行转列: 主要用到 Leteral view explode

  1. select
  2. stu_id
  3. ,subject,score
  4. from stu_score
  5. LATERAL VIEW explode (
  6. map(
  7. 'english',english,
  8. 'math',math,
  9. 'science',science
  10. ) ) tmp
  11. as subject,score;

列转行

数据准备:

  1. -- 直接拿行转列的结果演示
  2. create table stu_score_row
  3. as
  4. select
  5. stu_id
  6. ,subject,score
  7. from stu_score
  8. LATERAL VIEW explode (
  9. map(
  10. 'english',english,
  11. 'math',math,
  12. 'science',science
  13. ) ) tmp
  14. as subject,score;

列转行:主要用到 concat_wc collect_list

  1. select
  2. stu_id
  3. ,concat_ws(',',collect_list(subject)) as subs
  4. from stu_score_row
  5. group by stu_id
  6. ;
  7. -- collect_list 不去重,collect_set 去重。 字段类型需要是String

希望本文对你有帮助,请点个赞鼓励一下作者吧~ 谢谢!

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/894900
推荐阅读
相关标签
  

闽ICP备14008679号