Re: order by, but eliminating dupes

Lists: pgsql-novice
From: LH <_pgsql-novice_(at)geekhouse(dot)no-ip(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: order by, but eliminating dupes
Date: 2003-09-04 02:52:44
Message-ID: 3F56A8FC.451EC60@geekhouse.no-ip.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

So lets say I got a table A(x,y,z, Q) ordered by column Q, with contents
like so:

x y z Q
------------------------
21 5 x 1
21 5 x 2
43 10 t 3
1 2 a 5
43 10 t 10
21 5 x 50

what I would like is to take the results of this query, and maintaining
the order, only list x, y, z , AND
not list any duplicates.

So the result I'd want after getting this query would be:

x y z
----------------
21 5 x
43 10 t
1 2 a

So I'm pulling the results as they come along, and ignore any subsequent
dupes.

I don't know if this is even possible. I've tried combinations of SELECT
DISTINCT, GROUP BY, and others
with
no luck. The best I could do was to do the order by in a subquery, then
do a SELECT UNIQUE on the subquery.
But that kills the order of the subquery. I've tried group by x,y,z but
then I can't ORDER BY Q.

- L


From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: LH <_pgsql-novice_(at)geekhouse(dot)no-ip(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: order by, but eliminating dupes
Date: 2003-09-04 03:19:32
Message-ID: 20030904031932.GA28420@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

On Wed, Sep 03, 2003 at 22:52:44 -0400,
LH <_pgsql-novice_(at)geekhouse(dot)no-ip(dot)com> wrote:
> So lets say I got a table A(x,y,z, Q) ordered by column Q, with contents
> like so:
>
> But that kills the order of the subquery. I've tried group by x,y,z but
> then I can't ORDER BY Q.

I think you can do something like this:

select x, y, z from
(select distinct on (x, y, z) x, y, z, q order by x, y, z, q)
order by q;


From: Jeffrey Melloy <jmelloy(at)visualdistortion(dot)org>
To: LH <_pgsql-novice_(at)geekhouse(dot)no-ip(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: order by, but eliminating dupes
Date: 2003-09-04 06:12:43
Message-ID: CBB65ED2-DE9E-11D7-B534-000393C78AC0@visualdistortion.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Something like this works:
select a,b,c from t1 group by a,b,c order by min(q)

Jeff
On Wednesday, September 3, 2003, at 09:52 PM, LH wrote:

> So lets say I got a table A(x,y,z, Q) ordered by column Q, with
> contents
> like so:
>
> x y z Q
> ------------------------
> 21 5 x 1
> 21 5 x 2
> 43 10 t 3
> 1 2 a 5
> 43 10 t 10
> 21 5 x 50
>
> what I would like is to take the results of this query, and maintaining
> the order, only list x, y, z , AND
> not list any duplicates.
>
> So the result I'd want after getting this query would be:
>
> x y z
> ----------------
> 21 5 x
> 43 10 t
> 1 2 a
>
> So I'm pulling the results as they come along, and ignore any
> subsequent
> dupes.
>
> I don't know if this is even possible. I've tried combinations of
> SELECT
> DISTINCT, GROUP BY, and others
> with
> no luck. The best I could do was to do the order by in a subquery, then
> do a SELECT UNIQUE on the subquery.
> But that kills the order of the subquery. I've tried group by x,y,z but
> then I can't ORDER BY Q.
>
> - L
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster