title: Postgresql - How to get value from last record of each month
body: <p>I have a view like this:</p>
  
  <pre><code> Year | Month | Week | Category | Value |
   2017 | 1     | 1    | A     | 1
   2017 | 1     | 1    | B     | 2
   2017 | 1     | 1    | C     | 3
   2017 | 1     | 2    | A     | 4
   2017 | 1     | 2    | B     | 5
   2017 | 1     | 2    | C     | 6
   2017 | 1     | 3    | A     | 7
   2017 | 1     | 3    | B     | 8
   2017 | 1     | 3    | C     | 9
   2017 | 1     | 4    | A     | 10
   2017 | 1     | 4    | B     | 11
   2017 | 1     | 4    | C     | 12
   2017 | 2     | 5    | A     | 1
   2017 | 2     | 5    | B     | 2
   2017 | 2     | 5    | C     | 3
   2017 | 2     | 6    | A     | 4
   2017 | 2     | 6    | B     | 5
   2017 | 2     | 6    | C     | 6
   2017 | 2     | 7    | A     | 7
   2017 | 2     | 7    | B     | 8
   2017 | 2     | 7    | C     | 9
   2017 | 2     | 8    | A     | 10
   2017 | 2     | 8    | B     | 11
   2017 | 2     | 8    | C     | 12
  </code></pre>
  
  <p>And I need to make a new view which needs to show average of value column (let's call it avg_val) and the value from the max week of the month (max_val_of_month). Ex: max week of january is 4, so the value of category A is 10. Or something like this to be clear:</p>
  
  <pre><code> Year | Month | Category | avg_val | max_val_of_month
   2017 | 1     | A        | 5.5     | 10
   2017 | 1     | B        | 6.5     | 11
   2017 | 1     | C        | 7.5     | 12
   2017 | 2     | A        | 5.5     | 10
   2017 | 2     | B        | 6.5     | 11
   2017 | 2     | C        | 7.5     | 12
  </code></pre>
  
  <p>I have use window function, over partition by year, month, category to get the avg value. But how can I get the value of the max week of each month?</p>