title: SQL: Last_Value() returns wrong result (but First_Value() works fine)
body: <p>I have a table in SQL Server 2012 as the snapshot shows:</p>
  
  <p><img src="https://i.stack.imgur.com/sGWAg.png" alt="enter image description here"></p>
  
  <p>Then I'm using Last_Value() and First Value to get AverageAmount of each EmpID for different YearMonth. The script is as follows:</p>
  
  <pre><code>SELECT A.EmpID,  
         First_Value(A.AverageAmount) OVER (PARTITION BY A.EmpID Order by A.DimYearMonthKey asc) AS  '200901AvgAmount', 
         Last_Value(A.AverageAmount) OVER (PARTITION BY A.EmpID Order by A.DimYearMonthKey asc) AS '201112AvgAmount'
  
  FROM  Emp_Amt  AS A
  </code></pre>
  
  <p>However, the result for this query is:</p>
  
  <p><img src="https://i.stack.imgur.com/6IkLl.png" alt="result"></p>
  
  <p>In column of "201112AvgAmount", it shows different values for each EmpID while "200901AvgAmount" has correct values. </p>
  
  <p>Is there anything wrong with my SQL script? I did a lot research online but still cannot find the answer....</p>