how to run a different select statement based on c

2019-06-06 08:52发布

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条回答
闹够了就滚
2楼-- · 2019-06-06 09:21

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
查看更多
登录 后发表回答