2015-05-05 82 views

回答

1

內選擇計數訪問/天,外平均:

select avg(cnt) 
from (select count(*) as cnt 
     from tablename 
     group by EventDateUtc) 
+0

我只是需要時間部分 – user960567

+0

你能加樣表數據,和預期的結果,這個問題? – jarlh

2

你可以得到一個count通過UserAgent,EventDateUtc分組,然後在它上面做一個AVG

SELECT AVG(reccount) avg_count,EventDateUtc 
FROM 
(
SELECT COUNT(*) reccount,UserAgent, CONVERT(DATE,EventDateUtc) EventDateUtc 
FROM PageVisit 
GROUP BY CONVERT(DATE,EventDateUtc),UserAgent 
)T 
GROUP BY EventDateUtc 

如果您想要所有日子的總體平均值,請不要在外部查詢中執行GROUP BY。

SELECT AVG(reccount) avg_count 
FROM 
(
SELECT COUNT(*) reccount,UserAgent, CONVERT(DATE,EventDateUtc) EventDateUtc 
FROM PageVisit 
GROUP BY CONVERT(DATE,EventDateUtc),UserAgent 
)T