分不多,求优化

ahhfql 2015-01-13 04:40:20
select 年月,部门,姓名,编号,
sum(case when PayId<>'21' and 年月='201401' then (select sfgz from b a where right(年月,2)='01' And PayId<>'21' and 编号=b.编号) end ) '一月',
sum(case when PayId<>'21' and 年月='201402' then (select sfgz from b a where right(年月,2)='02' And PayId<>'21' and 编号=b.编号) end ) '二月',
sum(case when PayId<>'21' and 年月='201403' then (select sfgz from b a where right(年月,2)='03' And PayId<>'21' and 编号=b.编号) end ) '三月',
sum(case when PayId<>'21' and 年月='201404' then (select sfgz from b a where right(年月,2)='04' And PayId<>'21' and 编号=b.编号) end ) '四月',
sum(case when PayId<>'21' and 年月='201405' then (select sfgz from b a where right(年月,2)='05' And PayId<>'21' and 编号=b.编号) end ) '五月',
sum(case when PayId<>'21' and 年月='201406' then (select sfgz from b a where right(年月,2)='06' And PayId<>'21' and 编号=b.编号) end ) '六月',
sum(case when PayId<>'21' and 年月='201407' then (select sfgz from b a where right(年月,2)='07' And PayId<>'21' and 编号=b.编号) end ) '七月',
sum(case when PayId<>'21' and 年月='201408' then (select sfgz from b a where right(年月,2)='08' And PayId<>'21' and 编号=b.编号) end ) '八月',
sum(case when PayId<>'21' and 年月='201409' then (select sfgz from b a where right(年月,2)='09' And PayId<>'21' and 编号=b.编号) end ) '九月',
sum(case when PayId<>'21' and 年月='201410' then (select sfgz from b a where right(年月,2)='10' And PayId<>'21' and 编号=b.编号) end ) '十月',
sum(case when PayId<>'21' and 年月='201411' then (select sfgz from b a where right(年月,2)='11' And PayId<>'21' and 编号=b.编号) end ) '十一月',
sum(case when PayId<>'21' and 年月='201412' then (select sfgz from b a where right(年月,2)='12' And PayId<>'21' and 编号=b.编号) end ) '十二月'

from b,c
where c.id=b.id
and left(年月,4)='2014'
and 编号='18'




消息 130,级别 15,状态 1,第 3 行
不能对包含聚合或子查询的表达式执行聚合函数。
消息 130,级别 15,状态 1,第 4 行
不能对包含聚合或子查询的表达式执行聚合函数。
消息 130,级别 15,状态 1,第 5 行
不能对包含聚合或子查询的表达式执行聚合函数。
消息 130,级别 15,状态 1,第 6 行
...全文
90 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2015-01-13
  • 打赏
  • 举报
回复
坦率的说,没太看明白,试试:

select 年月,部门,姓名,编号,
    sum(case when 年月='201401' then sfgz else 0 end ) as '一月',
    sum(case when 年月='201402' then sfgz else 0 end ) as '二月',
    sum(case when 年月='201403' then sfgz else 0 end ) as '三月',
    sum(case when 年月='201404' then sfgz else 0 end ) as '四月',
    sum(case when 年月='201405' then sfgz else 0 end ) as '五月',
    sum(case when 年月='201406' then sfgz else 0 end ) as '六月',
    sum(case when 年月='201407' then sfgz else 0 end ) as '七月',
    sum(case when 年月='201408' then sfgz else 0 end ) as '八月',
    sum(case when 年月='201409' then sfgz else 0 end ) as '九月',
    sum(case when 年月='201410' then sfgz else 0 end ) as '十月',
    sum(case when 年月='201411' then sfgz else 0 end ) as '十一月',
    sum(case when 年月='201412' then sfgz else 0 end ) as '十二月'
from 
    b,c
where 
    b.编号=c.编号
    and
    c.id=b.id
    and 
    left(年月,4)='2014'
    and 
    b.编号='18'
    and
    PayId<>'21'
group by
    年月,部门,姓名,编号

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧