下面是我拥有Customer_ID和他们拥有不同电话的表。
customer_id phone_number
101 123456789
102 234567891
103 345678912
102 456789123
101 567891234
104 678912345
105 789123456
106 891234567
106 912345678
106 456457234
101 655435664
107 453426782
现在,我想找到customer_id和不同的电话号码计数。
所以我使用了此查询:
select distinct customer_id ,count(distinct phone_number)
from customer_phone;
customer_id no of phones
101 3
102 2
103 1
104 1
105 1
106 3
107 1
,从上表中,我的最终目标是实现以下输出,该输出需要计数并放入不同的存储桶,然后计算落入这些存储桶中的消费者数量。
Buckets no of consumers
3 2
2 1
1 4
有近2亿张记录。您能解释一下一种有效的方法吗?
您可以使用width_bucket
:
select bucket, count(*)
from (
select width_bucket(count(distinct phone_number), 1, 10, 10) as bucket
from customer_phone
group by customer_id
) t
group by bucket;
width_bucket(..., 1, 10, 10)
为值1到10创建十个存储桶。
在线示例:http://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1e6d55330570499f363837aba21bdc7e
使用两个聚合:
select cnt, count(*), min(customer_id), max(customer_id)
from (select customer_id, count(distinct phone_number) as cnt
from customer_phone
group by customer_id
) c
group by cnt
order by cnt;