我有张表A里面主要有三个字段,Code 用户设备代码(可重复),AppId应用Id(可重复)和一个LogDate字段(记录时间),我现在想要来统计每天有多少新用户.
例如 2011-12-01是表使用的第一天
select DISTINCT Code from A where logDate between '2011-12-01 00:00:00' and '2011-12-01 23:59:59'and AppId=3
第二天数据
select DISTINCT Code from A where logDate between '2011-12-02 00:00:00' and '2011-12-02 23:59:59'and AppId=3
第一天数据和第二天数据做比较,在第一天中不存在的code就是新用户,第三天的数据和前2天的做比较,如果code在前2天都不存在,那就算他是新用户,以此类推,我想统计一下每天的新用户数,应该怎么做啊??
例如 2011-12-01是表使用的第一天
select DISTINCT Code from A where logDate between '2011-12-01 00:00:00' and '2011-12-01 23:59:59'and AppId=3
第二天数据
select DISTINCT Code from A where logDate between '2011-12-02 00:00:00' and '2011-12-02 23:59:59'and AppId=3
第一天数据和第二天数据做比较,在第一天中不存在的code就是新用户,第三天的数据和前2天的做比较,如果code在前2天都不存在,那就算他是新用户,以此类推,我想统计一下每天的新用户数,应该怎么做啊??
count(1) as 新用户数
from A t
where not exists(select 1 from A where code=t.code and logDate<t.logDate)
and AppId=3
group by date_format(logDate,'%Y-%m')
josy哎 你发的那个sql 我一开始就是这么用的,但是完全不行,执行结果不对。
from A t
where logDate between '2011-12-02 00:00:00' and '2011-12-02 23:59:59' and AppId=3
and not exists (select 1 from A where code=t.code and logDate<'2011-12-02 00:00:00' and AppId=3 )
select *
from A t
where not exists(select 1 from A where code=t.code and logDate < t.logDate) and AppId=3
)T
group by date_format(logDate,'%Y%m%d')