How to delete rows number 2,3,4...

From: A B <gentosaker(at)gmail(dot)com>
To: postgresql Forums <pgsql-general(at)postgresql(dot)org>
Subject: How to delete rows number 2,3,4...
Date: 2010-10-08 06:59:37
Message-ID: AANLkTimgpMYqNaHsVd_rAPb+LEYeoRbyLM+G=buOMX4j@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello.

I have a table

create table foo (
a serial,
b int,
c int,
.... more fields ...);

and now I wish to remove for each combination of b and c, all the
rows except the one with the highest value of a.

For example
a b c other fields
=====
1 5 5 .....
2 5 5 ....
3 2 3 ....
4 2 2 ...
5 5 5 ...

should leave

a b c other fields
=====
3 2 3 ....
4 2 2 ...
5 5 5 ...

is there some clever command for this or will I have to write a
special function that loops through all values of b and c ?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dave Page 2010-10-08 07:46:38 Re: postgreSQL for Windows 7
Previous Message Timon 2010-10-08 04:43:57 Fwd: stuck spinlock detected at dynahash.c