2

我想使用Case When条件AND,它没有正确计算总和。

例如:

SELECT DATE(`SubmitDate`), 
     SUM(CASE status WHEN 'New' AND `Type` = 'consumer' THEN 1 ELSE 0 END) as new_consumer,
     SUM(CASE status WHEN 'New' AND `Type` = 'business' THEN 1 ELSE 0 END) as new_business
FROM report
WHERE `source` = 'net'
group by DATE(`SubmitDate`) Order by `SubmitDate` DESC
4

2 回答 2

6

您需要使用CASE WHEN [Condition] THEN...而不是简单的 case 表达式:

SELECT DATE(`SubmitDate`), 
     SUM(CASE WHEN status = 'New' AND `Type` = 'consumer' THEN 1 ELSE 0 END) as new_consumer,
     SUM(CASE WHEN status = 'New' AND `Type` = 'business' THEN 1 ELSE 0 END) as new_business
FROM report
WHERE `source` = 'net'
group by DATE(`SubmitDate`) Order by `SubmitDate` DESC
于 2012-06-15T12:46:09.813 回答
3

你应该写

CASE  WHEN status='New' AND `Type` = 'consumer' THEN 1 ELSE 0 END

检查CASE WHEN的语法

于 2012-06-15T12:46:17.270 回答