Re: DISTINCT ... ORDER BY - Mailing list pgsql-novice

From Stephan Szabo
Subject Re: DISTINCT ... ORDER BY
Date
Msg-id 20031104211053.K83029@megazone.bigpanda.com
Whole thread Raw
In response to DISTINCT ... ORDER BY  (Nabil Sayegh <postgresql@e-trolley.de>)
Responses Re: DISTINCT ... ORDER BY
List pgsql-novice
On Wed, 5 Nov 2003, Nabil Sayegh wrote:

> I have built a query that mainly orders all columns.
> It shouldn't matter how it orders but FYI:
> I need to order by 1 given column and then by all others from 'left to right'.
>
> No problem so far.
>
> Then I want to return only 1 column (e.g. the 3rd) with duplicates removed
> _AND_ still in the same order like above.
>
> e.g.:
>
> myCol
> -----
> a
> a
> c
> b
> b
> c
> b
>
> should return:
>
> myDistinctOrderedCol
> --------------------
> a
> c
> b
>
> The problem is that DISTINCT complains about ORDER BY entries not being in the target list.

Right, because given this data:
col1 | col2
 a   |  b
 b   |  c
 b   |  a

what ordering should
 select distinct col1 from tab order by col2
give you?

Does it put b first because there's a col2 with a, or b second because
there's a col2 with b or is it indeterminate?


Maybe something of this general form would work if you want to order by
the minimums:
select col3 from tablename group by col3 order by min(col1), min(col2),
col3, min(col4);

pgsql-novice by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: DISTINCT ... ORDER BY
Next
From: John Siggins
Date:
Subject: selecting rows based on unique fields in a column