title: SQL Server - Getting the 1st, 2nd and 3rd largest values in previous n rows
body: <p>How can I write a query that gets me the 1st, 2nd, 3rd largest values in the last N rows, for each combination of (W, X, Y, Z)</p>
  
  <p>I would like something like this (in this case N=4):</p>
  
  <pre><code>| W_ID | X_ID | Y_ID | Z_ID |  Yr  | Mnth |  Amount |   Max1   |   Max2   |   Max3   |
  |    5 |    5 |    7 |    6 | 2015 |    5 | 1000.00 |  1000.00 |   NULL   |   NULL   |
  |    5 |    5 |    7 |    6 | 2015 |    6 | 5000.00 |  5000.00 |  1000.00 |   NULL   |
  |    5 |    5 |    7 |    6 | 2015 |    7 | 4000.00 |  5000.00 |  4000.00 |  1000.00 |
  |    5 |    5 |    7 |    6 | 2015 |    8 | 7000.00 |  7000.00 |  5000.00 |  4000.00 |
  |    5 |    5 |    7 |    6 | 2015 |    9 | 9000.00 |  9000.00 |  7000.00 |  5000.00 |
  |    5 |    5 |    7 |    6 | 2015 |   10 |  500.00 |  9000.00 |  7000.00 |  4000.00 |
  |    5 |    5 |    7 |    6 | 2015 |   11 |  100.00 |  9000.00 |  7000.00 |   500.00 |
  |    5 |    5 |    7 |    6 | 2015 |   12 |  500.00 |  9000.00 |   500.00 |   500.00 |
  |    5 |    5 |    7 |    6 | 2016 |    1 | 2500.00 |  2500.00 |   500.00 |   500.00 |
  |    5 |    5 |    7 |    6 | 2016 |    2 |  100.00 |  2500.00 |   500.00 |   100.00 |
  </code></pre>
  
  <p>Thank you in advance for any help you can provide</p>