如何在配置单元sql中基于条件运行不同的select语句

xmjla07d  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(299)

我想知道如何根据配置单元sql中的条件运行不同的select语句。
以下查询不起作用,但引发错误。
编译语句时出错:失败:parseexception行4:2无法识别表达式规范中“(”选择“1”)附近的输入

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
bvhaajcl

bvhaajcl1#

在查询中使用union all+添加切换相应查询的条件:

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

相关问题