在sql Oracle中查找总命中数和访问者数

时间:2014-06-19 10:26:37

标签: sql oracle

我有一个表appWebStats,其中包含以下表字段appName,userName,dateTime

我试过

select Count(a.appName) as totalHits ,
       Count(a.distinct userName) as visitors ,
       a.dateTime 
from appWebStats a 
where date between time between '11-APR-12' and '14-APR-12'   
group by a.appName ,a.userName,a.dateTime;

但它在所有行中撤回

这里可能有什么问题?

编辑:

我想要的是

day      appname     totalHist uniqueVisitors
11         app1          56         5
11         app2          36         8
11         app3          26         7
12         app1          56         6
12         app2          36         8
13         app1          27         9
14         app2          34         4

1 个答案:

答案 0 :(得分:3)

您的查询不是在做您的问题所说的。如果您希望按应用分类访问者,请按应用分组并计算访问者数:

select a.appName, 
       count(*) as visitors, 
       count(distinct a.userName) as uniqueVisitors
from table a
where datetime between '11-APR-12' and '14-APR-12'
group by a.appName;

相反,如果您想要每天的应用和访问者数量,那么请使用时间;

select trunc(a.datetime) as theday, 
       count(distinct a.appName) as numapps,
       count(distinct a.userName) as uniqueVisitors
from table a
where datetime between '11-APR-12' and '14-APR-12'
group by trunc(a.datetime)
order by 1;

编辑:

我认为您正在寻找的查询是:

select trunc(a.datetime) as theday, a.appName as numapps,
       count(*) as TotalHist,
       count(distinct a.userName) as uniqueVisitors
from table a
where datetime between '11-APR-12' and '14-APR-12'
group by trunc(a.datetime), a.appName
order by trunc(a.datetime), a.appName;