Advertisement
Albinutte

Uni, DBMS, Script to task 6

Dec 15th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TYPE time_avg_row AS OBJECT (TIME TIMESTAMP, price FLOAT);
  2. /
  3. CREATE TYPE time_avg_table IS TABLE OF time_avg_row;
  4. /
  5. CREATE OR REPLACE FUNCTION moving_average
  6.     RETURN time_avg_table PIPELINED
  7. AS
  8.     val NUMBER := 0;
  9. BEGIN
  10.     SELECT * INTO val FROM (SELECT MEDIAN.price FROM MEDIAN ORDER BY MEDIAN.TIME) WHERE ROWNUM = 1;
  11.     FOR rec IN (SELECT * FROM MEDIAN ORDER BY TIME) LOOP
  12.         val := val + (rec.price - val) * 2 / 11;
  13.         PIPE ROW(time_avg_row(rec.TIME, val));
  14.     END LOOP;
  15.     RETURN;
  16. END;            
  17. /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement