1. ホーム
  2. mysql

[解決済み] ケース ANDの場合は?

2022-03-09 19:39:41

質問

を使いたいのですが Case WhenAND という条件があり、合計が正しく計算されない。

例えば

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

解決方法は?

を使用する必要があります。 CASE WHEN [Condition] THEN... は、単純な大文字小文字の表現ではありません。

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