Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id CB7C09FB4AA for ; Tue, 24 Apr 2007 18:00:03 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 83239-03-3 for ; Tue, 24 Apr 2007 17:59:58 -0300 (ADT) X-Greylist: delayed 00:17:50.543651 by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 254EC9FB5EB for ; Tue, 24 Apr 2007 17:59:55 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from insiderscore.com (mail01.insiderscore.com [69.84.139.233]) by svr4.postgresql.org (Postfix) with ESMTP id D739C5AF88E for ; Tue, 24 Apr 2007 20:42:04 +0000 (GMT) Received: from [192.168.1.111] (mail01.insiderscore.com [69.84.139.233]) by insiderscore.com (Postfix) with ESMTP id 0AABD9B07E8 for ; Tue, 24 Apr 2007 16:42:03 -0400 (EDT) Subject: plpgsql array looping From: Richard Albright To: pgsql-sql@postgresql.org Content-Type: text/plain Date: Tue, 24 Apr 2007 16:42:01 -0400 Message-Id: <1177447321.17092.9.camel@MSI1036> Mime-Version: 1.0 X-Mailer: Evolution 2.10.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200704/235 X-Sequence-Number: 28235 I am attempting to create a moving average elements function that will return a set of elements in a particular moving average and am having difficulty iterating thrrough the passed in array properly. Any help would be greatly appreciated. code below... select getmovavgelements( aggarray(trade_date), aggarray(close_price), '2004-02-10'::timestamp, 10 ) from ( select trade_date, close_price::numeric from quotedata where symbol='MSFT' and trade_date > '2004-01-01' order by trade_date asc) values; NOTICE: idx: {"2004-01-02 00:00:00","2004-01-05 00:00:00",...} NOTICE: vals: {27.45,28.14,...} NOTICE: maxdate: 2004-02-10 00:00:00 NOTICE: dims: [1:821] NOTICE: idx ptr: 2 NOTICE: idx ptr: 4 NOTICE: idx ptr: 6 NOTICE: idx ptr: 8 NOTICE: idx ptr: 10 NOTICE: idx ptr: 12 NOTICE: idx ptr: 14 NOTICE: idx ptr: 16 NOTICE: idx ptr: 18 NOTICE: idx ptr: 20 NOTICE: idx ptr: 22 NOTICE: idx ptr: 24 NOTICE: idx ptr: 26 NOTICE: row: 2004-01-02 00:00:00 27.45 ERROR: array value must start with "{" or dimension information CONTEXT: PL/pgSQL function "getmovavgelements" line 25 at assignment --- CREATE OR REPLACE FUNCTION getmovavgelements(p_idxarray _timestamp, p_valarray _numeric, p_idx "timestamp", p_periods int8) RETURNS setof resultset AS $BODY$ declare idxptr int8; idxendptr int8; v_rec record; v_rtn resultset%rowtype; v_sql text; begin raise notice 'idx: %', p_idxarray; raise notice 'vals: %', p_valarray; idxptr := array_lower(p_idxarray, 1); raise notice 'maxdate: %', p_idx; raise notice 'dims: % ', array_dims(p_idxarray); for idxptr in 1 .. array_upper(p_idxarray, 1) loop exit when p_idxarray[idxptr] >= p_idx; idxptr := idxptr +1; raise notice 'idx ptr: %', idxptr; end loop; idxendptr := idxptr + p_periods; for v_rec in select s.ser, p_idxarray[s.ser] as index, p_valarray[s.ser] as value from generate_series(idxptr, idxendptr) as s(ser) loop raise notice 'row: % %', v_rec.index, v_rec.value; v_rtn := (v_rec.index, v_rec.value); return next v_rtn; end loop; end $BODY$ LANGUAGE 'plpgsql' VOLATILE; -- Rick Albright Senior Quantitative Analyst Indie Research, LLC 254 Witherspoon Street Princeton, NJ 08542 (609)497-1030 ralbright@insiderscore.com