获取按日期和另一个字段分组的字段总数

时间:2012-10-31 05:08:44

标签: mysql sql group-by

我有一个数据表如下,

detectDate      |isp    |infection  | count
--------------------------------------
2012-10-02 01:00|aaaa   |malware    |3
2012-10-02 01:30|bbbb   |malware    |2
2012-10-02 01:33|bbbb   |spy-eye    |2
2012-10-02 01:45|aaaa   |DDos       |1
2012-10-03 01:50|cccc   |malware    |2
2012-10-03 02:00|dddd   |TDSS       |2
2012-10-03 04:50|dddd   |TDSS       |3

我需要像这样按照日期和感染进行分类,

detectDate      |infection  | count
--------------------------------------
2012-10-02      |malware    |5
2012-10-02      |spy-eye    |2
2012-10-02      |DDos       |1
2012-10-03      |malware    |2
2012-10-03      |TDSS       |5

我知道如何使用群组,但不是远远的:(请你帮助我,我需要按日期和感染进行分组,并获得所示的计数字段总数。

3 个答案:

答案 0 :(得分:3)

使用SUM功能汇总count列,并使用DATE_FORMAT功能仅按日期进行分组。

SELECT DATE_FORMAT(detectDate, '%Y-%m-%d') AS detectDate
      ,infection 
      ,SUM(`COUNT`) as `count`
FROM myTable
GROUP BY DATE_FORMAT(detectDate, '%Y-%m-%d'), infection

See this SQLFiddle

答案 1 :(得分:1)

您应该使用DATE mySql函数从日期/时间列中提取日期部分:

select DATE(detectDate),infection, sum(count) 
from t group by DATE(detectDate),infection
order by  DATE(detectDate);

答案 2 :(得分:-1)

select detectDate, infection, sum(count) as count
from table_name
group by detectDate, infection