2017-02-27 44 views
1
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....... 

子查詢的黑斑羚case語句在執行上面的查詢在帕拉我得到下面提到的錯誤

不兼容的返回類型數組和exprs「選擇 d.STATE_NAME的字符串從States_LIST d其中d.STATE_ID = CAST(c.user_state 爲INT)「和 'c.user_state'

請幫助!!!!

回答

1

由於錯誤消息顯示您試圖在同一個案中分配字符串和數組。 case語句只能在子查詢只有一個輸出的情況下工作。

你需要爲此目的的是一個標量子查詢。根據您需要解決的問題,您可以嘗試使用聚合功能。

從帕拉文檔:

標量子查詢產生與含有 單個列,通常通過聚合功能如 MAX()或SUM(生產)

單行的結果集

此問題無效的第二個原因是因爲Impala不允許在子句中使用子查詢。

子查詢可以返回一個結果集,用於FROM或WITH子句或IN或EXISTS等運算符。

根據你的表,你將不得不與de表,這樣需要一個子查詢自敗加入到解決這個問題。 例子:

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 
... 

請讓我知道其中的一個解決是否您的問題。

+0

感謝您的回覆。我確實嘗試加入州表,但仍然無效。我需要的是,用戶在下拉列表中選擇狀態,這些狀態將以字符串格式保存爲狀態代碼,如果是,請拔出狀態名稱,如果用戶狀態輸入,則需要檢查用戶輸入的狀態是否在我的狀態表列表中不是一個有效的值,那麼它應該直接接受用戶輸入的輸入。這是要求,請讓我知道如何在黑斑病中實現這一目標........... –

+0

新增了一個加入如何幫助你的例子。還沒有測試過,所以它可能包含小錯誤和優化可能性 – spijs

+0

是的我已經嘗試添加LIMIT 1,它說'在選擇列表中不支持子查詢' –