Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
修改mysql查询,使其提供列的和_Mysql_Sql - Fatal编程技术网

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

修改mysql查询,使其提供列的和,mysql,sql,Mysql,Sql,希望使用下面提到的mysql查询获得一个显示uptimeweightage列总和的结果 SELECT uptime.metricname, Sum(downtimesincelastbat) * weightage AS uptimeweightage FROM uptime INNER JOIN weightage ON weightage.metricname = uptime.metricname WHERE uptime.e

希望使用下面提到的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; 
希望看到如下所示的单个结果,我将对其执行更多的算术运算

+-----------------+
| TOTAL weightage |
+-----------------+
|          637.20 |
+-----------------+
这里有两个表:一个是权重,另一个是正常运行时间

权重表中的数据如下所示:

+---------------------+-----------------+
| metricname        | uptimeweightage |
+-------------------+-----------------+
| A                 |           84.00 |
| B                 |           36.00 |
| C                 |          505.20 |
| D                 |            6.00 |
| E                 |            6.00 |
+-------------------+-----------------+
+----+---------------------------------+-----------+
| 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:时间总是灵活的,它基于历元时间,由外部实体提供

您只需对整个结果集执行一个
Sum()
,而不必使用
分组依据

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)
选择
总和(DowntimesInclastBat*重量)作为总重量
从正常运行时间
weightage.metricname=uptime.metricname上的内部联接权重
其中uptime.environment=“xxxxxx 1”和
uptime.metricname不是REGEXP“xxxxx”和
uptime.metricname不是REGEXP“xxxxxx”和
uptime.metricname不是REGEXP“xxxxxxx”和
eventtime>=来自unixtime(1540616606)和
活动时间