@@ -40,7 +40,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="parameter">expression</replac
40
40
[ GROUP BY <replaceable class="parameter">expression</replaceable> [, ...] ]
41
41
[ HAVING <replaceable class="parameter">condition</replaceable> [, ...] ]
42
42
[ WINDOW <replaceable class="parameter">window_name</replaceable> AS ( <replaceable class="parameter">window_definition</replaceable> ) [, ...] ]
43
- [ { UNION | INTERSECT | EXCEPT } [ ALL ] <replaceable class="parameter">select</replaceable> ]
43
+ [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] <replaceable class="parameter">select</replaceable> ]
44
44
[ ORDER BY <replaceable class="parameter">expression</replaceable> [ ASC | DESC | USING <replaceable class="parameter">operator</replaceable> ] [ NULLS { FIRST | LAST } ] [, ...] ]
45
45
[ LIMIT { <replaceable class="parameter">count</replaceable> | ALL } ]
46
46
[ OFFSET <replaceable class="parameter">start</replaceable> [ ROW | ROWS ] ]
@@ -106,7 +106,7 @@ TABLE { [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ] |
106
106
<listitem>
107
107
<para>
108
108
If the <literal>GROUP BY</literal> clause is specified, the
109
- output is divided into groups of rows that match on one or more
109
+ output is combined into groups of rows that match on one or more
110
110
values. If the <literal>HAVING</literal> clause is present, it
111
111
eliminates groups that do not satisfy the given condition. (See
112
112
<xref linkend="sql-groupby" endterm="sql-groupby-title"> and
@@ -118,12 +118,23 @@ TABLE { [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ] |
118
118
<para>
119
119
The actual output rows are computed using the
120
120
<command>SELECT</command> output expressions for each selected
121
- row. (See
121
+ row or row group . (See
122
122
<xref linkend="sql-select-list" endterm="sql-select-list-title">
123
123
below.)
124
124
</para>
125
125
</listitem>
126
126
127
+ <listitem>
128
+ <para>
129
+ <literal>SELECT DISTINCT</literal> eliminates duplicate rows from the
130
+ result. <literal>SELECT DISTINCT ON</literal> eliminates rows that
131
+ match on all the specified expressions. <literal>SELECT ALL</literal>
132
+ (the default) will return all candidate rows, including
133
+ duplicates. (See <xref linkend="sql-distinct"
134
+ endterm="sql-distinct-title"> below.)
135
+ </para>
136
+ </listitem>
137
+
127
138
<listitem>
128
139
<para>
129
140
Using the operators <literal>UNION</literal>,
@@ -136,7 +147,11 @@ TABLE { [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ] |
136
147
strictly in both result sets. The <literal>EXCEPT</literal>
137
148
operator returns the rows that are in the first result set but
138
149
not in the second. In all three cases, duplicate rows are
139
- eliminated unless <literal>ALL</literal> is specified. (See
150
+ eliminated unless <literal>ALL</literal> is specified. The noise
151
+ word <literal>DISTINCT</> can be added to explicitly specify
152
+ eliminating duplicate rows. Notice that <literal>DISTINCT</> is
153
+ the default behavior here, even though <literal>ALL</literal> is
154
+ the default for <command>SELECT</> itself. (See
140
155
<xref linkend="sql-union" endterm="sql-union-title">, <xref
141
156
linkend="sql-intersect" endterm="sql-intersect-title">, and
142
157
<xref linkend="sql-except" endterm="sql-except-title"> below.)
@@ -153,17 +168,6 @@ TABLE { [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ] |
153
168
</para>
154
169
</listitem>
155
170
156
- <listitem>
157
- <para>
158
- <literal>DISTINCT</literal> eliminates duplicate rows from the
159
- result. <literal>DISTINCT ON</literal> eliminates rows that
160
- match on all the specified expressions. <literal>ALL</literal>
161
- (the default) will return all candidate rows, including
162
- duplicates. (See <xref linkend="sql-distinct"
163
- endterm="sql-distinct-title"> below.)
164
- </para>
165
- </listitem>
166
-
167
171
<listitem>
168
172
<para>
169
173
If the <literal>LIMIT</literal> (or <literal>FETCH FIRST</literal>) or <literal>OFFSET</literal>
@@ -219,7 +223,7 @@ TABLE { [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ] |
219
223
subquery to reference itself by name. Such a subquery must have
220
224
the form
221
225
<synopsis>
222
- <replaceable class="parameter">non_recursive_term</replaceable> UNION [ ALL ] <replaceable class="parameter">recursive_term</replaceable>
226
+ <replaceable class="parameter">non_recursive_term</replaceable> UNION [ ALL | DISTINCT ] <replaceable class="parameter">recursive_term</replaceable>
223
227
</synopsis>
224
228
where the recursive self-reference must appear on the right-hand
225
229
side of the <literal>UNION</>. Only one recursive self-reference
@@ -755,13 +759,52 @@ UNBOUNDED FOLLOWING
755
759
</para>
756
760
</refsect2>
757
761
762
+ <refsect2 id="sql-distinct">
763
+ <title id="sql-distinct-title"><literal>DISTINCT</literal> Clause</title>
764
+
765
+ <para>
766
+ If <literal>SELECT DISTINCT</> is specified, all duplicate rows are
767
+ removed from the result set (one row is kept from each group of
768
+ duplicates). <literal>SELECT ALL</> specifies the opposite: all rows are
769
+ kept; that is the default.
770
+ </para>
771
+
772
+ <para>
773
+ <literal>SELECT DISTINCT ON ( <replaceable
774
+ class="parameter">expression</replaceable> [, ...] )</literal>
775
+ keeps only the first row of each set of rows where the given
776
+ expressions evaluate to equal. The <literal>DISTINCT ON</literal>
777
+ expressions are interpreted using the same rules as for
778
+ <literal>ORDER BY</> (see above). Note that the <quote>first
779
+ row</quote> of each set is unpredictable unless <literal>ORDER
780
+ BY</> is used to ensure that the desired row appears first. For
781
+ example:
782
+ <programlisting>
783
+ SELECT DISTINCT ON (location) location, time, report
784
+ FROM weather_reports
785
+ ORDER BY location, time DESC;
786
+ </programlisting>
787
+ retrieves the most recent weather report for each location. But
788
+ if we had not used <literal>ORDER BY</> to force descending order
789
+ of time values for each location, we'd have gotten a report from
790
+ an unpredictable time for each location.
791
+ </para>
792
+
793
+ <para>
794
+ The <literal>DISTINCT ON</> expression(s) must match the leftmost
795
+ <literal>ORDER BY</> expression(s). The <literal>ORDER BY</> clause
796
+ will normally contain additional expression(s) that determine the
797
+ desired precedence of rows within each <literal>DISTINCT ON</> group.
798
+ </para>
799
+ </refsect2>
800
+
758
801
<refsect2 id="SQL-UNION">
759
802
<title id="sql-union-title"><literal>UNION</literal> Clause</title>
760
803
761
804
<para>
762
805
The <literal>UNION</literal> clause has this general form:
763
806
<synopsis>
764
- <replaceable class="parameter">select_statement</replaceable> UNION [ ALL ] <replaceable class="parameter">select_statement</replaceable>
807
+ <replaceable class="parameter">select_statement</replaceable> UNION [ ALL | DISTINCT ] <replaceable class="parameter">select_statement</replaceable>
765
808
</synopsis>
766
809
<replaceable class="parameter">select_statement</replaceable> is
767
810
any <command>SELECT</command> statement without an <literal>ORDER
@@ -791,6 +834,8 @@ UNBOUNDED FOLLOWING
791
834
<literal>ALL</> prevents elimination of duplicates. (Therefore,
792
835
<literal>UNION ALL</> is usually significantly quicker than
793
836
<literal>UNION</>; use <literal>ALL</> when you can.)
837
+ <literal>DISTINCT</> can be written to explicitly specify the
838
+ default behavior of eliminating duplicate rows.
794
839
</para>
795
840
796
841
<para>
@@ -812,7 +857,7 @@ UNBOUNDED FOLLOWING
812
857
<para>
813
858
The <literal>INTERSECT</literal> clause has this general form:
814
859
<synopsis>
815
- <replaceable class="parameter">select_statement</replaceable> INTERSECT [ ALL ] <replaceable class="parameter">select_statement</replaceable>
860
+ <replaceable class="parameter">select_statement</replaceable> INTERSECT [ ALL | DISTINCT ] <replaceable class="parameter">select_statement</replaceable>
816
861
</synopsis>
817
862
<replaceable class="parameter">select_statement</replaceable> is
818
863
any <command>SELECT</command> statement without an <literal>ORDER
@@ -833,6 +878,8 @@ UNBOUNDED FOLLOWING
833
878
With <literal>ALL</>, a row that has <replaceable>m</> duplicates in the
834
879
left table and <replaceable>n</> duplicates in the right table will appear
835
880
min(<replaceable>m</>,<replaceable>n</>) times in the result set.
881
+ <literal>DISTINCT</> can be written to explicitly specify the
882
+ default behavior of eliminating duplicate rows.
836
883
</para>
837
884
838
885
<para>
@@ -858,7 +905,7 @@ UNBOUNDED FOLLOWING
858
905
<para>
859
906
The <literal>EXCEPT</literal> clause has this general form:
860
907
<synopsis>
861
- <replaceable class="parameter">select_statement</replaceable> EXCEPT [ ALL ] <replaceable class="parameter">select_statement</replaceable>
908
+ <replaceable class="parameter">select_statement</replaceable> EXCEPT [ ALL | DISTINCT ] <replaceable class="parameter">select_statement</replaceable>
862
909
</synopsis>
863
910
<replaceable class="parameter">select_statement</replaceable> is
864
911
any <command>SELECT</command> statement without an <literal>ORDER
@@ -878,6 +925,8 @@ UNBOUNDED FOLLOWING
878
925
With <literal>ALL</>, a row that has <replaceable>m</> duplicates in the
879
926
left table and <replaceable>n</> duplicates in the right table will appear
880
927
max(<replaceable>m</>-<replaceable>n</>,0) times in the result set.
928
+ <literal>DISTINCT</> can be written to explicitly specify the
929
+ default behavior of eliminating duplicate rows.
881
930
</para>
882
931
883
932
<para>
@@ -987,45 +1036,6 @@ SELECT name FROM distributors ORDER BY code;
987
1036
</para>
988
1037
</refsect2>
989
1038
990
- <refsect2 id="sql-distinct">
991
- <title id="sql-distinct-title"><literal>DISTINCT</literal> Clause</title>
992
-
993
- <para>
994
- If <literal>DISTINCT</> is specified, all duplicate rows are
995
- removed from the result set (one row is kept from each group of
996
- duplicates). <literal>ALL</> specifies the opposite: all rows are
997
- kept; that is the default.
998
- </para>
999
-
1000
- <para>
1001
- <literal>DISTINCT ON ( <replaceable
1002
- class="parameter">expression</replaceable> [, ...] )</literal>
1003
- keeps only the first row of each set of rows where the given
1004
- expressions evaluate to equal. The <literal>DISTINCT ON</literal>
1005
- expressions are interpreted using the same rules as for
1006
- <literal>ORDER BY</> (see above). Note that the <quote>first
1007
- row</quote> of each set is unpredictable unless <literal>ORDER
1008
- BY</> is used to ensure that the desired row appears first. For
1009
- example:
1010
- <programlisting>
1011
- SELECT DISTINCT ON (location) location, time, report
1012
- FROM weather_reports
1013
- ORDER BY location, time DESC;
1014
- </programlisting>
1015
- retrieves the most recent weather report for each location. But
1016
- if we had not used <literal>ORDER BY</> to force descending order
1017
- of time values for each location, we'd have gotten a report from
1018
- an unpredictable time for each location.
1019
- </para>
1020
-
1021
- <para>
1022
- The <literal>DISTINCT ON</> expression(s) must match the leftmost
1023
- <literal>ORDER BY</> expression(s). The <literal>ORDER BY</> clause
1024
- will normally contain additional expression(s) that determine the
1025
- desired precedence of rows within each <literal>DISTINCT ON</> group.
1026
- </para>
1027
- </refsect2>
1028
-
1029
1039
<refsect2 id="SQL-LIMIT">
1030
1040
<title id="sql-limit-title"><literal>LIMIT</literal> Clause</title>
1031
1041
0 commit comments