gpt4 book ai didi

sql - Postgres - 如何使用最后几行的 AVG 并将其与另一列相乘?

转载 作者:行者123 更新时间:2023-11-29 12:35:21 27 4
gpt4 key购买 nike

我有下表:

date     | ratio | revenue  
---------|-------|-----------
03-30-18 | 1.2 | 918264
03-31-18 | 0.94 | 981247
04-01-18 | 1.1 | 957353
04-02-18 | 0.99 | 926274
04-03-18 | 1.05 |
04-04-18 | 0.97 |
04-05-18 | 1.23 |

如您所见,04-03-18 及以后尚未发生,因此这些天没有收入输入。但我对 future 的日子有一个比率。我想使用我确实拥有的最近 4 天的 AVG 收入并将其乘以比率来预测 future 的收入。

结果,我希望有下表:

date     | ratio | revenue  
---------|-------|-----------
03-30-18 | 1.2 | 918264
03-31-18 | 0.94 | 981247
04-01-18 | 1.1 | 957353
04-02-18 | 0.99 | 926274
04-03-18 | 1.05 | 993073.73
04-04-18 | 0.97 | 917410.97
04-05-18 | 1.23 | 1163314.94

最佳答案

我认为不需要窗口函数,所以我会这样表述:

select t.date, t.ratio, 
coalesce(t.revenue, a.avg4 * ratio) as revenue
from t cross join
(select avg(revenue) as avg4
from (select t.*
from t
where t.revenue is not null
order by date desc
limit 4
) t
) a
order by date;

关于sql - Postgres - 如何使用最后几行的 AVG 并将其与另一列相乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49619616/

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