我的PostgreSQL 9.5数据库中有两个表,即tbl1
和tbl2
。 tbl1包含数据(没有时区的时间戳),如:
Start_time
1996-06-07 00:00:00
1997-02-03 00:00:00
2000-07-09 00:00:00
和tbl2
包含一些像这样的值:
ID. veh_1995 veh_2000
1 200 425
2 400 478
3 150 300
4 700 800
5 900 1500
以下查询为'tbl2:
插入缺失年份的值(年份列之间的差距) SELECT
veh_1995,
(veh_1995 + (veh_2000 - veh_1995) * 0.2)::int AS veh_1996,
(veh_1995 + (veh_2000 - veh_1995) * 0.4)::int AS veh_1997,
(veh_1995 + (veh_2000 - veh_1995) * 0.6)::int AS veh_1998,
(veh_1995 + (veh_2000 - veh_1995) * 0.8)::int AS veh_1999,
veh_2000
from tbl2
我需要修改以上查询:
start time
中的每个tbl1
行,查询应检查tbl2
start_time
预期产出:
Year value
1996 ...
1997 ...
2000 ...
如果有人可以帮我修改上述查询以获得我想要的输出,我会很感激吗?
答案 0 :(得分:1)
这或多或少是你想要的吗?
with yrs as (
select
t2.*,
start_time,
generate_series(cast(extract(year from start_time) as integer), 2000) as interp_yr
from
tbl2 as t2,
tbl1 as t1
where
extract(year from start_time) between 1995 and 2000
)
select
iy.*,
veh_1995 + (veh_2000 - veh_1995) * (interp_yr - 1995)/5 as interp_val
from
yrs as iy;