按日期Hive计算单个组每月的交易数量

问题描述 投票:0回答:1

我有一张客户交易表,客户购买的每件物品都存储为一行。因此,对于单个事务,表中可以有多行。我还有一个叫visit_date的上校。有一个名为cal_month_nbr的类别列,根据发生的月份交易,其范围从1到12。

数据如下所示

Id          visit_date     Cal_month_nbr
----        ------          ------
1           01/01/2020      1
1           01/02/2020      1
1           01/01/2020      1
2           02/01/2020      2
1           02/01/2020      2
1           03/01/2020      3
3           03/01/2020      3

第一我想知道客户使用他们的visit_date每月访问多少次即我想要下面的输出

id    cal_month_nbr       visit_per_month
---        ---------     ----
1           1             2
1           2             1
1           3             1
2           2             1
3           3             1

以及每个ID的平均访问频率是多少即。

id            Avg_freq_per_month
----          -------------
1              1.33
2              1
3              1

我尝试使用以下查询,但将每个项目都计为一笔交易

select avg(count_e) as num_visits_per_month,individual_id
from
(
    select r.individual_id, cal_month_nbr, count(*) as count_e
 from 
  ww_customer_dl_secure.cust_scan 
         GROUP  by 
         r.individual_id, cal_month_nbr
         order by count_e desc
         ) as t
         group by individual_id

我将不胜感激,谢谢您的指导或建议

我有一张客户交易表,客户购买的每件物品都存储为一行。因此,对于单个事务,表中可以有多行。我还有一个叫...

sql hive bigdata hiveql data-lake
1个回答
0
投票

您可以将总访问次数除以月份数:

© www.soinside.com 2019 - 2024. All rights reserved.