Return equal number of rows with same column value

From: Nick <nboutelier(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Return equal number of rows with same column value
Date: 2012-06-05 00:27:04
Message-ID: 3c17ce26-a37e-47d3-a8f8-198770b3c338@pr7g2000pbb.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

For the data...

INSERT INTO test (id,val) VALUES (1,a);
INSERT INTO test (id,val) VALUES (2,a);
INSERT INTO test (id,val) VALUES (3,a);
INSERT INTO test (id,val) VALUES (4,a);
INSERT INTO test (id,val) VALUES (5,b);
INSERT INTO test (id,val) VALUES (6,b);

How could I return an even amount of val? For example, I would like to
return this...

1 | a
2 | a
5 | b
6 | b

Since the least number of b vals is 2, Id like to limit the a columns
to return only 2

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nick 2012-06-05 01:06:36 Re: Return equal number of rows with same column value
Previous Message Tom Lane 2012-06-04 18:13:17 Re: Building PostgreSQL 9.1.4 on Solaris 64-bit with gcc 4.6