Re: Remove duplicate rows

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Jiří Němec <konference(at)menea(dot)cz>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Remove duplicate rows
Date: 2007-01-11 20:18:13
Message-ID: 20070111201813.GA20521@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jan 11, 2007 at 18:51:57 +0100,
Jiří Němec <konference(at)menea(dot)cz> wrote:
> Hello,
>
> I need to remove duplicates rows from a subquery but order these
> results by a column what is not selected. There are logically two
> solutions but no works.
>
> SELECT DISTINCT sub.foo FROM (SELECT ...) AS sub ORDER BY sub.bar
> ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
>
> SELECT sub.foo FROM (SELECT ...) AS sub GROUP BY sub.foo ORDER BY sub.bar
> ERROR: column "sub.bar" must appear in the GROUP BY clause or be used
> in an aggregate function
>
> Does anybody know how to remove duplicate rows from a subquery and order
> these results by a column what is not selected but exists in a subquery?

Is that column dependent (just on) the column you are checking for duplicates
on? If so you can use GROUP BY on both columns, listing the column you want
to order by first. If not, you might want to take a look at DISTINCT ON.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Marcus Engene 2007-01-11 20:18:31 Re: generate_series with month intervals
Previous Message Tom Lane 2007-01-11 20:14:37 Re: Checkpoint request failed on version 8.2.1.