【问题标题】:Semantic exception error in HIVE while using last_value window function使用 last_value 窗口函数时 HIVE 中的语义异常错误
【发布时间】:2018-10-27 01:40:17
【问题描述】:

我有一个包含以下数据的表格:

dt  device  id  count
2018-10-05  computer    7541185957382   6
2018-10-20  computer    7541185957382   3
2018-10-14  computer    7553187775734   6
2018-10-17  computer    7553187775734   10
2018-10-21  computer    7553187775734   2
2018-10-22  computer    7549187067178   5
2018-10-20  computer    7553187757256   3
2018-10-11  computer    7549187067178   10

我想为每个id 获取最后一个和第一个dt。因此,我使用窗口函数 first_value 和 last_value 如下:

select id,last_value(dt) over (partition by id order by dt) last_dt
from table
order by id
;

但我收到此错误:

FAILED: SemanticException Failed to breakup Windowing invocations into Groups. At least 1 group must only depend on input columns. Also check for circular dependencies.
Underlying error: Primitve type DATE not supported in Value Boundary expression

我无法诊断问题,如果能提供任何帮助,我将不胜感激。

【问题讨论】:

    标签: hive hiveql window-functions


    【解决方案1】:

    如果您在查询中添加 rows between 子句,那么您的查询将正常工作。

    hive> select id,last_value(dt) over (partition by id order by dt 
          rows between unbounded preceding and unbounded following) last_dt 
          from table order by id;
    

    结果:

    +----------------+-------------+--+
    |       id       |   last_dt   |
    +----------------+-------------+--+
    | 7541185957382  | 2018-10-20  |
    | 7541185957382  | 2018-10-20  |
    | 7549187067178  | 2018-10-22  |
    | 7549187067178  | 2018-10-22  |
    | 7553187757256  | 2018-10-20  |
    | 7553187775734  | 2018-10-21  |
    | 7553187775734  | 2018-10-21  |
    | 7553187775734  | 2018-10-21  |
    +----------------+-------------+--+
    

    关于原始类型支持有Jira,并在Hive.2.1.0中得到修复

    更新:

    对于不同的记录,您可以使用 ROW_NUMBER 窗口函数并从结果集中仅过滤掉 first row

    hive> select id,last_dt from 
              (select id,last_value(dt) over (partition by id order by dt 
                  rows between unbounded preceding and unbounded following) last_dt,
                  ROW_NUMBER() over (partition by id order by dt)rn 
                  from so )t 
               where t.rn=1;
    

    结果:

    +----------------+-------------+--+
    |       id       |     dt      |
    +----------------+-------------+--+
    | 7541185957382  | 2018-10-20  |
    | 7553187757256  | 2018-10-20  |
    | 7553187775734  | 2018-10-21  |
    | 7549187067178  | 2018-10-22  |
    +----------------+-------------+--+
    

    【讨论】:

    • 谢谢。有用。一个问题,有没有办法只得到 distinct rows 而无需在顶部写 select distinct 声明。从上面的结果可以看出,有重复。如何一次性删除它们并仅获取没有sub-query 的唯一值。
    • @Raj,请检查我编辑的答案的 UPDATE 部分,更简单的方法是在顶部添加 distinct,但我们也可以在 sub 中使用 row_number 窗口函数-查询。
    猜你喜欢
    • 2013-04-22
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2018-09-08
    相关资源
    最近更新 更多