计算多个属性sql

时间:2012-11-13 13:35:09

标签: mysql sql

我正在寻找以下输出

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

通常是一个矩阵,顾客数量超过购买的商品和产品组,即。计算超过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

你能解释一下这是怎么回事吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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