gpt4 book ai didi

mysql - 如何跨表获取时间序列数据的值总和

转载 作者:行者123 更新时间:2023-11-29 09:40:38 24 4
gpt4 key购买 nike

我有跨多个表的 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) 的平均值?

最佳答案

您在 timestamp 列上声明了 UNIQUE KEY,但提供了两次值“2019-06-01 10:43:53”以插入表中。如果我猜测这个 UNIQUE KEY 设置是无意的,并且该列可以保存重复值,我们可以通过在 timestamp 列上应用 GROUP BY 来计算列 avg、min 和 max 的平均值,如下所示。 p>

注意:适用于 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

关于mysql - 如何跨表获取时间序列数据的值总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56729858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com