我想生成这样的数据从2009年12月18日早上8点开时为一条数据,然后每隔半个小时为一条数据,一直生成直至2010年1月20日下午6时,结果应该象这样:2009年12月18日08:00
2009年12月18日08:30
2009年12月18日09:00
2009年12月18日09:30
......................
2010年01月20日18:00谢谢!
2009年12月18日08:30
2009年12月18日09:00
2009年12月18日09:30
......................
2010年01月20日18:00谢谢!
insert into tb values('2007-1-1 10:00:23' , 8 )
insert into tb values('2007-1-1 10:01:24' , 4 )
insert into tb values('2007-1-1 10:05:00' , 2 )
insert into tb values('2007-1-1 10:06:12' , 3 )
insert into tb values('2007-1-1 10:08:00' , 1 )
insert into tb values('2007-1-1 10:12:11' , 5 )
go--时间段>=10:00:00 and 时间段<10:05:00
select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,时间),120),dateadd(ss,-1,时间))/5)*5,convert(varchar(10),时间,120)) as 时间段,
count(*) as 行数,
sum(金额) as 总金额
from tb
group by dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,时间),120),dateadd(ss,-1,时间))/5)*5,convert(varchar(10),时间,120))
/*
时间段 行数 总金额
------------------------------------------------------ ----------- -----------
2007-01-01 10:00:00.000 3 14
2007-01-01 10:05:00.000 2 4
2007-01-01 10:10:00.000 1 5
(所影响的行数为 3 行)
*/--时间段>10:00:00 and 时间段<=10:05:00
select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,1,时间),120),dateadd(ss,1,时间))/5)*5,convert(varchar(10),时间,120)) as 时间段,
count(*) as 行数,
sum(金额) as 总金额
from tb
group by dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,1,时间),120),dateadd(ss,1,时间))/5)*5,convert(varchar(10),时间,120))
/*
时间段 行数 总金额
------------------------------------------------------ ----------- -----------
2007-01-01 10:00:00.000 2 12
2007-01-01 10:05:00.000 3 6
2007-01-01 10:10:00.000 1 5(所影响的行数为 3 行)
*/drop table tb
godeclare @sdate datetime,@edate datetime
select @sdate='2009-12-18 08:00:00',@edate='2010-01-20 16:00:00'
select
dt=dateadd(ss,(number-1)*30,@sdate)
from #
where dateadd(ss,(number-1)*30,@sdate)<=@edatedrop table #
godeclare @dt1 as datetime
declare @dt2 as datetime
set @dt1 = '2009-10-18 08:00:00'
set @dt2 = '2010-01-20 18:00:00'while @dt1 <= @dt2
begin
insert into tb select @dt1
set @dt1 = dateadd(mi,30,@dt1)
endselect * from tbdrop table tb/*
dt
------------------------------------------------------
2009-10-18 08:00:00.000
2009-10-18 08:30:00.000
2009-10-18 09:00:00.000
2009-10-18 09:30:00.000
2009-10-18 10:00:00.000
2009-10-18 10:30:00.000...2010-01-20 16:30:00.000
2010-01-20 17:00:00.000
2010-01-20 17:30:00.000
2010-01-20 18:00:00.000(所影响的行数为 4533 行)*/
select @sdate='2009-12-18 08:00:00',@edate='2010-01-20 16:00:00'
select
dt=dateadd(mi,(number-1)*30,@sdate)
from #
where dateadd(mi,(number-1)*30,@sdate)<=@edate
set @fromDate=(convert(datetime,'2009-12-18 08:00',120));
while (@fromDate<=(convert(datetime,'2010-01-20 18:00',120)))
begin
insert into a(date) values(@fromDate);
set @fromDate=dateadd(mi,30,@fromDate);
end
select * from a;
SET @fromDate='2009-12-18 08:00'
WHILE (@fromDate<='2010-01-20 18:00')
BEGIN
INSERT INTO A(date) VALUES(@fromDate);
SET @fromDate=DATEADD(mi,30,@fromDate);
ENDSELECT * FROM a;
DELETE FROM a;
set @s1='2009-12-18 08:00'
set @s2='2010-1-20 6:00'
select dateadd(mi,number*30,@s1) from spt_values where type='p' and dateadd(mi,number*30,@s1)<=@s2/*
------------------------------------------------------
2009-12-18 08:00:00.000
2009-12-18 08:30:00.000
2009-12-18 09:00:00.000
2009-12-18 09:30:00.000
2009-12-18 10:00:00.000
2009-12-18 10:30:00.000
2009-12-18 11:00:00.000
2009-12-18 11:30:00.000
2009-12-18 12:00:00.000
2009-12-18 12:30:00.000
2009-12-18 13:00:00.000
2009-12-18 13:30:00.000
2009-12-18 14:00:00.000
2009-12-18 14:30:00.000......
*/