2016-11-08 15 views
0

假設我有一個表,使得:分組在SQL表

|ID | product |orderid | brand |number of product cust ord| 
|----|---------|--------|-------|--------------------------| 
| 1 | 123 | 111 | br |  1     | 
|----|---------|--------|-------|--------------------------| 
| 1 | 234 | 111 | br |  1     | 
|----|---------|--------|-------|--------------------------| 
| 1 | 345 | 333 | br |  1     | 
|----|---------|--------|-------|--------------------------| 
| 2 | 123 | 211 | br |  1     | 
|----|---------|--------|-------|--------------------------| 
| 2 | 456 | 212 | br |  2     | 
|----|---------|--------|-------|--------------------------| 
| 3 | 567 | 213 | br |  1     | 
|----|---------|--------|-------|--------------------------| 

我希望做的是將它們分組爲:

|ID | brand |number of product cust ord| 
|----|---------|--------------------------| 
| 1 | br |  3     | 
|----|---------|--------------------------| 
| 2 | br |  4     | 
|----|---------|--------------------------| 

進一步,我想分類他們嘗試了一個案件......但似乎無法做到。

如果ID購買超過3種獨特產品和訂單的次數超過兩次 - 我想稱他們爲常客(在上例中,ID'1'爲'常客'),如果平均他們購買的產品數量高於所銷售產品的平均數量 - 我想稱他們爲「商人」,否則就是購買者。

+0

您的描述,您想要的結果無關彼此。 –

+0

本質上,我們試圖計算顧客爲特定品牌訂購的產品數量。因此,我們不一定需要保留他們的產品或orderid ..但是對於每個客戶,我們想知道他們在特定品牌類別中購買了多少物品。 – Ally

+0

在嘗試提出更多問題之前,請閱讀[我如何提出一個好問題?](http://stackoverflow.com/help/how-to-ask)。 –

回答

-1

這是你可以做到的一種方法。請注意,根據您給出的描述,隨着平均價格的上漲和下降,買家可能會不斷在「商戶」和「買方」之間重新分類。這可能不是你想要的。

With cte As (
    Select ID, 
     Brand, 
     DistinctOrders = Count(Distinct OrderID), -- How many separate orders by this customer for the brand? 
     DistinctProducts = Count(Distinct Product), -- How many different products by this customer for the brand? 
     [number of product cust ord] = Sum(CountOfProduct), -- Total number of items by this customer for the brand. 
     AverageCountOfProductPerBuyer = 
      Sum(Sum(CountOfProduct)) Over() * 1.0/(Select Count(*) From (Select Distinct ID, Brand From #table) As tbl) 
      -- Average number of items per customer (for all customers) for this brand 
     From #table 
     Group By ID, Brand) 
Select ID, Brand, DistinctOrders, DistinctProducts, [number of product cust ord], 
    IsFrequentBuyer = iif(DistinctOrders > 1 And DistinctProducts > 2, 'Frequent Buyer', NULL), 
    IsMerchant = iif(AverageCountOfProductPerBuyer < [number of product cust ord], 'Merchant', 'Purchaser') 
    From cte; 

該查詢可以在不使用公用表表達式的情況下編寫,但是這樣寫就可以避免多次定義表達式。

請注意,根據您的描述,我有第一個ID爲'Frequent Buyer',所以我假設當您說'超過3個獨特產品'時,您的意思是3或更多。同樣有兩個或更多不同的訂單。

+0

謝謝!這工作完美! – Ally

0

爲簡潔起見,我已將最後一個字段更名爲qty,並將其稱爲表test1。

要獲得常旅客使用以下查詢。請注意,我使用> =而不是>。我改變了這種基於您的示例,其中ID 1是「常客」,儘管他只買了3個款產品,不超過3個。

SELECT ID, count(distinct product) as DistinctProducts, count(distinct orderid) DistinctOrders 
FROM test1 
GROUP BY ID 
HAVING count(distinct product) >= 3 and count(distinct orderid) >= 2 

不知道如果我理解正確的商人邏輯。下面是一個查詢,它可以讓你的客戶平均購買超過任何給定產品的總體平均產品。數據中沒有。

SELECT DISTINCT c.ID 
FROM 
(select ID, product, avg(qty) as AvgQty 
FROM test1 
GROUP BY ID, product) as c 
FULL OUTER JOIN 
(select product, avg(qty) as AvgQty 
FROM test1 
GROUP BY product) p ON p.product = c.product 
WHERE c.AvgQty > p.AvgQty; 

得到「購買」做,除非所有客戶和商家和買家頻繁的聯盟之間:

select distinct ID from test1 

EXCEPT 

(SELECT ID FROM (
select ID, count(distinct product) as DistinctProducts, count(distinct orderid) DistinctOrders 
FROM test1 
GROUP BY ID 
HAVING count(distinct product) >= 3 and count(distinct orderid) >= 2) t 

UNION 

SELECT DISTINCT c.ID 
FROM 
(select ID, product, avg(qty) as AvgQty 
FROM test1 
GROUP BY ID, product) as c 
FULL OUTER JOIN 
(select product, avg(qty) as AvgQty 
FROM test1 
GROUP BY product) p ON p.product = c.product 
WHERE c.AvgQty > p.AvgQty 
); 
+0

謝謝!這工作,但我在經常和商人表中得到重複,但我能夠工作:) – Ally