--按楼主的要求,与 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')
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')
解决方案 »
- 一个sql问题,是否能实现
- BOM结构展开
- 循环合并一条记录的字符串,ITEM表中有100个列,如何将这一百个列内的字符串合并成一个字符串,中间的","隔开.
- 一个很有难度的关联问题
- 小问题,关于表间关系
- 附加数据库时报错了,大家都看看啊
- 关于sql多数据库的问题
- 当数据库MDF文件已经损坏,但LDF文件是完好的情况下,是否可以恢复数据(已经有一个完整备份)
- sql server发生错误,17883,装了sp4,还是不行.
- VFP编译成可执行文件EXE后,运行时为什么一闪后就没了?(在线等待结果,急,谢谢)
- 又一道面试题(月薪5000-6000):举例说明inner join和子查询什么时候可通用,什么时候又不能代替对方。
- 紧急求解一个排序问题
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')
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 谢谢
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')
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