If a table has been grouped using a <literal>GROUP BY</literal>
If a table has been grouped using <literal>GROUP BY</literal>,
clause, but then only certain groups are of interest, the
but only certain groups are of interest, the
<literal>HAVING</literal> clause can be used, much like a
<literal>HAVING</literal> clause can be used, much like a
<literal>WHERE</> clause, to eliminate groups from a grouped
<literal>WHERE</> clause, to eliminate groups from the result.
table. The syntax is:
The syntax is:
<synopsis>
<synopsis>
SELECT <replaceable>select_list</replaceable> FROM ... <optional>WHERE ...</optional> GROUP BY ... HAVING <replaceable>boolean_expression</replaceable>
SELECT <replaceable>select_list</replaceable> FROM ... <optional>WHERE ...</optional> GROUP BY ... HAVING <replaceable>boolean_expression</replaceable>
</synopsis>
</synopsis>
@ -1068,8 +1088,7 @@ SELECT tbl1.*, tbl2.a FROM ...
the row's values substituted for any column references. But the
the row's values substituted for any column references. But the
expressions in the select list do not have to reference any
expressions in the select list do not have to reference any
columns in the table expression of the <literal>FROM</> clause;
columns in the table expression of the <literal>FROM</> clause;
they could be constant arithmetic expressions as well, for
they can be constant arithmetic expressions as well.
instance.
</para>
</para>
</sect2>
</sect2>
@ -1083,9 +1102,8 @@ SELECT tbl1.*, tbl2.a FROM ...
<para>
<para>
The entries in the select list can be assigned names for further
The entries in the select list can be assigned names for further
processing. The <quote>further processing</quote> in this case is
processing, perhaps for reference in an <literal>ORDER BY</> clause
an optional sort specification and the client application (e.g.,
or for display by the client application. For example:
column headers for display). For example:
<programlisting>
<programlisting>
SELECT a AS value, b + c AS sum FROM ...
SELECT a AS value, b + c AS sum FROM ...
</programlisting>
</programlisting>
@ -1122,8 +1140,8 @@ SELECT a "value", b + c AS sum FROM ...
<para>
<para>
The naming of output columns here is different from that done in
The naming of output columns here is different from that done in
the <literal>FROM</> clause (see <xref
the <literal>FROM</> clause (see <xref
linkend="queries-table-aliases">). This pipeline will in fact
linkend="queries-table-aliases">). It is possible
allow you to rename the same column twice, but the name chosen in
to rename the same column twice, but the name used in
the select list is the one that will be passed on.
the select list is the one that will be passed on.
</para>
</para>
</note>
</note>
@ -1181,7 +1199,7 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
The <literal>DISTINCT ON</> clause is not part of the SQL standard
The <literal>DISTINCT ON</> clause is not part of the SQL standard
and is sometimes considered bad style because of the potentially
and is sometimes considered bad style because of the potentially
indeterminate nature of its results. With judicious use of
indeterminate nature of its results. With judicious use of
<literal>GROUP BY</> and subqueries in <literal>FROM</> the
<literal>GROUP BY</> and subqueries in <literal>FROM</>, this
construct can be avoided, but it is often the most convenient
construct can be avoided, but it is often the most convenient
alternative.
alternative.
</para>
</para>
@ -1229,7 +1247,7 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
<synopsis>
<synopsis>
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> UNION <replaceable>query3</replaceable>
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> UNION <replaceable>query3</replaceable>
</synopsis>
</synopsis>
which really says
which is executed as:
<synopsis>
<synopsis>
(<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable>) UNION <replaceable>query3</replaceable>
(<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable>) UNION <replaceable>query3</replaceable>
</synopsis>
</synopsis>
@ -1328,9 +1346,9 @@ SELECT a, b FROM table1 ORDER BY a + b, c;
<para>
<para>
The <literal>NULLS FIRST</> and <literal>NULLS LAST</> options can be
The <literal>NULLS FIRST</> and <literal>NULLS LAST</> options can be
used to determine whether nulls appear before or after non-null values
used to determine whether nulls appear before or after non-null values
in the sort ordering. By default, null values sort as if larger than any
in the sort ordering. The default behavior is for null values sort as
non-null value; that is, <literal>NULLS FIRST</> is the default for
if larger than all non-null values (<literal>NULLS FIRST</>), except
<literal>DESC</> order, and <literal>NULLS LAST</> otherwise.
in <literal>DESC</> ordering, where <literal>NULLS LAST</> is the default.
</para>
</para>
<para>
<para>
@ -1341,15 +1359,14 @@ SELECT a, b FROM table1 ORDER BY a + b, c;
</para>
</para>
<para>
<para>
For backwards compatibility with the SQL92 version of the standard,
A <replaceable>sort_expression</> can also be the column label or number
a <replaceable>sort_expression</> can instead be the name or number
of an output column, as in:
of an output column, as in:
<programlisting>
<programlisting>
SELECT a + b AS sum, c FROM table1 ORDER BY sum;
SELECT a + b AS sum, c FROM table1 ORDER BY sum;
SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;
SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;
</programlisting>
</programlisting>
both of which sort by the first output column. Note that an output
both of which sort by the first output column. Note that an output
column name has to stand alone, it's not allowed as part of an expression
column name has to stand alone, e.g., it cannot be used in an expression
— for example, this is <emphasis>not</> correct:
— for example, this is <emphasis>not</> correct:
<programlisting>
<programlisting>
SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong
SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong
</optional> OUT <replaceable class="PARAMETER">word</replaceable> <type>text</>, OUT <replaceable class="PARAMETER">ndoc</replaceable> <type>integer</>,
OUT <replaceable class="PARAMETER">nentry</replaceable> <type>integer</>) returns <type>setof record</>
</synopsis>
</synopsis>
<para>
<para>
<replaceable>sqlquery</replaceable> is a text value containing a SQL
<replaceable>sqlquery</replaceable> is a text value containing an SQL
query which must return a single <type>tsvector</type> column.
query which must return a single <type>tsvector</type> column.
<function>ts_stat</> executes the query and returns statistics about
<function>ts_stat</> executes the query and returns statistics about
each distinct lexeme (word) contained in the <type>tsvector</type>
each distinct lexeme (word) contained in the <type>tsvector</type>
@ -1930,7 +1940,7 @@ LIMIT 10;
only the basic ASCII letters are reported as a separate token type,
only the basic ASCII letters are reported as a separate token type,
since it is sometimes useful to distinguish them. In most European
since it is sometimes useful to distinguish them. In most European
languages, token types <literal>word</> and <literal>asciiword</>
languages, token types <literal>word</> and <literal>asciiword</>
should always be treated alike.
should be treated alike.
</para>
</para>
</note>
</note>
@ -2077,7 +2087,7 @@ SELECT alias, description, token FROM ts_debug('http://example.com/stuff/index.h
by the parser, each dictionary in the list is consulted in turn,
by the parser, each dictionary in the list is consulted in turn,
until some dictionary recognizes it as a known word. If it is identified
until some dictionary recognizes it as a known word. If it is identified
as a stop word, or if no dictionary recognizes the token, it will be
as a stop word, or if no dictionary recognizes the token, it will be
discarded and not indexed or searched for.
discarded and not indexed or searched.
The general rule for configuring a list of dictionaries
The general rule for configuring a list of dictionaries
is to place first the most narrow, most specific dictionary, then the more
is to place first the most narrow, most specific dictionary, then the more
general dictionaries, finishing with a very general dictionary, like
general dictionaries, finishing with a very general dictionary, like
@ -2268,7 +2278,8 @@ CREATE TEXT SEARCH DICTIONARY my_synonym (
);
);
ALTER TEXT SEARCH CONFIGURATION english
ALTER TEXT SEARCH CONFIGURATION english
ALTER MAPPING FOR asciiword WITH my_synonym, english_stem;