@@ -125,3 +125,100 @@ If there are no non-NULL input rows then function returns ``NULL``.
125125 total sold items
126126 ------------------
127127 3750
128+
129+ Grouping data
130+ =============
131+
132+ Getting some aggregated values is not very helpful itself. Most cases require
133+ to group outputs in some specific manner.
134+
135+ For example, if the task is to count number of registered categories it is
136+ simple to solve as:
137+
138+ .. code-block::
139+
140+ SELECT count(DISTINCT category) FROM product;
141+
142+ ::
143+
144+ count
145+ -------
146+ 6
147+
148+ But in case, the task is to count **sold** items for each category, this
149+ becomes impossible to solve without grouping items. ``GROUP BY`` clause
150+ provides the ability to group rows based on some field(s) values.
151+
152+ General syntax is:
153+
154+ ::
155+
156+ SELECT select_list
157+ FROM ...
158+ [WHERE ...]
159+ GROUP BY grouping_column_reference [, grouping_column_reference]...
160+
161+ .. code-block::
162+
163+ SELECT category, sum(sold) as sold FROM product GROUP BY category;
164+
165+ .. table::
166+ :align: center
167+
168+ ======== ====
169+ category sold
170+ ======== ====
171+ Audio 0
172+ Printers 800
173+ Gaming 600
174+ Monitors 400
175+ Laptops 300
176+ Phones 800
177+ ======== ====
178+
179+ If the ``SELECT`` statement is an aggregate query with a ``GROUP BY`` clause,
180+ then each of the expressions specified as part of the GROUP BY clause is
181+ evaluated for each row of the dataset according to the processing rules stated
182+ below for ORDER BY expressions. Each row is then assigned to a "group" based on
183+ the results; rows for which the results of evaluating the GROUP BY expressions
184+ are the same get assigned to the same group. For the purposes of grouping rows,
185+ ``NULL`` values are considered equal. The usual rules for selecting a collation
186+ sequence with which to compare text values apply when evaluating expressions in
187+ a GROUP BY clause. The expressions in the GROUP BY clause do not have to be
188+ expressions that appear in the result. The expressions in a GROUP BY clause may
189+ not be aggregate expressions.
190+
191+ Having clause
192+ -------------
193+
194+ But what, if the task is to filter categories, that have less than 500 sold
195+ items in it? ``WHERE`` clause will be useless, cause it's evaluated before
196+ grouping output. In other words, only rows that suite WHERE filter will be
197+ selected.
198+
199+ .. code-block:: sql
200+
201+ SELECT category, sum(sold) as sold
202+ FROM product
203+ GROUP BY category
204+ HAVING sum(sold) < 500;
205+
206+ .. table::
207+ :align: center
208+
209+ ======== ====
210+ category sold
211+ ======== ====
212+ Audio 0
213+ Monitors 400
214+ Laptops 300
215+ ======== ====
216+
217+ If a ``HAVING`` clause is specified, it is evaluated once for each group of
218+ rows as a boolean expression. If the result of evaluating the HAVING clause is
219+ false, the group is discarded. If the HAVING clause is an aggregate expression,
220+ it is evaluated across all rows in the group.
221+
222+ If a HAVING clause is a non-aggregate expression, it is evaluated with respect
223+ to an arbitrarily selected row from the group. The HAVING expression may refer
224+ to values, even aggregate functions, that are not in the result.
0 commit comments