pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feed From: A. Kretschmer <andreas.kretschmer@schollglas.com>
To: pgsql-sql@postgresql.org
Subject: Re: Ask About SQL
Date: Wed, 19 Aug 2009 12:46:48 +0200
Message-ID: <20090819104648.GA11859@a-kretschmer.de> (raw )
In-Reply-To: <1703cd180908190317p3b1c4140hc9cbe41331c0725e@mail.gmail.com >
References: <1703cd180908190317p3b1c4140hc9cbe41331c0725e@mail.gmail.com >
In response to Otniel Michael :
> Hi All.
>
> Can help to create sql queries for this data :
>
> tabel A
> field1, field2, field3
> x1, y1, 5
> x1, y2, 1
> x2, y1, 2
> x2, y3, 4
> x1, y3, 4
>
> I want to get 2 record with the max value at field3 for each kombination of
> field1 :
>
> tabel B
> field1, field2, field3
> x1, y1, 5
> x1, y3, 4
> x2, y3, 4
> x2, y1, 2
>
> Anyone have an ideas?
Works since 8.4:
test=*# select * from table_a ;
field1 | field2 | field3
--------+--------+--------
x1 | y1 | 5
x1 | y2 | 1
x2 | y1 | 2
x2 | y3 | 4
x1 | y3 | 4
(5 rows)
test=*# select field1, field2, field3 from (select field1, field2,
field3, row_number() over(partition by field1 order by field3 desc) from
table_a order by field1, field3) foo where row_number < 3 order by
field1, field2;
field1 | field2 | field3
--------+--------+--------
x1 | y1 | 5
x1 | y3 | 4
x2 | y1 | 2
x2 | y3 | 4
(4 rows)
Regards, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
view thread (6+ messages) latest in thread
Message-ID: <20090819104648.GA11859@a-kretschmer.de>
Permalink: ../20090819104648.GA11859@a-kretschmer.de/
Also on: postgresql.org/message-id/20090819104648.GA11859@a-kretschmer.de
copy link · copy postgr.es
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: andreas.kretschmer@schollglas.com
Subject: Re: Ask About SQL
In-Reply-To: <20090819104648.GA11859@a-kretschmer.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox