sql查询补全数据
table1 (classid int,pricetime datetime,price money)insert into table1 select 1,'2012-03-01',22insert into table1 select 1,'2012-03-03',20insert into table1 select 1,'2012-03-04',20insert into table1 select 1,'2012-04-06',23insert into table1 select 1,'2012-04-07',32select top 6 year(datetime)years,datepart(mm,datetime) months,sum(money)from table1 group by year(datetime),datepart(mm,datetime) order by years desc,months desc
gocreate table table1(classid int,pricetime datetime,price money)goinsert into table1 select 1,'2012-03-01',22insert into table1 select 1,'2012-03-03',20insert into table1 select 1,'2012-03-04',20insert into table1 select 1,'2012-04-06',23insert into table1 select 1,'2012-04-07',32select b.years,b.months,ISNULL(a.price,0) pricefrom(select top 6 year(pricetime)years, datepart(mm,pricetime) months, sum(price) as pricefrom table1 group by year(pricetime),datepart(mm,pricetime)order by years desc,months desc)aright join(select distinctdatepart(yy,dateadd(MM,-number,MAX(pricetime))) as years,datepart(mm,dateadd(MM,-number,MAX(pricetime))) as monthsfrom table1 cross join master..spt_valueswhere number between -1 and 4 --and type='P'group by number)b on a.years=b.years and a.months=b.monthsorder by1,2/*years months price2011 12 0.002012 1 0.002012 2 0.002012 3 62.002012 4 55.002012 5 0.00*/更改了一下