我使用的表格示例如下:
id_user | process_number | date
---------------------------------------
01 | 1 | 2012-05-04
01 | 1 | 2012-05-04
02 | 0 | 2012-05-04
01 | 1 | 2012-05-05
01 | 2 | 2012-05-05
01 | 0 | 2012-05-05
02 | 0 | 2012-05-05
03 | 1 | 2012-05-05
04 | 2 | 2012-05-05
05 | 1 | 2012-05-05
05 | 1 | 2012-05-05
06 | 0 | 2012-05-05
07 | 3 | 2012-05-05
我想要得到的结果是唯一进程数(按 id_user 分组)、总进程数(按日期分组)和当 process_number 不等于“0”时计数不同用户(按日期和按用户分组)。我试图通过查询来实现这一点:
SELECT COUNT( DISTINCT id_user ) AS user_process, COUNT( * ) AS total_process, DATE( date) AS date_process, SUM( IF( process_number = '0', 0, 1 ) ) notification FROM a_proces GROUP BY DATE( date ) ORDER BY DATE( date ) DESC LIMIT 0 , 10
此查询添加给定日期内的所有用户,这些用户在通知列中有多个大于零的 process_number。此示例中的正确结果应如下所示:
date_process | user_process | total_process | notification
------------------------------------------------------------------------------
2012-05-04 | 2 | 3 | 1
2012-05-05 | 7 | 10 | 6
谢谢你的帮助。