title: PostgreSQL window function "lag()" only pulls from current resultset
body: <p>I'm making a stock ticker as a learning experience for PostgreSQL and AngularJS.</p>
  
  <p>In my ticker query, I attempt to discover the change in price from the previous day. I'm implementing the DB queries in PHP right now for ease of testing and I'll port to AngularJS later.</p>
  
  <p><strong>DB Setup</strong></p>
  
  <pre><code>prices
  --pk
  --fund (foreign key to funds.pk)
  --price
  --price_date
  
  funds
  --pk
  --fund_name
  --summary
  </code></pre>
  
  <p><strong>Query</strong>
  Get the latest price and the price before it (as well as other info) for each fund with an entry in the prices table.
  This $query is a single line in my PHP file.</p>
  
  <pre><code>$query = 'SELECT prices.price_date, 
  prices.price, 
  (lag(prices.price) over (ORDER BY prices.price_date DESC)) as last_price, 
  prices.fund, 
  funds.fund_name 
  FROM prices 
  INNER JOIN funds ON prices.fund=funds.pk 
  WHERE price_date=(SELECT price_date FROM prices ORDER BY price_date DESC LIMIT 1)';
  </code></pre>
  
  <p><strong>Result</strong></p>
  
  <pre><code>[
  {"price_date":"2015-09-08","price":"17.5901","last_price":null,"fund":"1","fund_name":"L Income"},
  {"price_date":"2015-09-08","price":"22.8859","last_price":"17.5901","fund":"2","fund_name":"L 2020"},
  {"price_date":"2015-09-08","price":"24.6693","last_price":"22.8859","fund":"3","fund_name":"L 2030"},
  {"price_date":"2015-09-08","price":"26.1456","last_price":"24.6693","fund":"4","fund_name":"L 2040"},
  {"price_date":"2015-09-08","price":"14.7756","last_price":"26.1456","fund":"5","fund_name":"L 2050"},
  {"price_date":"2015-09-08","price":"14.8181","last_price":"14.7756","fund":"6","fund_name":"G Fund"},
  {"price_date":"2015-09-08","price":"16.93","last_price":"14.8181","fund":"7","fund_name":"F Fund"},
  {"price_date":"2015-09-08","price":"26.369","last_price":"16.93","fund":"8","fund_name":"C Fund"},
  {"price_date":"2015-09-08","price":"35.9595","last_price":"26.369","fund":"9","fund_name":"S Fund"},
  {"price_date":"2015-09-08","price":"24.0362","last_price":"35.9595","fund":"10","fund_name":"I Fund"}
  ]
  </code></pre>
  
  <p>As you can see, the lag() window function is only drawing on the current resultset for pulling the previous record's prices.price field.</p>
  
  <p>I am at a loss now. Does anyone have guidance?</p>