求一个查询方法,新增用户数
直观一些 表结构主要部分大概这样
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 [date],COUNT(DISTINCT(userid)),SUM(price) FROM tableName GROUP BY [date]
新增用户不知道怎么搞。。。
[解决办法]
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