MySQL - Sum和Count

时间:2013-12-05 09:37:14

标签: mysql

我想计算每个服务的平均值(百分比值)除以开始日期和停止日期之间特定服务的所有服务器,还必须忽略值“nil”

start_date stop_date conn ftp http imap pop3 smtp ssh

2013-11-28 2013-12-05 100  100 99.89 99.89 99.89 100 100

表格

id   timestamp            server               service  start_date       stop_date        percentage 

 1  2013-12-05 11:46:57  a.martin.sg     conn     2013-11-28  2013-12-05  100        
 2  2013-12-05 11:46:57  a.martin.sg     ftp      2013-11-28   2013-12-05  100        
 3  2013-12-05 11:46:57  a.martin.sg     http     2013-11-28   2013-12-05  100        
 4  2013-12-05 11:46:57  a.martin.sg     imap     2013-11-28  2013-12-05  nil        
 5  2013-12-05 11:46:57  a.martin.sg     pop3     2013-11-28   2013-12-05  100        
 6  2013-12-05 11:46:57  a.martin.sg     smtp     2013-11-28   2013-12-05  100        
 7  2013-12-05 11:46:57  a.martin.sg     ssh      2013-11-28   2013-12-05  100        
 8  2013-12-05 11:46:57  b.martin.sg   conn     2013-11-28  2013-12-05  100        
 9  2013-12-05 11:46:57  b.martin.sg   ftp      2013-11-28  2013-12-05  100        
10  2013-12-05 11:46:57  b.martin.sg   http     2013-11-28  2013-12-05  99.95      
11  2013-12-05 11:46:57  b.martin.sg   imap     2013-11-28  2013-12-05  nil        
12  2013-12-05 11:46:57  b.martin.sg   pop3     2013-11-28  2013-12-05  99.47      
13  2013-12-05 11:46:57  b.martin.sg   smtp     2013-11-28  2013-12-05  99.19      
14  2013-12-05 11:46:57  b.martin.sg   ssh      2013-11-28  2013-12-05  100        
15  2013-12-05 11:46:57  c.martin.sg   conn     2013-11-28  2013-12-05  100        
16  2013-12-05 11:46:57  c.martin.sg   ftp      2013-11-28  2013-12-05  100        
17  2013-12-05 11:46:57  c.martin.sg   http     2013-11-28  2013-12-05  100        
18  2013-12-05 11:46:57  c.martin.sg   imap     2013-11-28  2013-12-05  nil        
19  2013-12-05 11:46:57  c.martin.sg   pop3     2013-11-28  2013-12-05  100        
20  2013-12-05 11:46:57  c.martin.sg   smtp     2013-11-28  2013-12-05  100        
21  2013-12-05 11:46:57  c.martin.sg   ssh      2013-11-28  2013-12-05  100        
22  2013-12-05 11:46:57  d.martin.sg     conn     2013-11-28  2013-12-05  100        
23  2013-12-05 11:46:57  d.martin.sg     ftp      2013-11-28  2013-12-05  100        
24  2013-12-05 11:46:57  d.martin.sg     http     2013-11-28  2013-12-05  99.89        
25  2013-12-05 11:46:57  d.martin.sg     imap     2013-11-28  2013-12-05  nil        
26  2013-12-05 11:46:57  d.martin.sg     pop3     2013-11-28  2013-12-05  99.89      
27  2013-12-05 11:46:57  d.martin.sg     smtp     2013-11-28  2013-12-05  99.78      
28  2013-12-05 11:46:57  d.martin.sg     ssh      2013-11-28  2013-12-05  100 

目前的尝试:

SELECT   service,
         SUM(IF(service = 'http', percentage, 0)) AS 'http',
         SUM(percentage) AS Total
FROM     virtualunix
WHERE    start = '2013-11-28'
GROUP BY service;
+---------+---------+---------+
| service | http    | Total   |
+---------+---------+---------+
| conn    |       0 | 3392.39 |
| ftp     |       0 | 2699.68 |
| http    | 3399.34 | 3399.34 |
| imap    |       0 |     900 |
| pop3    |       0 | 2897.09 |
| smtp    |       0 | 2897.01 |
| ssh     |       0 | 3399.95 |
+---------+---------+---------+

1 个答案:

答案 0 :(得分:1)

除非我误解了,你不是只想使用AVG()代替SUM()吗?

SELECT   service, AVG(percentage)
FROM     virtualunix
WHERE    start_date = '2013-11-28'
GROUP BY service