我在Dim RowHeaderNum As Variant
RowHeaderNum = Application.Match("Current", ActiveSheet.Range("B:B"), False)
If IsError(RowHeaderNum) Then
MsgBox "'Current' not found."
Exit Sub
End If
Dim CurrActColNum As Variant
CurrActColNum = Application.Match("CurrentActual", Rows(RowHeaderNum), False)
If IsError(CurrActColNum) Then
MsgBox "'CurrentActual' not found."
Exit Sub
End If
Dim currActRev As Variant, currMatch As Variant
currMatch = Application.Match("Gross Operating Profit", Columns("N:N"), False)
If Not IsError(currMatch) Then
currActRev = Application.Index(Columns(CurrActColNum), currMatch)
End If
Debug.Print currActRev 'print result in immediate window
中有一个视图,根据条件,它应该返回一个空数组或Hive 1.1.0
的数组
这是我的代码:
struct<name: string, jobslots: int>
这里的问题是,空数组select
case when <condition>
then array()
else array(struct(t1.name, t1.jobslots))
end
from table t1;
的类型为array()
。因此,当我尝试将其插入表中时,会引发错误。
如何更改此类型以返回类型为array<string>
的空数组,以便array<struct<name: string, jobslots:int>>
函数在该数组上返回0?
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以使用 collect_list
或 colect_set
来收集从连接中获取的结构数组,如果连接条件为假,则 collect_list 将生成一个空的结构数组。
此查询返回大小为 0 的数组:
select a.id, size(collect_list(b.str))=0 array_size_zero
from
(select 2 id ) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on a.id=b.id
group by a.id
结果:
a.id array_size_zero
2 true
如果在第一个子查询 a 中更改 id 以与 b 连接,它将返回具有 1 个元素的数组。而且这些结果是同类型的,你可以使用union all轻松查看。
检查结果类型相同:
select a.id, collect_list(b.str) my_array
from
(select 1 id ) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on a.id=b.id
group by a.id
union all
select a.id, collect_list(b.str) my_array
from
(select 2 id ) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on a.id=b.id
group by a.id
结果:
id my_array
1 [{"name":null,"value":null}]
2 []
如果我尝试联合所有不同类型的空结构数组,例如 array() 会发生什么:
select 1 id, array() my_array
union all
select a.id, collect_list(b.str) my_array
from
(select 2 id ) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on a.id=b.id
group by a.id
异常:
<块引用>编译语句时出错:FAILED: SemanticException Schema of
联合的双方应该匹配:列 my_array 是类型
第一个表上的数组和类型
array
这表明第一个查询确实返回空的结构数组。 您可以轻松地在查询中执行类似的联接。
如何在带条件的查询中使用它? 演示:
select a.id, case when true --Put your condition here instead of dummy <true>
then collect_list(a.str) --not empty
else collect_list(b.str) --this one is empty array of the same type
end as my_array
from
(select 2 id, named_struct('name',null,'value',null) str) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on FALSE
group by a.id
CASE 表达式很愉快,不会引发不兼容类型的异常