Re: Query to return normalized floats

From: Kip Warner <kip(at)thevertigo(dot)com>
To: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Query to return normalized floats
Date: 2016-04-14 08:21:35
Message-ID: 1460622095.15278.1.camel@thevertigo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hey Andreas,

I figured it out with the help of some folks on IRC. It turns out the
view schema was almost correct. It needed some adjustments, in
particular safe handling of divide by zero errors.

CREATE VIEW my_view AS
SELECT
id,
COALESCE((col1 - (SELECT MIN(col1) FROM my_table)) / NULLIF((SELECT MAX(col1) FROM my_table) - (SELECT MIN(col1) FROM my_table), 0), 0) AS col1_norm,
COALESCE((col2 - (SELECT MIN(col2) FROM my_table)) / NULLIF((SELECT MAX(col2) FROM my_table) - (SELECT MIN(col2) FROM my_table), 0), 0) AS col2_norm,
COALESCE((col3 - (SELECT MIN(col3) FROM my_table)) / NULLIF((SELECT MAX(col3) FROM my_table) - (SELECT MIN(col3) FROM my_table), 0), 0) AS col3_norm
FROM my_table GROUP_BY id;

Since my_table contains hundreds of thousands of rows, it was also
suggested to me to create indices for every column in my_table. e.g.

CREATE INDEX col1_index ON my_table(col1);
CREATE INDEX col2_index ON my_table(col2);
CREATE INDEX col3_index ON my_table(col3);

Hopefully that will not only work, but will also be efficient too.

--
Kip Warner -- Senior Software Engineer
OpenPGP encrypted/signed mail preferred
http://www.thevertigo.com

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Yaroslav 2016-04-14 23:36:45 Re: Query to return normalized floats
Previous Message Kip Warner 2016-04-12 00:17:00 Re: Query to return normalized floats