名称              fs1      fs2      fs3      fs4       fs5     hj 
绩效考核员 0 0 0 1 0
网络管理员 0 1 0 0 0
新产品开发员 0 0 0 0 2
网络管理员 4 0 0 0 0
新产品开发员 4 4 4 0 0

解决方案 »

  1.   

    说明一下 fs1,fs2,fs3,fs4,fs5........ fsn 也就是说项目不固定
      

  2.   

    declare @t table(名称 varchar(20),fs1 int,fs2 int,fs3 int,fs4 int,fs5 int)
    insert into @t select '绩效考核员',0,0 ,0 ,1 ,0
    union all select '网络管理员' ,0 ,1 ,0 ,0 ,0
    union all select '新产品开发员' ,0 ,0 ,0 ,0 ,2
    union all select '网络管理员' ,4 ,0 ,0 ,0 ,0
    union all select '新产品开发员' ,4 ,4 ,4 ,0 ,0select 名称,fs1,fs2,fs3,fs4,fs5,(fs1+fs2+fs3+fs4+fs5) as hj from @t  group by 名称,fs1,fs2,fs3,fs4,fs5
      

  3.   

    fs1,fs2,fs3,fs4,fs5........ fsn 也就是说项目不固定
      

  4.   

    create table tb(名称 varchar(20),fs1 int,fs2 int,fs3 int,fs4 int,fs5 int)
    insert into tb select '绩效考核员',0,0 ,0 ,1 ,0
    union all select '网络管理员' ,0 ,1 ,0 ,0 ,0
    union all select '新产品开发员' ,0 ,0 ,0 ,0 ,2
    union all select '网络管理员' ,4 ,0 ,0 ,0 ,0
    union all select '新产品开发员' ,4 ,4 ,4 ,0 ,0declare @sql varchar(1000)
    declare @sql1 varchar(1000)
    set @sql=''
    select @sql=@sql+','+name from syscolumns where id=object_id('tb') and name<>'名称' order by colid
    set @sql1=stuff(@sql,1,1,'')
    select @sql=stuff(@sql,1,1,'')+',sum('+stuff(replace(@sql,',','+'),1,1,'')+') as hj'
    set @sql='select '+@sql+' from tb group by '+@sql1
    exec(@sql)drop table tb
      

  5.   

    --刚才写的繁琐了,简化一下
    create table tb(名称 varchar(20),fs1 int,fs2 int,fs3 int,fs4 int,fs5 int)
    insert into tb select '绩效考核员',0,0 ,0 ,1 ,0
    union all select '网络管理员' ,0 ,1 ,0 ,0 ,0
    union all select '新产品开发员' ,0 ,0 ,0 ,0 ,2
    union all select '网络管理员' ,4 ,0 ,0 ,0 ,0
    union all select '新产品开发员' ,4 ,4 ,4 ,0 ,0declare @sql varchar(1000)
    set @sql=''
    select @sql=@sql+','+name from syscolumns where id=object_id('tb') and name<>'名称' order by colid
    select @sql='select '+stuff(@sql,1,1,'')+',sum('+stuff(replace(@sql,',','+'),1,1,'')+') as hj from tb group by '+stuff(@sql,1,1,'')
    exec(@sql)drop table tb
      

  6.   

    create table tb(名称 varchar(20),fs1 int,fs2 int,fs3 int,fs4 int,fs5 int)
    insert into tb select '绩效考核员',0,0 ,0 ,1 ,0
    union all select '网络管理员' ,0 ,1 ,0 ,0 ,0
    union all select '新产品开发员' ,0 ,0 ,0 ,0 ,2
    union all select '网络管理员' ,4 ,0 ,0 ,0 ,0
    union all select '新产品开发员' ,4 ,4 ,4 ,0 ,0declare @sql varchar(1000)
    set @sql=''
    select @sql=@sql+','+name from syscolumns where id=object_id('tb') and name<>'名称' order by colid
    select @sql='select 名称,'+stuff(@sql,1,1,'')+',sum('+stuff(replace(@sql,',','+'),1,1,'')+') as hj from tb group by 名称,'+stuff(@sql,1,1,'')
    exec(@sql)drop table tb
      

  7.   

    请问shaka 报将数据类型 varchar 转换为 numeric 时出错。是什么原因
      

  8.   

    数据库结构
             被评价岗位 varchar 50 1
    spjfs           varchar 50 1
    合计分数           numeric 17 1
    数据记录被评价岗位 spjfs 合计分数
    网络管理员 创新性 4
    新产品开发员 创新性 4
    网络管理员 工作负荷 1
    新产品开发员 工作负荷 4
    新产品开发员 管理决策 4
    绩效考核员 计算机和外语水平 1
    新产品开发员 自主权利 2
    我给你的是经过行转列后的,然后求每行的合计数
      

  9.   

    我说的是行转列后的合计
    名称              fs1      fs2      fs3      fs4       fs5  ....fsn    hj 
    绩效考核员 0 0 0 1 0
    网络管理员 0 1 0 0 0
    新产品开发员 0 0 0 0 2
    网络管理员 4 0 0 0 0
    新产品开发员 4 4 4 0 0
      

  10.   

    你在exec(@sql)上面加一句print @sql,看看生成的语句是什么样子?
      

  11.   

    create table tb(名称 varchar(20),fs1 int,fs2 int,fs3 int,fs4 int,fs5 int,hj int)
    insert into tb select '绩效考核员',0,0 ,0 ,1 ,0,null
    union all select '网络管理员' ,0 ,1 ,0 ,0 ,0,null
    union all select '新产品开发员' ,0 ,0 ,0 ,0 ,2,null
    union all select '网络管理员' ,4 ,0 ,0 ,0 ,0,null
    union all select '新产品开发员' ,4 ,4 ,4 ,0 ,0,nulldeclare @sql varchar(1000)
    set @sql=''
    select @sql=@sql+','+name from syscolumns where id=object_id('tb') and name not in('名称','hj') order by colid
    select @sql='select 名称,'+stuff(@sql,1,1,'')+',sum('+stuff(replace(@sql,',','+'),1,1,'')+') as hj from tb group by 名称,'+stuff(@sql,1,1,'')
    exec(@sql)drop table tb
    --不用sum的字段写在not in的后面