34,593
社区成员
发帖
与我相关
我的任务
分享
create table table_1(order_id smallint,order_year smallint,m1 smallint,m2 smallint,m3 smallint,m4 smallint,m5 smallint,m6 smallint,m7 smallint,m8 smallint,m9 smallint,m10 smallint,m11 smallint,m12 smallint,prd_id char(20))
insert into table_1(order_id,order_year,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,prd_id)
select 1,2008,1,2,3,4,5,6,7,8,9,10,11,12,'prd_1'
union all select 2,2009,11,22,33,44,55,66,0,0,0,0,0,0,'prd_1'
create table table_1(order_id smallint,order_year smallint,m1 smallint,m2 smallint,m3 smallint,m4 smallint,m5 smallint,m6 smallint,m7 smallint,m8 smallint,m9 smallint,m10 smallint,m11 smallint,m12 smallint,prd_id char(20))
insert into table_1(order_id,order_year,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,prd_id)
select 1,2008,1,2,3,4,5,6,7,8,9,10,11,12,'prd_1'
union all select 2,2009,11,22,33,44,55,66,0,0,0,0,0,0,'prd_1'
declare @year int,@month int
select @year=year(getdate()),@month=month(getdate())
--select @year=2008,@month=12
if @month>8
select order_id,order_year,
m1=case when 1 between @month and @month+4 then m1 else 0 end,
m2=case when 2 between @month and @month+4 then m2 else 0 end,
m3=case when 3 between @month and @month+4 then m3 else 0 end,
m4=case when 4 between @month and @month+4 then m4 else 0 end,
m5=case when 5 between @month and @month+4 then m5 else 0 end,
m6=case when 6 between @month and @month+4 then m6 else 0 end,
m7=case when 7 between @month and @month+4 then m7 else 0 end,
m8=case when 8 between @month and @month+4 then m8 else 0 end,
m9=case when 9 between @month and @month+4 then m9 else 0 end,
m10=case when 10 between @month and @month+4 then m10 else 0 end,
m11=case when 11 between @month and @month+4 then m11 else 0 end,
m12=case when 12 between @month and @month+4 then m12 else 0 end
from table_1 where order_year=@year
union
select order_id,order_year,
m1=case when 1 between @month-12 and @month+4-12 then m1 else 0 end,
m2=case when 2 between @month-12 and @month+4-12 then m2 else 0 end,
m3=case when 3 between @month-12 and @month+4-12 then m3 else 0 end,
m4=case when 4 between @month-12 and @month+4-12 then m4 else 0 end,
m5=0,m6=0,m7=0,m8=0,m9=0,m10=0,m11=0,m12=0
from table_1 where order_year=@year+1
else
select order_id,order_year,
m1=case when 1 between @month and @month+4 then m1 else 0 end,
m2=case when 2 between @month and @month+4 then m2 else 0 end,
m3=case when 3 between @month and @month+4 then m3 else 0 end,
m4=case when 4 between @month and @month+4 then m4 else 0 end,
m5=case when 5 between @month and @month+4 then m5 else 0 end,
m6=case when 6 between @month and @month+4 then m6 else 0 end,
m7=case when 7 between @month and @month+4 then m7 else 0 end,
m8=case when 8 between @month and @month+4 then m8 else 0 end,
m9=case when 9 between @month and @month+4 then m9 else 0 end,
m10=case when 10 between @month and @month+4 then m10 else 0 end,
m11=case when 11 between @month and @month+4 then m11 else 0 end,
m12=case when 12 between @month and @month+4 then m12 else 0 end
from table_1 where order_year=@year
if object_id('table_1') is not null drop table table_1
create table table_1(order_id smallint,order_year smallint,m1 smallint,m2 smallint,m3 smallint,m4 smallint,m5 smallint,m6 smallint,m7 smallint,m8 smallint,m9 smallint,m10 smallint,m11 smallint,m12 smallint,prd_id char(20))
insert into table_1(order_id,order_year,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,prd_id)
select 1,2008,1,2,3,4,5,6,7,8,9,10,11,12,'prd_1'
union all select 2,2009,11,22,33,44,55,66,0,0,0,0,0,0,'prd_1'
go
declare @y smallint,@m tinyint,@sql nvarchar(4000)
select @y = 2008, @m = 9
set @sql = 'select order_id=min(order_id),prd_id=min(prd_id)'
select top 6 @sql = @sql+','+quotename(ltrim(y)+'_'+name)+'=max(case order_year when '+ltrim(y)+' then '+name+' else -1 end)' from
(
select y=@y, m=colid-2, name from syscolumns where id = object_id('table_1') and colid between 3 and 14
union all
select y=@y+1,m=colid-2+12, name from syscolumns where id = object_id('table_1') and colid between 3 and 14
) t
where m >=@m
set @sql = @sql + ' from table_1 where order_year in (' + case when @y < 8 then ltrim(@y) else ltrim(@y)+','+ltrim(@y+1) end+')'
exec (@sql)
/*
order_id prd_id 2008_m9 2008_m10 2008_m11 2008_m12 2009_m1 2009_m2
-------- -------------------- ------- -------- -------- -------- ------- -------
1 prd_1 9 10 11 12 11 22
*/
if object_id('table_1') is not null drop table table_1