在SQL中,这看起来很简单,但我在使用日期范围的HiveQL时遇到了麻烦。
我有这样的数据集:
hive> describe logs;
id string,
ts string,
app_id int
hive> select * from logs limit 5;
1389 2014-10-05 13:57:01 12
1656 2014-10-06 03:57:59 15
1746 2014-10-06 10:58:25 19
1389 2014-10-09 08:57:01 12
1656 2014-10-10 01:57:59 15我的目标是在过去的3天里得到一个清晰的id。最好的方法是读取当前的系统时间,并获得最后3天的唯一id,但不确定我需要将"unix_timestamp()“放在哪里。考虑到日志是实时记录的,并且有今天在ts中的日期,我尝试使用这个查询(第一种方法)
hive > SELECT distinct id FROM logs HAVING to_date(ts) > date_sub(max(ts), 3) and to_date(ts) < max(ts);
FAILED: SemanticException [Error 10025]: Line 1:45 Expression not in GROUP BY key 'ts'如果我按“ts”添加组,如下所示,则会出现以下错误:
hive> SELECT distinct ext FROM pas_api_logs group by ts HAVING to_date(ts) > date_sub(max(ts), 7) and to_date(ts) < max(ts);
FAILED: SemanticException 1:47 SELECT DISTINCT and GROUP BY can not be in the same query. Error encountered near token 'ts'经过多次的尝试,最后的方法就是这样,经过类似的课题研究。
Select distinct id from (SELECT * FROM logs JOIN logs ON (max(logs.ts) = to_date(logs.ts))
UNION ALL
SELECT * FROM logs JOIN logs ON (to_date(logs.ts) = date_sub(max(logs.ts), 1))
UNION ALL
SELECT * FROM logs JOIN logs ON (to_date(logs.ts) = date_sub(max(logs.ts), 2)));显然这也不起作用。有人能在这上面放点灯吗?
发布于 2014-10-29 20:17:47
使用以下语句可以获得所需的结果:
从DATEDIFF(from_unixtime(unix_timestamp()),ts) <= 3的日志中选择不同的id;
希望能帮上忙!
https://stackoverflow.com/questions/26305038
复制相似问题