#客户放置#order查询

时间:2015-04-02 18:33:03

标签: sql-server

你能告诉我过去12个月有多少顾客放置

  • 1个订单
  • 2个订单
  • 3个订单
  • 4个订单
  • 5个或更多订单

1个订单800,000个客户800,000个订单 2个订单100,000个客户20万个订单 3个订单30,000个客户90,000个订单 等

2 个答案:

答案 0 :(得分:0)

这将做你想要的,除了它不会总和超过5 - 它会显示尽可能多的故障(所以如果客户订购了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

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