2017-04-10 64 views
0

我试图执行空检查。对于e.g:在配置单元中的指定条件下从单行创建多行

Col_A | Col_B | Col_C | Col_D 
null | boy | null | dust 

然后我想作为输出:

Col_A | Col_B | Col_C | Col_D | New_Col 
null | boy | null | dust | Col_A failed null check 
null | boy | null | dust | Col_D failed null check 

什么是做到这一点的正确方法?

回答

1
select t.* 
     ,concat(elt(e.pos+1,'Col_A','Col_B','Col_C','Col_D'),' failed null check') as New_Col 
from mytable t lateral view posexplode (array(Col_A,Col_B,Col_C,Col_D)) e 
where e.val is null 
+0

非常感谢。我是蜂房新手,请你详细说明'elt'做了什么? –

+0

'elt'返回第N个元素的位置('e.pos'从0开始) –

1

一种方法是使用union all

select Col_A, Col_B, Col_C, Col_D, 'Col_A failed NULL check' as new_col 
from t 
where Col_A is null 
union all 
select Col_A, Col_B, Col_C, Col_D, 'Col_B failed NULL check' as new_col 
from t 
where Col_B is null 
union all 
select Col_A, Col_B, Col_C, Col_D, 'Col_C failed NULL check' as new_col 
from t 
where Col_C is null 
union all 
select Col_A, Col_B, Col_C, Col_D, 'Col_D failed NULL check' as new_col 
from t 
where Col_D is null; 

这是相当强力。如果您有很多列,则可以使用电子表格生成SQL。这也需要为每个子查询单独扫描。

+0

这不起作用,因为我们有许多支票和大约1000万条记录。 –

+0

@ManishVishnoi。 。 。这将工作,你只需编写代码。无论如何,它只能回答你所问的问题。你问了2列和一种支票。如果您还有其他问题,请将其作为另一个问题。 –

相关问题