我有一个视图,我试图合并两组以不同方式存储的时间序列数据。集合D具有与每个时间点一起存储的单个值。集合R的值仅以生效日期存储,并且在被取代之前一直有效。这是一个示例表结构:
Table D
---------+------------------+--------
D_series | D_time | D_value
---------+------------------+--------
1 | 2012-01-01 00:00 | 4.52
1 | 2012-01-01 01:00 | 2.41
1 | 2012-01-01 02:00 | 5.98
1 | 2012-01-01 03:00 | 3.51
2 | 2012-01-01 00:00 | 4.54
2 | 2012-01-01 01:00 | 6.41
2 | 2012-01-01 02:00 | 5.28
2 | 2012-01-01 03:00 | 3.11
3 | 2012-01-01 00:00 | 4.22
3 | 2012-01-01 01:00 | 9.41
3 | 2012-01-01 02:00 | 3.98
3 | 2012-01-01 03:00 | 3.53
Table L
---------+---------
D_series | R_series
---------+---------
1 | 1
2 | 1
3 | 2
Table RV
---------+----------+--------
R_series | R_header | R_value
---------+----------+--------
1 | 1 | 5.23
1 | 2 | 2.98
2 | 1 | 1.35
Table RH
---------+-----------------
R_header | R_start
---------+-----------------
1 | 2012-01-01 00:00
2 | 2012-01-01 01:49
3 | 2012-01-01 02:10
我希望视图将所有点D_time
连同它们的对应点D_value
和R_value
当前点一起返回:
---------+------------------+---------+--------
D_series | D_time | D_value | R_value
---------+------------------+---------+--------
1 | 2012-01-01 00:00 | 4.52 | 5.23
1 | 2012-01-01 01:00 | 2.41 | 5.23
1 | 2012-01-01 02:00 | 5.98 | 2.98
1 | 2012-01-01 03:00 | 3.51 | 2.98
2 | 2012-01-01 00:00 | 4.54 | 5.23
2 | 2012-01-01 01:00 | 6.41 | 5.23
2 | 2012-01-01 02:00 | 5.28 | 2.98
2 | 2012-01-01 03:00 | 3.11 | 2.98
3 | 2012-01-01 00:00 | 4.22 | 1.35
3 | 2012-01-01 01:00 | 9.41 | 1.35
3 | 2012-01-01 02:00 | 3.98 | 1.35
3 | 2012-01-01 03:00 | 3.53 | 1.35
我知道如果我创建一个子查询并加入它,我可以做到这一点:
select D.D_series, D_time, D_value, RV1.R_value
from D
join L on L.D_series = D.D_series
join RV RV1 on RV1.R_series = L.R_series
join RH RH1 on RH1.R_header = RV1.R_header and RH1.R_start <= D.D_time
left join (
select R_series, R_value, R_start
from RV RV2
join RH RH2 on RH2.R_header = RV2.R_header
) RZ on RZ.R_series = RV1.R_series and RZ.R_start > RH1.R_start
where RZ.R_start is null or RZ.R_start > D_time
但据我了解,这个子查询将首先获取每条记录RV
,RH
即使视图只涉及几个R_series
. 有什么办法可以消除这个子查询并使其成为标准连接?