求一个sql 语句《高手请进》

8292669 2006-03-14 03:31:42
我有一个数据表:

total_reg有下字段:
id int
total_num int 总数
total_date datetime 时间


记录每天注册总数,现在我要做一个报表,需要计算出每天新注册的用户量。那么我该怎么样写这个语句呢。

例如:

total_reg 表

id total_num total_date
1 30000 2006-1-1
2 35000 2006-1-2
3 38000 2006-1-3


那么我得到报表是:

id new_num total_date
1 5000 2006-1-1
2 3000 2006-1-2


请高手出手啊!
...全文
41 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
-狙击手- 2006-03-14
declare @t table (id int,total_num int,total_date datetime)
insert @t
select 1,30000,'2006-1-1' union all
select 2,35000,'2006-1-2' union all
select 3,38000,'2006-1-3'



select id,(select total_num from @t where id = a.id + 1 ) - total_num as total_num,
total_date
from @t a
where
exists(select 1 from @t where datediff(dd,a.total_date,total_date)=1)

/*

id total_num total_date
----------- ----------- ------------------------------------------------------
1 5000 2006-01-01 00:00:00.000
2 3000 2006-01-02 00:00:00.000

*/
回复
-狙击手- 2006-03-14
select id,(select total_num from @t where id = a.id + 1 ) - total_num as total_num,
total_date
from @t a
回复
子陌红尘 2006-03-14
declare @total_reg table(id int,total_num int,total_date datetime)
insert into @total_reg select 1,30000,'2006-1-1'
insert into @total_reg select 2,35000,'2006-1-2'
insert into @total_reg select 3,38000,'2006-1-3'

select
a.id,
total_num=(select total_num from @total_reg where datediff(dd,a.total_date,total_date)=1)-a.total_num,
a.total_date
from
@total_reg a
where
exists(select 1 from @total_reg where datediff(dd,a.total_date,total_date)=1)

/*
id total_num total_date
----------- ----------- ------------------------------------------------------
1 5000 2006-01-01 00:00:00.000
2 3000 2006-01-02 00:00:00.000
*/
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2006-03-14 03:31
社区公告
暂无公告