Semantic exception error in HIVE while using last_value window function

拥有回忆 提交于 2019-12-06 15:50:51

If you add rows between clause in your query, then your query will work fine.

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;

Result:

+----------------+-------------+--+
|       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  |
+----------------+-------------+--+

There is Jira regards to primitive type support and got fixed in Hive.2.1.0

UPDATE:

For distinct records you can use ROW_NUMBER window function and filter out only the first row from the result set.

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;

Result:

+----------------+-------------+--+
|       id       |     dt      |
+----------------+-------------+--+
| 7541185957382  | 2018-10-20  |
| 7553187757256  | 2018-10-20  |
| 7553187775734  | 2018-10-21  |
| 7549187067178  | 2018-10-22  |
+----------------+-------------+--+
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!