在hive db中选择日期范围?

时间:2018-04-11 14:53:05

标签: sql hive

我在Hive中有一个交易表。 目标:计算从最长日期到3个月前的不同交易数量:最大(日期) - 3M。

我的日期字段是时间戳的变体:2017-09-30 23:59:00.0

这是我到目前为止所做的:

select count(distinct(transaction_id)) from table
where max(cast(date_fld_ts as date)) - INTERVAL '3' MONTH

它无法运行,请告知我在这里错过了什么?

ERROR:

An error occurred when executing the SQL command:
select count(distinct(transaction_id)) from db.table 
where max(cast(date_fld_ts as date)) - INTERVAL '3' MONTH

Error while compiling statement: FAILED: SemanticException [Error 10128]: Line 2:6 Not yet supported place for UDAF 'max' [SQL State=42000, DB Errorcode=10128]
1 statement failed.

Execution time: 0.48s

1 个答案:

答案 0 :(得分:1)

您无法在where中使用聚合函数。而是获取max日期并在以后使用它。

select count(distinct transaction_id)
from (select max(cast(date_fld_ts as date)) over() as max_date,t.*
      from table t
     ) t
where date_fld_ts >= add_months(max_date,-3) and date_fld_ts <= max_date