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

Commitcfb3a42

Browse files
committed
Rewrite discussion of ORDER BY to emphasize the SQL99 expression case
instead of the SQL92 output-column-ID case.
1 parent66f5264 commitcfb3a42

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

‎doc/src/sgml/queries.sgml

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/queries.sgml,v 1.38 2006/10/23 18:10:31 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/queries.sgml,v 1.39 2006/10/24 02:24:27 tgl Exp $ -->
22

33
<chapter id="queries">
44
<title>Queries</title>
@@ -514,8 +514,8 @@ SELECT * FROM my_table AS m WHERE my_table.a &gt; 5;
514514
is not valid according to the SQL standard. In
515515
<productname>PostgreSQL</productname> this will draw an error if the
516516
<xref linkend="guc-add-missing-from"> configuration variable is
517-
<literal>off</>. If it is <literal>on</>, an implicit table reference
518-
will be added to the
517+
<literal>off</> (as it is by default). If it is <literal>on</>,
518+
an implicit table referencewill be added to the
519519
<literal>FROM</literal> clause, so the query is processed as if
520520
it were written as
521521
<programlisting>
@@ -1224,38 +1224,17 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
12241224
<synopsis>
12251225
SELECT <replaceable>select_list</replaceable>
12261226
FROM <replaceable>table_expression</replaceable>
1227-
ORDER BY <replaceable>column1</replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>column2</replaceable> <optional>ASC | DESC</optional> ...</optional>
1227+
ORDER BY <replaceable>sort_expression1</replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>sort_expression2</replaceable> <optional>ASC | DESC</optional> ...</optional>
12281228
</synopsis>
1229-
<replaceable>column1</replaceable>, etc., refer to select list
1230-
columns. These can be either the output name of a column (see
1231-
<xref linkend="queries-column-labels">) or the number of a column. Some
1232-
examples:
1229+
The sort expression(s) can be any expression that would be valid in the
1230+
query's select list. An example is
12331231
<programlisting>
1234-
SELECT a, b FROM table1 ORDER BY a;
1235-
SELECT a + b AS sum, c FROM table1 ORDER BY sum;
1236-
SELECT a, sum(b) FROM table1 GROUP BY a ORDER BY 1;
1237-
</programlisting>
1238-
</para>
1239-
1240-
<para>
1241-
As an extension to the SQL standard, <productname>PostgreSQL</productname> also allows ordering
1242-
by arbitrary expressions:
1243-
<programlisting>
1244-
SELECT a, b FROM table1 ORDER BY a + b;
1245-
</programlisting>
1246-
References to column names of the <literal>FROM</> clause that are
1247-
not present in the select list are also allowed:
1248-
<programlisting>
1249-
SELECT a FROM table1 ORDER BY b;
1232+
SELECT a, b FROM table1 ORDER BY a + b, c;
12501233
</programlisting>
1251-
But these extensions do not work in queries involving
1252-
<literal>UNION</>, <literal>INTERSECT</>, or <literal>EXCEPT</>,
1253-
and are not portable to other SQL databases.
1254-
</para>
1255-
1256-
<para>
1257-
Each column specification may be followed by an optional
1258-
<literal>ASC</> or <literal>DESC</> to set the sort direction to
1234+
When more than one expression is specified,
1235+
the later values are used to sort rows that are equal according to the
1236+
earlier values. Each expression may be followed by an optional
1237+
<literal>ASC</> or <literal>DESC</> keyword to set the sort direction to
12591238
ascending or descending. <literal>ASC</> order is the default.
12601239
Ascending order puts smaller values first, where
12611240
<quote>smaller</quote> is defined in terms of the
@@ -1264,7 +1243,7 @@ SELECT a FROM table1 ORDER BY b;
12641243
<footnote>
12651244
<para>
12661245
Actually, <productname>PostgreSQL</> uses the <firstterm>default B-tree
1267-
operator class</> for thecolumn's data type to determine the sort
1246+
operator class</> for theexpression's data type to determine the sort
12681247
ordering for <literal>ASC</> and <literal>DESC</>. Conventionally,
12691248
data types will be set up so that the <literal>&lt;</literal> and
12701249
<literal>&gt;</literal> operators correspond to this sort ordering,
@@ -1275,9 +1254,32 @@ SELECT a FROM table1 ORDER BY b;
12751254
</para>
12761255

12771256
<para>
1278-
If more than one sort column is specified, the later entries are
1279-
used to sort rows that are equal under the order imposed by the
1280-
earlier sort columns.
1257+
For backwards compatibility with the SQL92 version of the standard,
1258+
a <replaceable>sort_expression</> can instead be the name or number
1259+
of an output column, as in
1260+
<programlisting>
1261+
SELECT a + b AS sum, c FROM table1 ORDER BY sum;
1262+
SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;
1263+
</programlisting>
1264+
both of which sort by the first output column. Note that an output
1265+
column name has to stand alone, it's not allowed as part of an expression
1266+
&mdash; for example, this is <emphasis>not</> correct:
1267+
<programlisting>
1268+
SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong
1269+
</programlisting>
1270+
This restriction is made to reduce ambiguity. There is still
1271+
ambiguity if an <literal>ORDER BY</> item is a simple name that
1272+
could match either an output column name or a column from the table
1273+
expression. The output column is used in such cases. This would
1274+
only cause confusion if you use <literal>AS</> to rename an output
1275+
column to match some other table column's name.
1276+
</para>
1277+
1278+
<para>
1279+
<literal>ORDER BY</> can be applied to the result of a
1280+
<literal>UNION</>, <literal>INTERSECT</>, or <literal>EXCEPT</>
1281+
combination, but in this case it is only permitted to sort by
1282+
output column names or numbers, not by expressions.
12811283
</para>
12821284
</sect1>
12831285

@@ -1299,6 +1301,7 @@ SELECT a FROM table1 ORDER BY b;
12991301
<synopsis>
13001302
SELECT <replaceable>select_list</replaceable>
13011303
FROM <replaceable>table_expression</replaceable>
1304+
<optional> ORDER BY <replaceable>sort_expression1</replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>sort_expression2</replaceable> <optional>ASC | DESC</optional> ...</optional> </optional>
13021305
<optional> LIMIT { <replaceable>number</replaceable> | ALL } </optional> <optional> OFFSET <replaceable>number</replaceable> </optional>
13031306
</synopsis>
13041307
</para>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp