Re: Query to return normalized floats - Mailing list pgsql-novice

From Kip Warner
Subject Re: Query to return normalized floats
Date
Msg-id 1460622095.15278.1.camel@thevertigo.com
Whole thread Raw
In response to Re: Query to return normalized floats  (Kip Warner <kip@thevertigo.com>)
Responses Re: Query to return normalized floats
List 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


Attachment

pgsql-novice by date:

Previous
From: Kip Warner
Date:
Subject: Re: Query to return normalized floats
Next
From: Yaroslav
Date:
Subject: Re: Query to return normalized floats