你需要的日期格式調整到您的語言環境,但我得到這個在SQL 2000(美國日期格式)的工作:
create table Table1 (id varchar(3), period varchar(10), fromdate datetime, todate datetime, value varchar(2))
go
insert into Table1 values ('001','01/2012','1/2/2012','1/10/2012','AB')
insert into Table1 values ('002','01/2012','1/5/2012','1/18/2012','AL')
insert into Table1 values ('003','02/2012','2/10/2012','2/12/2012', 'DA')
go
select id
, period
, [1]=case when DATEADD(dd,0,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [2]=case when DATEADD(dd,1,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [3]=case when DATEADD(dd,2,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [4]=case when DATEADD(dd,3,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [5]=case when DATEADD(dd,4,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [6]=case when DATEADD(dd,5,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [7]=case when DATEADD(dd,6,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [8]=case when DATEADD(dd,7,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [9]=case when DATEADD(dd,8,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [10]=case when DATEADD(dd,9,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [11]=case when DATEADD(dd,10,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [12]=case when DATEADD(dd,11,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [13]=case when DATEADD(dd,12,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [14]=case when DATEADD(dd,13,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [15]=case when DATEADD(dd,14,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [16]=case when DATEADD(dd,15,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [17]=case when DATEADD(dd,16,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [18]=case when DATEADD(dd,17,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [19]=case when DATEADD(dd,18,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [20]=case when DATEADD(dd,19,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [21]=case when DATEADD(dd,20,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [22]=case when DATEADD(dd,21,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [23]=case when DATEADD(dd,22,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [24]=case when DATEADD(dd,23,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [25]=case when DATEADD(dd,24,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [26]=case when DATEADD(dd,25,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [27]=case when DATEADD(dd,26,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [28]=case when DATEADD(dd,27,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [29]=case when DATEADD(dd,28,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [30]=case when DATEADD(dd,29,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, [31]=case when DATEADD(dd,30,left(period,2)+'/01/'+right(period,4)) between fromdate and todate then value else 'N' end
, total=DATEDIFF(dd,left(period,2)+'/01/'+right(period,4),DATEADD(m,1,left(period,2)+'/01/'+right(period,4)))
from Table1
go
結果:
支點和遞歸CTE的不SQL 2000兼容:( – 2012-02-19 14:51:34