0

我有像客户订单这样的父子表。每个订单必须属于一个且仅一个客户,一个客户有零个、一个或多个订单。

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

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

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

谢谢亚历克斯

4

1 回答 1

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 
于 2012-12-07T18:29:57.963 回答