如何在Impala中将结果的一行分成两行

问题描述

以下是要求,需要在sql中进行转换。

我只会从来源收到一条记录,但是我需要验证以下情况并将其分成黑斑羚中的两行。

final_columns来自源的期望值为('IC','OG','BK')

仅当我在'BK'中收到值final_columns时,才需要检查以下条件。

if (final_columns in ('BK')) {
  split into rows based on below conditions
  when dr_type = 'IT' THEN dr_cid else dr_pid
  when cr_type = 'OR' THEN cr_cid else cr_pid
}
else {
  value as it is in source final columns
}

解决方法

能否请您尝试使用此sql?我创建了虚拟内联视图以复制数据。

select 
    case when final_columns in ('BK') AND 
    case when dr_type = 'IT' THEN dr_cid 
    when dr_type <> 'IT' THEN dr_pid 
    when cr_type = 'OR' THEN cr_cid 
    when cr_type <> 'OR' THEN   cr_pid 
    END 
    else final_columns 
    end 
from 
    table,( select 1 col union all select 1 union all select 2 ) dummy_record_creation
where 
    case when final_columns in ('BK') then 1 else 2 end = dummy_record_creation.col