修改mysql查询,使其提供列总和

时间:2018-10-28 05:34:18

标签: mysql sql

想使用下面提到的mysql查询得到一个显示uptimeweightage列总和的结果。

SELECT uptime.metricname,
       Sum(downtimesincelastbat) * weightage AS uptimeweightage
FROM   uptime
       INNER JOIN weightage
               ON weightage.metricname = uptime.metricname
WHERE  uptime.environment = "xxxxxx1"
       AND uptime.metricname NOT REGEXP "xxxxx"
       AND uptime.metricname NOT REGEXP "xxxxxx"
       AND uptime.metricname NOT REGEXP "xxxxxxx"
       AND eventtime >= From_unixtime(1540616606)
       AND eventtime <= From_unixtime(1540703006)
GROUP  BY uptime.metricname; 

以上查询的输出如下:

+---------------------+-----------------+
| metricname        | uptimeweightage |
+-------------------+-----------------+
| A                 |           84.00 |
| B                 |           36.00 |
| C                 |          505.20 |
| D                 |            6.00 |
| E                 |            6.00 |
+-------------------+-----------------+

想要看到如下所示的单个结果,我将在该结果上执行更多的算术运算。

+-----------------+
| TOTAL weightage |
+-----------------+
|          637.20 |
+-----------------+

这里有两个表:一个是权重,另一个是正常运行时间。

体重表中的数据如下:

+----+---------------------------------+-----------+
| id | metricname                      | weightage |
+----+---------------------------------+-----------+
|  1 | A                               |      0.30 |
|  2 | B                               |      0.30 |
|  3 | C                               |      0.20 |
|  4 | D                               |      0.10 |
|  5 | E                               |      0.10 |
+----+---------------------------------+-----------+

正常运行时间表中的数据如下:

+--------+----------------------------+---------------------------------+-------------------------+-------------+----------------------+--------+---------------------+
| id | eventtime | metricname | environment | jobduration | downtimesincelastbat | status | inserttime |
+--------+----------------------------+---------------------------------+-------------------------+-------------+----------------------+--------+---------------------+
| 1 | 2018-10-28 18:54:45.445 | A | xxxxxx1 | 37 | 0 | 1 | 2018-10-28 18:54:46 |
| 2 | 2018-10-28 18:54:44.087 | B | xxxxxx2 | 23 | 0 | 1 | 2018-10-28 18:54:45 |
| 3 | 2018-10-28 18:54:44.087 | C | xxxxxx1 | 23 | 0 | 1 | 2018-10-28 18:54:44 |
| 4 | 2018-10-28 18:54:42.428 | D | xxxxxx3 | 21 | 0 | 1 | 2018-10-28 18:54:43 |
| 5 | 2018-10-28 18:54:43.061 | E | xxxxxx2 | 24 | 0 | 1 | 2018-10-28 18:54:43 |
+--------+----------------------------+---------------------------------+-------------------------+-------------+----------------------+--------+---------------------+ 

PS:时间总是灵活的,它基于纪元时间,由外部实体提供。

1 个答案:

答案 0 :(得分:0)

您可以简单地对整个结果集执行Sum(),而不是使用Group By

SELECT 
  Sum(downtimesincelastbat * weightage) AS total_weightage 
FROM uptime 
INNER JOIN weightage ON weightage.metricname = uptime.metricname 
WHERE uptime.environment = "xxxxxx1" AND 
      uptime.metricname NOT REGEXP "xxxxx" AND 
      uptime.metricname NOT REGEXP "xxxxxx" AND 
      uptime.metricname NOT REGEXP "xxxxxxx" AND 
      eventtime >= From_unixtime(1540616606) AND 
      eventtime <= From_unixtime(1540703006)