4

我有一个查询,需要对其他 2 列为真的列进行计数。当 'DealerName' like '%MINI%' AND 'DealerContact_Y' = 1 时,我需要计算 'DealerName'

但我不确定语法。此查询产生错误

SELECT DealerName,
       count(DealerShipId) as dealersContacted,
       CASE WHEN
       DealerName LIKE "%MINI%", WHEN DealerContact_Y = 1
       THEN Count(DealerContact_Y) END as Mini_contacted_yes,
       Campaign, 
       DealerId 
       FROM tblsummaryResults

有没有办法在 case 语句中做多个 WHENS?

4

2 回答 2

4

您可以使用以下方式编写它DealerName LIKE '%MINI%' AND DealerContact_Y = 1

SELECT DealerName,
  count(DealerShipId) as dealersContacted,
  count(CASE 
          WHEN DealerName LIKE '%MINI%' 
              AND DealerContact_Y = 1
          THEN DealerContact_Y END) as Mini_contacted_yes,
  Campaign, 
  DealerId 
FROM tblsummaryResults
于 2013-02-07T13:54:54.370 回答
1

所以,像这样计数:

COUNT(
  IF(DealerName LIKE "%MINI%" AND DealerContact_Y = 1, DealerContact_Y, NULL)
) AS Mini_contacted_yes
于 2013-02-07T13:55:27.903 回答