0

我有这个 T-sql 来获取当月每个销售代表对客户的计数我想问一下如何应用 avg 函数来获取当月的平均访问

SELECT COUNT(VisitTracking.customerID) AS #VISIT, 
       MAX(VisitTracking.visitID) AS VisitID, 
       Customers.title AS Title, 
       Customers.firstName AS [First Name], 
       Customers.LastName AS [Last Name], 
       Company.companyName AS [Company Name], 
       MAX(VisitTracking.DateVisited) AS [Date Visited], 
       CONVERT(DATE, MAX(VisitTracking.nextVisit)) AS [Next Visit], 
       Customers.customerID
FROM VisitTracking 
INNER JOIN Customers ON VisitTracking.customerID = Customers.customerID
INNER JOIN Customer_Company ON Customers.customerID = Customer_Company.customerID     
INNER JOIN Company ON Customer_Company.companyID = Company.companyID
WHERE VisitTracking.DateVisited BETWEEN '11/01/2012'  AND '11/31/2012' 
GROUP BY Customers.title, Customers.firstName, Customers.LastName, Company.companyName, Customers.customerID
4

2 回答 2

2

你试过计算吗?

    SELECT [sParID], count([sParID]) as '#sID'
      FROM [docSVsys]
     group by [sParID]
     order by [sParID] 
   compute avg(count([sParID]))
于 2012-11-30T22:41:22.123 回答
0

为什么不是子选择?是的,我知道,子选择很糟糕,不是很好,而且表现不佳。但他们在为报告和分析生成数据时确实完成了工作,所以,也许:

SELECT AVG(a.#VISIT) FROM (SELECT COUNT(VisitTracking.customerID) AS #VISIT, 
       MAX(VisitTracking.visitID) AS VisitID, 
       Customers.title AS Title, 
       Customers.firstName AS [First Name], 
       Customers.LastName AS [Last Name], 
       Company.companyName AS [Company Name], 
       MAX(VisitTracking.DateVisited) AS [Date Visited], 
       CONVERT(DATE, MAX(VisitTracking.nextVisit)) AS [Next Visit], 
       Customers.customerID
FROM VisitTracking 
INNER JOIN Customers ON VisitTracking.customerID = Customers.customerID
INNER JOIN Customer_Company ON Customers.customerID = Customer_Company.customerID     
INNER JOIN Company ON Customer_Company.companyID = Company.companyID
WHERE VisitTracking.DateVisited BETWEEN '11/01/2012'  AND '11/31/2012' 
GROUP BY Customers.title, Customers.firstName, Customers.LastName, Company.companyName, Customers.customerID) AS a
于 2012-11-30T21:07:20.493 回答