可以一起使用SUM - CASE - IF吗?

时间:2014-01-07 11:22:14

标签: mysql

当我尝试使用它时:

SUM(CASE WHEN IF(SUM(CASE WHEN "b.count_students_status" = 1 THEN 1 ELSE 0 END) >= 1, 1, 0) = 1 THEN 1 ELSE 0 END)

它说查询错误:组功能的使用不当

以下是完整的代码:

   SELECT 
  "a.batch" AS Batch,
  SUM(
    CASE
      WHEN IF(
        SUM(
          CASE
            WHEN "b.count_students_status" = 1 
            THEN 1 
            ELSE 0 
          END
        ) >= 1,
        1,
        0
      ) = 1 
      THEN 1 
      ELSE 0 
    END
  ) AS Payments_Not_Received 
FROM
  "DBU - Complete" 
WHERE "a.suspended" = 'no' 
GROUP BY "a.batch" 

如果有多次出现,我想将状态转换为1,然后将总出现次数相加。

请帮忙 - 我正在使用ZOHO构建查询?

1 个答案:

答案 0 :(得分:0)

您尝试在运行之前引用GROUP BY的结果。尝试这样的事情:

select t.BATCH,
sum(case when t.pnr >=1 then 1 else 0 end) as Payments_Not_Received
from
(
select 
a.batch as BATCH,
SUM(CASE WHEN "b.count_students_status" = 1 THEN 1 
         ELSE 0 
    END) as pnr
from yourTable a
WHERE a.suspended = 'no'
group by a.batch
) t
group by t.BATCH;