如何查找每个客户的不同电话数量并根据计数将客户(计数)放入不同的桶中?
How to find number of distinct phones per customer and put the customers(counts) in different buckets as per the counts?
下面是 table,我有 customer_id 和他们有不同的 phone。
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 和不同的 phone 数字计数。
所以我使用了这个查询:
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
并且,从上面 table 我的最终目标是实现下面的输出,它获取计数并放入不同的桶中,然后计算落入这些桶中的消费者数量。
Buckets no of consumers
3 2
2 1
1 4
有近2亿条记录。您能否解释一下解决此问题的有效方法?
使用两个聚合:
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;
您可以使用 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=1e6d55305570499f363837aba21bdc7e
下面是 table,我有 customer_id 和他们有不同的 phone。
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 和不同的 phone 数字计数。
所以我使用了这个查询:
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
并且,从上面 table 我的最终目标是实现下面的输出,它获取计数并放入不同的桶中,然后计算落入这些桶中的消费者数量。
Buckets no of consumers
3 2
2 1
1 4
有近2亿条记录。您能否解释一下解决此问题的有效方法?
使用两个聚合:
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;
您可以使用 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=1e6d55305570499f363837aba21bdc7e