• Hive数据仓库行转列


    查了很多资料发现网上很多文章都是转发和抄袭,有些问题。这里分享一个自己项目中使用的行转列例子,供大家参考。代码如下:

    SELECT
      my_id,
      nm_cd_map['A'] AS my_cd_a,
      nm_cd_map['B'] AS my_cd_b,
      nm_cd_map['C'] AS my_cd_c,
      nm_num_map['A'] AS my_num_a,
      nm_num_map['B'] AS my_num_b,
      nm_num_map['C'] AS my_num_c
    FROM
      (
        SELECT
          t.my_id,
          STR_TO_MAP(my_nm_cds,';',':') AS nm_cd_map,
          STR_TO_MAP(my_nm_nums,';',':') AS nm_num_map
        FROM
          (
            SELECT
              my_id,
              CONCAT_WS(';',COLLECT_LIST(CONCAT(my_nm,':',my_cd))) AS my_nm_cds,
              CONCAT_WS(';',COLLECT_LIST(CONCAT(my_nm,':',my_num))) AS my_nm_nums
            FROM
              (
                SELECT '1' AS my_id,'A' AS my_nm,'D01' AS my_cd,19 AS my_num
                UNION ALL
                SELECT '1' AS my_id,'B' AS my_nm,'D04' AS my_cd,18 AS my_num
                UNION ALL
                SELECT '1' AS my_id,'C' AS my_nm,'D02' AS my_cd,17 AS my_num
                UNION ALL
                SELECT '2' AS my_id,'A' AS my_nm,'D03' AS my_cd,16 AS my_num
                UNION ALL
                SELECT '2' AS my_id,'B' AS my_nm,'D05' AS my_cd,15 AS my_num
                UNION ALL
                SELECT '2' AS my_id,'C' AS my_nm,'D06' AS my_cd,14 AS my_num
              )
            GROUP BY my_id
          ) t
      ) t
    WHERE 1=1;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    如果是在SparkSQL或Presto平台,或者阿里云的MaxCompute平台,还可使用如下方式:

    -- 其实也可使用CONCAT然后STR_TO_MAP的方式,或者用MAP_FROM_ARRAYS,再或者用数组排序后ARRAY[n] AS的方式
    SELECT
      my_id,
      nm_cd_map['A'] AS my_cd_a,
      nm_cd_map['B'] AS my_cd_b,
      nm_cd_map['C'] AS my_cd_c,
      nm_num_map['A'] AS my_num_a,
      nm_num_map['B'] AS my_num_b,
      nm_num_map['C'] AS my_num_c
    FROM
      (
        SELECT
          t.my_id,
          MAP_FROM_ENTRIES(COLLECT_LIST(nm_cd)) AS nm_cd_map,
          MAP_FROM_ENTRIES(COLLECT_LIST(nm_num)) AS nm_num_map
        FROM
          (
            SELECT
              my_id,
              my_nm,
              my_cd,
              my_num,
              STRUCT(my_nm,my_cd) AS nm_cd,
              STRUCT(my_nm,my_num) AS nm_num
            FROM
              (
                SELECT '1' AS my_id,'A' AS my_nm,'D01' AS my_cd,19 AS my_num
                UNION ALL
                SELECT '1' AS my_id,'B' AS my_nm,'D04' AS my_cd,18 AS my_num
                UNION ALL
                SELECT '1' AS my_id,'C' AS my_nm,'D02' AS my_cd,17 AS my_num
                UNION ALL
                SELECT '2' AS my_id,'A' AS my_nm,'D03' AS my_cd,16 AS my_num
                UNION ALL
                SELECT '2' AS my_id,'B' AS my_nm,'D05' AS my_cd,15 AS my_num
                UNION ALL
                SELECT '2' AS my_id,'C' AS my_nm,'D06' AS my_cd,14 AS my_num
              )
          ) t
        GROUP BY my_id
      ) t
    WHERE 1=1;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
  • 相关阅读:
    使用ElementUI完成登入注册的跨域请求提高开发效率
    ElasticSearch
    猿创征文|宝藏工具篇|数字芯片设计,嵌入式开发,人工智能|没我可以,没你不行!
    SpringBoot集成ES、使用Java API对其进行简单的测试
    双目视觉实战---三维重建基础与极几何
    神经网络国内外发展概况,神经网络最新研究方向
    vue中使用electron
    深度翻页导出导致慢SQL,mysqlCPU飙升优化方案
    Docker+K8s基础(重要知识点总结)
    电脑找不到vcruntime140_1.dll丢失的解决方法-一键修复教程
  • 原文地址:https://blog.csdn.net/XueminXu/article/details/136190761