别看我说这么多其实就一个意思即如何把表  ttest  (N行M列)
弄成  表  tnew   (M行N列)
?????

解决方案 »

  1.   

    create table t (id int identity,name varchar(10),code varchar(10))
    go
    insert t values('人口','rk')
    insert t values('经济','jj')
    insert t values('文化','wh')
    insert t values('土地','td')select * from t
    declare @sql varchar(1000)
    set @sql = ''
    select @sql = @sql+name+'=max(case when name='''+name+''' then code else null end),' from t
    --print @sql 
    set @sql = left(@sql,len(@sql) - 1)
    set @sql = 'select '+@sql+' from t'
    exec (@sql)
    drop table t/*
    id          name       code       
    ----------- ---------- ---------- 
    1           人口         rk
    2           经济         jj
    3           文化         wh
    4           土地         td(所影响的行数为 4 行)人口         经济         文化         土地         
    ---------- ---------- ---------- ---------- 
    rk         jj         wh         td*/
      

  2.   

    create table ttest(pname varchar(20),rstate varchar(20),psum int)
    insert into ttest select '单品种','通过',16
    insert into ttest select '季度'  ,'否决',7
    insert into ttest select '季度'  ,'通过',105
    insert into ttest select '合同'  ,'否决',6
    insert into ttest select '合同'  ,'通过',119
    insert into ttest select '全国'  ,'通过',2
    declare @s varchar(8000)
    set @s='select isnull(rstate,''合计'')'
    select @s=@s+',['+pname+']=sum(case pname when '''+pname+''' then psum else 0 end)'
    from ttest group by pname
    set @s=@s+',合计=sum(psum) from ttest group by rstate with rollup'
    exec(@s)/*
           单品种      合同        季度        全国        合计          
    ------ ----------- ----------- ----------- ----------- ----------- 
    否决   0           6           7           0           13
    通过   16          119         105         2           242
    合计   16          125         112         2           255
    */drop table ttest
      

  3.   

    create table t (pname varchar(10),rstate varchar(8),psum int)
    go
    insert into t select '单品种','通过',16 union all
    select '季度','否决',7 union all
    select '季度','通过',105 union all
    select '合同','否决',6 union all
    select '合同','通过',119 union all
    select '全国','通过',2
    declare @sql varchar(8000)
    declare @sql1 varchar(8000)
    declare @sql2 varchar(8000)
    set @sql = ''
    set @sql1 = ''
    set @sql2 = ''
    select @sql = @sql+','+pname+'=(select isnull(sum(psum),0) from t where pname = '''+pname+''' and rstate=''通过'' )'
    from (select distinct pname from t) c
    select @sql1 = @sql1+','+pname+'=(select isnull(sum(psum),0) from t where pname = '''+pname+''' and rstate=''否决'' )'
    from (select distinct pname from t) c
    select @sql2 = @sql2+','+pname+'=(select isnull(sum(psum),0) from t where pname = '''+pname+''')'
    from (select distinct pname from t) cset @sql = right(@sql,len(@sql) -1)
    set @sql1 = right(@sql1,len(@sql1) -1)
    set @sql2 = right(@sql2,len(@sql2) -1)exec('select distinct ''通过'','+@sql+' from t union all select distinct ''否决'','
    +@sql1+' from t union all select distinct ''合计'','+@sql2+'from t')drop table t
    /*     单品种         合同          季度          全国          
    ---- ----------- ----------- ----------- ----------- 
    通过   16          119         105         2
    否决   0           6           7           0
    合计   16          125         112         2
    */
      

  4.   

    红尘GG你终于来啦难道这种行列之间的转换都必须要知道具体的列才能进行吗?N行M列的可以有个通用的范例没有呀?
      

  5.   


    create table ttest(pname varchar(10),rstate varchar(10),psum int)
    insert ttest
    select '单品种','通过',16 union all
    select '季度','否决',7 union all
    select '季度','通过',105 union all
    select '合同','否决',6 union all
    select '合同','通过',119 union all
    select '全国','通过',2 declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',['+pname+']=isnull(sum(case when pname='''+pname+''' then psum end),0)' from ttest group by pname
    print @sql
    set @sql='select rstate=case when grouping(rstate)=1 then ''合计'' else rstate end '+@sql+',合计=sum(psum) from ttest group by rstate  with rollup order by grouping(rstate),rstate desc'
    exec(@sql)
    drop table ttest
    /*
    rstate     单品种         合同          季度          全国          合计          
    ---------- ----------- ----------- ----------- ----------- ----------- 
    通过         16          119         105         2           242
    否决         0           6           7           0           13
    合计         16          125         112         2           255
    */
      

  6.   

    难道没有一种象线性代数里的那种公式直接转换行列的吗?比如将这个a1  a2  a3   a4  ...
    b1  b2  b3   b4  ...
    c1  c2  c3   c4  ...
    ..  ..  ..   ..  ...代入公式  f(n,m)  一计算就成这样a1  b1  c1 ..
    a2  b2  c2 ..
    a3  b3  c3 ..
    a4  b4  c4 ..
    ..  ..  .. ..如果能这样  多好呀??
      

  7.   

    红尘GG和幽谷GG的都好使谢谢我就想问问我上面的问题:难道没有一种象线性代数里的那种公式直接转换行列的吗?比如将这个a1  a2  a3   a4  ...
    b1  b2  b3   b4  ...
    c1  c2  c3   c4  ...
    ..  ..  ..   ..  ...代入公式  f(n,m)  一计算就成这样a1  b1  c1 ..
    a2  b2  c2 ..
    a3  b3  c3 ..
    a4  b4  c4 ..
    ..  ..  .. ..如果能这样  多好呀??
      

  8.   

    SQL Server 2005里面好象已经支持行列转换处理了,但SQL Server 2000里还没有这个功能。