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
这种情况该用 什么方法替换,提示“不能对包含聚合或子查询的表达式执行聚合函数。”
...全文
197 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
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的子查询

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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