Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abuhujair Javed
Postgres FD Implementation
Commits
cfb3a428
Commit
cfb3a428
authored
Oct 24, 2006
by
Tom Lane
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrite discussion of ORDER BY to emphasize the SQL99 expression case
instead of the SQL92 output-column-ID case.
parent
66f5264a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
36 deletions
+39
-36
doc/src/sgml/queries.sgml
doc/src/sgml/queries.sgml
+39
-36
No files found.
doc/src/sgml/queries.sgml
View file @
cfb3a428
<!-- $PostgreSQL: pgsql/doc/src/sgml/queries.sgml,v 1.3
8 2006/10/23 18:10:31 petere
Exp $ -->
<!-- $PostgreSQL: pgsql/doc/src/sgml/queries.sgml,v 1.3
9 2006/10/24 02:24:27 tgl
Exp $ -->
<chapter id="queries">
<chapter id="queries">
<title>Queries</title>
<title>Queries</title>
...
@@ -514,8 +514,8 @@ SELECT * FROM my_table AS m WHERE my_table.a > 5;
...
@@ -514,8 +514,8 @@ SELECT * FROM my_table AS m WHERE my_table.a > 5;
is not valid according to the SQL standard. In
is not valid according to the SQL standard. In
<productname>PostgreSQL</productname> this will draw an error if the
<productname>PostgreSQL</productname> this will draw an error if the
<xref linkend="guc-add-missing-from"> configuration variable is
<xref linkend="guc-add-missing-from"> configuration variable is
<literal>off</>
. If it is <literal>on</>, an implicit table reference
<literal>off</>
(as it is by default). If it is <literal>on</>,
will be added to the
an implicit table reference
will be added to the
<literal>FROM</literal> clause, so the query is processed as if
<literal>FROM</literal> clause, so the query is processed as if
it were written as
it were written as
<programlisting>
<programlisting>
...
@@ -1224,38 +1224,17 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
...
@@ -1224,38 +1224,17 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
<synopsis>
<synopsis>
SELECT <replaceable>select_list</replaceable>
SELECT <replaceable>select_list</replaceable>
FROM <replaceable>table_expression</replaceable>
FROM <replaceable>table_expression</replaceable>
ORDER BY <replaceable>
column1</replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>colum
n2</replaceable> <optional>ASC | DESC</optional> ...</optional>
ORDER BY <replaceable>
sort_expression1</replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>sort_expressio
n2</replaceable> <optional>ASC | DESC</optional> ...</optional>
</synopsis>
</synopsis>
<replaceable>column1</replaceable>, etc., refer to select list
The sort expression(s) can be any expression that would be valid in the
columns. These can be either the output name of a column (see
query's select list. An example is
<xref linkend="queries-column-labels">) or the number of a column. Some
examples:
<programlisting>
<programlisting>
SELECT a, b FROM table1 ORDER BY a;
SELECT a, b FROM table1 ORDER BY a + b, c;
SELECT a + b AS sum, c FROM table1 ORDER BY sum;
SELECT a, sum(b) FROM table1 GROUP BY a ORDER BY 1;
</programlisting>
</para>
<para>
As an extension to the SQL standard, <productname>PostgreSQL</productname> also allows ordering
by arbitrary expressions:
<programlisting>
SELECT a, b FROM table1 ORDER BY a + b;
</programlisting>
References to column names of the <literal>FROM</> clause that are
not present in the select list are also allowed:
<programlisting>
SELECT a FROM table1 ORDER BY b;
</programlisting>
</programlisting>
But these extensions do not work in queries involving
When more than one expression is specified,
<literal>UNION</>, <literal>INTERSECT</>, or <literal>EXCEPT</>,
the later values are used to sort rows that are equal according to the
and are not portable to other SQL databases.
earlier values. Each expression may be followed by an optional
</para>
<literal>ASC</> or <literal>DESC</> keyword to set the sort direction to
<para>
Each column specification may be followed by an optional
<literal>ASC</> or <literal>DESC</> to set the sort direction to
ascending or descending. <literal>ASC</> order is the default.
ascending or descending. <literal>ASC</> order is the default.
Ascending order puts smaller values first, where
Ascending order puts smaller values first, where
<quote>smaller</quote> is defined in terms of the
<quote>smaller</quote> is defined in terms of the
...
@@ -1264,7 +1243,7 @@ SELECT a FROM table1 ORDER BY b;
...
@@ -1264,7 +1243,7 @@ SELECT a FROM table1 ORDER BY b;
<footnote>
<footnote>
<para>
<para>
Actually, <productname>PostgreSQL</> uses the <firstterm>default B-tree
Actually, <productname>PostgreSQL</> uses the <firstterm>default B-tree
operator class</> for the
colum
n's data type to determine the sort
operator class</> for the
expressio
n's data type to determine the sort
ordering for <literal>ASC</> and <literal>DESC</>. Conventionally,
ordering for <literal>ASC</> and <literal>DESC</>. Conventionally,
data types will be set up so that the <literal><</literal> and
data types will be set up so that the <literal><</literal> and
<literal>></literal> operators correspond to this sort ordering,
<literal>></literal> operators correspond to this sort ordering,
...
@@ -1275,9 +1254,32 @@ SELECT a FROM table1 ORDER BY b;
...
@@ -1275,9 +1254,32 @@ SELECT a FROM table1 ORDER BY b;
</para>
</para>
<para>
<para>
If more than one sort column is specified, the later entries are
For backwards compatibility with the SQL92 version of the standard,
used to sort rows that are equal under the order imposed by the
a <replaceable>sort_expression</> can instead be the name or number
earlier sort columns.
of an output column, as in
<programlisting>
SELECT a + b AS sum, c FROM table1 ORDER BY sum;
SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;
</programlisting>
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
— for example, this is <emphasis>not</> correct:
<programlisting>
SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong
</programlisting>
This restriction is made to reduce ambiguity. There is still
ambiguity if an <literal>ORDER BY</> item is a simple name that
could match either an output column name or a column from the table
expression. The output column is used in such cases. This would
only cause confusion if you use <literal>AS</> to rename an output
column to match some other table column's name.
</para>
<para>
<literal>ORDER BY</> can be applied to the result of a
<literal>UNION</>, <literal>INTERSECT</>, or <literal>EXCEPT</>
combination, but in this case it is only permitted to sort by
output column names or numbers, not by expressions.
</para>
</para>
</sect1>
</sect1>
...
@@ -1299,6 +1301,7 @@ SELECT a FROM table1 ORDER BY b;
...
@@ -1299,6 +1301,7 @@ SELECT a FROM table1 ORDER BY b;
<synopsis>
<synopsis>
SELECT <replaceable>select_list</replaceable>
SELECT <replaceable>select_list</replaceable>
FROM <replaceable>table_expression</replaceable>
FROM <replaceable>table_expression</replaceable>
<optional> ORDER BY <replaceable>sort_expression1</replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>sort_expression2</replaceable> <optional>ASC | DESC</optional> ...</optional> </optional>
<optional> LIMIT { <replaceable>number</replaceable> | ALL } </optional> <optional> OFFSET <replaceable>number</replaceable> </optional>
<optional> LIMIT { <replaceable>number</replaceable> | ALL } </optional> <optional> OFFSET <replaceable>number</replaceable> </optional>
</synopsis>
</synopsis>
</para>
</para>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment