Re: how to return the first record from the sorted records which may have duplicated value.

From: Yi Zhao <yi(dot)zhao(at)alibaba-inc(dot)com>
To: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: how to return the first record from the sorted records which may have duplicated value.
Date: 2008-09-23 02:31:47
Message-ID: 1222137107.3223.14.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

yes, thanks u very much, it's work:)

regards,
Yi
On Fri, 2008-09-19 at 11:06 +0200, Andreas Kretschmer wrote:
> Yi Zhao <yi(dot)zhao(at)alibaba-inc(dot)com> schrieb:
>
> > hi all:
> > I have a table with columns(>2) named "query", "pop", "dfk".
> > what I want is:
> > when I do some select, if the column "query" in result records have
> > duplicate value, I only want the record which have the maximum value of
> > the "pop".
> >
> > for example, the content of table:
> > query pop dfk
> > -----------------------
> > abc 30 1 --max
> > foo 20 lk --max
> > def 16 kj --max
> > foo 15 fk --discard
> > abc 10 2 --discard
> > bar 8 are --max
> >
> > the result should be:
> > query pop dfk
> > -----------------------
> > abc 30 1
> > foo 20 lk
> > def 16 kj
> > bar 8 are
>
> test=*# select * from d;
> query | pop | dfk
> -------+-----+-----
> abc | 30 | 1
> foo | 20 | lk
> def | 16 | kj
> foo | 15 | fk
> abc | 10 | 2
> bar | 8 | are
> (6 Zeilen)
>
> Zeit: 0,213 ms
> test=*# select distinct on (query) * from d order by query, pop desc;
> query | pop | dfk
> -------+-----+-----
> abc | 30 | 1
> bar | 8 | are
> def | 16 | kj
> foo | 20 | lk
> (4 Zeilen)
>
> Hint: distinct on isn't standard-sql, it's an PG-extension.
>
>
> Andreas
> --
> Really, I'm not out to destroy Microsoft. That will just be a completely
> unintentional side effect. (Linus Torvalds)
> "If I was god, I would recompile penguin with --enable-fly." (unknown)
> Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-09-23 03:02:28 Re: Help with query to return indexes (including functional ones!) on a given table
Previous Message Bruce Momjian 2008-09-23 02:18:52 Re: pg_start_backup() takes too long