title: ClickHouse: Efficient way to aggregate data by different time ranges at
body: <p>I need to aggregate time-series data (with average functions) on different timeslots, like:</p>
  <ul>
  <li><p>Today</p>
  </li>
  <li><p>last X days</p>
  </li>
  <li><p>Last weekend</p>
  </li>
  <li><p>This week</p>
  </li>
  <li><p>Last X weeks</p>
  </li>
  <li><p>This month</p>
  </li>
  <li><p>List item</p>
  <p>etc...</p>
  </li>
  </ul>
  <p><strong>Q1:</strong> Can it be done within <code>GROUP BY</code> statement or at least with a single query?</p>
  <p><strong>Q2:</strong> Do I need any Materialized View for that?</p>
  <p>The table is partitioned by <code>Month</code> and sharded by <code>UserID</code></p>
  <p>All queries are within <code>UserID</code> <em>(single shard)</em></p>