title: MySQL return previous row if no data
body: <p>I'm not able to do a proper select syntax. Please help <br />
  I have two tables: 1st contain the working data</p>
  
  <pre><code>  CREATE TABLE ProdStatus (
    Id    INT(6) UNSIGNED ZEROFILL,
    ProdId INT,
    StatusId INT,
    Date  DATE,
    Total  INT
  );
  </code></pre>
  
  <p>Data from this table:</p>
  
  <pre><code>INSERT INTO ProdStatus
  VALUES
    (000001, 1, 1, '2014-02-01', 5),
    (000002, 1, 2, '2014-02-01', 3),
    (000003, 1, 2, '2014-02-05', 4),
    (000004, 2, 1, '2014-02-07', 2),
    (000005, 2, 1, '2014-02-08', 7),
    (000006, 2, 2, '2014-02-08', 9);
  </code></pre>
  
  <p>2nd is a calendar table</p>
  
  <pre><code>    CREATE TABLE calendar (
        id int(11),
        Date date
  );
  </code></pre>
  
  <p>data for calendar table:</p>
  
  <pre><code>INSERT INTO calendar (id, Date)
  VALUES
  (1, '2014-02-01'),
  (2, '2014-02-02'),
  (3, '2014-02-03'),
  (4, '2014-02-04'),
  (5, '2014-02-05'),
  (6, '2014-02-06'),
  (7, '2014-02-07'),
  (8, '2014-02-08'),
  (9, '2014-02-09'),
  (10, '2014-02-10');
  </code></pre>
  
  <p>I need to be able to select the sum of all values from the ProdStatus table, grouped by ProdId and Date (sum because i can have multiple statuses) but joined with the calendar table because I need the values for every single day in a selected range.
  If I don't have info in the ProdStatus table for a day, then the result from the previous day should be returned. </p>
  
  <p>Here is it the query I have made:</p>
  
  <pre><code>SELECT p.ProdId, c.Date, sum(p.Total) as Result
  FROM ProdStatus p
  right outer join calendar c on c.Date = p.Date
  WHERE p.ProdId in (1, 2)
  and c.date between '2014-02-01' and '2014-02-10'
  group by c.date, p.ProdId
  </code></pre>
  
  <p>This query return this result:</p>
  
  <pre>ProdId    Date    Result
  1    2014-02-01    8
  1    2014-02-05    4
  2    2014-02-07    2
  2    2014-02-08    16
  </pre>
  
  <p>Expected result:</p>
  
  <pre>ProdId    Date    Result
  1    2014-02-01    8
  2    2014-02-01    0
  1    2014-02-02    8
  2    2014-02-02    0
  1    2014-02-03    8
  2    2014-02-03    0
  1    2014-02-04    8
  2    2014-02-04    0
  1    2014-02-05    4
  2    2014-02-05    0
  1    2014-02-06    4
  2    2014-02-06    0
  1    2014-02-07    4
  2    2014-02-07    2
  1    2014-02-08    4
  2    2014-02-08    16
  1    2014-02-09    4
  2    2014-02-09    16
  1    2014-02-10    4
  2    2014-02-10    16
  </pre>
  
  <p>Any suggestions please?
  Thank you </p>