Skip to content

Commit

Permalink
documenting SUM, TOTAL, AVG, COUNT
Browse files Browse the repository at this point in the history
* by some insane oversight on my part, these were missing until now.
  I think perhaps I'd thought I'd caught them under "Math"
  • Loading branch information
deobald committed Dec 7, 2023
1 parent 768cf6b commit 7e405d6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
24 changes: 22 additions & 2 deletions docs/print.html
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,11 @@ <h2 id="group-by"><a class="header" href="#group-by">GROUP BY</a></h2>
clause or a column created with an <a href="sql/functions.html#aggregate-functions">aggregate function</a>,
such as <code>SUM</code>.</p>
<pre><code class="language-sql">SELECT name, price FROM products GROUP BY name, price;
SELECT name, sum(price) FROM products GROUP BY name;
SELECT name, SUM(price) FROM products GROUP BY name;
</code></pre>
<h2 id="having"><a class="header" href="#having">HAVING</a></h2>
<p><code>HAVING</code> adds a search condition to an aggregate query.</p>
<pre><code class="language-sql">SELECT name, sum(price) FROM products GROUP BY name HAVING LENGTH(name) &gt; 4;
<pre><code class="language-sql">SELECT name, SUM(price) FROM products GROUP BY name HAVING LENGTH(name) &gt; 4;
</code></pre>
<p>It is most often used with <code>GROUP BY</code> (seen above), but it is also legal
with other aggregates:</p>
Expand Down Expand Up @@ -1231,6 +1231,26 @@ <h3 id="min-max"><a class="header" href="#min-max">MIN, MAX</a></h3>
</code></pre>
<p>NOTE: <code>MIN</code> and <code>MAX</code> also have non-aggregate equivalents, which are 2-arity.
When used that way, they each return the minimum or maximum value of the two values provided.</p>
<h3 id="sum"><a class="header" href="#sum">SUM</a></h3>
<p>The <code>SUM</code> function returns the sum of all non-null values under the column given as a parameter.</p>
<pre><code class="language-sql">SELECT SUM(price) FROM products;
</code></pre>
<p>If all values for the given column are <code>NULL</code>, <code>SUM</code> returns <code>NULL</code>.</p>
<h3 id="total"><a class="header" href="#total">TOTAL</a></h3>
<p>The <code>TOTAL</code> function is equivalent to <code>SUM</code> except that it returns <code>0.0</code> in the case where
all input values are <code>NULL</code>.</p>
<h3 id="avg"><a class="header" href="#avg">AVG</a></h3>
<p>The <code>AVG</code> function takes a numerical-type-agnostic average of all values under the
column given as a parameter.</p>
<pre><code class="language-sql">SELECT AVG(price) FROM products;
</code></pre>
<h3 id="count"><a class="header" href="#count">COUNT</a></h3>
<p>The <code>COUNT</code> function returns the count of <em>non-null</em>, <em>non-empty</em> values for the specified column.</p>
<pre><code>SELECT COUNT(price) FROM sales;
</code></pre>
<p>NOTE: Because null/empty values are ignored, the behaviour of <code>COUNT</code> will differ from other
SQL dialects. Whether or not <code>COUNT(price)</code> and <code>COUNT(1)</code> are equivalent is dependent on
whether the <code>price</code> attribute exists with a non-null value on each document.</p>
<h3 id="array_agg"><a class="header" href="#array_agg">ARRAY_AGG</a></h3>
<p>The <code>ARRAY_AGG</code> function concatenates the results of an expression into an array.
The parameter may be <a href="sql/queries.html#order-by-sorting-results">ordered</a>
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/searchindex.json

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions docs/sql/functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,26 @@ <h3 id="min-max"><a class="header" href="#min-max">MIN, MAX</a></h3>
</code></pre>
<p>NOTE: <code>MIN</code> and <code>MAX</code> also have non-aggregate equivalents, which are 2-arity.
When used that way, they each return the minimum or maximum value of the two values provided.</p>
<h3 id="sum"><a class="header" href="#sum">SUM</a></h3>
<p>The <code>SUM</code> function returns the sum of all non-null values under the column given as a parameter.</p>
<pre><code class="language-sql">SELECT SUM(price) FROM products;
</code></pre>
<p>If all values for the given column are <code>NULL</code>, <code>SUM</code> returns <code>NULL</code>.</p>
<h3 id="total"><a class="header" href="#total">TOTAL</a></h3>
<p>The <code>TOTAL</code> function is equivalent to <code>SUM</code> except that it returns <code>0.0</code> in the case where
all input values are <code>NULL</code>.</p>
<h3 id="avg"><a class="header" href="#avg">AVG</a></h3>
<p>The <code>AVG</code> function takes a numerical-type-agnostic average of all values under the
column given as a parameter.</p>
<pre><code class="language-sql">SELECT AVG(price) FROM products;
</code></pre>
<h3 id="count"><a class="header" href="#count">COUNT</a></h3>
<p>The <code>COUNT</code> function returns the count of <em>non-null</em>, <em>non-empty</em> values for the specified column.</p>
<pre><code>SELECT COUNT(price) FROM sales;
</code></pre>
<p>NOTE: Because null/empty values are ignored, the behaviour of <code>COUNT</code> will differ from other
SQL dialects. Whether or not <code>COUNT(price)</code> and <code>COUNT(1)</code> are equivalent is dependent on
whether the <code>price</code> attribute exists with a non-null value on each document.</p>
<h3 id="array_agg"><a class="header" href="#array_agg">ARRAY_AGG</a></h3>
<p>The <code>ARRAY_AGG</code> function concatenates the results of an expression into an array.
The parameter may be <a href="queries.html#order-by-sorting-results">ordered</a>
Expand Down
4 changes: 2 additions & 2 deletions docs/sql/queries.html
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,11 @@ <h2 id="group-by"><a class="header" href="#group-by">GROUP BY</a></h2>
clause or a column created with an <a href="functions.html#aggregate-functions">aggregate function</a>,
such as <code>SUM</code>.</p>
<pre><code class="language-sql">SELECT name, price FROM products GROUP BY name, price;
SELECT name, sum(price) FROM products GROUP BY name;
SELECT name, SUM(price) FROM products GROUP BY name;
</code></pre>
<h2 id="having"><a class="header" href="#having">HAVING</a></h2>
<p><code>HAVING</code> adds a search condition to an aggregate query.</p>
<pre><code class="language-sql">SELECT name, sum(price) FROM products GROUP BY name HAVING LENGTH(name) &gt; 4;
<pre><code class="language-sql">SELECT name, SUM(price) FROM products GROUP BY name HAVING LENGTH(name) &gt; 4;
</code></pre>
<p>It is most often used with <code>GROUP BY</code> (seen above), but it is also legal
with other aggregates:</p>
Expand Down
36 changes: 36 additions & 0 deletions src/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,42 @@ SELECT MAX(price) FROM products;
NOTE: `MIN` and `MAX` also have non-aggregate equivalents, which are 2-arity.
When used that way, they each return the minimum or maximum value of the two values provided.

### SUM

The `SUM` function returns the sum of all non-null values under the column given as a parameter.

```sql
SELECT SUM(price) FROM products;
```

If all values for the given column are `NULL`, `SUM` returns `NULL`.

### TOTAL

The `TOTAL` function is equivalent to `SUM` except that it returns `0.0` in the case where
all input values are `NULL`.

### AVG

The `AVG` function takes a numerical-type-agnostic average of all values under the
column given as a parameter.

```sql
SELECT AVG(price) FROM products;
```

### COUNT

The `COUNT` function returns the count of _non-null_, _non-empty_ values for the specified column.

```
SELECT COUNT(price) FROM sales;
```

NOTE: Because null/empty values are ignored, the behaviour of `COUNT` will differ from other
SQL dialects. Whether or not `COUNT(price)` and `COUNT(1)` are equivalent is dependent on
whether the `price` attribute exists with a non-null value on each document.

### ARRAY_AGG

The `ARRAY_AGG` function concatenates the results of an expression into an array.
Expand Down
4 changes: 2 additions & 2 deletions src/sql/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ such as `SUM`.

```sql
SELECT name, price FROM products GROUP BY name, price;
SELECT name, sum(price) FROM products GROUP BY name;
SELECT name, SUM(price) FROM products GROUP BY name;
```

## HAVING

`HAVING` adds a search condition to an aggregate query.

```sql
SELECT name, sum(price) FROM products GROUP BY name HAVING LENGTH(name) > 4;
SELECT name, SUM(price) FROM products GROUP BY name HAVING LENGTH(name) > 4;
```

It is most often used with `GROUP BY` (seen above), but it is also legal
Expand Down

0 comments on commit 7e405d6

Please sign in to comment.