---
title: "Coming soon on ClickHouse®: Window functions"
excerpt: "Discover ClickHouse® window functions, the new experimental feature that brings advanced SQL analytics to your datasets."
authors: "Xoel Lopez"
categories: "The Data Base"
createdOn: "2023-09-25 00:00:00"
publishedOn: "2021-03-16 00:00:00"
updatedOn: "2025-04-24 00:00:00"
status: "published"
---

<p>Window functions have been a very <a href="https://www.google.com/search?q=window+functions+clickhouse+site%3Astackoverflow.com">requested</a> feature for ClickHouse® for years. They exist in other databases like Postgres and let you perform calculations across a set of table rows that are somehow related to the current row. They behave similar to regular aggregate functions but, with window functions, rows don’t get grouped into a single output row.</p><p>That enables you to write simpler queries and to do certain things with window functions that you couldn’t do otherwise. Also, you can use the same syntax that you’re already used to from other databases like <a href="https://clickhouse.tech/docs/en/sql-reference/window-functions/#postgres-docs">Postgres</a> or <a href="https://clickhouse.tech/docs/en/sql-reference/window-functions/#mysql-docs">MySQL</a>.</p><p>Lots of work has gone into adding window functions to ClickHouse® in the last few months, and they’re now available as an experimental feature (<a href="https://clickhouse.tech/docs/en/sql-reference/window-functions/">docs</a>).</p><p>In this post we’ll explore a bit what you can do with them.</p><h2 id="window-functions">Window functions</h2><p>Let’s load some data first. We’ll use a sample of the events dataset. Download it from <a href="https://storage.googleapis.com/tinybird-assets/datasets/guides/events_10K.csv">here</a> or running <code>curl https://storage.googleapis.com/tinybird-assets/datasets/guides/events_10K.csv -o events_10K.csv</code>.</p><p>Then create a table to store it like this:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=DROP%20TABLE%20IF%20EXISTS%20events%3B%0A%0ACREATE%20TABLE%20events%0A%28%0A%20%20%20%20%60date%60%20DateTime%2C%20%0A%20%20%20%20%60product_id%60%20String%2C%20%0A%20%20%20%20%60user_id%60%20Int64%2C%20%0A%20%20%20%20%60event%60%20String%2C%20%0A%20%20%20%20%60extra_data%60%20String%0A%29%0A%0AENGINE%20%3D%20MergeTree%28%29%0AORDER%20BY%20date%3B%0A&amp;language=Tinybird&amp;title=create_table&amp;run=&amp;token="></iframe></figure><p>And load the data onto the table you’ve just create doing <code>ch client -q 'insert into events format CSV' &lt; events_10K.csv</code>.</p><p>This is what a sample of the data looks like</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=mbp-de-xoel%20%3A%29%20select%20%2A%20from%20events%20limit%205%0A%0ASELECT%20%2A%0AFROM%20events%0ALIMIT%205%0A%0AQuery%20id%3A%20cd605b33-2cab-42d4-ac6d-1c9ee3ecbf9d%0A%0A%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80date%E2%94%80%E2%94%AC%E2%94%80product_id%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%AC%E2%94%80user_id%E2%94%80%E2%94%AC%E2%94%80event%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%AC%E2%94%80extra_data%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%90%0A%E2%94%82%202017-01-01%2000%3A02%3A15%20%E2%94%82%206afd3c18-1aaa-11eb-a161-acde48001122%20%E2%94%82%20%20591085%20%E2%94%82%20remove_item_from_cart%20%E2%94%82%20%7B%22city%22%3A%20%22Sacramento%22%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%E2%94%82%202017-01-01%2000%3A03%3A17%20%E2%94%82%206a9bb09c-1aaa-11eb-80e7-acde48001122%20%E2%94%82%20%20725414%20%E2%94%82%20remove_item_from_cart%20%E2%94%82%20%7B%22city%22%3A%20%22Indianapolis%22%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%E2%94%82%202017-01-01%2000%3A05%3A08%20%E2%94%82%2066f007d8-1aaa-11eb-b1c1-acde48001122%20%E2%94%82%20%20543642%20%E2%94%82%20remove_item_from_cart%20%E2%94%82%20%7B%22city%22%3A%20%22Tallahassee%22%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%E2%94%82%202017-01-01%2000%3A08%3A14%20%E2%94%82%2069edd65c-1aaa-11eb-b042-acde48001122%20%E2%94%82%20%20122790%20%E2%94%82%20search%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%20%7B%22city%22%3A%20%22Sacramento%22%2C%20%22term%22%3A%20%22Sarat%22%7D%20%E2%94%82%0A%E2%94%82%202017-01-01%2000%3A08%3A19%20%E2%94%82%205db95746-1aaa-11eb-a212-acde48001122%20%E2%94%82%20%20915043%20%E2%94%82%20view%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%20%7B%22city%22%3A%20%22Phoenix%22%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%0A%0A5%20rows%20in%20set.%20Elapsed%3A%200.059%20sec.%20%0A&amp;language=Tinybird&amp;title=events_sample&amp;run=&amp;token="></iframe></figure><p>Now we’re ready to make some queries. These are some of the things that window functions enable you to do.</p><p><strong>Note:</strong> This is an experimental feature. To be able to use it, you’ll have to run the following command in your ClickHouse® client console before you use any window function:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=mbp-de-xoel%20%3A%29%20set%20allow_experimental_window_functions%20%3D%201%0A%0ASET%20allow_experimental_window_functions%20%3D%201%0A%0AQuery%20id%3A%20af40e217-6c70-4acc-a4f9-006d12cde825%0A%0AOk.%0A%0A0%20rows%20in%20set.%20Elapsed%3A%200.002%20sec.%20%0A&amp;language=shell&amp;title=allow_experimental_window_functions&amp;run=&amp;token="></iframe></figure><p>Now we’re ready to go.</p><h3 id="cumulative-sums-averages-and-other-aggregate-functions">Cumulative sums, averages and other aggregate functions</h3><p>Until now, the only way to do cumulative sums and averages was using <a href="https://clickhouse.tech/docs/en/sql-reference/aggregate-functions/reference/grouparray/"><code>groupArray</code></a>, <a href="https://clickhouse.tech/docs/en/sql-reference/functions/array-join/"><code>arrayJoin</code></a>, <a href="https://clickhouse.tech/docs/en/sql-reference/aggregate-functions/reference/grouparraymovingsum/"><code>groupArrayMovingSum</code></a> and <a href="https://clickhouse.tech/docs/en/sql-reference/aggregate-functions/reference/grouparraymovingavg/"><code>groupArrayMovingAvg</code></a> functions, like described <a href="https://github.com/ClickHouse®/ClickHouse®/issues/1469#issuecomment-356969871">here</a>.</p><p>For example, this is how you’d go about getting the daily and cumulative count of how many <strong>buy</strong> events happened since we started recording data, and the daily and cumulative revenue as well.</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=mbp-de-xoel%20%3A%29%20select%20toDate%28date%29%20date%2C%20count%28%29%20purchases%2C%20sum%28purchases%29%20over%20%28order%20by%20date%20asc%20rows%20unbounded%20preceding%29%20cum_purchases%2C%20sum%28JSONExtractFloat%28extra_data%2C%20%27price%27%29%29%20AS%20revenue%2C%20sum%28revenue%29%20over%20%28order%20by%20date%20asc%20rows%20unbounded%20preceding%29%20as%20cum_revenue%20from%20events%20where%20event%20%3D%20%27buy%27%20group%20by%20date%20order%20by%20date%20asc%20limit%2010%0A%0ASELECT%0A%20%20%20%20toDate%28date%29%20AS%20date%2C%0A%20%20%20%20count%28%29%20AS%20purchases%2C%0A%20%20%20%20sum%28purchases%29%20OVER%20%28ORDER%20BY%20date%20ASC%20ROWS%20BETWEEN%20UNBOUNDED%20PRECEDING%20AND%20CURRENT%20ROW%29%20AS%20cum_purchases%2C%0A%20%20%20%20sum%28JSONExtractFloat%28extra_data%2C%20%27price%27%29%29%20AS%20revenue%2C%0A%20%20%20%20sum%28revenue%29%20OVER%20%28ORDER%20BY%20date%20ASC%20ROWS%20BETWEEN%20UNBOUNDED%20PRECEDING%20AND%20CURRENT%20ROW%29%20AS%20cum_revenue%0AFROM%20events%0AWHERE%20event%20%3D%20%27buy%27%0AGROUP%20BY%20date%0AORDER%20BY%20date%20ASC%0ALIMIT%2010%0A%0AQuery%20id%3A%20ab4dd0dc-6951-415a-9cd8-1467aa43d335%0A%0A%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80date%E2%94%80%E2%94%AC%E2%94%80purchases%E2%94%80%E2%94%AC%E2%94%80cum_purchases%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80revenue%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80cum_revenue%E2%94%80%E2%94%90%0A%E2%94%82%202017-01-01%20%E2%94%82%20%20%20%20%20%20%20128%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20128%20%E2%94%82%203757.6299999999987%20%E2%94%82%203757.6299999999987%20%E2%94%82%0A%E2%94%82%202017-01-02%20%E2%94%82%20%20%20%20%20%20%20146%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20274%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%204204.31%20%E2%94%82%20%207961.939999999999%20%E2%94%82%0A%E2%94%82%202017-01-03%20%E2%94%82%20%20%20%20%20%20%20184%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20458%20%E2%94%82%20%205851.919999999996%20%E2%94%82%2013813.859999999995%20%E2%94%82%0A%E2%94%82%202017-01-04%20%E2%94%82%20%20%20%20%20%20%20139%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20597%20%E2%94%82%20%204058.539999999999%20%E2%94%82%2017872.399999999994%20%E2%94%82%0A%E2%94%82%202017-01-05%20%E2%94%82%20%20%20%20%20%20%20155%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20752%20%E2%94%82%20%204743.689999999999%20%E2%94%82%2022616.089999999993%20%E2%94%82%0A%E2%94%82%202017-01-06%20%E2%94%82%20%20%20%20%20%20%20132%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20884%20%E2%94%82%20%204210.899999999998%20%E2%94%82%20%2026826.98999999999%20%E2%94%82%0A%E2%94%82%202017-01-07%20%E2%94%82%20%20%20%20%20%20%20148%20%E2%94%82%20%20%20%20%20%20%20%20%20%201032%20%E2%94%82%20%204032.169999999999%20%E2%94%82%20%2030859.15999999999%20%E2%94%82%0A%E2%94%82%202017-01-08%20%E2%94%82%20%20%20%20%20%20%20136%20%E2%94%82%20%20%20%20%20%20%20%20%20%201168%20%E2%94%82%20%205499.699999999999%20%E2%94%82%2036358.859999999986%20%E2%94%82%0A%E2%94%82%202017-01-09%20%E2%94%82%20%20%20%20%20%20%20142%20%E2%94%82%20%20%20%20%20%20%20%20%20%201310%20%E2%94%82%20%204790.849999999999%20%E2%94%82%2041149.709999999985%20%E2%94%82%0A%E2%94%82%202017-01-10%20%E2%94%82%20%20%20%20%20%20%20126%20%E2%94%82%20%20%20%20%20%20%20%20%20%201436%20%E2%94%82%203875.1299999999997%20%E2%94%82%20%2045024.83999999998%20%E2%94%82%0A%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%0A%0A10%20rows%20in%20set.%20Elapsed%3A%200.097%20sec.%20Processed%201.00%20million%20rows%2C%2059.49%20MB%20%2810.26%20million%20rows%2Fs.%2C%20610.54%20MB%2Fs.%29%20%0A&amp;language=Tinybird&amp;title=cum_revenue&amp;run=&amp;token="></iframe></figure><h3 id="window-clause">WINDOW clause</h3><p>In the previous query we repeated twice the <code>over (order by date asc rows unbounded preceding)</code> expression. That’s error prone, and to avoid that we can define the window just once and reuse it as many times as we want in our query.</p><p>The syntax is the same as for Postgres (<a href="https://www.postgresql.org/docs/current/sql-select.html#SQL-WINDOW">docs</a>)</p><p>The previous query would be rewritten like this, using the window clause:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=mbp-de-xoel%20%3A%29%20select%20toDate%28date%29%20date%2C%20count%28%29%20purchases%2C%20sum%28purchases%29%20over%20w%20cum_purchases%2C%20sum%28JSONExtractFloat%28extra_data%2C%20%27price%27%29%29%20AS%20revenue%2C%20sum%28revenue%29%20over%20w%20as%20cum_revenue%20from%20events%20where%20event%20%3D%20%27buy%27%20group%20by%20date%20window%20w%20as%20%28order%20by%20date%20asc%20rows%20unbounded%20preceding%29%20order%20by%20date%20asc%20limit%2010%0A%0ASELECT%0A%20%20%20%20toDate%28date%29%20AS%20date%2C%0A%20%20%20%20count%28%29%20AS%20purchases%2C%0A%20%20%20%20sum%28purchases%29%20OVER%20w%20AS%20cum_purchases%2C%0A%20%20%20%20sum%28JSONExtractFloat%28extra_data%2C%20%27price%27%29%29%20AS%20revenue%2C%0A%20%20%20%20sum%28revenue%29%20OVER%20w%20AS%20cum_revenue%0AFROM%20events%0AWHERE%20event%20%3D%20%27buy%27%0AGROUP%20BY%20date%0AWINDOW%20%20w%20AS%20%28ORDER%20BY%20%20date%20ASC%20ROWS%20BETWEEN%20UNBOUNDED%20PRECEDING%20AND%20CURRENT%20ROW%29%0AORDER%20BY%20date%20ASC%0ALIMIT%2010%0A%0AQuery%20id%3A%20565508ad-3ff7-4700-83e7-8740215667bf%0A%0A%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80date%E2%94%80%E2%94%AC%E2%94%80purchases%E2%94%80%E2%94%AC%E2%94%80cum_purchases%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80revenue%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80cum_revenue%E2%94%80%E2%94%90%0A%E2%94%82%202017-01-01%20%E2%94%82%20%20%20%20%20%20%20128%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20128%20%E2%94%82%203757.6299999999987%20%E2%94%82%203757.6299999999987%20%E2%94%82%0A%E2%94%82%202017-01-02%20%E2%94%82%20%20%20%20%20%20%20146%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20274%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%204204.31%20%E2%94%82%20%207961.939999999999%20%E2%94%82%0A%E2%94%82%202017-01-03%20%E2%94%82%20%20%20%20%20%20%20184%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20458%20%E2%94%82%20%205851.919999999996%20%E2%94%82%2013813.859999999995%20%E2%94%82%0A%E2%94%82%202017-01-04%20%E2%94%82%20%20%20%20%20%20%20139%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20597%20%E2%94%82%20%204058.539999999999%20%E2%94%82%2017872.399999999994%20%E2%94%82%0A%E2%94%82%202017-01-05%20%E2%94%82%20%20%20%20%20%20%20155%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20752%20%E2%94%82%20%204743.689999999999%20%E2%94%82%2022616.089999999993%20%E2%94%82%0A%E2%94%82%202017-01-06%20%E2%94%82%20%20%20%20%20%20%20132%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20884%20%E2%94%82%20%204210.899999999998%20%E2%94%82%20%2026826.98999999999%20%E2%94%82%0A%E2%94%82%202017-01-07%20%E2%94%82%20%20%20%20%20%20%20148%20%E2%94%82%20%20%20%20%20%20%20%20%20%201032%20%E2%94%82%20%204032.169999999999%20%E2%94%82%20%2030859.15999999999%20%E2%94%82%0A%E2%94%82%202017-01-08%20%E2%94%82%20%20%20%20%20%20%20136%20%E2%94%82%20%20%20%20%20%20%20%20%20%201168%20%E2%94%82%20%205499.699999999999%20%E2%94%82%2036358.859999999986%20%E2%94%82%0A%E2%94%82%202017-01-09%20%E2%94%82%20%20%20%20%20%20%20142%20%E2%94%82%20%20%20%20%20%20%20%20%20%201310%20%E2%94%82%20%204790.849999999999%20%E2%94%82%2041149.709999999985%20%E2%94%82%0A%E2%94%82%202017-01-10%20%E2%94%82%20%20%20%20%20%20%20126%20%E2%94%82%20%20%20%20%20%20%20%20%20%201436%20%E2%94%82%203875.1299999999997%20%E2%94%82%20%2045024.83999999998%20%E2%94%82%0A%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%0A%0A10%20rows%20in%20set.%20Elapsed%3A%200.091%20sec.%20Processed%201.00%20million%20rows%2C%2059.49%20MB%20%2810.93%20million%20rows%2Fs.%2C%20650.45%20MB%2Fs.%29%20%0A&amp;language=Tinybird&amp;title=cum_revenue_and_evens_with_window_clause&amp;run=&amp;token="></iframe></figure><h2 id="moving-counts-and-averages">Moving counts and averages</h2><p>If we wanted to calculate rolling aggregate functions for only the last (or next) N rows, that’s also possible. For example, this is how you’d calculate the 30-day moving average of revenue, and the sum of revenue for the past 30 days.</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=mbp-de-xoel%20%3A%29%20select%20toDate%28date%29%20d%2C%20sum%28JSONExtractFloat%28extra_data%2C%20%27price%27%29%20AS%20price%29%20revenue%2C%20sum%28revenue%29%20over%20w%20as%2030d_total_revenue%2C%20avg%28revenue%29%20over%20w%20as%2030d_avg_revenue%20from%20events%20group%20by%20d%20window%20w%20as%20%28ORDER%20BY%20d%20ASC%20ROWS%20BETWEEN%2030%20PRECEDING%20AND%20CURRENT%20ROW%29%20order%20by%20d%20limit%2010%0A%0ASELECT%0A%20%20%20%20toDate%28date%29%20AS%20d%2C%0A%20%20%20%20sum%28JSONExtractFloat%28extra_data%2C%20%27price%27%29%20AS%20price%29%20AS%20revenue%2C%0A%20%20%20%20sum%28revenue%29%20OVER%20w%20AS%20%6030d_total_revenue%60%2C%0A%20%20%20%20avg%28revenue%29%20OVER%20w%20AS%20%6030d_avg_revenue%60%0AFROM%20events%0AGROUP%20BY%20d%0AWINDOW%20%20w%20AS%20%28ORDER%20BY%20%20d%20ASC%20ROWS%20BETWEEN%2030%20PRECEDING%20AND%20CURRENT%20ROW%29%0AORDER%20BY%20d%20ASC%0ALIMIT%2010%0A%0AQuery%20id%3A%209d9f4e0e-068a-4cba-ae79-a9d92f23ef62%0A%0A%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80d%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80revenue%E2%94%80%E2%94%AC%E2%94%80%E2%94%8030d_total_revenue%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%8030d_avg_revenue%E2%94%80%E2%94%90%0A%E2%94%82%202017-01-01%20%E2%94%82%203757.6299999999987%20%E2%94%82%203757.6299999999987%20%E2%94%82%203757.6299999999987%20%E2%94%82%0A%E2%94%82%202017-01-02%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%204204.31%20%E2%94%82%20%207961.939999999999%20%E2%94%82%203980.9699999999993%20%E2%94%82%0A%E2%94%82%202017-01-03%20%E2%94%82%20%205851.919999999996%20%E2%94%82%2013813.859999999995%20%E2%94%82%20%204604.619999999998%20%E2%94%82%0A%E2%94%82%202017-01-04%20%E2%94%82%20%204058.539999999999%20%E2%94%82%2017872.399999999994%20%E2%94%82%204468.0999999999985%20%E2%94%82%0A%E2%94%82%202017-01-05%20%E2%94%82%20%204743.689999999999%20%E2%94%82%2022616.089999999993%20%E2%94%82%20%204523.217999999999%20%E2%94%82%0A%E2%94%82%202017-01-06%20%E2%94%82%20%204210.899999999998%20%E2%94%82%20%2026826.98999999999%20%E2%94%82%20%204471.164999999998%20%E2%94%82%0A%E2%94%82%202017-01-07%20%E2%94%82%20%204032.169999999999%20%E2%94%82%20%2030859.15999999999%20%E2%94%82%20%204408.451428571427%20%E2%94%82%0A%E2%94%82%202017-01-08%20%E2%94%82%20%205499.699999999999%20%E2%94%82%2036358.859999999986%20%E2%94%82%20%204544.857499999998%20%E2%94%82%0A%E2%94%82%202017-01-09%20%E2%94%82%20%204790.849999999999%20%E2%94%82%2041149.709999999985%20%E2%94%82%20%204572.189999999999%20%E2%94%82%0A%E2%94%82%202017-01-10%20%E2%94%82%203875.1299999999997%20%E2%94%82%20%2045024.83999999998%20%E2%94%82%20%204502.483999999999%20%E2%94%82%0A%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%0A%0A10%20rows%20in%20set.%20Elapsed%3A%200.152%20sec.%20Processed%201.00%20million%20rows%2C%2040.48%20MB%20%286.57%20million%20rows%2Fs.%2C%20266.07%20MB%2Fs.%29%20%0A&amp;language=Tinybird&amp;title=n_rows_rolling_funcs&amp;run=&amp;token="></iframe></figure><h3 id="partition-by-clause">PARTITION BY clause</h3><p>The PARTITION BY clause divides rows into multiple groups or partitions to which the window function is applied. So if you wanted to calculate the cumulative revenue per month, since the beginning of each year, you’d do it like this:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=%3E%20cat%20window-funcs%2Fq7_revenue_per_month_and_percentage.sql%20%26%26%20echo%20%27%5Cn___________%5Cn%27%20%26%26%20ch%20client%20-mn%20%3C%20window-funcs%2Fq7_revenue_per_month_and_percentage.sql%20%0Aset%20allow_experimental_window_functions%20%3D%201%3B%0A%0ASELECT%20%0A%20%20%20%20month%2C%0A%20%20%20%20revenue%2C%0A%20%20%20%20revenue_year%2C%0A%20%20%20%20revenue%20%2F%20revenue_year%20percentage%2C%0A%20%20%20%20bar%28percentage%2C%200%2C%201%29%20bar%0AFROM%20%28%0A%20%20%20%20SELECT%20%0A%20%20%20%20%20%20%20%20toYYYYMM%28date%29%20month%0A%20%20%20%20%20%20%20%20%2C%20toStartOfMonth%28date%29%20start_month_date%0A%20%20%20%20%20%20%20%20%2C%20sum%28JSONExtractFloat%28extra_data%2C%20%27price%27%29%29%20AS%20revenue%0A%20%20%20%20%20%20%20%20%2C%20sum%28revenue%29%20OVER%20%28PARTITION%20BY%20toYear%28start_month_date%29%29%20AS%20revenue_year%0A%20%20%20%20FROM%20events%0A%20%20%20%20WHERE%20event%20%3D%20%27buy%27%0A%20%20%20%20GROUP%20BY%20month%2C%20start_month_date%0A%29%20data_per_month%0AORDER%20BY%20month%20ASC%0ALIMIT%2024%0AFORMAT%20PrettyCompact%3B%0A___________%0A%0A%E2%94%8C%E2%94%80%E2%94%80month%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80revenue%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80revenue_year%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80percentage%E2%94%80%E2%94%AC%E2%94%80bar%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%90%0A%E2%94%82%20201701%20%E2%94%82%20121771.59999999913%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.08564799803512266%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8B%20%20%20%20%E2%94%82%0A%E2%94%82%20201702%20%E2%94%82%20103257.34999999928%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.07262600893732178%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8B%20%20%20%20%20%E2%94%82%0A%E2%94%82%20201703%20%E2%94%82%20112965.44999999889%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.07945419654202393%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8E%20%20%20%20%E2%94%82%0A%E2%94%82%20201704%20%E2%94%82%20107637.81999999967%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.07570701046766999%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%20%20%20%20%20%E2%94%82%0A%E2%94%82%20201705%20%E2%94%82%20118362.24999999927%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%20%200.0832500333036004%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8B%20%20%20%20%E2%94%82%0A%E2%94%82%20201706%20%E2%94%82%20113429.39999999941%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.07978051555802142%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8D%20%20%20%20%E2%94%82%0A%E2%94%82%20201707%20%E2%94%82%20%2074661.41999999958%20%E2%94%82%201421768.1999999913%20%E2%94%82%200.052513074916150204%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8F%20%20%20%20%20%20%E2%94%82%0A%E2%94%82%20201708%20%E2%94%82%20%20132285.9599999991%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.09304326823458275%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8D%20%20%20%E2%94%82%0A%E2%94%82%20201709%20%E2%94%82%20121553.16999999923%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.08549436539655336%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8B%20%20%20%20%E2%94%82%0A%E2%94%82%20201710%20%E2%94%82%20130530.15999999936%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.09180832712392932%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8E%20%20%20%E2%94%82%0A%E2%94%82%20201711%20%E2%94%82%20106178.85999999967%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.07468085163249559%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8A%20%20%20%20%20%E2%94%82%0A%E2%94%82%20201712%20%E2%94%82%20179134.75999999896%20%E2%94%82%201421768.1999999913%20%E2%94%82%20%200.12599434985252875%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%20%E2%94%82%0A%E2%94%82%20201801%20%E2%94%82%20%20137455.0499999993%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.09014725874487607%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8F%20%20%20%E2%94%82%0A%E2%94%82%20201802%20%E2%94%82%20114627.38999999943%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.07517617567037241%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%20%20%20%20%20%E2%94%82%0A%E2%94%82%20201803%20%E2%94%82%20132162.03999999928%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.08667593963357954%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8A%20%20%20%20%E2%94%82%0A%E2%94%82%20201804%20%E2%94%82%20124963.18999999943%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%20%200.0819547119041106%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8C%20%20%20%20%E2%94%82%0A%E2%94%82%20201805%20%E2%94%82%20131530.67999999915%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.08626187428435318%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8A%20%20%20%20%E2%94%82%0A%E2%94%82%20201806%20%E2%94%82%20124697.98999999918%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.08178078556950766%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8C%20%20%20%20%E2%94%82%0A%E2%94%82%20201807%20%E2%94%82%20%2080235.58000000005%20%E2%94%82%201524783.4699999923%20%E2%94%82%200.052620966569109286%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8F%20%20%20%20%20%20%E2%94%82%0A%E2%94%82%20201808%20%E2%94%82%20148473.43999999904%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.09737345854096896%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8B%20%20%20%E2%94%82%0A%E2%94%82%20201809%20%E2%94%82%20123176.24999999936%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.08078278157094658%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8D%20%20%20%20%E2%94%82%0A%E2%94%82%20201810%20%E2%94%82%20138994.97999999952%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.09115719230613133%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8E%20%20%20%E2%94%82%0A%E2%94%82%20201811%20%E2%94%82%20%20104641.9299999995%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.06862740320761743%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8D%20%20%20%20%20%E2%94%82%0A%E2%94%82%20201812%20%E2%94%82%20163824.94999999902%20%E2%94%82%201524783.4699999923%20%E2%94%82%20%200.10744145199842693%20%E2%94%82%20%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%88%E2%96%8C%20%20%E2%94%82%0A%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%0A&amp;language=Tinybird&amp;title=revenue_per_month_vs_total_year_with_bar&amp;run=&amp;token="></iframe></figure><h3 id="range-clause">RANGE clause</h3><p>It’s possible to define the window boundaries by a given <em>value</em> difference, instead of rows. If the column used for the window is a DateTime column, the value will be measured in seconds. This is how you’d calculate the total number of purchases and the revenue for the past hour, for every <strong>buy</strong> event:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/XQAAAAILEgAAAAAAAABBKUqGk9nLKvqfvTg59cNYS38JSedH3Q7PBQ-4_0YgKmCg1CjG8gOAD1MBJ_dFXLD9xpZhTFV95NJEffbevghOgnHSMFr5JPiVlODJIqo6O5QMcHOInhwF8rvBswDfGfvVrUSOSO2o-8pUnmgC2Iug5_54eSCGPjh9K64mkTdbfdVVdVZbf_sF33rIrd7BTwu_cKEEFLNbSGxpD39Db_wEu44RV-76VVbvLkd0-hheRiZuGcL08NOd7PP85Vg1eyl_oxmKLBSg2Os0aCCy1cX9pXRz2-rMkGo0uNicQ33GBmiUt3bI1TlPqRPcn4jRu0xiyBgQyGbrWZPb28kHQzRt2aNRDhp7e7poMkNkcsWFpRXws6mx0wD9quKG5UbupWGLutJVM-fLa4xJe9TrQs4nJr4CdyP_GhK5xQvOQUCFhhqF4atbxVvcb5gIHGiJ2tYJNjHc0yExvrpRcKs4atRT9TGCsij_ltfLYc7MyMQ3lsL4MtTUS1_dW6U038Q5MNE-Sr9bYa4U0sr7rf_LCBt3W6QdYkftS5E9_uYsAyTcO1Evfgr2HD2NujlhrDTpR-OwhBffJ5xbBSU6ln3ZucRdoqlh82381R3-NPOBmmyBFMD_6ISVkk-V640uKfBiSuAgYAL2JM7KPcfmujNYyQwUohqIAW4ibOg06PD9ea09vaM8t0qGBg_5ibaDXZ4QDUKLwxLUR1RR4yZlqNFg3QW7jygcCU7Dz-C10jWcn59mqGrOHzuj1WbUc2GqxhXhYPQL-WgGMmj3q_lRV3dSFGunAnhZdsS77Y_QepHBitZCjEj3gJeZMXM4z7ptBb22ZGbSwLZx3w96IFa_T6Oqxr0dpUrBaGmfO1--HK-CXpF9XgJcpDJxlJV64JXWFjmpmmzdmFlS5ZXkOsJ1m2rfIoWvT62GSzuv9ADTG8rsEZZ-MIisBXtX202O32f3yjEGCjHrPaMhA5n8ubRHct_J1serorJCn-ty5sxzuvNXrIYqxRqRDj6g5OaLBs3GviHDy3vSLHBWYoAEGjIGe6gbmOUKH21DiU03S5J-avchddty_GBctJxSooht9n71ql4PCtH6D3jsFK5v7e8HRpi4NIUXF7WwNlcnm_z2h9nDW7F2UTN34So1FFm5LT8vghcW2fLSq4Vvpzk8oJN0vMRfIRrHS_7vfBgiRBoTDOUMbpY6C4sDFd2-3zhwltnis71oxlOqiNgoIDM8C0V8qwIhRZI-sLqgiunWb0tizSmkL9ZFYJpLHZJzaJhadsBu8VY4DuGwbrb-sgx9JU1K20o53R__32uvZQ/embed"></iframe></figure><h3 id="rank-rownumber-and-denserank">RANK, ROW_NUMBER and DENSE_RANK</h3><p>These functions are also supported, and they behave the same way as in Postgres. This is how you’d be the top purchases of each day, ranked by the money spent:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=mbp-de-xoel%20%3A%29%20select%20date%2C%20product_id%2C%20user_id%2C%20JSONExtractFloat%28extra_data%2C%20%27price%27%29%20AS%20price%2C%20rank%28%29%20over%20w%20rank%2C%20dense_rank%28%29%20over%20w%20dense_rank%2C%20row_number%28%29%20over%20w%20row_number%20from%20events%20where%20event%20%3D%20%27buy%27%20window%20w%20as%20%28partition%20by%20toDate%28date%29%20order%20by%20price%20desc%29%20order%20by%20date%20limit%2010%3B%0A%0ASELECT%0A%20%20%20%20date%2C%0A%20%20%20%20product_id%2C%0A%20%20%20%20user_id%2C%0A%20%20%20%20JSONExtractFloat%28extra_data%2C%20%27price%27%29%20AS%20price%2C%0A%20%20%20%20rank%28%29%20OVER%20w%20AS%20rank%2C%0A%20%20%20%20dense_rank%28%29%20OVER%20w%20AS%20dense_rank%2C%0A%20%20%20%20row_number%28%29%20OVER%20w%20AS%20row_number%0AFROM%20events%0AWHERE%20event%20%3D%20%27buy%27%0AWINDOW%20%20w%20AS%20%28PARTITION%20BY%20%20toDate%28date%29%20ORDER%20BY%20%20price%20DESC%29%0AORDER%20BY%20date%20ASC%0ALIMIT%2010%0A%0AQuery%20id%3A%20f1ff3af5-588a-469e-884f-615d33a0c9aa%0A%0A%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80date%E2%94%80%E2%94%AC%E2%94%80product_id%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%AC%E2%94%80user_id%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80price%E2%94%80%E2%94%AC%E2%94%80rank%E2%94%80%E2%94%AC%E2%94%80dense_rank%E2%94%80%E2%94%AC%E2%94%80row_number%E2%94%80%E2%94%90%0A%E2%94%82%202017-01-01%2016%3A01%3A18%20%E2%94%82%20678be58c-1aaa-11eb-8120-acde48001122%20%E2%94%82%20%20617799%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20132.75%20%E2%94%82%20%20%20%201%20%E2%94%82%20%20%20%20%20%20%20%20%20%201%20%E2%94%82%20%20%20%20%20%20%20%20%20%201%20%E2%94%82%0A%E2%94%82%202017-01-01%2004%3A20%3A27%20%E2%94%82%2066ba6acc-1aaa-11eb-ade4-acde48001122%20%E2%94%82%20%20991969%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20132.75%20%E2%94%82%20%20%20%201%20%E2%94%82%20%20%20%20%20%20%20%20%20%201%20%E2%94%82%20%20%20%20%20%20%20%20%20%202%20%E2%94%82%0A%E2%94%82%202017-01-01%2003%3A42%3A37%20%E2%94%82%206a333dbe-1aaa-11eb-a00a-acde48001122%20%E2%94%82%20%20108823%20%E2%94%82%20110.78999999999999%20%E2%94%82%20%20%20%203%20%E2%94%82%20%20%20%20%20%20%20%20%20%202%20%E2%94%82%20%20%20%20%20%20%20%20%20%203%20%E2%94%82%0A%E2%94%82%202017-01-01%2012%3A30%3A07%20%E2%94%82%206b1d4bac-1aaa-11eb-8870-acde48001122%20%E2%94%82%20%20798297%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%2099.95%20%E2%94%82%20%20%20%204%20%E2%94%82%20%20%20%20%20%20%20%20%20%203%20%E2%94%82%20%20%20%20%20%20%20%20%20%204%20%E2%94%82%0A%E2%94%82%202017-01-01%2015%3A18%3A14%20%E2%94%82%205a67447a-1aaa-11eb-9876-acde48001122%20%E2%94%82%20%20938780%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%2099.95%20%E2%94%82%20%20%20%204%20%E2%94%82%20%20%20%20%20%20%20%20%20%203%20%E2%94%82%20%20%20%20%20%20%20%20%20%205%20%E2%94%82%0A%E2%94%82%202017-01-01%2001%3A00%3A59%20%E2%94%82%206af87cdc-1aaa-11eb-b6f6-acde48001122%20%E2%94%82%20%20358305%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%2099.95%20%E2%94%82%20%20%20%204%20%E2%94%82%20%20%20%20%20%20%20%20%20%203%20%E2%94%82%20%20%20%20%20%20%20%20%20%206%20%E2%94%82%0A%E2%94%82%202017-01-01%2018%3A21%3A12%20%E2%94%82%20606b8e08-1aaa-11eb-9999-acde48001122%20%E2%94%82%20%20401162%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%2099.13%20%E2%94%82%20%20%20%207%20%E2%94%82%20%20%20%20%20%20%20%20%20%204%20%E2%94%82%20%20%20%20%20%20%20%20%20%207%20%E2%94%82%0A%E2%94%82%202017-01-01%2002%3A08%3A23%20%E2%94%82%206b524034-1aaa-11eb-8c8c-acde48001122%20%E2%94%82%20%20530731%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%2088.47%20%E2%94%82%20%20%20%208%20%E2%94%82%20%20%20%20%20%20%20%20%20%205%20%E2%94%82%20%20%20%20%20%20%20%20%20%208%20%E2%94%82%0A%E2%94%82%202017-01-01%2012%3A58%3A55%20%E2%94%82%20697aef8c-1aaa-11eb-8a5f-acde48001122%20%E2%94%82%20%20602259%20%E2%94%82%20%2088.28999999999999%20%E2%94%82%20%20%20%209%20%E2%94%82%20%20%20%20%20%20%20%20%20%206%20%E2%94%82%20%20%20%20%20%20%20%20%20%209%20%E2%94%82%0A%E2%94%82%202017-01-01%2004%3A45%3A52%20%E2%94%82%206bfb869c-1aaa-11eb-8eb0-acde48001122%20%E2%94%82%20%20%2093517%20%E2%94%82%20%2088.28999999999999%20%E2%94%82%20%20%20%209%20%E2%94%82%20%20%20%20%20%20%20%20%20%206%20%E2%94%82%20%20%20%20%20%20%20%20%2010%20%E2%94%82%0A%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%B4%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%0A&amp;language=Tinybird&amp;title=rank_dense_rank_row_number&amp;run=&amp;token="></iframe></figure><p>As of the time of writing this, there seems to be some <a href="https://github.com/ClickHouse®/ClickHouse®/issues/21828#issuecomment-801117521">issues</a> related to sorting when a window with a partition by expression is used. <strong>Update</strong> (2021-03-22): this is now <a href="https://github.com/ClickHouse®/ClickHouse®/pull/21915">fixed</a>.</p><p>As a side note, a similar result could be obtained with the <a href="https://clickhouse.tech/docs/en/sql-reference/statements/select/limit-by/">LIMIT BY</a> clause.</p><h2 id="what-else">What else?</h2><p>There are many other promising features coming up. On the ClickHouse® GitHub repository, there is a <a href="https://github.com/ClickHouse®/ClickHouse®/issues/17623">ticket</a> with all the features that are in the roadmap for 2021 and their current status. Here are some additional ones we’re quite excited about:</p><h3 id="nested-and-semistructured-data">Nested and semistructured data</h3><p>Right now, ClickHouse® already has a <a href="https://clickhouse.tech/docs/en/sql-reference/data-types/nested-data-structures/nested/">Nested</a> data type that lets you have columns that store more than one field inside, with a single nesting level.</p><p>There’s work being done to allow an arbitrary nesting level. They’d eliminate the need to save JSON columns as strings, allow different column representations to store the same DataType, dynamic columns and much more</p><h3 id="separation-of-storage-and-compute">Separation of storage and compute</h3><p>To allow, for example, to store replicas of the data on S3. Follow the progress on this <a href="https://github.com/ClickHouse®/ClickHouse®/pull/16240">PR</a></p><h3 id="a-postgresql-table-engine">A PostgreSQL table engine</h3><p>An experimental MySQL table engine already <a href="https://clickhouse.tech/docs/en/engines/table-engines/integrations/mysql/">exists</a>, that lets you run queries over data stored originally on a MySQL database. A PostgreSQL engine is also <a href="https://github.com/ClickHouse®/ClickHouse®/pull/18554">in the works</a></p><h3 id="projections">Projections</h3><p>Projections will let you do some simple aggregations with the performance of a Materialized View, but without having to create a MV. They’ll be defined at the table level. Read a much more detailed explanation with examples in <a href="https://github.com/ClickHouse®/ClickHouse®/issues/14730">this issue</a></p><h3 id="backups">Backups</h3><p>Having the possibility to import and export data and metadata to S3, a local filesystem or another ClickHouse® server. Allowing incremental backups, and much more. See all the details in <a href="https://github.com/ClickHouse®/ClickHouse®/issues/13953">this ticket</a></p><h2 id="one-more-thing">One more thing</h2><p>Tinybird lets you define dynamic endpoints to do real-time analytics at scale on top of ClickHouse®. If you’d like to use our product, sign up <a href="https://tinybird.co/signup">here</a></p>
