【问题标题】:Unable to use CTE in hive table to write a query无法在 hive 表中使用 CTE 编写查询
【发布时间】:2020-11-26 00:07:06
【问题描述】:
   WITH month_revenue AS 
   (SELECT SUM(price) AS Oct_Revenue FROM attribute_partition WHERE event_type= 'purchase' AND 
   MONTH(event_time) = '10')  , 
   (SELECT SUM(price) AS Nov_Revenue FROM attribute_partition WHERE event_type= 'purchase' AND 
   MONTH(event_time) = '11')
   SELECT (Oct_Revenue - Nov_Revenue) FROM month_revenue ;

编写查询以查找从 10 月到 11 月因购买而产生的收入变化。 我得到的错误是:

失败:ParseException 行 2:0 丢失(在 'SELECT' 附近的 ',' 中 语句第 2:117 行缺少) 在语句行 3:0 中的 ',' 附近 无法识别 'SELECT' 'SUM' '(' in statement 附近的输入

请帮助我了解我做错了什么

【问题讨论】:

    标签: hive hiveql common-table-expression


    【解决方案1】:

    一个 CTE 是一个查询。可以像这样在单个语句中计算两个月:

     WITH month_revenue AS 
       (SELECT 
             SUM(case when MONTH(event_time) = '10' then price else 0 end) AS Oct_Revenue,
             SUM(case when MONTH(event_time) = '11' then price else 0 end) AS Nov_Revenue
        FROM attribute_partition 
        WHERE event_type= 'purchase' 
          AND MONTH(event_time) in ('10', '11')
      )  
    
       SELECT (Oct_Revenue - Nov_Revenue) FROM month_revenue ;
    

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多