有两个时间序列覆盖大致相同的区间
prices (date date, price double)
和 rates (date date, rate double)
。第二个具有更高的分辨率和更多的条目。我如何加入他们,从 prices
中选择 rates
中每行较早的最新行?方言并不重要,但 PostgreSQL 更好
因此假设您的数据看起来有点像这样:
with prices as (
select * from (values (1, '2024-01-01'::timestamp, 10.0), (2, '2024-01-02'::timestamp, 10.1), (3, '2024-01-03'::timestamp, 10.2)) as t (id, date, price)
),
rates as (
select * from (values
(0, '2023-12-31 12:00:00'::timestamp, 99.0),
(1, '2024-01-01 12:00:00'::timestamp, 11.0),
(2, '2024-01-01 12:00:00'::timestamp, 11.0),
(3, '2024-01-01 13:00:00'::timestamp, 11.5),
(4, '2024-01-01 14:00:00'::timestamp, 11.1),
(5, '2024-01-02 12:00:00'::timestamp, 11.0),
(6, '2024-01-02 13:00:00'::timestamp, 11.5),
(7, '2024-01-02 14:00:00'::timestamp, 11.1),
(8, '2024-01-03 12:00:00'::timestamp, 11.0),
(9, '2024-01-03 13:00:00'::timestamp, 11.5),
(10, '2024-01-03 14:00:00'::timestamp, 11.1)
) as t (id, date, rate)
)
您可以使用子选择来获取每个费率的价格:
select
rates.date,
rates.rate,
(select price from prices where prices.date < rates.date order by date desc limit 1)
from rates
按日期订购并选择第一个以获得正确的。我假设
<
是你想要的,因为你已经“早些时候”写过。但这还取决于您拥有的色谱柱类型和分辨率。也许您需要 date_trunc
才能找到正确的价格。
您也可以使用此加入:
select
rates.date,
rates.rate,
prices.price
from rates
left join prices on date_trunc('day', (rates.date - interval '1' day)) = date_trunc('day', prices.date)
这里我使用了左连接,因为并非每个费率都有关联的价格。但这取决于您的数据。