得到连续日期需要借助一个有连续序号的表,参考如何得到连续序号
--〉生成连续日期的方法
IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #t
GO
create table #t(id int identity,Dt varchar(10))
go
declare @starttime datetime,@endtime datetime
set @starttime = '2010-5-01'
set @endtime ='2010-5-31'
insert #t
select convert(varchar(10),dateadd(day,number,@starttime),120) dt
from master..spt_values
where type='P' and number between 0 and datediff(day,@starttime,@endtime)
--结果
select * from #t
/*
id Dt
----------- ----------
1 2010-05-01
2 2010-05-02
3 2010-05-03
4 2010-05-04
5 2010-05-05
6 2010-05-06
7 2010-05-07
8 2010-05-08
9 2010-05-09
10 2010-05-10
11 2010-05-11
12 2010-05-12
13 2010-05-13
14 2010-05-14
15 2010-05-15
16 2010-05-16
17 2010-05-17
18 2010-05-18
19 2010-05-19
20 2010-05-20
21 2010-05-21
22 2010-05-22
23 2010-05-23
24 2010-05-24
25 2010-05-25
26 2010-05-26
27 2010-05-27
28 2010-05-28
29 2010-05-29
30 2010-05-30
31 2010-05-31
(31 行受影响)
*/
实际例子:
原帖:
http://topic.csdn.net/u/20100619/18/3b4d60f2-b477-414f-adec-569ccee3fec6.html
http://topic.csdn.net/u/20100619/15/367ed306-b8ea-4b2b-9614-2acd64c0e07e.html
根据开始时间,结束时间 生成连续的时间
然后在用这个连续的时间去比较表里的数据
if object_id('TZ_Money')is not null drop table TZ_Money go
create table TZ_Money
(
Id int identity(1,1) primary key,
Uid varchar(8) not null, --用户ID
Income numeric(10,2) not null, --收入
Expenditure numeric(10,2) not null, --支出
Balance numeric(10,2) not null, --余额
[Time] datetime not null, --日期(天为单位)
LastTime datetime null --最后时间
)
go
set identity_insert TZ_Money on
insert TZ_Money (id,uid,Income,Expenditure,Balance,Time)
select 1 ,'abc1', 1.00 ,5.00 ,96 ,'2010-06-7 12:12:12' union