0

我有一个查询,它会提取活跃客户的数量并找到客户活跃的总天数......我想找到这个列的平均值......

是否可以在这样的查询中提取平均值并将其显示在 1 行上?或者这需要在报告方面完成吗?我目前的查询是:

SELECT        
   dtCreated, bActive, dtLastUpdated, dtLastVisit, 
   DATEDIFF(d, dtCreated, dtLastUpdated) AS Difference
FROM 
   Customers
WHERE        
   (bActive = 'true') 
   AND (dtLastUpdated > CONVERT(DATETIME, '2012-01-01 00:00:00', 102))

谢谢。

4

1 回答 1

4

使用 AVG:http: //msdn.microsoft.com/en-us/library/ms177677

SELECT         
   dtCreated, bActive, dtLastUpdated, dtLastVisit,  
   DATEDIFF(d, dtCreated, dtLastUpdated) AS Difference,
   AVG(DATEDIFF(d, dtCreated, dtLastUpdated)) OVER() AS AvgDifference
FROM  
   Customers 
WHERE         
   (bActive = 'true')  
   AND (dtLastUpdated > CONVERT(DATETIME, '2012-01-01 00:00:00', 102)) 

将其更新为使用 OVER():http: //msdn.microsoft.com/en-us/library/ms189461.aspx

于 2012-07-10T18:56:49.420 回答