我制作了MSSQL查询,用于计算过去30天内的唯一IP地址和总IP记录。
SELECT count(IPAddress) AS totalPageHits, count(DISTINCT IPAddress) AS totalVisitors, count(DateTime)
FROM recordTable
WHERE DateTime > GETDATE()-30
现在我很难计算每日平均点击次数和平均每日唯一身份访问者数量。
我曾尝试使用AVG
,但它给出了一条错误消息“操作数据类型varchar对avg运算符无效”。
非常感谢任何帮助。
答案 0 :(得分:3)
SELECT AVG(HitCount) as AverageHits,
AVG(UniqueHitCount) As AverageUniqueHits
FROM
(
SELECT COUNT(IPAddress) as HitCount,
COUNT(DISTINCT IPAddress) as UniqueHitCount,
DATEDIFF(d, GETDATE(), DateTime) as Day
FROM tblHitCounter
WHERE DateTime > GETDATE() - 30
GROUP BY DATEDIFF(d, GETDATE(), DateTime)
) sub
答案 1 :(得分:2)
您需要按天分组,然后执行平均值:
select avg(totalPageHits) as avgDailyTotalPageHits,
avg(totalVisitors) as avgDailyTotalVisitors
from (SELECT cast(DateTime as date) as thedate,
count(IPAddress) AS totalPageHits,
count(DISTINCT IPAddress) AS totalVisitors, count(DateTime) as cnt
FROM recordTable
WHERE DateTime > GETDATE()-30
group by cast(DateTime as date)
) t