按日期分组的多个计数

时间:2015-03-08 20:45:12

标签: mysql sql count

我有一张表:

ConnectDate ErrorCode  
2015-01-01  NULL  
2015-01-01  A  
2015-01-01  A  
2015-01-01  B  
2015-01-02  B  

MySQL 上,如何按天计算每个ErrorCode的数量:

Date        ErrorA  ErrorB  
2015-01-01  2       1  
2015-01-02  0       1

1 个答案:

答案 0 :(得分:2)

您将使用条件聚合:

select connectdate, sum(errorcode = 'A') as ErrorA, sum(errorcode = 'B') as ErrorB
from table t
group by connectdate;