MySQL - 根据一个表的 MAX(聚合值)从 2 个表中检索一行
我有 2 个具有相同结构的表,其中每 5 分钟捕获 3 行数据。由此,我尝试使用 T1 的最大聚合值每天检索一行。例如,
T1
日期时间 |名称 |值
2011-06-05 00:00:00 |约翰尼 | 20
2011-06-05 00:00:00 |罗伯特| 30
2011-06-05 00:00:00 |劳伦| 24
2011-06-05 00:00:05 |约翰尼 | 12
2011-06-05 00:00:05 |罗伯特| 10
2011-06-05 00:00:05 |劳伦| 10
...
2011-06-06 00:00:00 |等
T2
日期时间 |名称 |值
2011-06-05 00:00:00 |约翰尼 | 8
2011-06-05 00:00:00 |罗伯特| 12
2011-06-05 00:00:00 |劳伦| 24
2011-06-05 00:00:05 |约翰尼 | 52
2011-06-05 00:00:05 |罗伯特| 16
2011-06-05 00:00:05 |劳伦| 25
...
2011-06-06 00:00:00 | 在上面的示例
中,T1 的前 3 行聚合后的值 (20+30+24) 高于第二组 3 行 (12+10+10)。一旦我确定了哪个时间戳具有 T1 中的最高 (MAX) 聚合值,我想从 T2 中获取相对聚合值。在本例中,它将是 00:00:00 处的值,即 8+12+24。请注意,这将跨越几天,因此所需的输出将是:
dateTime | T1 聚合值 | T2聚合值
2011-06-05 00:00:00 | 74 | 74 44
2011-06-06 xTime | y | z
2011-06-07 xTime | y | z
我尝试使用 SELECT * FROM T1 WHERE value=(SELECT MAX(value) FROM T1) 作为起点,但这只是为我提供了 T1 的单个最大值。我真的很迷茫,不知道如何进步?
I have 2 tables with the same structure in which 3 rows of data are captured every 5 minutes. From this, I'm trying to retrieve a single row per day using the maximum aggregated value of T1. For example,
T1
dateTime | name | value
2011-06-05 00:00:00 | Johnny | 20
2011-06-05 00:00:00 | Robert | 30
2011-06-05 00:00:00 | Lauren | 24
2011-06-05 00:00:05 | Johnny | 12
2011-06-05 00:00:05 | Robert | 10
2011-06-05 00:00:05 | Lauren | 10
...
2011-06-06 00:00:00 | etc
T2
dateTime | name | value
2011-06-05 00:00:00 | Johnny | 8
2011-06-05 00:00:00 | Robert | 12
2011-06-05 00:00:00 | Lauren | 24
2011-06-05 00:00:05 | Johnny | 52
2011-06-05 00:00:05 | Robert | 16
2011-06-05 00:00:05 | Lauren | 25
...
2011-06-06 00:00:00 | etc
In the above example, T1's first 3 rows when aggregated have a higher value (20+30+24) than the second set of 3 rows (12+10+10). Once I have established which timestamp has the highest (MAX) aggregated value from T1, I want to grab the relative aggregated value from T2. In this case it would be the values at 00:00:00 which is 8+12+24. Note that this would span several days so the desired output would be:
dateTime | T1AggregatedValues | T2AggregatedValues
2011-06-05 00:00:00 | 74 | 44
2011-06-06 xTime | y | z
2011-06-07 xTime | y | z
I've tried using SELECT * FROM T1 WHERE value=(SELECT MAX(value) FROM T1)
as a starting point but this just gives me a single max value for T1. I'm really at a loss as to how to progress?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将为您提供所有日期时间组,首先列出最高的聚合。根据您的逻辑,您可能必须处理多个日期时间组具有相同聚合值的可能性,而不是假设第一个结果是最高的。
如果您绝对确定您只会获得 1 个最高聚合组(不可能与最高聚合并列),那么您可以这样做:
无论您使用什么方法来隔离您声明为具有最高聚合的一组,一旦您识别出该组,您就可以获取该组的日期时间并从
T2
中检索SUM(value)
。This will give you all the dateTime group(s), listing the highest aggregate(s) first. Depending on your logic you'll probably have to handle the possibility of more than one dateTime group having the same aggregate value instead of assuming that the 1st result is the highest.
If you knew absolutely for sure that you'd only get 1 highest aggregate group (no possibility of a tie for highest aggregate) then you could do this:
Whatever method you use to isolate the one group that you declare as having the highest aggregate, once you identify that group you then take the dateTime of said group and retrieve
SUM(value)
fromT2
for it.