每个Bucket中的Oracle WIDTH_BUCKET计数

时间:2012-12-06 17:52:00

标签: frequency

我有像客户订购的父子表。每个订单必须属于一个且只有一个客户,而客户只有零个,一个或多个订单。

如何获得订单的COUNTS频率分布。所以输出将是: 订单数为0-100的客户数 拥有101-200个订单的客户数量 201-300订单的客户数量。

我不关心客户ID,只关心订单数量。

我试图使用WIDTH_BUCKET函数,但它不会对每个存储桶中的项目数进行分组和计数。也许还有另一种方式,除了WIDTH_BUCKET。

由于 亚历

1 个答案:

答案 0 :(得分:0)

我昨晚在这个问题上做了一些研究,并且上帝的恩典找到了一个解决方案,让你(好的,你的商业伙伴) - 定义桶大小,并计算分配频率计数。

这是 - (对不起这个网站的格式不好) 感谢你的帮助!! 亚历

with starter as(
select cust.customer_id, count(o.order_id) as row_count
from CUSTOMER cust  LEFT JOIN ORDER o
using (customer_id)
where cust.last_update_user = 'MDM_ETL' and
o.last_update_user = 'MDM_ETL' -- and
group by cust.customer_id
order by row_count desc)
select 5 , '2000 or more' as cnt_of_orders, count(starter.customer_id) as          
nmb_customers_with_this_cnt from starter where starter.row_count >= 2000
union 
select 4, '1500 - 1999' as cnt_of_orders, count(starter.customer_id) as    
nmb_customers_with_this_cnt from starter where starter.row_count between 1500 and 1999
union 
select 3, '1000 - 1499' as cnt_of_orders, count(starter.customer_id) as    
nmb_customers_with_this_cnt from starter where starter.row_count between 1000 and 1499
union 
select 2, '500 - 999' as cnt_of_orders, count(starter.customer_id) as   
nmb_customers_with_this_cnt from starter where starter.row_count between 500 and 999
union 
select 1, '0 - 499' as cnt_of_orders, count(starter.customer_id) as    
nmb_customers_with_this_cnt from starter where starter.row_count between 0 and 499