大小写或解码多行

时间:2018-12-18 06:55:42

标签: sql hive bigdata hiveql

我必须在给定条件下从输入表中获得HIVE的结果,并在图像中共享。是否可以在具有某些功能的单个查询中获得它而无需添加任何其他列?enter image description here

创建临时表的查询:-

 Create table test(`serial_no` string,`location` string, `status` string,`row_num` int);
     INSERT INTO test values('ABC','CA','S',1);
     INSERT INTO test values('ABC','CA','P',2);
     INSERT INTO test values('ABC','CA','F',3);

3 个答案:

答案 0 :(得分:1)

在case语句中使用min()或max()聚合:

 select serial_no, location, 
        max(case when (row_num=1 and status='S') OR ( row_num=2 and status='P') then 'Passed' end) result 
   from test 
  group by serial_no, location;

结果:

OK
serial_no       location        result
ABC             CA              Passed
Time taken: 61.036 seconds, Fetched: 1 row(s)

答案 1 :(得分:0)

尝试

/s

答案 2 :(得分:0)

您还可以在下面使用collect_list函数尝试,然后应用分组依据。我尚未验证性能,您可以查看在这种情况下使用的映射器。

select  serial_no,  uslocation , 
case when 
collect_list(condition_1)[0] = 1 and 
collect_list(condition_2)[0] = 'S' and 
collect_list(condition_3)[0] = 2 and 
collect_list(condition_4)[0] = 'P'
then 'Passed' else 'Failed' end result_field
from 
(
select
serial_no,
uslocation,
CASE WHEN row_num=1 THEN row_num END AS condition_1,
CASE WHEN status='S' THEN status END AS condition_2,
CASE WHEN row_num=2 THEN row_num END AS condition_3,
CASE WHEN status='P' THEN status END AS condition_4
from 
test_tbl
) dummy_tble
group by serial_no, uslocation

Result before applying case statement

Result from cloudera hive console