Note that there are some explanatory texts on larger screens.

plurals
  1. POOracle: Find previous record for a ranked list of forecasts
    primarykey
    data
    text
    <p>Hi I am faced with a difficult problem:</p> <p>I have a table (oracle 9i) of weather forecasts (many 100's of millions of records in size.) whose makeup looks like this:</p> <pre><code>stationid forecastdate forecastinterval forecastcreated forecastvalue --------------------------------------------------------------------------------- varchar (pk) datetime (pk) integer (pk) datetime (pk) integer </code></pre> <p>where:</p> <ul> <li><code>stationid</code> refers to one of the many weather stations that may create a forecast;</li> <li><code>forecastdate</code> refers to the date the forecast is for (date only not time.)</li> <li><code>forecastinterval</code> refers to the hour in the <code>forecastdate</code> for the forecast (0 - 23).</li> <li><code>forecastcreated</code> refers to the time the forecast was made, can be many days beforehand.</li> <li><code>forecastvalue</code> refers to the actual value of the forecast (as the name implies.)</li> </ul> <p>I need to determine for a given <code>stationid</code> and a given <code>forecastdate</code> and <code>forecastinterval</code> pair, the records where a <code>forecastvalue</code> increments more than a nominal number (say 500). I'll show a table of the condition here:</p> <pre><code>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 </code></pre> <p>In the above scenario, I'd like to pull out the third record. This is the record where the forecast value increased by a nominal (say 100) amount.</p> <p>The task is proving to be very difficult due to the sheer size of the table (many 100s of millions of records.) and taking so long to finish (so long in fact that my query has never returned.)</p> <p>Here is my attempt so far to grab these values:</p> <pre><code>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 &gt; ( 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload