Hive创建表错误。

时间:2017-03-02 08:55:44

标签: sql 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行在'SELECT'

时缺少ALL

2 个答案:

答案 0 :(得分:1)

    您的Hive版本不支持
  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. 您无法在create语句中提供列列表 将相关别名添加到SELECT子句中。

  4. 这是您查询的固定版本

    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    
    ;
    

答案 1 :(得分:0)

这将有效。正如Dudu所提到的,在使用select编写create时,不能给出列名。你可以使用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
;