【发布时间】:2014-12-19 07:53:04
【问题描述】:
我在 hive 中有一张桌子,看起来像这样
cust_id prod_id timestamp
1 11 2011-01-01 03:30:23
2 22 2011-01-01 03:34:53
1 22 2011-01-01 04:21:03
2 33 2011-01-01 04:44:09
3 33 2011-01-01 04:54:49
以此类推。
对于每条记录,我想检查该客户在过去 24 小时内购买了多少独特产品,不包括当前交易。所以输出应该是这样的 -
1 0
2 0
1 1
2 1
3 0
我的 hive 查询看起来像这样
select * from(
select t1.cust_id, count(distinct t1.prod_id) as freq from temp_table t1
left outer join temp_table t2 on (t1.cust_id=t2.cust_id)
where t1.timestamp>=t2.timestamp
and unix_timestamp(t1.timestamp)-unix_timestamp(t2.timestamp) < 24*60*60
group by t1.cust_id
union all
select t.cust_id, 0 as freq from temp_table t2
)unioned;
【问题讨论】:
-
“排除当前交易”是什么意思?你的意思是“最近的交易”吗?
标签: mysql hadoop hive hiveql union-all