Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XuulY-0005xN-Qm for pgsql-sql@arkaria.postgresql.org; Sun, 30 Nov 2014 03:01:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XuulX-0001Ne-Jt for pgsql-sql@arkaria.postgresql.org; Sun, 30 Nov 2014 03:01:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XuulV-0001NV-IW for pgsql-sql@postgresql.org; Sun, 30 Nov 2014 03:01:37 +0000 Received: from [162.253.133.43] (helo=mwork.nabble.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XuulM-0005B7-OT for pgsql-sql@postgresql.org; Sun, 30 Nov 2014 03:01:30 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mwork.nabble.com (Postfix) with ESMTP id 13979BA9BF9 for ; Sat, 29 Nov 2014 19:01:28 -0800 (PST) Date: Sat, 29 Nov 2014 20:01:27 -0700 (MST) From: David G Johnston To: pgsql-sql@postgresql.org Message-ID: <1417316487139-5828676.post@n5.nabble.com> In-Reply-To: References: Subject: Re: regr_slope function with auto creation of X column MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 162.253.133.43 (failed) X-Pg-Spam-Score: 0.5 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Jason Aleksi wrote > --Storing 30 day regression slopes into table (DOES NOT WORK) > INSERT INTO historical_data_regr_slope (department_id, date, > regr_slope30sales) ( > SELECT historical_data.department_id, historical_data.date, > regr_slope(row_number(), historical_data.salesDollarK) OVER > (PARTITION BY historical_data.department_id ORDER BY historical_data.date > DESC ROWS BETWEEN 1 PRECEDING AND 29 FOLLOWING) AS regr_slope30sales > FROM historical_data > GROUP BY historical_data.department_id, historical_data.date, > historical_data.salesDollarK > ORDER BY historical_data.department_id, historical_data.date DESC > ) > > Any suggestions on how to auto-create the regr_slope X column? You shoud be able to use a subquery to first generate the relevant row numbers and then in the outer query apply the regr_slope function. You could also try (theory here - the documentation should be improved in this area) two applications of the OVER clause. regr_expr( row_number() over (...), sales ) over (...) You should probably define the window in the main body and refer to it by name if you attempt this. I honestly have no idea if it will work but the syntax you used before is defined as invalid because nothing can come between the function and the OVER part; which negates the possibility of using a single OVER to cover two functions. I would suggest you not intermix window and group by until you get the window working. Then put that into a cte/with and run the group by separately - you might need to put the group by in the cte and the window in the main query. That said I haven't fully contemplated what it is you are attempting to calculate. Typically moving averages are not going to require a group by clause...you just need to add a where clause that can filter out the first N records where the number of input rows is less than N. David J. -- View this message in context: http://postgresql.nabble.com/regr-slope-function-with-auto-creation-of-X-column-tp5828673p5828676.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql