2015-04-02 67 views
-4

你能告訴我在過去12個月中有多少客戶放在#客戶放在#訂單查詢

  • 1爲了
  • 2訂單
  • 3訂單
  • 4訂單
  • 5更多訂單

1個訂單800,000個客戶800,000個訂單 個2訂單10萬級的用戶20萬的訂單 3個數量30,000個客戶訂單90000等等

+0

我該如何做這個查詢? – Chris 2015-04-02 18:33:39

+1

select \t count(distinct o.OrigOrderid)as Orderscount,count(distinct o.BilltoPersonId)as UniqueCustomers from indo.dbo.Orders o where o.SourceId = 80 and o.isCancelled = 0 and o.isTransacted = 1 and o.DateOrdered> ='4/1/14' Group By o.BilltoPersonId – Chris 2015-04-02 18:44:53

+0

它給了我這個... – Chris 2015-04-02 18:45:20

回答

0

這會做你想要它除了在5不會和什麼 - 它會shpw您擊穿儘可能多的有(因此,如果一個客戶做50個訂單將有一排爲:

with d1 as 
(select COUNT(*) as [Number of Orders], row_number() 
    over (order by custid) as [How Many Orders] 
from t2 group by custid) 
    select * from d1 
0

根據您的評論,這裏是你所需要的。 我刪除了「SourceId = 80」,因爲我不知道這種情況的目的。

SELECT NbOrdersPerCustomer.NBOrders, 
     COUNT(NbOrdersPerCustomer.CustomerId) 
FROM ( SELECT CustomerId = o.BilltoPersonId, 
       NBOrders = COUNT(o.OrigOrderid) 
     FROM indo.dbo.Orders o 
     WHERE o.isCancelled = 0 
     AND o.isTransacted = 1 
     AND o.DateOrdered >= '4/1/14' 
     GROUP BY o.BilltoPersonId) AS NbOrdersPerCustomer 
GROUP BY NbOrdersPerCustomer.NBOrders