how to run a different select statement based on condition in Hive SQL

萝らか妹 提交于 2019-12-08 08:49:39

问题


I would like to know how to run a different select statement based on condition in Hive SQL.

The following query does not work but throws an error.

Error while compiling statement: FAILED: ParseException line 4:2 cannot recognize input near '(' 'SELECT' '1' in expression specification

SELECT 
    CASE WHEN '${UN}'!= '' THEN 
        (
        SELECT * 
            from table1 t 
            WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
            AND t.un in ('${UN}')
        )
    ELSE
        (
        SELECT * 
            from table1 t 
            WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
            AND t.un in (
               (SELECT
                o.unq_num as un
                FROM table2 as o
                WHERE o.date >= '2017-01-01'
                    AND upper(o.srl_num) in ('${R}')
                LIMIT 1)            
            )       
        )
    END 

回答1:


Use UNION ALL with your queries + add conditions for switching corresponding query:

 select * 
   from table1 t 
   where (t.yymmddval BETWEEN '${D1}' and '${D2}')
     and t.un in ('${UN}') 
     and '${UN}'!= '' --switching condition 

union all

select * 
  from table1 t 
 where (t.yymmddval BETWEEN '${D1}' AND '${D2}')
   and t.un in 
              (SELECT
               o.unq_num as un
               FROM table2 as o
               WHERE o.date >= '2017-01-01'
                   AND upper(o.srl_num) in ('${R}')
               LIMIT 1) 
    and '${UN}'= '' --switching condition


来源:https://stackoverflow.com/questions/50038841/how-to-run-a-different-select-statement-based-on-condition-in-hive-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!