BigQuery:查找 2 个值的最常见组合

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

我有一个这样的数据集:

人# 水果
001 橙色
001 葡萄
001 苹果
002 橙色
002 苹果
003 橙色
003 葡萄
004 橙色
004 苹果

这是我期待的输出:

**组合 unique_person_count
苹果、橙子 3
橙子、葡萄 2
苹果、葡萄 1

请在 BiqQuery 中提供您非常感谢的帮助,因为这是在 GCP 中完成的。

sql google-cloud-platform google-bigquery
1个回答
0
投票

尝试以下简单方法

select 
  format('%s, %s', a.fruit, b.fruit) combination, 
  count(distinct a.person) unique_person_count
from your_table a
join your_table b
on a.person = b.person
and a.fruit < b.fruit
group by combination
order by unique_person_count desc

如果应用于您问题中的样本数据 - 输出为

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