agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: David G Johnston <david.g.johnston@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: regr_slope function with auto creation of X column
Date: Sat, 29 Nov 2014 20:01:27 -0700 (MST)
Message-ID: <1417316487139-5828676.post@n5.nabble.com> (raw)
In-Reply-To: <CALN462YFf+Nu+8HGd6+7kYDj5Gbb9XEsasZ5d3c9Y=FUOaryug@mail.gmail.com>
References: <CALN462YFf+Nu+8HGd6+7kYDj5Gbb9XEsasZ5d3c9Y=FUOaryug@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
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.ht...
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
view thread (2+ messages)
Message-ID: <1417316487139-5828676.post@n5.nabble.com>
Permalink: ../1417316487139-5828676.post@n5.nabble.com/
Also on: postgresql.org/message-id/1417316487139-5828676.post@n5.nabble.com
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: david.g.johnston@gmail.com
Subject: Re: regr_slope function with auto creation of X column
In-Reply-To: <1417316487139-5828676.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox