直观一些 表结构主要部分大概这样
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)
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)
select count(*) from tb where userid not in(select distinct userid from tb where date<'2013-1-3')
SELECT [date],COUNT(DISTINCT(userid)),SUM(price) FROM tableName GROUP BY [date]新增用户不知道怎么搞
(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)
(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