title: LAG of MIN in SQL Analytic
body: <p>I have a table containing employees id, year id, client id, and the number of sales. For example:</p>
  
  <pre><code> --------------------------------------
    id_emp | id_year | sales | client id
   --------------------------------------
      4    |    1    |   14  |    1
      4    |    1    |   10  |    2
      4    |    2    |   11  |    1
      4    |    2    |   17  |    2
  </code></pre>
  
  <p>For a employee, I want to obtain rows with the minimum sales per year and the minimum sales of the previous year. </p>
  
  <p>One of the queries I tried is the following:</p>
  
  <pre><code>select distinct 
    id_emp,
    id_year, 
    MIN(sales) OVER(partition by id_emp, id_year) AS min_sales,
    LAG(min(sales), 1) OVER(PARTITION BY id_emp, id_year
                                ORDER BY id_emp, id_year) AS previous
   from facts
  where id_emp = 4
  group by id_emp, id_year, sales;
  </code></pre>
  
  <p><strong>I get the result:</strong></p>
  
  <pre><code> -------------------------------------    
    id_emp | id_year | sales | previous
   -------------------------------------
      4    |    1    |   10  |  (null)
      4    |    1    |   10  |    10
      4    |    2    |   11  |  (null)
  </code></pre>
  
  <p><strong>but I expect to get:</strong></p>
  
  <pre><code> -------------------------------------
    id_emp | id_year | sales | previous
   -------------------------------------
      4    |    1    |   10  |  (null)
      4    |    2    |   11  |    10
  </code></pre>