1

下面是我有 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 亿条记录。你能解释一下解决这个问题的有效方法吗?

4

2 回答 2

1

您可以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

于 2018-02-22T13:02:47.610 回答
0

使用两个聚合:

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;
于 2018-02-22T12:53:51.530 回答