From dd979f66be20fc54aad06da743f788fbc505bbe1 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 27 Jan 2000 18:11:50 +0000 Subject: Redesign DISTINCT ON as discussed in pgsql-sql 1/25/00: syntax is now SELECT DISTINCT ON (expr [, expr ...]) targetlist ... and there is a check to make sure that the user didn't specify an ORDER BY that's incompatible with the DISTINCT operation. Reimplement nodeUnique and nodeGroup to use the proper datatype-specific equality function for each column being compared --- they used to do bitwise comparisons or convert the data to text strings and strcmp(). (To add insult to injury, they'd look up the conversion functions once for each tuple...) Parse/plan representation of DISTINCT is now a list of SortClause nodes. initdb forced by querytree change... --- doc/src/sgml/ref/select.sgml | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index 070f8b43d0f..ed180ac91c1 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -1,5 +1,5 @@ @@ -22,7 +22,7 @@ Postgres documentation 1999-07-20 -SELECT [ ALL | DISTINCT [ ON column ] ] +SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] expression [ AS name ] [, ...] [ INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table ] [ FROM table [ alias ] [, ...] ] @@ -201,16 +201,29 @@ SELECT [ ALL | DISTINCT [ ON column - DISTINCT will eliminate all duplicate rows from the + DISTINCT will eliminate duplicate rows from the result. - DISTINCT ON column - will eliminate all duplicates in the specified column; this is - similar to using - GROUP BY column. - ALL will return all candidate rows, + ALL (the default) will return all candidate rows, including duplicates. + + DISTINCT ON eliminates rows that match on all the + specified expressions, keeping only the first row of each set of + duplicates. Note that "the first row" of each set is unpredictable + unless ORDER BY is used to ensure that the desired + row appears first. For example, + + SELECT DISTINCT ON (location) location, time, report + FROM weatherReports + ORDER BY location, time DESC; + + retrieves the most recent weather report for each location. But if + we had not used ORDER BY to force descending order of time values + for each location, we'd have gotten a report of unpredictable age + for each location. + + The GROUP BY clause allows a user to divide a table conceptually into groups. -- cgit v1.2.3