Note that there are some explanatory texts on larger screens.

plurals
  1. POSum of the most recent non-null columns (window function with "ignore nulls")
    primarykey
    data
    text
    <p>I am using PostgreSQL 9.1.9.</p> <p>In the project I am working on, some most recent records have null columns because that information was not available when that row was created. I have a view that lists the sum of rows that belongs to the members of a group. As of right now, the view shows the sum of the most recent columns, which uses null values if those are the most recent values. For example, </p> <pre><code>table1 group_name | member ------------------- group1 | Andy group1 | Bob table2 name | stat_date | col1 | col2 | col 3 -------------------------------------- Andy | 6/19/13 | null | 1 | 2 Andy | 6/18/13 | 100 | 3 | 5 Bob | 6/19/13 | 50 | 9 | 12 Bob | 6/18/13 | 111 | 31 | 51 -- creating view would be something like this... create view v_grouped as select table1.group_name, stat_date, sum(col1) as col1_sum, sum(col2) as col2_sum, sum(col3) as col3_sum from table1 join table2 on table1.member = table2.name group by table1.group_name, table2.stat_date; </code></pre> <p>Current view looks like this:</p> <pre><code>group_name | stat_date | col1_sum | col2_sum | col3_sum ------------------------------------------------------- group1 | 6/19/13 | 50 | 10 | 14 group2 | 6/18/13 | 211 | 34 | 56 </code></pre> <p>Instead of 50, 150 would be a closer representation of what the actual group total is, despite lack of data for 6/19. So, I want an output of</p> <pre><code>group_name | stat_date | col1_sum | col2_sum | col3_sum ------------------------------------------------------- group1 | 6/19/13 | 150 | 10 | 14 group2 | 6/18/13 | 211 | 34 | 56 </code></pre> <p>I've been looking at <code>first_value()</code> from window functions as a possible function to use. I found that Oracle's <code>first_value()</code> supports the <code>ignore nulls</code> option which I believe will do what I want (<a href="http://psoug.org/definition/FIRST_VALUE.htm" rel="nofollow">http://psoug.org/definition/FIRST_VALUE.htm</a>). According to this page I linked, about PL/SQL's <code>first_value()</code> function: </p> <blockquote> <p>If the first value in the result set is NULL then the function returns NULL unless you specify IGNORE NULLS. If you use the IGNORE NULLS parameter then FIRST_VALUE will return the first non-null value found in the result set. (If all values are null then it will return NULL.)</p> <p>Example Syntax: FIRST_VALUE(expression [INGORE NULLS]) OVER (analytic_clause)</p> </blockquote> <p>But PostgreSQL's <code>first_value()</code> does not support such an option. Is there a way to do this in PostgreSql? Thank you in advance! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload