Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

selecting last value for a given date

SolerousFeb 18 2009 — edited Feb 27 2009
I've read some of the other threads on the this tope, but can't find a resolution.

My query is supposed to get the last values for the given two days (start date and end date) and I return the difference between the two values. The query as it is only works for days where there is an entry for 12:00 pm. On some days, it will therefore return nothing. But I'm not sure how to get it to choose the last value for the given day and still remain a fast query.

Here is a sample of the query with dates that return a result.
SELECT max_query.sgnl_id as sgnl, max_query.max_val - min_query.min_val result_val
   FROM
  (SELECT t1.sgnl_id, t2.smpl_dte, t1.pv_id,
    MAX(t2.integ_since_val) AS max_val
     FROM diag.blm_acct t2, diag.pv_sgnl_id_assc t1
    WHERE t2.smpl_dte = TO_DATE ('11/15/2008 12:00:00 PM', 'MM-DD-YYYY HH:MI:SS AM')
    AND t1.pv_id = t2.pv_id
  AND t2.pv_id        IN
    (SELECT pv_id
       FROM diag.pv_sgnl_id_assc
      WHERE sgnl_id IN
      (SELECT sgnl_id
         FROM diag.series_sgnl_rec_asgn
        WHERE series_id = 'Series5'
      )
    )
 GROUP BY t1.sgnl_id, t2.smpl_dte, t1.pv_id
  ) max_query  ,
  (SELECT pv_id,
    MAX(integ_since_val) AS min_val
     FROM diag.blm_acct
    WHERE smpl_dte = TO_DATE ('11/10/2008 12:00:00 PM', 'MM-DD-YYYY HH:MI:SS AM')
  AND pv_id        IN
    (SELECT pv_id
       FROM diag.pv_sgnl_id_assc
      WHERE sgnl_id IN
      (SELECT sgnl_id
         FROM diag.series_sgnl_rec_asgn
        WHERE series_id = 'Series5'
      )
    )
 GROUP BY pv_id
  ) min_query WHERE max_query.pv_id  = min_query.pv_id
   ORDER BY max_query.sgnl_id
The data set in the table is quite large, but the above query returns only 49 results (one for each signal in 'Series5').

I tried the following statement by itself to get the last value, but it takes quite a long time to execute (increased the overall query time from 1 sec to 400+ sec when used in the query above):
SELECT DISTINCT trunc(smpl_dte) as the_day, 
         first_value(integ_since_val) over (partition by trunc(smpl_dte) order by smpl_dte desc) as last_val_per_day
  FROM diag.blm_acct WHERE pv_id = 3 and trunc(smpl_dte) = to_date('05/18/2008', 'MM-DD-YYYY ')
Any thoughts?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2009
Added on Feb 18 2009
17 comments
778 views