title: Create materialized view based on aggregate materialized view
body: <p>The base table</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE IF NOT EXISTS test_sessions
  (
      session_id   UInt64,
      session_name String,
      created_at   DateTime
  )
  ENGINE = MergeTree()
  PARTITION BY toYYYYMM(created_at)
  ORDER BY (session_id);
  </code></pre>
  <p>With the following data</p>
  <pre class="lang-sql prettyprint-override"><code>INSERT INTO test_sessions (session_id, session_name, created_at) VALUES
  (1, 'start', '2021-01-31 00:00:00'),
  (1, 'stop', '2021-01-31 01:00:00'),
  (2, 'start', '2021-01-31 01:00:00')
  ;
  </code></pre>
  <p>Created 2 materialized views to get closed sessions</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE MATERIALIZED VIEW IF NOT EXISTS test_session_aggregate_states
  (
      session_id UInt64,
      started_at AggregateFunction(minIf, DateTime, UInt8),
      stopped_at AggregateFunction(maxIf, DateTime, UInt8)
  )
  ENGINE = AggregatingMergeTree
  PARTITION BY tuple()
  ORDER BY (session_id)
  POPULATE AS
  SELECT session_id,
         minIfState(created_at, session_name = 'start') AS started_at,
         maxIfState(created_at, session_name = 'stop')  AS stopped_at
  FROM test_sessions
  GROUP BY session_id;
  
  CREATE VIEW IF NOT EXISTS test_session_completed
  (
      session_id UInt64,
      started_at DateTime,
      stopped_at DateTime
  )
  AS
  SELECT session_id,
         minIfMerge(started_at) AS started_at,
         maxIfMerge(stopped_at) AS stopped_at
  FROM test_session_aggregate_states
  GROUP BY session_id
  HAVING (started_at != '0000-00-00 00:00:00') AND
         (stopped_at != '0000-00-00 00:00:00')
  ;
  </code></pre>
  <p>It works normally: return 1 row with existing &quot;start&quot; and &quot;stop&quot;</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM test_session_completed;
  -- 1,2021-01-31 00:00:00,2021-01-31 01:00:00
  </code></pre>
  <p>Trying to create a materialized view based on <code>test_session_completed</code> with joins to other tables (there are no joins in the example)</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE MATERIALIZED VIEW IF NOT EXISTS test_mv
  (
      session_id UInt64
  )
  ENGINE = MergeTree
  PARTITION BY tuple()
  ORDER BY (session_id)
  POPULATE AS
  SELECT session_id
  FROM test_session_completed
  ;
  </code></pre>
  <p>Writing a test queries to test the <code>test_mv</code></p>
  <pre class="lang-sql prettyprint-override"><code>INSERT INTO test_sessions (session_id, session_name, created_at) VALUES
  (3, 'start', '2021-01-31 02:00:00'),
  (3, 'stop', '2021-01-31 03:00:00');
  
  SELECT * FROM test_session_completed;
  -- SUCCESS
  -- 3,2021-01-31 02:00:00,2021-01-31 03:00:00
  -- 1,2021-01-31 00:00:00,2021-01-31 01:00:00
  
  SELECT * FROM test_mv;
  -- FAILURE
  -- 1
  -- EXPECTED RESULT
  -- 3
  -- 1
  </code></pre>
  <p><strong>How to fill <code>test_mv</code> based on <code>test_session_completed</code> ?</strong></p>
  <p>ClickHouse version: <code>20.11.4.13</code></p>