【问题标题】:how to run a different select statement based on condition in Hive SQL如何根据 Hive SQL 中的条件运行不同的选择语句
【发布时间】:2018-10-06 21:48:17
【问题描述】:

我想知道如何根据 Hive SQL 中的条件运行不同的 select 语句。

以下查询不起作用,但会引发错误。

编译语句时出错:FAILED: ParseException line 4:2 无法识别表达式中 '(' 'SELECT' '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 

【问题讨论】:

  • 已经回答了类似的问题(使用 UNION ALL+ WHERE 代替您的 CASE 语句):stackoverflow.com/a/37696700/2700344 - 只需将您的条件和反向条件放在 WHERE 中

标签: sql select hive conditional-statements hiveql


【解决方案1】:

在您的查询中使用 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

【讨论】:

  • 这是哥伦布之蛋。杰出的!我只需要添加额外的指令:set hive.mapred.mode=nonstrict;允许联合所有。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多