我在 MySQL 中有一张表,如下所示。
State marketId
CA Ind
CO Ind
CA Grp
CA Grp
我想选择数据作为计数和计数总数(应如下所示)。
State marketId Count totalCountInd
CA Ind 1 2
CO Ind 1
目前我正在使用以下查询。那没有给出正确的输出。
select state,marketId,count(*),sum(CASE marketID WHEN 'ind' THEN 1 ELSE 0 END) AS totalCountInd from BatchReport where marketId='ind' group by state,marketId;
+-------+----------+----------+---------------+
| state | marketId | count(*) | totalCountInd |
+-------+----------+----------+---------------+
| CA | in | 1 | |
| CO | in | 1 | |
|+-------+----------+----------+---------------+