title: How to reference output rows with window functions?
body: <p>Suppose I have a table with <code>quantity</code> column.</p>
  
  <pre><code>CREATE TABLE transfers (
    user_id integer,
    quantity integer,
    created timestamp default now()
  );
  </code></pre>
  
  <p>I'd like to iteratively go thru a partition using window functions, but access the output rows, not the input table rows. </p>
  
  <p>To access the input table rows I could do something like this:</p>
  
  <pre><code>SELECT LAG(quantity, 1, 0)
    OVER (PARTITION BY user_id ORDER BY created)
  FROM transfers;
  </code></pre>
  
  <p>I need to access the previous output row to calculate the next output row. How can i access the lag row in the output? Something like:</p>
  
  <pre><code>CREATE VIEW balance AS
  SELECT LAG(balance.total, 1, 0) + quantity AS total
    OVER (PARTITION BY user_id ORDER BY created)
  FROM transfers;
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>This is a minimal example to support the question of how to access the previous output row within a window partition. I don't actually want a sum. </p>