title: How to get column value from previous row for each group?
body: <p><strong>Please read the question, is not a duplicate.</strong></p>
  
  <p>I'll try to explain the problem right:</p>
  
  <p>I have a table like this:</p>
  
  <pre><code>Table: stg_mov_salida
  
  id_movement | id_product | id_warehouse | movement_date | id_sale | sale_value | ...
  209338          54545          2          "2011-05-21"    163299     75.95
  209306          54545          2          "2011-06-05"    163267     75.95
  209347          54545          2          "2012-10-26"    163308     77.95
  209399          54545          3          "2011-01-22"    163360     79.95
  209302          54545          3          "2011-01-23"    163263     72.95
  209304          54545          3          "2011-01-24"    163265     72.95
  </code></pre>
  
  <p>And I want to get the <code>sale_value</code> of the previous row, but only if the previous row belongs to the same <code>id_product</code> and <code>id_warehouse</code> that the current row.</p>
  
  <p>The table <code>stg_mov_salida</code> is a staging table ordered by:</p>
  
  <pre><code> id_product asc, id_warehouse asc, movement_date asc
  </code></pre>
  
  <p>As I've read on other questions (like <a href="https://stackoverflow.com/questions/13196190/mysql-subtracting-value-from-previous-row-group-by">this</a> or similar), I've tried to do it without success. The last query (and the closest to my solution) I've tried is this:</p>
  
  <pre><code>SELECT x2.*,
     (SELECT x2.sale_value AS last_sale_value
              FROM stg_mov_salida ms 
                  WHERE x2.id_product = ms.id_product 
                  AND x2.id_warehouse = ms.id_warehouse
                  AND x2.date_movement  &gt; ms.date_movement 
              ORDER BY ms.id_product asc, 
                               ms.id_warehouse asc, 
                               ms.date_movement asc
              LIMIT 1)
  FROM stg_mov_salida x2
  </code></pre>
  
  <p>But this query is so slow that I've never seen it finishing, even for only one given <code>id_product</code>, therefore if this query is correct, I can't use it because is too slow.</p>
  
  <p>Anyone knows how to solve my problem?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p><strong>Expected result:</strong></p>
  
  <pre><code>id_movement | id_product | id_ware | mov_date | id_sale | sale_value | prev_sale_value
    209338       54545          2    "2011-05-21"  163299     75.95           null
    209306       54545          2    "2011-06-05"  163267     75.95           75.95
    209347       54545          2    "2012-10-26"  163308     77.95           75.95
    209399       54545          3    "2011-01-22"  163360     79.95           null
    209302       54545          3    "2011-01-23"  163263     72.95           79.95
    209304       54545          3    "2011-01-24"  163265     72.95           72.95
  </code></pre>
  
  <p>The <code>null</code> values on <code>prev_sale_value</code> would be because that row is the first on the group of <code>(id_product, id_warehouse)</code>, but the idea is to replace that <code>null</code> with the <code>sale_value</code> of the same row directly, but I'll do it when I get the correct <code>prev_sale_value</code> for each row.</p>
  
  <p>Any help would be appreciated</p>
  
  <p>For easily response, here is the <a href="http://sqlfiddle.com/#!12/ce21c/2" rel="nofollow noreferrer">SQLFiddle</a></p>
  
  <p>I've tried this query too, but it doesn't works properly:</p>
  
  <pre><code>select x2.*,
  lag(sale_value, 1) over (partition by sale_value
                                  order by id_product asc,
                                  id_warehouse asc,
                                  date_movement asc) as last_sale_value
  
  from stg_mov_salida x2
  order by id_product asc, id_warehouse asc, date_movement asc
  </code></pre>