简体   繁体   中英

Modify mysql query so that it provides sum of column

Want to get a single result which shows the sum of uptimeweightage column using below-mentioned mysql query.

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; 

The output of the above query looks like:

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

Want to see the single result as shown below on which i would perform some more arithmetic operations.

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

Two tables are in action here: one is weightage and other is uptime.

data in weightage table looks like:

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

data in uptime table looks like:

+--------+----------------------------+---------------------------------+-------------------------+-------------+----------------------+--------+---------------------+
| 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: time is always flexible which is based on epoch time and is provided by an external entity.

You can simply do a Sum() on overall result-set, instead of using 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM