嗨,我面临一个难题:
我有一张天气预报表(oracle 9i)(数百万条记录),其组成如下:
stationid forecastdate forecastinterval forecastcreated forecastvalue
---------------------------------------------------------------------------------
varchar (pk) datetime (pk) integer (pk) datetime (pk) integer
在哪里:
stationid
指可以创建预报的众多气象站之一;forecastdate
指预测的日期(仅限日期而不是时间。)forecastinterval
指forecastdate
预测中的小时 (0 - 23)。forecastcreated
指做出预测的时间,可以提前很多天。forecastvalue
指预测的实际值(顾名思义)。
我需要确定给定stationid
和给定forecastdate
和forecastinterval
对的记录,其中 a 的forecastvalue
增量超过名义数字(例如 500)。我将在此处显示条件表:
stationid forecastdate forecastinterval forecastcreated forecastvalue
---------------------------------------------------------------------------------
'stationa' 13-dec-09 10 10-dec-09 04:50:10 0
'stationa' 13-dec-09 10 10-dec-09 17:06:13 0
'stationa' 13-dec-09 10 12-dec-09 05:20:50 300
'stationa' 13-dec-09 10 13-dec-09 09:20:50 300
在上述情况下,我想提取第三条记录。这是预测值增加了名义(比如 100)数量的记录。
由于表的庞大规模(数以百万计的记录),并且需要很长时间才能完成(事实上时间很长,以至于我的查询从未返回),因此这项任务被证明是非常困难的。
到目前为止,这是我获取这些值的尝试:
select
wtr.stationid,
wtr.forecastcreated,
wtr.forecastvalue,
(wtr.forecastdate + wtr.forecastinterval / 24) fcst_date
from
(select inner.*
rank() over (partition by stationid,
(inner.forecastdate + inner.forecastinterval),
inner.forecastcreated
order by stationid,
(inner.forecastdate + inner.forecastinterval) asc,
inner.forecastcreated asc
) rk
from weathertable inner) wtr
where
wtr.forecastvalue - 100 > (
select lastvalue
from (select y.*,
rank() over (partition by stationid,
(forecastdate + forecastinterval),
forecastcreated
order by stationid,
(forecastdate + forecastinterval) asc,
forecastcreated asc) rk
from weathertable y
) z
where z.stationid = wtr.stationid
and z.forecastdate = wtr.forecastdate
and (z.forecastinterval =
wtr.forecastinterval)
/* here is where i try to get the 'previous' forecast value.*/
and wtr.rk = z.rk + 1)