我正在搜索一个简单的查询,该查询可以将较低时间范围的股票数据更改为较高时间范围。考虑下表
Open High Low Close Volume
Date
1999-01-04 10:22:00 1.1801 1.1819 1.1801 1.1817 4
1999-01-04 10:23:00 1.1817 1.1818 1.1804 1.1814 18
1999-01-04 10:24:00 1.1817 1.1817 1.1802 1.1806 12
1999-01-04 10:25:00 1.1807 1.1815 1.1795 1.1808 26
1999-01-04 10:26:00 1.1803 1.1806 1.1790 1.1806 4
1999-01-04 10:27:00 1.1801 1.1801 1.1779 1.1786 23
1999-01-04 10:28:00 1.1795 1.1801 1.1776 1.1788 28
1999-01-04 10:29:00 1.1793 1.1795 1.1782 1.1789 10
1999-01-04 10:31:00 1.1780 1.1792 1.1776 1.1792 12
1999-01-04 10:32:00 1.1788 1.1792 1.1788 1.1791 4
它每1分钟更新一次。如何通过查询将其更改为2分钟数据?
最佳答案
您想对事物进行分组:
select floor(time/2) as time,
sum(volume) as volume,
max(case when time = 2*floor(time/2) then open end) as open,
max(high) as high,
max(low) as low),
max(case when time = 2*floor(time/2)+1 then close end) as close
from t
group by floor(time/2)
使用
row_number()
可以使打开和关闭的公式更简单,更通用,但是您需要指定数据库。关于sql - 查询库存数据时间范围的变化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14204260/