配置单元将多个映射值合并为一列

时间:2019-12-01 23:25:22

标签: hive hiveql transpose

表格(输入数据)

+--------+---------+
| col_id | col_val |
+--------+---------+
| axc    | col_x   |
| bdf    | col_f   |
| cde    | col_x   |
| yhc    | col_f   |
| idx    | col_a   |
| dft    | col_b   |
+--------+---------+

转置逻辑。基本上想将col_a或col_b值合并为col_ab的一列。尝试如下。但遇到错误。请帮助

SELECT a.col_id,IF(array_contains(collect_list(a.map_values['col_x']),'1'),'Y','N') AS col_x,
IF(array_contains(collect_list(a.map_values['col_y']),'1'),'Y','N') AS col_y,
IF(array_contains(collect_list(a.map_values['col_a']),'1'),'Y','N') OR IF(array_contains(collect_list(a.map_values['col_b']),'1'),'Y','N') AS col_ab,
IF(array_contains(collect_list(a.map_values['col_f']),'1'),'Y','N') AS col_f 
 FROM (
       SELECT col_id,
              col_val,
              map(col_val, '1') map_values
       FROM   transpose) a GROUP BY a.col_id;

输出数据

+--------+-------+-------+-----------+-------+
| col_id | col_x | col_y | col_ab    | col_f |
+--------+-------+-------+-----------+-------+
| axc    | Y     | N     | N         | N     |
| bdf    | N     | N     | N         | Y     |
| cde    | Y     | N     | N         | N     |
| dft    | N     | Y     | Y         | N     |
| idx    | N     | N     | Y         | N     |
| yhc    | N     | N     | N         | Y     |
+--------+-------+-------+-----------+-------+

2 个答案:

答案 0 :(得分:0)

具有聚合的用例语句:

select  col_id, 
        max( case col_val = 'col_x' then 'Y' alse 'N' end )               as col_x, 
        max( case col_val = 'col_y' then 'Y' alse 'N' end )               as col_y,
        max( case col_val in ( 'col_a', 'col_b')  then 'Y' alse 'N' end ) as col_ab,
        max( case col_val = 'col_f' then 'Y' alse 'N' end )               as col_f
from your_table
 group by col_id;

答案 1 :(得分:0)

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    var propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    var yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
    yamlPropertiesFactoryBean.setResources(new ClassPathResource("custom.yaml"));
    propertySourcesPlaceholderConfigurer.setProperties(yamlPropertiesFactoryBean.getObject());
    return propertySourcesPlaceholderConfigurer;
}