SQL查询结果补行。
select * from tab 的结果为:
2007-03-09138.00
2007-03-1074.50
也就是说这个月只有9号和10号有数据,但是我想让那些没有数据的日期也显示出来,即:
2007-03-01 0.00
2007-03-02 0.00
……
2007-03-09 138.00
2007-03-10 74.50
……
2007-03-31 0.00
不知道能不能实现?
[解决办法]
declare @t table(date datetime,amount numeric(5,2))
insert into @t select '2007-03-09 ',138.00
insert into @t select '2007-03-10 ',74.50
select top 31 identity(int,0,1) as id into # from syscolumns
select
a.date,isnull(b.amount,0) as amount
from
(select dateadd(dd,id, '2007-03-01 ') as date from #) a
left join
@t b
on
a.date=b.date
drop table #