你能帮我解决高级SQL查询吗?
我有这样的记录:
id, ProcessName, Date
1, Monitor.exe, 2016-05-01 17:00
2, Monitor.exe, 2016-05-01 17:58
3, Monitor.exe, 2016-06-11 15:43
4, Monitor.exe, 2016-05-10 17:24
5, Monitor2.exe, 2016-06-01 16:01
6, Monitor2.exe, 2016-06-11 19:05
7, Keeper.exe, 2016-06-01 13:02
8, Keeper.exe, 2016-06-14 17:07
9, Keeper.exe, 2016-06-14 11:00
任务是显示按进程名称分组的最近两个月的记录,并按条目计数并按天排序。 像:
date, ProcessName, count(*)
2016-05-01, Monitor.exe, 2
2016-05-10, Monitor.exe, 1
2016-06-01, Keeper.exe, 1
2016-06-01, Monitor2.exe, 1
2016-06-11, Monitor.exe, 1
2016-06-11, Monitor2.exe, 1
2016-06-14, Keeper.exe, 2
解决! 最终查询是:
SELECT DATE_FORMAT(date, '%m-%d') as EventDate, ProcessName, count(*)
FROM table_name
WHERE
date BETWEEN ADDDATE(NOW(), INTERVAL -30 day) AND NOW()
group by EventDate, ProcessName
order by EventDate
谢谢scaisEdge
答案 0 :(得分:2)
您可以使用计数和分组
select date(date), ProcessName, count(*)
from my_table
group by date(date), ProcessName
order by date(date)