直观一些     表结构主要部分大概这样
userid   price      date
1         100       2013-1-1
2         50        2013-1-1
1         100       2013-1-2
2         100       2013-1-2
1         100       2013-1-2
3         100       2013-1-3
2         200       2013-1-3
2         200       2013-1-3
4         100       2013-1-3写过一个方法,但是太乱太复杂了
我现在根据每天统计出
日期       充值用户数   总价   新增用户数   ...现在主要新增用户数这不知道简单些方法如何统计出
2013-1-1   2            150      ?(2)
2013-1-2   2            300      ?(0)
2013-1-3   3            600      ?(2)

解决方案 »

  1.   

    新增用户数:
    select count(*) from tb where userid not in(select distinct userid from tb where date<'2013-1-3')
      

  2.   


    SELECT [date],COUNT(DISTINCT(userid)),SUM(price) FROM tableName GROUP BY [date]新增用户不知道怎么搞
      

  3.   

    select convert(varchar(10),date,120)日期,count(distinct userid) 充值用户数,sum(price)总价,
    (select count(distinct tb2.userid) from tb tb2 where convert(varchar(10),tb1.date,120)=convert(varchar(10),tb2.date,120) and not exists(select 1 from tb tb3 where tb2.userid=tb3.userid and tb3.date<tb2.date)
    )新增用户数 from tb tb1 group by convert(varchar(10),date,120)
      

  4.   

    create table [table]
    (userid int,
    price int,
    date datetime)insert into [table]
    select 1,100,'2013-1-1' union all
    select 2     ,    50        ,'2013-1-1' union all
    select 1     ,    100       ,'2013-1-2' union all
    select 2     ,    100       ,'2013-1-2' union all
    select 1      ,   100       ,'2013-1-2' union all
    select 3      ,   100       ,'2013-1-3' union all
    select 2      ,   200       ,'2013-1-3' union all
    select 2      ,   200       ,'2013-1-3' union all
    select 4      ,   100       ,'2013-1-3'select date,
    count(distinct userid),
    sum(price),
    (select count(1) from (select userid,min(date) as date from [table] group by userid) a where a.date=[table].date
    ) from [table]
    group by date