http://expert.csdn.net/Expert/topic/2440/2440306.xml?temp=.6941645
 [交流]行列转换 

解决方案 »

  1.   

    create table table1 
    (年 char(4),项目 char(6),平均值 decimal(5,2),最大值 decimal(5,2),最小值 decimal(5,2),超标率 decimal(5,2))insert into table1 select '2001','项目1',1.5,2.1,1.1,1.4
    union select '2001', '项目2',2.0,5.0,1.5,1.2
    union select '2001', '项目3',2.0,5.0,1.5,1.2
    union select '2001', '项目4',2.0,5.0,1.5,1.2
    union select '2001', '项目2',2.0,5.0,1.5,1.2
    union select '2002', '项目1',2.0,5.0,1.5,1.2
    union select '2002', '项目3',2.0,5.0,1.5,1.2declare @sql varchar(8000)
    set @sql = 'select 年'
    select @sql = @sql + ' ,sum(case 项目 when '''+项目+''' then 平均值 end) ['+项目+']'
     from (select distinct 项目 from table1) as a
    select @sql = @sql+' from table1 group by 年'
    exec(@sql)所影响的行数为 6 行)年    项目1                   项目2                项目3                项目4                                      
    ---- -------------------------------------------------------------------- -------------
    2001 1.50                     2.00                 2.00                  2.00
    2002 2.00                     NULL                 2.00                 NULL
      

  2.   

    谢谢大家的回复!
    1ssp(新来的) ,只有平均值是不行的,要什么值都在一个表中显示!
      

  3.   

    --搞定了--处理
    declare @s varchar(8000),@s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)--处理项目
    set @s=''
    select @s=@s+',['+项目+']=max(case 项目 when '''''+项目+''''' then 值 end)'
    from(select distinct 项目,id=cast(right(项目,len(项目)-2) as int) from 表
    ) a order by id--处理字段(指标)
    select @s1='',@s2='',@s3=''
    select @s1=@s1+',@'+id+' varchar(8000)'
    ,@s2=@s2+'
    set @'+id+'=''select 年,项目,id='+id+
    ',内容='''''+name+''''',值='+name+' from 表'''
    ,@s3=@s3+'+'' union all ''+@'+id
    from(
    select name,id=cast(colid as varchar),colid
    from syscolumns where object_id('表')=id
    and name not in('年','项目')
    ) a order by colidselect @s1=substring(@s1,2,8000),@s3=substring(@s3,16,8000)
    exec('declare '+@s1+'
    '+@s2+'
    exec(''select 年,内容'+@s+' from(''+'
    +@s3+'+'') a group by 年,id,内容 order by 年,id'')')
    go
      

  4.   

    --下面是测试--创建测试数据
    create table 表(年 int,项目 varchar(10)
    ,平均值 decimal(20,1)
    ,最大值 decimal(20,1)
    ,最小值 decimal(20,1)
    ,超标率 decimal(20,1)
    )
    insert into 表
    select 2001,'项目1',1.5,2.1,1.1,0.1
    union all select 2001,'项目2',2.0,5.0,1.5,5.5
    union all select 2003,'项目10',2.1,5.2,2.5,5.5
    go--处理
    declare @s varchar(8000),@s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)--处理项目
    set @s=''
    select @s=@s+',['+项目+']=max(case 项目 when '''''+项目+''''' then 值 end)'
    from(select distinct 项目,id=cast(right(项目,len(项目)-2) as int) from 表
    ) a order by id--处理字段(指标)
    select @s1='',@s2='',@s3=''
    select @s1=@s1+',@'+id+' varchar(8000)'
    ,@s2=@s2+'
    set @'+id+'=''select 年,项目,id='+id+
    ',内容='''''+name+''''',值='+name+' from 表'''
    ,@s3=@s3+'+'' union all ''+@'+id
    from(
    select name,id=cast(colid as varchar),colid
    from syscolumns where object_id('表')=id
    and name not in('年','项目')
    ) a order by colidselect @s1=substring(@s1,2,8000),@s3=substring(@s3,16,8000)
    exec('declare '+@s1+'
    '+@s2+'
    exec(''select 年,内容'+@s+' from(''+'
    +@s3+'+'') a group by 年,id,内容 order by 年,id'')')
    go--删除测试数据
    drop table 表/*--测试结果年           内容     项目1                    项目2                    项目10                   
    ----------- ------ ---------------------- ---------------------- ---------------------- 
    2001        平均值    1.5                    2.0                    NULL
    2001        最大值    2.1                    5.0                    NULL
    2001        最小值    1.1                    1.5                    NULL
    2001        超标率    .1                     5.5                    NULL
    2003        平均值    NULL                   NULL                   2.1
    2003        最大值    NULL                   NULL                   5.2
    2003        最小值    NULL                   NULL                   2.5
    2003        超标率    NULL                   NULL                   5.5(所影响的行数为 8 行)
    --*/
      

  5.   

    几点说明:
    1.我的处理方法是通用的,无论你的指标字段有多少都可以自动处理
    2.如果你的表中,除: [年],[项目]为非指标字段外,还有其他的非指标字段,就要修改下面这句:from(
    select name,id=cast(colid as varchar),colid
    from syscolumns where object_id('表')=id
    and name not in('年','项目')  --非指标字段列表,将其他非指标字段也加入这个列表
    ) a order by colid