title: Oracle LAG Without a Where Clause
body: <p>I need to select the previous salary and current for calculating a percentage. </p>
  
  <p>I'm attempting to use the Oracle Analytical function LAG which is processed only before the final order by clause. Because the LAG function is not ran until after the where clause, if I include my where clause to narrow down just one employee, it works flawlessly however when including all employees it returns whatever row is above it. </p>
  
  <p>Has anyone faced this before? </p>
  
  <p><strong>Working (returns in order with proper amounts):</strong></p>
  
  <pre><code>select
    employee,
    payrate,
    lag(payrate) over (order by effectdate) as prevrow
  from
    payratetable;
  where employee = 1234;
  </code></pre>
  
  <p><strong>Not Working (returns with random rates from other employees):</strong></p>
  
  <pre><code>select
    employee,
    payrate,
    lag(payrate) over (order by effectdate) as prevrow
  from
    payratetable;
  </code></pre>