我有一个包含6个字段的表,如下所示:
Field1 Field2 Field3 Field4 Field5 Field6
ABC 45 XYZ JKL BNM
65 QWE JKL
WER YUI IOP GHJ
我想将上表中的数据拉到一个包含5个字段的新表中,我们忽略空值。我的决赛桌应该是这样的:
Result1 Result2 Result3 Result4 Result5
ABC 45 XYZ JKL BNM
65 QWE JKL
WER YUI IOP GHJ
我已经开始用CASE WHEN编写一个大规模的条件查询,但它已经失控并且容易出错。 是否可以在Hive中使用regex_extract查询获取该表?
答案 0 :(得分:3)
假设"空值"是空的
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
假设"空值"是空字符串
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)+','$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(regexp_replace(concat_ws(',',*),'^,+|,+$|(,)+','$1'),',') as fields
from mytable
) t