0

我正在寻找以下输出

  • 10 位客户在 2 个产品组中购买了 10 件商品
  • 8 位客户在 1 个产品组中购买了 10 件商品。

通常是一个矩阵,其中客户计数超过购买的项目和产品组,即。计算超过 2 个属性(项目和产品组)

我尝试了下面的代码,但它只给了我

  • 1 位客户拥有 10 个项目和 2 个产品组
  • 1 位客户拥有 10 件商品和 1 个产品组,尽管每行中有更多客户:

片段,

count (distinct customer_id) over (Partition by customer_id) as Customer_ID
,count (distinct customer_shipment_item_id) Over (Partition by customer_id) as customer_items
,count (distinct product_group) Over (Partition by customer_id) as customer_product_groups

你能解释一下这是如何工作的吗?

4

2 回答 2

0

我认为这样的事情会给你你正在寻找的东西:

SELECT    COUNT(customer_id) Customer_ID, customer_items, customer_product_groups

FROM 

(SELECT   customer_id, COUNT(DISTINCT customer_shipment_item_id) as customer_items,
          COUNT(DISTINCT product_group) as customer_product_groups
 FROM     TABLENAME
          JOIN <YOUR JOINS>
 WHERE    customer_id IN (SELECT customer_ID FROM <YOUR JOINS> WHERE customer_shipment_item_id = x)
 GROUP BY customer_id
) a

GROUP BY customer_items, customer_product_groups
于 2012-11-13T18:49:18.673 回答
0

尝试这个。除非我不明白您在寻找什么,否则您不需要 Partition By 子句。

count (distinct customer_id) as Customer_ID
,count (distinct customer_shipment_item_id) as customer_items
,count (distinct product_group) as customer_product_groups
于 2012-11-13T13:45:55.420 回答