sql—修改mysql查询,使其提供列的和

zbwhf8kr  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(340)

希望使用下面提到的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:时间总是灵活的,它是以历元时间为基础,由外部实体提供的。

8yparm6h

8yparm6h1#

你可以简单地做一个 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)

相关问题