在配置单元中的指定条件下从单行创建多个行

时间:2017-04-10 11:09:11

标签: sql hadoop hive

我正在尝试实施Null检查。例如:

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

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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。这还需要对每个子查询进行单独扫描。