Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1763a7c

Browse files
committed
Tweak GROUP BY so that it will still accept result-column names, but only
after trying to resolve the item as an input-column name. This allows us to be compliant with the SQL92 spec for queries that fall within the spec, while still accepting the same out-of-spec queries as 6.5 did. You'll only lose if there is an output column name that is the same as an input column name, but doesn't refer to the same value. 7.0 will interpret such a GROUP BY spec differently than 6.5 did. No way around that, because 6.5 was clearly not spec compliant.
1 parent 8e07905 commit 1763a7c

File tree

4 files changed

+139
-76
lines changed

4 files changed

+139
-76
lines changed

doc/src/sgml/ref/select.sgml

Lines changed: 71 additions & 38 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.25 2000/02/21 01:13:52 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/select.sgml,v 1.26 2000/03/15 23:31:19 tgl Exp $
33
Postgres documentation
44
-->
55

@@ -37,7 +37,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
3737

3838
<refsect2 id="R2-SQL-SELECT-1">
3939
<refsect2info>
40-
<date>1998-09-24</date>
40+
<date>2000-03-15</date>
4141
</refsect2info>
4242
<title>
4343
Inputs
@@ -59,10 +59,12 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
5959
<listitem>
6060
<para>
6161
Specifies another name for a column or an expression using
62-
the AS clause. This name is primarily used to label the output
63-
column. The <replaceable class="PARAMETER">name</replaceable>
64-
cannot be used in the WHERE, GROUP BY, or HAVING clauses.
65-
It can, however, be referenced in ORDER BY clauses.
62+
the AS clause. This name is primarily used to label the column
63+
for display. It can also be used to refer to the column's value in
64+
ORDER BY and GROUP BY clauses. But the
65+
<replaceable class="PARAMETER">name</replaceable>
66+
cannot be used in the WHERE or HAVING clauses; write out the
67+
expression instead.
6668
</para>
6769
</listitem>
6870
</varlistentry>
@@ -72,7 +74,8 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
7274
<term>TEMP</term>
7375
<listitem>
7476
<para>
75-
The table is created unique to this session, and is
77+
If TEMPORARY or TEMP is specified,
78+
the table is created unique to this session, and is
7679
automatically dropped on session exit.
7780
</para>
7881
</listitem>
@@ -83,10 +86,10 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
8386
<listitem>
8487
<para>
8588
If the INTO TABLE clause is specified, the result of the
86-
query will be stored in another table with the indicated
89+
query will be stored in a new table with the indicated
8790
name.
8891
The target table (<replaceable class="PARAMETER">new_table</replaceable>) will
89-
be created automatically and should not exist before this command.
92+
be created automatically and must not exist before this command.
9093
Refer to <command>SELECT INTO</command> for more information.
9194

9295
<note>
@@ -143,7 +146,8 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
143146
<term><replaceable class="PARAMETER">select</replaceable></term>
144147
<listitem>
145148
<para>
146-
A select statement with all features except the ORDER BY clause.
149+
A select statement with all features except the ORDER BY and
150+
LIMIT clauses.
147151
</para>
148152
</listitem>
149153
</varlistentry>
@@ -188,7 +192,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
188192

189193
<refsect1 id="R1-SQL-SELECT-1">
190194
<refsect1info>
191-
<date>1998-09-24</date>
195+
<date>2000-03-15</date>
192196
</refsect1info>
193197
<title>
194198
Description
@@ -210,7 +214,9 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
210214
<para>
211215
<command>DISTINCT ON</command> eliminates rows that match on all the
212216
specified expressions, keeping only the first row of each set of
213-
duplicates. Note that "the first row" of each set is unpredictable
217+
duplicates. The DISTINCT ON expressions are interpreted using the
218+
same rules as for ORDER BY items; see below.
219+
Note that "the first row" of each set is unpredictable
214220
unless <command>ORDER BY</command> is used to ensure that the desired
215221
row appears first. For example,
216222
<programlisting>
@@ -226,21 +232,20 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
226232

227233
<para>
228234
The GROUP BY clause allows a user to divide a table
229-
conceptually into groups.
235+
into groups of rows that match on one or more values.
230236
(See <xref linkend="sql-groupby" endterm="sql-groupby-title">.)
231237
</para>
232238

233239
<para>
234-
The HAVING clause specifies a grouped table derived by the
235-
elimination of groups from the result of the previously
236-
specified clause.
240+
The HAVING clause allows selection of only those groups of rows
241+
meeting the specified condition.
237242
(See <xref linkend="sql-having" endterm="sql-having-title">.)
238243
</para>
239244

240245
<para>
241-
The ORDER BY clause allows a user to specify that he/she
242-
wishes the rows sorted according to the ASCending or
243-
DESCending mode operator.
246+
The ORDER BY clause causes the returned rows to be sorted in a specified
247+
order. If ORDER BY is not given, the rows are returned in whatever order
248+
the system finds cheapest to produce.
244249
(See <xref linkend="sql-orderby-title" endterm="sql-orderby-title">.)
245250
</para>
246251

@@ -279,7 +284,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
279284

280285
<refsect2 id="SQL-WHERE">
281286
<refsect2info>
282-
<date>1998-09-24</date>
287+
<date>2000-03-15</date>
283288
</refsect2info>
284289
<title id="sql-where-title">
285290
WHERE Clause
@@ -312,15 +317,14 @@ WHERE <replaceable class="PARAMETER">boolean_expr</replaceable>
312317
locally-defined operator,
313318
and <replaceable class="PARAMETER">log_op</replaceable> can be one
314319
of: AND, OR, NOT.
315-
The comparison returns either TRUE or FALSE and all
316-
instances will be discarded
317-
if the expression evaluates to FALSE.
320+
SELECT will ignore all rows for which the WHERE condition does not return
321+
TRUE.
318322
</para>
319323
</refsect2>
320324

321325
<refsect2 id="SQL-GROUPBY">
322326
<refsect2info>
323-
<date>1998-09-24</date>
327+
<date>2000-03-15</date>
324328
</refsect2info>
325329
<title id="sql-groupby-title">
326330
GROUP BY Clause
@@ -334,20 +338,28 @@ GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...]
334338
</para>
335339

336340
<para>
337-
GROUP BY will condense into a single row all rows that share the
341+
GROUP BY will condense into a single row all selected rows that share the
338342
same values for the grouped columns. Aggregate functions, if any,
339343
are computed across all rows making up each group, producing a
340344
separate value for each group (whereas without GROUP BY, an
341345
aggregate produces a single value computed across all the selected
342-
rows). When GROUP BY is present, it is not valid to refer to
346+
rows). When GROUP BY is present, it is not valid for the SELECT
347+
output expression(s) to refer to
343348
ungrouped columns except within aggregate functions, since there
344349
would be more than one possible value to return for an ungrouped column.
345350
</para>
351+
352+
<para>
353+
An item in GROUP BY can also be the name or ordinal number of an output
354+
column (SELECT expression), or it can be an arbitrary expression formed
355+
from input-column values. In case of ambiguity, a GROUP BY name will
356+
be interpreted as an input-column name rather than an output column name.
357+
</para>
346358
</refsect2>
347359

348360
<refsect2 id="SQL-HAVING">
349361
<refsect2info>
350-
<date>1998-09-24</date>
362+
<date>2000-03-15</date>
351363
</refsect2info>
352364
<title id="sql-having-title">
353365
HAVING Clause
@@ -365,8 +377,12 @@ HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
365377

366378
<para>
367379
HAVING specifies a grouped table derived by the elimination
368-
of groups from the result of the previously specified clause
369-
that do not meet the <replaceable class="PARAMETER">cond_expr</replaceable>.</para>
380+
of group rows that do not satisfy the
381+
<replaceable class="PARAMETER">cond_expr</replaceable>.
382+
HAVING is different from WHERE:
383+
WHERE filters individual rows before application of GROUP BY,
384+
while HAVING filters group rows created by GROUP BY.
385+
</para>
370386

371387
<para>
372388
Each column referenced in
@@ -378,7 +394,7 @@ HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
378394

379395
<refsect2 id="SQL-ORDERBY">
380396
<refsect2info>
381-
<date>1998-09-24</date>
397+
<date>2000-03-15</date>
382398
</refsect2info>
383399
<title id="sql-orderby-title">
384400
ORDER BY Clause
@@ -389,15 +405,15 @@ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, .
389405
</synopsis></para>
390406

391407
<para>
392-
<replaceable class="PARAMETER">column</replaceable> can be either a column
393-
name or an ordinal number.
408+
<replaceable class="PARAMETER">column</replaceable> can be either a
409+
result column name or an ordinal number.
394410
</para>
395411
<para>
396412
The ordinal numbers refers to the ordinal (left-to-right) position
397-
of the column. This feature makes it possible to define an ordering
413+
of the result column. This feature makes it possible to define an ordering
398414
on the basis of a column that does not have a proper name.
399415
This is never absolutely necessary because it is always possible
400-
to assign a name to a calculated column using the AS clause, e.g.:
416+
to assign a name to a result column using the AS clause, e.g.:
401417
<programlisting>
402418
SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
403419
</programlisting></para>
@@ -410,6 +426,11 @@ SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
410426
<programlisting>
411427
SELECT name FROM distributors ORDER BY code;
412428
</programlisting>
429+
Note that if an ORDER BY item is a simple name that matches both
430+
a result column name and an input column name, ORDER BY will interpret
431+
it as the result column name. This is the opposite of the choice that
432+
GROUP BY will make in the same situation. This inconsistency is
433+
mandated by the SQL92 standard.
413434
</para>
414435

415436
<para>
@@ -436,7 +457,7 @@ SELECT name FROM distributors ORDER BY code;
436457

437458
where
438459
<replaceable class="PARAMETER">table_query</replaceable>
439-
specifies any select expression without an ORDER BY clause.
460+
specifies any select expression without an ORDER BY or LIMIT clause.
440461
</para>
441462

442463
<para>
@@ -476,7 +497,7 @@ SELECT name FROM distributors ORDER BY code;
476497

477498
where
478499
<replaceable class="PARAMETER">table_query</replaceable>
479-
specifies any select expression without an ORDER BY clause.
500+
specifies any select expression without an ORDER BY or LIMIT clause.
480501
</para>
481502

482503
<para>
@@ -507,7 +528,7 @@ SELECT name FROM distributors ORDER BY code;
507528

508529
where
509530
<replaceable class="PARAMETER">table_query</replaceable>
510-
specifies any select expression without an ORDER BY clause.
531+
specifies any select expression without an ORDER BY or LIMIT clause.
511532
</para>
512533

513534
<para>
@@ -560,7 +581,7 @@ SELECT name FROM distributors ORDER BY code;
560581
</para>
561582

562583
<para>
563-
As of Postgres 7.0, the
584+
As of PostgreSQL 7.0, the
564585
query optimizer takes LIMIT into account when generating a query plan,
565586
so you are very likely to get different plans (yielding different row
566587
orders) depending on what you give for LIMIT and OFFSET. Thus, using
@@ -765,6 +786,18 @@ SELECT distributors.* WHERE name = 'Westwood';
765786
The DISTINCT ON phrase is not part of <acronym>SQL92</acronym>.
766787
Nor are LIMIT and OFFSET.
767788
</para>
789+
790+
<para>
791+
In <acronym>SQL92</acronym>, an ORDER BY clause may only use result
792+
column names or numbers, while a GROUP BY clause may only use input
793+
column names.
794+
<productname>Postgres</productname> extends each of these clauses to
795+
allow the other choice as well (but it uses the standard's interpretation
796+
if there is ambiguity).
797+
<productname>Postgres</productname> also allows both clauses to specify
798+
arbitrary expressions. Note that names appearing in an expression will
799+
always be taken as input-column names, not as result-column names.
800+
</para>
768801
</refsect3>
769802

770803
<refsect3 id="R3-SQL-UNION-1">

0 commit comments

Comments
 (0)