sum中的case 语句包含子查询的改如何处理?

CCC的 2015-10-19 09:34:51
select op_ymd,M.depart_id, D.depart_name,
Sum(Case when MealType=0 and Kind=1 and clock_id in (select clock_id from clocks where dinroom_id='1') then 1 else 0 end) OneCount,
Sum(Case when MealType=0 and Kind=1 and clock_id in (select clock_id from clocks where dinroom_id='2') then 1 else 0 end) OneCount2,
Sum(Case when MealType=0 and Kind=1 and clock_id in (select clock_id from clocks where dinroom_id='3') then 1 else 0 end) OneCount3,

from MealRecords M left join (select * from DinLev where isValid=1) DD on M.kind=DD.DinLev_Id
left join employee E on M.emp_id=e.emp_id left join Departs D on M.Depart_id=D.Depart_id
where sign_time>='2015-10-01' and sign_time<='2015-10-19' group by op_ymd, M.depart_id, D.depart_name
这种情况该用 什么方法替换,提示“不能对包含聚合或子查询的表达式执行聚合函数。”
...全文
128 3 点赞 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xxfvba 2015-10-20
--是否可用count代替 (select count(*) from MealRecords a, clocks b where a. clock_id =b. clock_id and a.MealType=0 and a.Kind=1 and b.dinroom_id='1') as OneCount
  • 打赏
  • 举报
回复
zbdzjx 2015-10-20
类似于:select sum() from (select case from ) as aaa group by这样,将case放在子查询中,外面sum。
  • 打赏
  • 举报
回复
ChinaITOldMan 2015-10-19
变为不用case的子查询
  • 打赏
  • 举报
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2015-10-19 09:34
社区公告
暂无公告