0

所以,基本上我必须生成一个关于页面访问次数的报告。假设这是我的表格数据。(注意:请假设所有记录的日期时间值)
访问者dtime
a
c
b
a
d
b
e 2013-04-31 03:14:29
s 2013-04-01 03:14:29
a 2013-04-01 03 :14:29
b 2013-04-01 03:14:29
b 2013-04-01 03:14:29
c 2013-04-31 03:14:29
c 2nd
b 2nd
3rd
3rd
a 3rd
b 4th
b

因此,如果用户从中选择1st to 3rd,我必须打印 Date PageLoads Unique Visits First time Visits Returning Visits

1st   4   3   1  2

2nd   3   2   0  2

3rd   3   2   0  2

pageloads for 1st are - s a b bunique visits - s a b 问题是找到第一次访问,这是唯一s的,当与之前的每个访问者进行比较时。第二次第一次访问为 0,因为没有人第一次访问

我查找页面加载和唯一访问的查询是-

select distinct DATE(dtime) as ddate,count(visitors) as ploads, 
count(distinct visitors) as uvisits,
from table1 where  dtime >= '2013-04-1 00:00:01' and dtime <= '2013-04-3 23:59:59'
group by DATE(dtime) order by ddate desc;

如何找到每个日期的第一次访问?

4

2 回答 2

0

如果's'是第一次访问,那么

使用求和函数

sum(ifnull((select visitors from yourtable where dtime<'2013-04-1 00:00:01' ),1,0)) as FirstTime


select DATE(dtime) as ddate,
    count(visitors) as ploads, 
    count(distinct visitors) as uvisits,
    sum(ifnull((select visitors from yourtable where dtime<'2013-04-1 00:00:01' ),1,0)) as FirstTime,
    count(distinct visitors)-
    sum(ifnull((select visitors from yourtable where dtime<'2013-04-1 00:00:01' ),1,0)) as ReturningVisits
from yourtable where dtime >= '2013-04-1 00:00:01' 
and dtime <= '2013-04-3 23:59:59' 
group by DATE(dtime) 
order by ddate desc;
于 2013-07-04T10:21:22.823 回答
0

首先,您需要创建一个返回首次访问者的子查询。

select visitor,min(dtime) as firstDate from table
group by visitor

现在你知道访客和他的第一次访问日期..

select distinct DATE(dtime) as ddate,
       count(A.visitors) as ploads, 
       count(distinct visitors) as uvisits,
       count(b.visitors) as FirstTime,
       count(a.visitors)-count(b.visitors) as ReturnVisits
from table A
left join (select visitors,min(dtime) as firstDate from table group by visitors) B 
     on b.visitors=a.visitors and b.firstDate = a.dtime
where  dtime >= '2013-04-1 00:00:01' and dtime <= '2013-04-3 23:59:59'
group by DATE(dtime) 
order by ddate desc;

我没有在这台机器上安装 mySQL,所以请原谅任何语法错误,但我希望这能为您指明正确的方向。

于 2013-07-04T10:34:11.650 回答