--按楼主的要求,与 XF/ZF 表无关啊
declare @s varchar(8000)
set @s=''
select @s=@s+',['+code+']=sum(case code when '''+code+''' then xf_je else 0 end)'
from account group by code
select @s=@s+',['+zf_code+']=sum(case zf_code when '''+zf_code+''' then zf_je else 0 end)'
from account group by zf_codeexec('select id'+@s+' from account group by id')

解决方案 »

  1.   

    --忘了过滤掉不合格的数据declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+code+']=sum(case code when '''+code+''' then xf_je else 0 end)'
    from account where isnull(code,'')<>'' group by code
    select @s=@s+',['+zf_code+']=sum(case zf_code when '''+zf_code+''' then zf_je else 0 end)'
    from account where isnull(zf_code,'')<>'' group by zf_code
    exec('select id'+@s+' from account group by id')
      

  2.   

    谢谢高手,谢谢zjcxc(邹建) ,我试试先
      

  3.   

    请问邹建大哥,假如我要没有数据的也一起显示,也就是
    ABCDE,RMB,XYK,ZP,GZ,KD都要显示,该怎么写效果如下
     id       A     B   C   D   E         RMB   ZP XYK  GZ KD
      1       250   0   50   0  0         250    0  0   0  0
      2       0     0   0  100  0         0    100  0   0  0                               谢谢
      

  4.   

    --那就改数据来源就行了declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+code+']=sum(case code when '''+code+''' then xf_je else 0 end)'
    from XF
    select @s=@s+',['+code+']=sum(case zf_code when '''+code+''' then zf_je else 0 end)'
    from ZF
    exec('select id'+@s+' from account group by id')
      

  5.   

    to:zjcxc(邹建) 大哥,我按您的写法,不行啊,结果如下。请再帮忙!谢谢1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0