我在 MySQL 中有一张表,如下所示。
State marketId
CA Ind
CO Ind
CA Grp
CA Grp
我想选择数据作为计数和计数总数(应如下所示)。
State marketId Count totalCountInd totalCountGrp
CA Ind 1 2 2
CO Ind 1
CA Grp 2
目前我正在使用以下查询。那没有给出正确的输出。
select state, marketId, count(*) as 'count', (select count(1) from BatchReport where marketId='Ind') as 'total' from BatchReport group by state,marketId having marketId='Ind'
union
select state, marketId, count(*) as 'count1', (select count(1) from BatchReport where marketId='Grp') as 'total1' from BatchReport group by state,marketId having marketId='Grp'
请给我建议。