Select 配置单元中的下一个非空字段

Select the next non empty field in hive

我有一个包含 6 个字段的 table,如下所示:

Field1 Field2 Field3 Field4 Field5 Field6
ABC    45     XYZ           JKL    BNM
       65            QWE    JKL    
WER           YUI    IOP    GHJ

我想将数据从上面的 table 提取到一个新的 table,其中有 5 个字段,我们忽略空值。我的最终 table 应该是这样的:

Result1 Result2 Result3 Result4 Result5
ABC     45      XYZ     JKL     BNM
65      QWE     JKL
WER     YUI     IOP     GHJ

我已经开始使用 CASE WHEN 编写大量条件查询,但它已经失控并且容易出错。 是否可以在 Hive 中使用 regex_extract 查询获得 table?

假设 "empty values" 为 Null

select  fields[0]   as Field1
       ,fields[1]   as Field2
       ,fields[2]   as Field3
       ,fields[3]   as Field4
       ,fields[4]   as Field5

from   (select  split(concat_ws(string(unhex(1)),*),'\x01') as fields
        from    mytable
        ) t

+--------+--------+--------+--------+--------+
| field1 | field2 | field3 | field4 | field5 |
+--------+--------+--------+--------+--------+
| ABC    | 45     | XYZ    | JKL    | BNM    |
| 65     | QWE    | JKL    | (null) | (null) |
| WER    | YUI    | IOP    | GHJ    | (null) |
+--------+--------+--------+--------+--------+

简化版本,假设您的字段中没有出现逗号 (,):

select  ...

from   (select  split(concat_ws(',',*),',') as fields
        from    mytable
        ) t

假设"empty values"是空字符串

select  fields[0]   as Field1
       ,fields[1]   as Field2
       ,fields[2]   as Field3
       ,fields[3]   as Field4
       ,fields[4]   as Field5

from   (select  split(regexp_replace(concat_ws(string(unhex(1)),*),'^\x01+|\x01+$|(\x01)+',''),'\x01') as fields
        from    mytable
        ) t

+--------+--------+--------+--------+--------+
| field1 | field2 | field3 | field4 | field5 |
+--------+--------+--------+--------+--------+
| ABC    | 45     | XYZ    | JKL    | BNM    |
| 65     | QWE    | JKL    | (null) | (null) |
| WER    | YUI    | IOP    | GHJ    | (null) |
+--------+--------+--------+--------+--------+

简化版本,假设您的字段中没有出现逗号 (,):

select  ...

from   (select  split(regexp_replace(concat_ws(',',*),'^,+|,+$|(,)+',''),',') as fields
        from    mytable
        ) t