如何在表中获取时间序列数据的值总和

时间:2019-06-24 04:29:08

标签: mysql database

我在多个表中都有sql数据。每个表都是一个时间戳和值数据表。我希望在不同时间戳的表之间获取值的总和。

不确定前进的方向

这是数据表创建的sql代码

--
-- Server version   5.6.28-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `table_name`
--

DROP TABLE IF EXISTS `table_name`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `table_name` (
  `timestamp` datetime NOT NULL,
  `avg` float NOT NULL,
  `min` float NOT NULL,
  `max` float NOT NULL,
  `tag_name` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `table_name`
--

LOCK TABLES `table_name` WRITE;
/*!40000 ALTER TABLE `table_name` DISABLE KEYS */;
INSERT INTO `table_name` VALUES ('2019-06-01 10:43:53',10.5,8.2,12.5,'tag_1'),('2019-06-04 10:44:29',15.8,12.7,18,'tag_1'),('2019-06-08 10:45:23',9.4,6,11.5,'tag_1'),('2019-06-12 10:45:49',11,10,14,'tag_1'),('2019-06-24 10:46:15',24,17,25,'tag_1'),('2019-06-28 10:46:43',10.5,22.2,27.6,'tag_1'),('2019-06-01 10:43:53',105,82,125,'tag_2'),('2019-06-04 10:44:29',158,127,180,'tag_2'),('2019-06-08 10:45:23',94,60,115,'tag_2'),('2019-06-12 10:45:49',120,100,140,'tag_2'),('2019-06-24 10:46:15',240,170,250,'tag_2'),('2019-06-28 10:46:43',249,222,276,'tag_2');
/*!40000 ALTER TABLE `table_name` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

例如,目标将在时间戳'2019-06-01 10:43:53'处获取tag_1 + tag_2(总和)的平均值。

另一个问题。如何处理其他算术运算。例如如果我希望获取(tag_1-tag_2)或(tag_1 * 1000-tag_2)的平均值?

1 个答案:

答案 0 :(得分:1)

您在列timestamp上声明了UNIQUE KEY,但两次提供了值'2019-06-01 10:43:53'来插入表中。如果我认为此UNIQUE KEY设置不是故意的,并且该列可能包含重复值,则可以通过在列timestamp上应用GROUP BY来如下计算avg,min和max列的平均值。

注意:适用于MySQL和MSSQL。

SELECT timestamp,
round(avg(avg),2) avg_avg,
round(avg(min),2) avg_min,
round(avg(max),2) avg_max 
FROM test_sum_table
GROUP BY timestamp
ORDER BY 1   

要进行进一步的算术运算,可以按以下方式自定义查询。我在其中添加了2个示例算术运算,但是您可以根据需要添加其他任何运算符。

SELECT 
timestamp,
Tag_1_Avg-Tag_2_Avg,
Tag_1_Avg*1000-Tag_2_Avg
FROM
(
    SELECT timestamp,
    ROUND(SUM(CASE WHEN tag_name = 'tag_1' THEN avg ELSE 0 END),2) Tag_1_Avg,
    ROUND(SUM(CASE WHEN tag_name = 'tag_2' THEN avg ELSE 0 END),2) Tag_2_Avg,
    ROUND(SUM(CASE WHEN tag_name = 'tag_1' THEN min ELSE 0 END),2) Tag_1_min,
    ROUND(SUM(CASE WHEN tag_name = 'tag_2' THEN min ELSE 0 END),2) Tag_2_min,
    ROUND(SUM(CASE WHEN tag_name = 'tag_1' THEN max ELSE 0 END),2) Tag_1_max,
    ROUND(SUM(CASE WHEN tag_name = 'tag_2' THEN max ELSE 0 END),2) Tag_2_max
    FROM test_sum_table
    group by timestamp
)A