Re: how to do this select?

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: Yi Zhao <yi(dot)zhao(at)alibaba-inc(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: how to do this select?
Date: 2009-02-19 07:31:05
Message-ID: 499D0AB9.5030707@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Yi Zhao wrote:
> thanks Ringer.
> my mean is that:
> I want less than 2 rows which have the same value of column "b"!
>
> for example, there is 3 columns have the same value "A",
> X | A
> X | A
> Y | A
> I want my result have two of them.

Less than two? ie just one? That's easy:

test=# SELECT DISTINCT ON (b) a, b FROM ab;
a | b
---+---
Y | A
D | B
P | C
(3 rows)

but if you want a randomly selected TWO rows, rather than just one, it's
harder. In PostgreSQL 8.4 (not yet released) you could use the new
windowing functions to do it quite easily. I can't off the top of my
head think of any reasonably nice way to do it in 8.3.

--
Craig Ringer

In response to

Browse pgsql-general by date

  From Date Subject
Next Message A. Kretschmer 2009-02-19 07:33:10 Re: how to do this select?
Previous Message Craig Ringer 2009-02-19 07:26:11 Re: postgres wish list