gpt4 book ai didi

mysql - 获取另一个mysql的列和值的平均值

转载 作者:行者123 更新时间:2023-11-29 13:32:39 25 4
gpt4 key购买 nike

我不确定我使用的标题是否正确,但让我解释一下发生了什么。我有一张汇率表,每天更新。这是表格:

DateCreated |  Bank  |  Buy
2013-10-09 | tc | 2.40
2013-10-09 | bank1 | 2.45
2013-10-09 | bank2 | 2.46
2013-10-09 | bank3 | 2.45

所以我需要的是与“tc”银行的平均比较,如下所示:

Date        | tc    |  banks
2013-10-09 | 2.40 | 2.453 <- this is the average of all three banks for that date

希望这是有道理的,这是我尝试过的:

SELECT e.datecreated, e.buy, (SELECT AVG(buy) FROM exchange WHERE idbank <>  'tc'
AND datecreated = e.datecreated) AS banks
FROM exchange e
WHERE idbank = 'tc'
ORDER BY e.datecreated ASC

但是这是转储重复项,并且看起来像是错误的数据..帮助?

最佳答案

这是获取指定结果的一种方法:

SELECT e.datecreated
, MAX(IF(e.idbank='tc',e.buy,NULL)) AS tc
, AVG(IF(e.idbank='tc',NULL,e.buy)) AS banks
FROM exchange e
GROUP BY e.datecreated

还有许多其他查询将返回等效结果。

关于mysql - 获取另一个mysql的列和值的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19192348/

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