You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
added count distinct as countdistinct agg. tested to work, that is is gated if aggs are off. tested against collisions with tables and columns named countdistincts. Returns based on postgres rules name count().
Copy file name to clipboardExpand all lines: docs/references/api/aggregate_functions.rst
+38-1Lines changed: 38 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
Aggregate Functions
4
4
###################
5
5
6
-
PostgREST supports the following aggregate functions: ``avg()``, ``count()``, ``max()``, ``min()``, and ``sum()``.
6
+
PostgREST supports the following aggregate functions: ``avg()``, ``count()``, ``countdistinct()``, ``max()``, ``min()``, and ``sum()``.
7
7
Please refer to the `section on aggregate functions in the PostgreSQL documentation <https://www.postgresql.org/docs/current/functions-aggregate.html>`_ for a detailed explanation of these functions.
8
8
9
9
.. note::
@@ -98,6 +98,43 @@ Note that there is a difference between the result of ``count()`` and ``observat
98
98
The former counts the whole row, while the latter counts the non ``NULL`` values of the ``observation`` column (both grouped by ``order_date``).
99
99
This is due to how PostgreSQL itself implements the ``count()`` function.
100
100
101
+
The ``countdistinct()`` Aggregate
102
+
=================================
103
+
104
+
``countdistinct()`` returns the number of distinct non ``NULL`` values of a column.
105
+
It maps to PostgreSQL's ``COUNT(DISTINCT col)`` and must be attached to a specific column — there is no ``*`` form.
returns the number of distinct customers that placed an order:
112
+
113
+
.. code-block:: json
114
+
115
+
[
116
+
{
117
+
"count": 17
118
+
}
119
+
]
120
+
121
+
.. note::
122
+
The default JSON key is ``"count"`` (not ``"countdistinct"``), because PostgreSQL labels the result of ``COUNT(DISTINCT col)`` as ``count`` — same as the plain ``count()`` aggregate. Provide an explicit alias (e.g. ``dc:col.countdistinct()``) if you want a different key, or to disambiguate when combining ``count()`` and ``countdistinct()`` in the same query.
123
+
124
+
Aliases and casts work the same way as for the other aggregates:
it "combines every aggregate flavour (count, countdistinct, sum, avg, max, min) in one query"$
80
+
get "/project_invoices?select=c:count(),dp:project_id.countdistinct(),di:invoice_total.countdistinct(),s:invoice_total.sum(),a:invoice_total.avg(),mx:invoice_total.max(),mn:invoice_total.min()"`shouldRespondWith`
0 commit comments