select
a.time, b.name, c.user_city,
case
when c.user_country='xxxx' and c.user_state in (select cast(state_id as string) from states_list)
then (select d.STATE_NAME from States_LIST d where d.STATE_ID = cast(c.user_state as int) )
else c.user_state
end state,
case
when c.user_country in (select cast(COUNTRY_ID as string) from COUNTRIES_LIST)
then (select e.COUNTRY_NAME from COUNTRIES_LIST e where e.COUNTRY_ID = cast(c.user_country as int))
else null
end country, col6, col7, ......
from ......
where.......
在Impala中执行上述查询时,我收到以下错误:
不兼容的返回类型数组和exprs的字符串'select 来自States_LIST的d.STATE_NAME d其中d.STATE_ID = cast(c.user_state as int)'和'c.user_state'
请帮助!!!!
答案 0 :(得分:1)
由于错误消息显示您尝试在同一个案例中分配字符串和数组。因此,case语句只有在子查询只有一个输出时才有效。
为此目的需要的是标量子查询。根据您需要解决的问题,您可以尝试使用聚合函数。
来自Impala文档:
标量子查询生成一个结果集,其中包含一行的单行 单列,通常由聚合函数产生,例如 MAX()或SUM()
这不会起作用的第二个原因是 Impala不允许在select子句中使用子查询。
子查询可以返回结果集,以便在FROM或WITH子句中使用,或者使用IN或EXISTS等运算符。
根据您的表格,您必须通过加入d
和e
表来解决此问题,因此需要子查询消失。
例如:
select
a.time, b.name, new.user_city,
case
when new.user_country='xxxx' and new.user_state is not null
then new.STATE_NAME
else new.user_state
end state,
e.country_name country,
col6, col7, ......
from
a,
b,
countries_list e right outer join
(select * from
(select * from states_list) as d
right outer join c on cast(d.state_id as string)=c.user_state
) as new
on e.COUNTRY_ID = cast(new.user_country as int)
,..
where
...
请告诉我其中一个是否解决了您的问题。