Hive 逐月查找重复用户
我有下面这样的数据 第一个月的交易Hive 逐月查找重复用户,hive,hiveql,Hive,Hiveql,我有下面这样的数据 第一个月的交易 User_id trsaction_completed_date user_type 1234 7-Jan-19 New 5657 8-Jan-19 New 7890 9-Jan-19 New 98456 20-Jan-19
User_id trsaction_completed_date user_type
1234 7-Jan-19 New
5657 8-Jan-19 New
7890 9-Jan-19 New
98456 20-Jan-19 new
User_id trsaction_completed_date user_type
1234 4-Feb-19 Existing
5657 5-Feb-19 Existing
567567 2/13/2019 New
第二个月的交易
User_id trsaction_completed_date user_type
1234 7-Jan-19 New
5657 8-Jan-19 New
7890 9-Jan-19 New
98456 20-Jan-19 new
User_id trsaction_completed_date user_type
1234 4-Feb-19 Existing
5657 5-Feb-19 Existing
567567 2/13/2019 New
需要从上月数据中查找当月的重复用户。根据我的数据,本月为2月,上月为1月
根据我的数据,我需要得到以下输出
用户id 12345657在1月和2月交易
输出:
-----------
Month count
Feb 2
将日期转换为
yyyy-MM-dd
格式后,可以计算每个月的用户事务数,使用lag()获取上一个月的计数
select month, user_id
from
(
select month, user_id , cnt,
lag(cnt) over(partition by user_id order by month) prev_month_cnt
(
select month(trsaction_completed_date) as month, user_id
count(*) cnt
from transaction_table
where trsaction_completed_date between '2019-01-01' and '2019-02-28'
group by month(trsaction_completed_date), user_id
)s
)s where month='02' --Feb users
and prev_month_cnt>0 --available in previous month
如有必要,请添加计数以获得按月合计此处的问题是日期的格式不正确(可排序),甚至同一月份的格式也不相同谢谢您的建议。我会试着给你回电话。