Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitff6fe15

Browse files
committed
Update documentation to reflect availability of aggregate(DISTINCT).
Try to provide a more lucid discussion in 'Using Aggregate Functions'tutorial section.
1 parent662371c commitff6fe15

File tree

3 files changed

+111
-36
lines changed

3 files changed

+111
-36
lines changed

‎doc/src/sgml/query.sgml

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,39 +361,90 @@ DELETE FROM classname;
361361
Like most other query languages,
362362
<ProductName>PostgreSQL</ProductName> supports
363363
aggregate functions.
364-
The current implementation of
365-
<ProductName>Postgres</ProductName> aggregate functions have some limitations.
366-
Specifically, while there are aggregates to compute
367-
such functions as the <Function>count</Function>, <Function>sum</Function>,
364+
An aggregate function computes a single result from multiple input rows.
365+
For example, there are aggregates to compute the
366+
<Function>count</Function>, <Function>sum</Function>,
368367
<Function>avg</Function> (average), <Function>max</Function> (maximum) and
369-
<Function>min</Function> (minimum) over a set of instances, aggregates can only
370-
appear in the target list of a query and not directly in the
371-
qualification (the where clause). As an example,
368+
<Function>min</Function> (minimum) over a set of instances.
369+
</para>
370+
371+
<Para>
372+
It is important to understand the interaction between aggregates and
373+
SQL's <Command>where</Command> and <Command>having</Command> clauses.
374+
The fundamental difference between <Command>where</Command> and
375+
<Command>having</Command> is this: <Command>where</Command> selects
376+
input rows before groups and aggregates are computed (thus, it controls
377+
which rows go into the aggregate computation), whereas
378+
<Command>having</Command> selects group rows after groups and
379+
aggregates are computed. Thus, the
380+
<Command>where</Command> clause may not contain aggregate functions;
381+
it makes no sense to try to use an aggregate to determine which rows
382+
will be inputs to the aggregates. On the other hand,
383+
<Command>having</Command> clauses always contain aggregate functions.
384+
(Strictly speaking, you are allowed to write a <Command>having</Command>
385+
clause that doesn't use aggregates, but it's wasteful; the same condition
386+
could be used more efficiently at the <Command>where</Command> stage.)
387+
</para>
388+
389+
<Para>
390+
As an example, we can find the highest low-temperature reading anywhere
391+
with
372392

373393
<ProgramListing>
374394
SELECT max(temp_lo) FROM weather;
375395
</ProgramListing>
376396

377-
is allowed, while
397+
If we want to know which city (or cities) that reading occurred in,
398+
we might try
378399

379400
<ProgramListing>
380401
SELECT city FROM weather WHERE temp_lo = max(temp_lo);
381402
</ProgramListing>
382403

383-
is not. However, as is often the case the query can be restated to accomplish
384-
the intended result; here by using a <FirstTerm>subselect</FirstTerm>:
404+
but this will not work since the aggregate max() can't be used in
405+
<Command>where</Command>. However, as is often the case the query can be
406+
restated to accomplish the intended result; here by using a
407+
<FirstTerm>subselect</FirstTerm>:
385408
<ProgramListing>
386409
SELECT city FROM weather WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
387410
</ProgramListing>
411+
This is OK because the sub-select is an independent computation that
412+
computes its own aggregate separately from what's happening in the outer
413+
select.
388414
</Para>
389415

390416
<Para>
391-
Aggregates may also have <FirstTerm>group by</FirstTerm> clauses:
417+
Aggregates are also very useful in combination with
418+
<FirstTerm>group by</FirstTerm> clauses. For example, we can get the
419+
maximum low temperature observed in each city with
392420
<ProgramListing>
393421
SELECT city, max(temp_lo)
394422
FROM weather
395423
GROUP BY city;
396424
</ProgramListing>
425+
which gives us one output row per city. We can filter these grouped
426+
rows using <Command>having</Command>:
427+
<ProgramListing>
428+
SELECT city, max(temp_lo)
429+
FROM weather
430+
GROUP BY city
431+
HAVING min(temp_lo) < 0;
432+
</ProgramListing>
433+
which gives us the same results for only the cities that have some
434+
below-zero readings. Finally, if we only care about cities whose
435+
names begin with 'P', we might do
436+
<ProgramListing>
437+
SELECT city, max(temp_lo)
438+
FROM weather
439+
WHERE city like 'P%'
440+
GROUP BY city
441+
HAVING min(temp_lo) < 0;
442+
</ProgramListing>
443+
Note that we can apply the city-name restriction in
444+
<Command>where</Command>, since it needs no aggregate. This is
445+
more efficient than adding the restriction to <Command>having</Command>,
446+
because we avoid doing the grouping and aggregate calculations
447+
for all rows that fail the <Command>where</Command> check.
397448
</Para>
398449
</sect1>
399450
</Chapter>

‎doc/src/sgml/ref/select.sgml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/select.sgml,v 1.22 1999/08/06 13:50:31 thomas Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/select.sgml,v 1.23 1999/12/13 17:39:38 tgl Exp $
33
Postgres documentation
44
-->
55

@@ -202,10 +202,10 @@ SELECT [ ALL | DISTINCT [ ON <replaceable class="PARAMETER">column</replaceable>
202202

203203
<para>
204204
<command>DISTINCT</command> will eliminate all duplicate rows from the
205-
selection.
205+
result.
206206
<command>DISTINCT ON <replaceable class="PARAMETER">column</replaceable></command>
207207
will eliminate all duplicates in the specified column; this is
208-
equivalent to using
208+
similar to using
209209
<command>GROUP BY <replaceable class="PARAMETER">column</replaceable></command>.
210210
<command>ALL</command> will return all candidate rows,
211211
including duplicates.
@@ -320,11 +320,13 @@ GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...]
320320

321321
<para>
322322
GROUP BY will condense into a single row all rows that share the
323-
same values for the
324-
grouped columns; aggregates return values derived from all rows
325-
that make up the group. The value returned for an ungrouped
326-
and unaggregated column is dependent on the order in which rows
327-
happen to be read from the database.
323+
same values for the grouped columns. Aggregate functions, if any,
324+
are computed across all rows making up each group, producing a
325+
separate value for each group (whereas without GROUP BY, an
326+
aggregate produces a single value computed across all the selected
327+
rows). When GROUP BY is present, it is not valid to refer to
328+
ungrouped columns except within aggregate functions, since there
329+
would be more than one possible value to return for an ungrouped column.
328330
</para>
329331
</refsect2>
330332

@@ -354,7 +356,8 @@ HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
354356
<para>
355357
Each column referenced in
356358
<replaceable class="PARAMETER">cond_expr</replaceable> shall unambiguously
357-
reference a grouping column.
359+
reference a grouping column, unless the reference appears within an
360+
aggregate function.
358361
</para>
359362
</refsect2>
360363

‎doc/src/sgml/syntax.sgml

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,16 @@ CAST '<replaceable>string</replaceable>' AS <replaceable>type</replaceable>
642642
<member><replaceable>a_expr</replaceable> <replaceable>right_unary_operator</replaceable></member>
643643
<member><replaceable>left_unary_operator</replaceable> <replaceable>a_expr</replaceable></member>
644644
<member>parameter</member>
645-
<member>functionalexpressions</member>
646-
<member>aggregateexpressions</member>
645+
<member>functionalexpression</member>
646+
<member>aggregateexpression</member>
647647
</simplelist>
648648
</para>
649649

650650
<para>
651-
We have already discussed constants and attributes. The two kinds of
652-
operator expressions indicate respectively binary and left_unary
653-
expressions. The following sections discuss the remaining options.
651+
We have already discussed constants and attributes. The three kinds of
652+
operator expressions indicate respectively binary (infix), right-unary
653+
(suffix) and left-unary (prefix) operators. The following sections
654+
discuss the remaining options.
654655
</para>
655656

656657
<sect2>
@@ -690,7 +691,7 @@ CREATE FUNCTION dept (name)
690691
enclosed in parentheses:
691692

692693
<synopsis>
693-
<replaceable>function</replaceable> (<replaceable>a_expr</replaceable> [, <replaceable>a_expr</replaceable> )
694+
<replaceable>function</replaceable> (<replaceable>a_expr</replaceable> [, <replaceable>a_expr</replaceable>... ])
694695
</synopsis>
695696
</para>
696697

@@ -705,20 +706,40 @@ sqrt(emp.salary)
705706
</sect2>
706707

707708
<sect2>
708-
<title>AggregateExpression</title>
709+
<title>AggregateExpressions</title>
709710

710711
<para>
711-
An <firstterm>aggregate expression</firstterm>
712-
represents a simple aggregate (i.e., one that computes a single value)
713-
or an aggregate function (i.e., one that computes a set of values).
714-
The syntax is the following:
712+
An <firstterm>aggregate expression</firstterm> represents the application
713+
of an aggregate function across the rows selected by a query.
714+
An aggregate function reduces multiple inputs to a single output value,
715+
such as the sum or average of the inputs.
716+
The syntax of an aggregate expression is one of the following:
715717

716-
<synopsis>
717-
<replaceable>aggregate_name</replaceable> (<replaceable>attribute</replaceable>)
718-
</synopsis>
718+
<simplelist>
719+
<member><replaceable>aggregate_name</replaceable> (<replaceable>expression</replaceable>)</member>
720+
<member><replaceable>aggregate_name</replaceable> (DISTINCT <replaceable>expression</replaceable>)</member>
721+
<member><replaceable>aggregate_name</replaceable> ( * )</member>
722+
</simplelist>
723+
724+
where <replaceable>aggregate_name</replaceable> is a previously defined
725+
aggregate, and <replaceable>expression</replaceable> is any expression
726+
that doesn't itself contain an aggregate expression.
727+
</para>
719728

720-
where <replaceable>aggregate_name</replaceable>
721-
must be a previously defined aggregate.
729+
<para>
730+
The first form of aggregate expression invokes the aggregate across all
731+
input rows for which the given expression yields a non-null value.
732+
The second form invokes the aggregate for all distinct non-null values
733+
of the expression found in the input rows. The last form invokes the
734+
aggregate once for each input row regardless of null or non-null values;
735+
since no particular input value is specified, it is generally only useful
736+
for the count() aggregate.
737+
</para>
738+
739+
<para>
740+
For example, count(*) yields the total number of input rows;
741+
count(f1) yields the number of input rows in which f1 is non-null;
742+
count(distinct f1) yields the number of distinct non-null values of f1.
722743
</para>
723744
</sect2>
724745

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp