注册 登录
编程论坛 SQL Server论坛

sql 语句求助

棉花糖ONE 发布于 2006-11-09 15:31, 796 次点击
表中有个日期字段(包括时间),想要统计每个日期之前所有的用户数(包括当天)
13 回复
#2
lengxie2006-11-09 16:48
看不懂!!
#3
LouisXIV2006-11-09 17:24

declare @table table
(
uid int,
date datetime
)

insert into @table
select 1,getdate()-1 union all
select 2,getdate()-1 union all
select 3,getdate()-1 union all
select 4,getdate()-2 union all
select 5,getdate()-2 union all
select 6,getdate()-3 union all
select 7,getdate()

select distinct (select count(uid) from @table where date<=a.date) as Totol_U, date from @table a

#4
棉花糖ONE2006-11-09 17:40

老大可以一句实现吗

#5
LouisXIV2006-11-09 17:41
上面都是测试数据,最后一行是查询

你自己体会去
#6
棉花糖ONE2006-11-09 17:42
还有啊,date里面不是只有日期还有,还包括时间啊
#7
棉花糖ONE2006-11-09 17:44
我写的和你一样啊.但是我用了left函数取出日期后,用表来取值的时候老报错啊
#8
棉花糖ONE2006-11-09 17:45
date&lt;=a.date我就是把这个改成left(date,8)&lt;a.left(date,8),就老报错,我不知道怎么解决了,我本想建临时表,先把日期部分导出来,但是一句没法实现啊
#9
LouisXIV2006-11-09 17:47
datetime类型没有取left,right这样的写法

你自己去看看convert函数的用法
#10
棉花糖ONE2006-11-09 17:49

是这样啊,用convert取出日期部分就可以了,谢谢斑竹

#11
棉花糖ONE2006-11-09 18:05
解决了,斑竹太感谢了
#12
Web2006-11-14 23:44
学习了.........                           
#13
漯河2006-11-17 20:04
没有看懂!

#14
zxd1987782006-11-17 23:10
declare @table table
(
uid int,
date datetime
)
斑竹你的这句话是建表吗?
怎么我用的sql sever建表怎么不是这样的啊
你是用的SQL SEVER吗
select 1,getdate()-1 union all
select 2,getdate()-1 union all
select 3,getdate()-1 union all
select 4,getdate()-2 union all
select 5,getdate()-2 union all
select 6,getdate()-3 union all
select 7,getdate()
这一段是什么意思啊
小弟看不懂能解释一下吗?
1