views:

23

answers:

1

I just came across a SQL query, specifically against a Postgres database, that uses a function named "distinct". Namely:

select distinct(pattern) as pattern, style, ... etc ...
from styleview
where ... etc ...

Note this is NOT the ordinary DISTINCT qualifier on a SELECT -- at least it's not the normal syntax for the DISTINCT qualifier, note the parentheses. It is apparently using DISTINCT as a function, or maybe this is some special syntax.

Any idea what this means?

I tried playing with it a little and if I write

select distinct(foo)
from bar

I get the same results as

select distinct foo
from bar

When I combine it with other fields in the same select, it's not clear to me exactly what it's doing.

I can't find anything in the Postgres documentation.

Thanks for any help!

+1  A: 

From the documentation:

If DISTINCT is specified, all duplicate rows are removed from the result set (one row is kept from each group of duplicates). ALL specifies the opposite: all rows are kept; that is the default.

DISTINCT ON ( expression [, ...] ) keeps only the first row of each set of rows where the given expressions evaluate to equal. The DISTINCT ON expressions are interpreted using the same rules as for ORDER BY (see above). 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,

The ON portion is optional, so it really comes down to:

  1. The brackets being used
  2. Placement in the query - SQL Server & MySQL throw an error if you use DISTINCT in any but the first position of the SELECT clause

PostgreSQL is the only database to my knowledge to support this syntax.

OMG Ponies
+1 - Better than my answer, as always Ponies.
JNK
Thank you for your reply, but I don't think that's it, or at least, not quite. I am familiar with the "distinct on" option. The documentation does not indicate that the "on" is optional, and if I write "select distinct(foo) from bar" I get a list of foo's, but if I write "select distinct on (foo) from bar" I get an error message for failing to give any fields in the select. ("select distinct on (foo) foo from bar" works.) But you are correct that if the "distinct" is not the first thing after "select" it gives an error. So maybe this is just some alternate syntax of "distinct on"?
Jay
@Jay: I don't have PostgreSQL handy to test with, sorry.
OMG Ponies