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
这种情况该用 什么方法替换,提示“不能对包含聚合或子查询的表达式执行聚合函数。”
------解决思路----------------------
变为不用case的子查询
------解决思路----------------------
类似于:select sum() from (select case from ) as aaa group by这样,将case放在子查询中,外面sum。
------解决思路----------------------
--是否可用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