<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.foji.io/index.php?action=history&amp;feed=atom&amp;title=Relational_Data_-_Aggregate_Functions</id>
	<title>Relational Data - Aggregate Functions - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://docs.foji.io/index.php?action=history&amp;feed=atom&amp;title=Relational_Data_-_Aggregate_Functions"/>
	<link rel="alternate" type="text/html" href="https://docs.foji.io/index.php?title=Relational_Data_-_Aggregate_Functions&amp;action=history"/>
	<updated>2026-06-13T08:11:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.40.1</generator>
	<entry>
		<id>https://docs.foji.io/index.php?title=Relational_Data_-_Aggregate_Functions&amp;diff=1756&amp;oldid=prev</id>
		<title>Chris.Hansen: Import ClickHouse Docs: Wed Aug 28 2024 14:43:29 GMT-0400 (Eastern Daylight Time)</title>
		<link rel="alternate" type="text/html" href="https://docs.foji.io/index.php?title=Relational_Data_-_Aggregate_Functions&amp;diff=1756&amp;oldid=prev"/>
		<updated>2024-08-28T18:43:30Z</updated>

		<summary type="html">&lt;p&gt;Import ClickHouse Docs: Wed Aug 28 2024 14:43:29 GMT-0400 (Eastern Daylight Time)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Aggregate functions work in the [http://www.sql-tutorial.com/sql-aggregate-functions-sql-tutorial normal] way as expected by database experts.&lt;br /&gt;
&lt;br /&gt;
ClickHouse also supports:&lt;br /&gt;
&lt;br /&gt;
* [[Relational_Data_-_Aggregate_Functions_-_Parametric_Functions#aggregate_functions_parametric|Parametric aggregate functions]], which accept other parameters in addition to columns.&lt;br /&gt;
* [[Relational_Data_-_Aggregate_Functions_-_Combinators#aggregate_functions_combinators|Combinators]], which change the behavior of aggregate functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;null-processing&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== NULL Processing ==&lt;br /&gt;
&lt;br /&gt;
During aggregation, all &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; arguments are skipped. If the aggregation has several arguments it will ignore any row in which one or more of them are NULL.&lt;br /&gt;
&lt;br /&gt;
There is an exception to this rule, which are the functions [[Relational_Data_-_Aggregate_Functions_-_Reference_-_First_Value|&amp;lt;code&amp;gt;first_value&amp;lt;/code&amp;gt;]], [[Relational_Data_-_Aggregate_Functions_-_Reference_-_Last_Value|&amp;lt;code&amp;gt;last_value&amp;lt;/code&amp;gt;]] and their aliases (&amp;lt;code&amp;gt;any&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;anyLast&amp;lt;/code&amp;gt; respectively) when followed by the modifier &amp;lt;code&amp;gt;RESPECT NULLS&amp;lt;/code&amp;gt;. For example, &amp;lt;code&amp;gt;FIRST_VALUE(b) RESPECT NULLS&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Examples:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Consider this table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;┌─x─┬────y─┐&lt;br /&gt;
│ 1 │    2 │&lt;br /&gt;
│ 2 │ ᴺᵁᴸᴸ │&lt;br /&gt;
│ 3 │    2 │&lt;br /&gt;
│ 3 │    3 │&lt;br /&gt;
│ 3 │ ᴺᵁᴸᴸ │&lt;br /&gt;
└───┴──────┘&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s say you need to total the values in the &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; column:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;SELECT sum(y) FROM t_null_big&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;┌─sum(y)─┐&lt;br /&gt;
│      7 │&lt;br /&gt;
└────────┘&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Now you can use the &amp;lt;code&amp;gt;groupArray&amp;lt;/code&amp;gt; function to create an array from the &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; column:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;SELECT groupArray(y) FROM t_null_big&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;┌─groupArray(y)─┐&lt;br /&gt;
│ [2,2,3]       │&lt;br /&gt;
└───────────────┘&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;groupArray&amp;lt;/code&amp;gt; does not include &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; in the resulting array.&lt;br /&gt;
&lt;br /&gt;
You can use [[Relational_Data_-_Functions_-_Functions_For_Nulls#coalesce|COALESCE]] to change NULL into a value that makes sense in your use case. For example: &amp;lt;code&amp;gt;avg(COALESCE(column, 0))&amp;lt;/code&amp;gt; with use the column value in the aggregation or zero if NULL:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;SELECT&lt;br /&gt;
    avg(y),&lt;br /&gt;
    avg(coalesce(y, 0))&lt;br /&gt;
FROM t_null_big&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;┌─────────────avg(y)─┬─avg(coalesce(y, 0))─┐&lt;br /&gt;
│ 2.3333333333333335 │                 1.4 │&lt;br /&gt;
└────────────────────┴─────────────────────┘&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Also you can use [[Relational_Data_-_Data_Types_-_Tuple|Tuple]] to work around NULL skipping behavior. The a &amp;lt;code&amp;gt;Tuple&amp;lt;/code&amp;gt; that contains only a &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; value is not &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt;, so the aggregate functions won’t skip that row because of that &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;SELECT&lt;br /&gt;
    groupArray(y),&lt;br /&gt;
    groupArray(tuple(y)).1&lt;br /&gt;
FROM t_null_big;&lt;br /&gt;
&lt;br /&gt;
┌─groupArray(y)─┬─tupleElement(groupArray(tuple(y)), 1)─┐&lt;br /&gt;
│ [2,2,3]       │ [2,NULL,2,3,NULL]                     │&lt;br /&gt;
└───────────────┴───────────────────────────────────────┘&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that aggregations are skipped when the columns are used as arguments to an aggregated function. For example [[Relational_Data_-_Aggregate_Functions_-_Reference_-_Count|&amp;lt;code&amp;gt;count&amp;lt;/code&amp;gt;]] without parameters (&amp;lt;code&amp;gt;count()&amp;lt;/code&amp;gt;) or with constant ones (&amp;lt;code&amp;gt;count(1)&amp;lt;/code&amp;gt;) will count all rows in the block (independently of the value of the GROUP BY column as it’s not an argument), while &amp;lt;code&amp;gt;count(column)&amp;lt;/code&amp;gt; will only return the number of rows where column is not NULL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;SELECT&lt;br /&gt;
    v,&lt;br /&gt;
    count(1),&lt;br /&gt;
    count(v)&lt;br /&gt;
FROM&lt;br /&gt;
(&lt;br /&gt;
    SELECT if(number &amp;lt; 10, NULL, number % 3) AS v&lt;br /&gt;
    FROM numbers(15)&lt;br /&gt;
)&lt;br /&gt;
GROUP BY v&lt;br /&gt;
&lt;br /&gt;
┌────v─┬─count()─┬─count(v)─┐&lt;br /&gt;
│ ᴺᵁᴸᴸ │      10 │        0 │&lt;br /&gt;
│    0 │       1 │        1 │&lt;br /&gt;
│    1 │       2 │        2 │&lt;br /&gt;
│    2 │       2 │        2 │&lt;br /&gt;
└──────┴─────────┴──────────┘&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
And here is an example of of first_value with &amp;lt;code&amp;gt;RESPECT NULLS&amp;lt;/code&amp;gt; where we can see that NULL inputs are respected and it will return the first value read, whether it’s NULL or not:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;SELECT&lt;br /&gt;
    col || &amp;#039;_&amp;#039; || ((col + 1) * 5 - 1) as range,&lt;br /&gt;
    first_value(odd_or_null) as first,&lt;br /&gt;
    first_value(odd_or_null) IGNORE NULLS as first_ignore_null,&lt;br /&gt;
    first_value(odd_or_null) RESPECT NULLS as first_respect_nulls&lt;br /&gt;
FROM&lt;br /&gt;
(&lt;br /&gt;
    SELECT&lt;br /&gt;
        intDiv(number, 5) AS col,&lt;br /&gt;
        if(number % 2 == 0, NULL, number) as odd_or_null&lt;br /&gt;
    FROM numbers(15)&lt;br /&gt;
)&lt;br /&gt;
GROUP BY col&lt;br /&gt;
ORDER BY col&lt;br /&gt;
&lt;br /&gt;
┌─range─┬─first─┬─first_ignore_null─┬─first_respect_nulls─┐&lt;br /&gt;
│ 0_4   │     1 │                 1 │                ᴺᵁᴸᴸ │&lt;br /&gt;
│ 1_9   │     5 │                 5 │                   5 │&lt;br /&gt;
│ 2_14  │    11 │                11 │                ᴺᵁᴸᴸ │&lt;br /&gt;
└───────┴───────┴───────────────────┴─────────────────────┘&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Relational_Data]]&lt;/div&gt;</summary>
		<author><name>Chris.Hansen</name></author>
	</entry>
</feed>