Splunk中如何计算Top100客户的产品类别购买占比
如何计算高价值客户各产品类别的购买占比?
我原本通过以下Splunk查询查看高价值客户的购买产品类型:
# 获取所有购买记录 index=customer_data action=purchase # 筛选出购买量前100的高价值客户 [search index=customer_data action=purchase | top limit=100 user_id | table user_id] # 展示每个用户购买的产品类别 | stats count by user_id, list(product_category)
但这个查询只能输出每个用户的产品类别列表,我需要得到每个用户各产品类别的购买占比(比如clothing=75%, food=25%的格式)。我试过用statistical_distribution函数但找不到,请问该怎么实现?
解决方案
你可以通过多步统计计算实现需求,完整查询如下:
index=customer_data action=purchase [search index=customer_data action=purchase | top limit=100 user_id | table user_id] | stats count as category_count by user_id product_category | eventstats sum(category_count) as total_purchases by user_id | eval percentage=round((category_count/total_purchases)*100, 1) | eval category_percent=product_category."=".percentage."%" | stats list(category_percent) as category_distribution by user_id
步骤说明:
- 第一步:筛选目标用户后,统计每个用户各产品类别的购买次数
- 第二步:用
eventstats计算每个用户的总购买量,并将该值关联到对应用户的每一条品类记录中 - 第三步:通过
eval计算品类购买占比(保留1位小数),并拼接成品类=百分比%的格式 - 第四步:将每个用户的所有品类占比聚合为一个列表输出
内容的提问来源于stack exchange,提问作者James Wierzba
相关产品推荐
相关产品推荐

