From: | Shaun Thomas <sthomas(at)optionshouse(dot)com> |
---|---|
To: | Alexander Reichstadt <lxr(at)mac(dot)com> |
Cc: | <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: DISTINCT ON changes sort order |
Date: | 2013-04-24 14:26:23 |
Message-ID: | 5177EB8F.1060502@optionshouse.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 04/24/2013 09:03 AM, Alexander Reichstadt wrote:
> The order is correct. Now from the outer SELECT I would expect then to get:
> 53
> 29
> 46
Please re-read the manual on DISTINCT ON.
"SELECT 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."
You're running into the "unpredictable" part in that stanza. The
distinct on may change the query plan, which will result in a different
ordering of tuples than your inner clause, unless you used an
optimization fence.
Use an order-by clause. It's good practice to choose the column you want
so that it always comes first, and eliminates other candidates.
--
Shaun Thomas
OptionsHouse | 141 W. Jackson Blvd. | Suite 500 | Chicago IL, 60604
312-676-8870
sthomas(at)optionshouse(dot)com
______________________________________________
See http://www.peak6.com/email_disclaimer/ for terms and conditions related to this email
From | Date | Subject | |
---|---|---|---|
Next Message | Kevin Grittner | 2013-04-24 15:00:06 | Re: DISTINCT ON changes sort order |
Previous Message | Alexander Reichstadt | 2013-04-24 14:03:09 | DISTINCT ON changes sort order |