mysql - SQL Find the average of 3 day closest
问题描述
I have an SQL structure like this:
Create Table Transactions (
Id integer primary key not null auto_increment,
ResourceId varchar(255),
Price Integer,
TransactionTime date
);
I would like to get the time (TransactionTime) along with the average of 3 days price. For example, the 3 day average of the 22nd will be the average of the 20th, 21st, and 22nd. Thanks so much.
解决方案
大概,您需要每一行和给定资源的这些信息。如果是这样:
select t.*,
avg(price) over (partition by resourceid
order by transactiontime
range between interval 2 day preceding and current row
) as avg_3
from transactions t;
推荐阅读
- javascript - 要求测试:计算器功能 javascript
- apache - 使用 composer 安装 Apache=pack 安装失败
- javascript - 事件委托问题
- c# - 如何在 ASP.NET Core 中使用客户端下载文件并做出反应?
- php - 过滤正则表达式中的波斯语单词
- python - 如何在 python 3.7 fedora 中安装 tkinter?
- html - 引导照片库的意外行为
- csv - 如何在没有表名的情况下获取 hive 表的 csv 输出作为列的一部分
- excel - Excel公式包含无法识别的文本
- ruby - 在 Watir 中检测到 DOM 变化