SQL查询日期的问题

来源:百度知道 编辑:UC知道 时间:2024/05/09 11:08:45
我要查询二个日期之间的连续日期,应该怎样查?

表:
[nowtime],[title],[cont]
2009-1-1 标题1 100.00
2009-1-2 标题1 200.00
2009-1-5 标题1 400.00
2009-1-8 标题1 150.00

怎样才可以让SQL输出:
2009-1-1 标题1 100.00
2009-1-2 标题1 200.00
2009-1-3 标题1 0.00=====================为空时补上行
2009-1-4 标题1 0.00=====================为空时补上行
2009-1-5 标题1 400.00
2009-1-6 标题1 0.00=====================为空时补上行
2009-1-7 标题1 0.00=====================为空时补上行
2009-1-8 标题1 150.00

我现在用下面代码,但当查询效率非常差:
for i=0 to DateDiff("d", '2009-1-1', '2009-1-8')
set rs=conn.execute("select [cont] from [表] where DateDiff(""d"",[nowtime] , '"&dateadd("d",i,"2009-1-1")&"')=0 ")
if rs.eof then
'====补上行
else
end if
next

<%
date1="2009-1-1"
date2="2009-1-8"

set rs=conn.execute("select nowtime,[cont] from [表] where nowtime between '"&date1&"' and '"&date2&"'")
for i=0 to DateDiff("d", date1, date2)
if rs("nowtime")=dateAdd("d",i,date1) and not rs.eof then
cont=rs("cont")
rs.movenext
else
cont=0
end if
next
%>

SQL2000临时表,05以上用CTE

如:

--> -->

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([nowtime] Datetime,[title] nvarchar(3),[cont] decimal(18,2))
Insert #T
select '2009-1-1',N'标题1',100.00 union all
select '2009-1-2',N'标题1',200.00 union all
select '2009-1-5',N'标题1',400.00 union all
select '2009-1-8',N'标题1',150.00
Go
declare @