Sql 在配置单元中的指定条件下从单行创建多行
我正在尝试实现空检查。例如:Sql 在配置单元中的指定条件下从单行创建多行,sql,hadoop,hive,Sql,Hadoop,Hive,我正在尝试实现空检查。例如: 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 正确的方法是什么?一种方
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
正确的方法是什么?一种方法使用
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。这还需要对每个子查询进行单独的扫描。一种方法使用
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;
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
这是相当残忍的暴力。如果有很多列,可以使用电子表格生成SQL。这还需要对每个子查询进行单独扫描。这不起作用,因为我们有很多检查和大约1000万条记录。@ManishVishnoi。这就行了,你只需要写代码。在任何情况下,都只能回答你提出的问题。你问了两列和一种支票。如果你还有其他问题,那就把它当作另一个问题来问。这行不通,因为我们有很多支票和大约1000万条记录。@ManishVishnoi。这就行了,你只需要写代码。在任何情况下,都只能回答你提出的问题。你问了两列和一种支票。如果你还有问题,那就把它当作另一个问题来问。非常感谢。我是hive的新手,请您详细说明一下“elt”做了什么?
elt
返回第n个位置的元素(e.pos
从0开始)非常感谢。我是hive的新手,您能详细说明一下“elt”做了什么吗?elt
返回第n个位置的元素(e.pos
从0开始)
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