MySQL Forums
Forum List  »  Newbie

Re: Recreating a dated series from a change log
Posted by: Rick James
Date: April 27, 2015 06:30PM

What do you want? The "Expected" column?

SET optimizer_switch = 'block_nested_loop=off;  -- I don't know why this was necessary.
SELECT  d.T_day,
        @units := IFNULL(p.Units, @units) AS Units
    FROM (
        SELECT @units := Units
            FROM puc_changes
            WHERE    T_day <= '2015-02-24'
            ORDER BY T_day DESC
            LIMIT 1
         ) init
    JOIN T_days AS d
    LEFT JOIN puc_changes AS p
          ON p.T_day = d.T_day 
    WHERE    d.T_day BETWEEN '2015-02-24' AND '2015-03-04'
    ORDER BY d.T_day;
-- Output:
+------------+-------+
| T_day      | Units |
+------------+-------+
| 2015-02-24 |   222 |
| 2015-02-25 |   222 |
| 2015-02-26 |   222 |
| 2015-03-01 |   333 |
| 2015-03-02 |   333 |
| 2015-03-03 |   333 |
| 2015-03-04 |   333 |
+------------+-------+
7 rows in set (0.00 sec)

footnote:
With block_nested_loop=on:
+----+-------------+-------------+--------+---------------+---------+---------+------+------+----------------------------------------------------+
| id | select_type | table       | type   | possible_keys | key     | key_len | ref  | rows | Extra                                              |
+----+-------------+-------------+--------+---------------+---------+---------+------+------+----------------------------------------------------+
|  1 | PRIMARY     | <derived2>  | system | NULL          | NULL    | NULL    | NULL |    1 | Using temporary; Using filesort                    |
|  1 | PRIMARY     | d           | range  | PRIMARY       | PRIMARY | 3       | NULL |    7 | Using where; Using index                           |
|  1 | PRIMARY     | p           | ALL    | PRIMARY       | NULL    | NULL    | NULL |    2 | Using where; Using join buffer (Block Nested Loop) |
|  2 | DERIVED     | puc_changes | range  | PRIMARY       | PRIMARY | 3       | NULL |    2 | Using where                                        |
+----+-------------+-------------+--------+---------------+---------+---------+------+------+----------------------------------------------------+

With block_nested_loop=off:
+----+-------------+-------------+--------+---------------+---------+---------+-----------------+------+--------------------------+
| id | select_type | table       | type   | possible_keys | key     | key_len | ref             | rows | Extra                    |
+----+-------------+-------------+--------+---------------+---------+---------+-----------------+------+--------------------------+
|  1 | PRIMARY     | <derived2>  | system | NULL          | NULL    | NULL    | NULL            |    1 | NULL                     |
|  1 | PRIMARY     | d           | range  | PRIMARY       | PRIMARY | 3       | NULL            |    7 | Using where; Using index |
|  1 | PRIMARY     | p           | eq_ref | PRIMARY       | PRIMARY | 3       | f630218.d.T_day |    1 | NULL                     |
|  2 | DERIVED     | puc_changes | range  | PRIMARY       | PRIMARY | 3       | NULL            |    2 | Using where              |
+----+-------------+-------------+--------+---------------+---------+---------+-----------------+------+--------------------------+

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.