日期:2014-05-18  浏览次数:20566 次

怎么实现这个SQL问题?
times number 
2006-1 5 
2006-2 10 
2006-5 8 
2006-8 16 
2006-12 9 
表名是:asset
问题: 上表左列内容不全,1-12个月少了7个月,请用T-SQL语言实现,补全左列内容,补上的左列对应的右列值为0,
上表在数据库中不变.
求问题的代码....

------解决方案--------------------
SQL code
--原始数据:#asset
create table #asset(times varchar(7),number int)
insert #asset
select '2006-1',5 union all
select '2006-2',10 union all
select '2006-5',8 union all
select '2006-8',16 union all
select '2006-12',9

select top 12 mon=identity(int,1,1) into #mon from syscolumns

select times='2006-'+ltrim(a.mon),number=isnull(b.number,0) from #mon a left join #asset b on '2006-'+ltrim(a.mon)=b.times

/*
times             number      
----------------- ----------- 
2006-1            5
2006-2            10
2006-3            0
2006-4            0
2006-5            8
2006-6            0
2006-7            0
2006-8            16
2006-9            0
2006-10           0
2006-11           0
2006-12           9
*/

drop table #asset,#mon