2017-03-02 124 views
0

你能幫我克服當我嘗試創建下表時遇到的錯誤嗎?Hive創建表錯誤。

感謝

CREATE TABLE RANGE_FT (HOSP_VAR STRING, RANGE INT) 
AS 
      SELECT 'EMR' ,MAX(emr_avg_score) - MIN(emr_avg_score) from JOIN9 
    UNION SELECT 'SCI' ,MAX(sci_avg_score) - MIN(sci_avg_score) from JOIN9 
    UNION SELECT 'ASTH' ,MAX(asth_avg_score) - MIN(asth_avg_score) from JOIN9 
    UNION SELECT 'HF' ,MAX(hf_avg_score) - MIN(hf_avg_score) from JOIN9 
    UNION SELECT 'SC' ,MAX(sc_avg_score) - MIN(sc_avg_score) from JOIN9 
    UNION SELECT 'PNEU' ,MAX(pneu_avg_score) - MIN(pneu_avg_score) from JOIN9 
    UNION SELECT 'PREV' ,MAX(prev_avg_score) - MIN(prev_avg_score) from JOIN9 
    UNION SELECT 'BC' ,MAX(BC_avg_score) - MIN(BC_avg_score) from JOIN9 
    UNION SELECT 'HEART' ,MAX(heart_avg_score) - MIN(heart_avg_score) from JOIN9 
    UNION SELECT 'PREG' ,MAX(preg_avg_score) - MIN(preg_avg_score) from JOIN9 
; 

線7:6 ALL在 'SELECT'

+0

試試SET NOCOUNT查詢 –

+0

之前,請用文本替換圖像(粘貼後,選中它,然後按CTRL + K對其進行格式化代碼) –

+0

嗨辛格,謝謝你你的幫助。你可以更加關於如何設置nocount嗎?我的意思是我應該把這個命令放在哪裏。我的代碼是:DROP TABLE RANGE_FT; CREATE TABLE RANGE_FT (HOSP_VAR STRING, RANGE INT) AS SELECT 'EMR',MAX(emr_avg_score) - 從JOIN9 MIN(sci_avg_score) - MIN(emr_avg_score)從JOIN9 UNION SELECT 'SCI',MAX(sci_avg_score) 來自JOIN9的UNION SELECT'ASTH',MAX(asth_avg_score)-min(asth_avg_score)來自JOIN9的UNION SELECT'HF',MAX(hf_avg_score)-min(hf_avg_score) –

回答

1
  1. UNION丟失不被你的蜂巢版本支持(而這也不是你所需要的這裏)。
    改爲使用UNION ALLhttps://cwiki.apache.org/confluence/display/Hive/LanguageManual+Union

  2. UNION [ALL]需要雙方都有相同的模式。
    在Hive中,與其他SQL方言不同,不僅類型應該相似,而且列別名也應該相似。 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Union

  3. 您不能在創建語句中列出列。
    將相關別名添加到SELECT子句。


這是您的查詢

CREATE TABLE RANGE_FT 
AS 
       SELECT 'EMR' as HOSP_VAR ,MAX (emr_avg_score) - MIN (emr_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'SCI' as HOSP_VAR ,MAX (sci_avg_score) - MIN (sci_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'ASTH' as HOSP_VAR ,MAX (asth_avg_score) - MIN (asth_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'HF' as HOSP_VAR ,MAX (hf_avg_score) - MIN (hf_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'SC' as HOSP_VAR ,MAX (sc_avg_score) - MIN (sc_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'PNEU' as HOSP_VAR ,MAX (pneu_avg_score) - MIN (pneu_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'PREV' as HOSP_VAR ,MAX (prev_avg_score) - MIN (prev_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'BC' as HOSP_VAR ,MAX (BC_avg_score) - MIN (BC_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'HEART' as HOSP_VAR ,MAX (heart_avg_score) - MIN (heart_avg_score) as RANGE from JOIN9 
    UNION ALL SELECT 'PREG' as HOSP_VAR ,MAX (preg_avg_score) - MIN (preg_avg_score) as RANGE from JOIN9 
; 

的固定版本,而這裏是只有一個SELECT

create table RANGE_FT 
as 
select inline 
     (
      array 
      (
       struct ('EMR' ,MAX (emr_avg_score) - MIN (emr_avg_score) ) 
       ,struct ('SCI' ,MAX (sci_avg_score) - MIN (sci_avg_score) ) 
       ,struct ('ASTH' ,MAX (asth_avg_score) - MIN (asth_avg_score)) 
       ,struct ('HF' ,MAX (hf_avg_score) - MIN (hf_avg_score) ) 
       ,struct ('SC' ,MAX (sc_avg_score) - MIN (sc_avg_score) ) 
       ,struct ('PNEU' ,MAX (pneu_avg_score) - MIN (pneu_avg_score)) 
       ,struct ('PREV' ,MAX (prev_avg_score) - MIN (prev_avg_score)) 
       ,struct ('BC' ,MAX (BC_avg_score) - MIN (BC_avg_score) ) 
       ,struct ('HEART' ,MAX (heart_avg_score) - MIN (heart_avg_score)) 
       ,struct ('PREG' ,MAX (preg_avg_score) - MIN (preg_avg_score)) 
      ) 
     ) as (HOSP_VAR,RANGE) 

from JOIN9  
; 
+0

我得到了一個錯誤失敗:SemanticException [錯誤10065]:CREATE TABLE AS SELECT命令無法指定目標表的列列表 –

+0

我只是將它發佈到文本中。非常感謝您的幫助Dudu –

+0

Hi Dudu,你能告訴我你如何創建這張桌子的魔術?非常感謝你的回覆。 –

0

這將工作的替代解決方案正如Dudu所提到的,你不能給列名稱w hile用select創建。您可以使用Union,但性能明智的UNION ALL更好。根據您的用例

CREATE TABLE RANGE_FT 
AS 
      SELECT 'EMR' AS HOSP_VAR ,MAX(emr_avg_score) - MIN(emr_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'SCI' AS HOSP_VAR ,MAX(sci_avg_score) - MIN(sci_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'ASTH' AS HOSP_VAR ,MAX(asth_avg_score) - MIN(asth_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'HF' AS HOSP_VAR ,MAX(hf_avg_score) - MIN(hf_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'SC' AS HOSP_VAR ,MAX(sc_avg_score) - MIN(sc_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'PNEU' AS HOSP_VAR ,MAX(pneu_avg_score) - MIN(pneu_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'PREV' AS HOSP_VAR ,MAX(prev_avg_score) - MIN(prev_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'BC' AS HOSP_VAR ,MAX(BC_avg_score) - MIN(BC_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'HEART' AS HOSP_VAR ,MAX(heart_avg_score) - MIN(heart_avg_score) AS `RANGE` from JOIN9 
    UNION SELECT 'PREG' AS HOSP_VAR ,MAX(preg_avg_score) - MIN(preg_avg_score) AS `RANGE` from JOIN9 
; 
+0

嗨阿希什,謝謝你的幫助。但即使我刪除列名稱並運行代碼,錯誤仍然顯示像這樣失敗:ParseException行4:12在'SELECT'在''附近缺少ALL' 行5:12在'SELECT'附近'' –

+0

@QingZhang請別名列名並將Range列放入反斜槓中' –