我有张表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天都不存在,那就算他是新用户,以此类推,我想统计一下每天的新用户数,应该怎么做啊??

解决方案 »

  1.   

    select date_format(logDate,'%Y-%m') as 月份,
    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')
      

  2.   

    先感谢2位的回复,其实一开始设计表的时候没想到这个问题,本来这个表是用来记录玩家的行为的,现在在改已经不行了。
    josy哎 你发的那个sql 我一开始就是这么用的,但是完全不行,执行结果不对。
      

  3.   

    select DISTINCT Code
    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  )
      

  4.   

    select date_format(logDate,'%Y%m%d') as 月份,count(1) as 新用户数
    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')