Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-15 Thread Bruno Wolff III
On Sun, Dec 14, 2003 at 22:17:35 -0500, Tom Lane [EMAIL PROTECTED] wrote: Bruno Wolff III [EMAIL PROTECTED] writes: If the sort used to select the records sorts on both the distinct expressions and the order by expressions you will get a sensible deterministic result. Sensible in what

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-15 Thread Bruno Wolff III
On Mon, Dec 15, 2003 at 06:14:59 -0600, Bruno Wolff III [EMAIL PROTECTED] wrote: Doing things as above is pretty much the same as normal distinct on for purposes of which rows get selected. Of the possible rows that might get returned for a specific set of values from the distinct on

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-15 Thread Tom Lane
Bruno Wolff III [EMAIL PROTECTED] writes: Specifically the interpretation I think makes sense is that SELECT DISTINCT ON (a, b, c) * FROM tablename ORDER BY d, e, f should be treated as equvialent to SELECT * FROM (SELECT DISTINCT ON (a, b, c) FROM tablename ORDER BY a, b, c, d, e, f) AS t

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-14 Thread Tom Lane
Greg Stark [EMAIL PROTECTED] writes: So, like DISTINCT ON, GROUP BY also insists on the user providing the ORDER BY clause. I suppose you could argue postgres could implicitly introduce an extra sort step when the user-provided ORDER BY doesn't match the GROUP BY or DISTINCT ON clause but it

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-14 Thread Neil Conway
Tom Lane [EMAIL PROTECTED] writes: This was discussed before --- see the archives. I believe the conclusion was that the results would actually be nondeterministic if we used two sort steps (that's what the code comment means by rather unpredictable). Does the non-determinism you're

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-14 Thread Bruno Wolff III
On Sun, Dec 14, 2003 at 18:09:33 -0500, Tom Lane [EMAIL PROTECTED] wrote: This was discussed before --- see the archives. I believe the conclusion was that the results would actually be nondeterministic if we used two sort steps (that's what the code comment means by rather

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-14 Thread Tom Lane
Neil Conway [EMAIL PROTECTED] writes: Does the non-determinism you're referring to result from an ORDER BY on a non-deterministic expression, or the non-determinism that results from picking an effectively random row because the ORDER BY isn't sufficient? The latter --- you don't know which

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-14 Thread Tom Lane
Bruno Wolff III [EMAIL PROTECTED] writes: If the sort used to select the records sorts on both the distinct expressions and the order by expressions you will get a sensible deterministic result. Sensible in what sense? ;-) It seems to me that the existing documentation defines the behavior

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-13 Thread Bruno Wolff III
On Fri, Dec 12, 2003 at 18:39:20 -0500, Neil Conway [EMAIL PROTECTED] wrote: /* * If the user writes both DISTINCT ON and ORDER BY, then the * two expression lists must match (until one or the other * runs out). Otherwise the ORDER

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-13 Thread Greg Stark
Neil Conway [EMAIL PROTECTED] writes: We reject the following query: nconway=# create table abc (a int, b int, c int); CREATE TABLE nconway=# select distinct on (a) a, b, c from abc order by b, c, a; ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions What

Re: [HACKERS] ORDER BY and DISTINCT ON

2003-12-13 Thread Neil Conway
Greg Stark [EMAIL PROTECTED] writes: Do you really want: select distinct on (b,c,a) a,b,c from abc order by b,c,a; or is that you want select * from (select distinct on (a) a,b,c order by a) order by b,c,a; If I understand you correctly, I don't think I would expect either. - ORDER BY

[HACKERS] ORDER BY and DISTINCT ON

2003-12-12 Thread Neil Conway
We reject the following query: nconway=# create table abc (a int, b int, c int); CREATE TABLE nconway=# select distinct on (a) a, b, c from abc order by b, c, a; ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions This works fine, of course: nconway=# select distinct