Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 45904632F07 for ; Wed, 19 Aug 2009 11:01:16 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 20874-04 for ; Wed, 19 Aug 2009 14:01:10 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-px0-f204.google.com (mail-px0-f204.google.com [209.85.216.204]) by mail.postgresql.org (Postfix) with ESMTP id 5120D63195D for ; Wed, 19 Aug 2009 11:01:10 -0300 (ADT) Received: by pxi42 with SMTP id 42so2470273pxi.20 for ; Wed, 19 Aug 2009 07:01:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:date:from:to:cc:subject :message-id:references:mime-version:content-type:content-disposition :in-reply-to:user-agent; bh=DlumUTDMhWrw7nTW+jj2lkQPjcyR1a9xIxQUQgfMROE=; b=tXGwB2cm6hkHkOQ98F2/XrjdCghBvHAZFEgzJxWvReEED0XaYGeYUdS8sfXwMqkx4S EiYF9VP41JPeAP+RXC8iw0xozJEjZGsM3c6oboq9Ft+bnVLdDRK9aiTdYypuAPotXTS6 zVnIgNMtdaYLNuudiDB6pl871jx3HYgsCZoaA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; b=XjKZH7tfERuPYrxEpHsCH+jS2z6a51giLwzw7bKScjlnfAmeeU4mCqP6QdqouCfPar X6GsZoOEdaRiZICpQgyix1eI6uJKLnwAAAYnU4wl0shmMVgXHXdeHew6ExxLsus9WdhH O+NOCWudsaI6TI1BJje1pcZu2Fi7JE3d4axqA= Received: by 10.115.100.29 with SMTP id c29mr7386260wam.127.1250690467272; Wed, 19 Aug 2009 07:01:07 -0700 (PDT) Received: from eddie (c-67-182-227-82.hsd1.ut.comcast.net [67.182.227.82]) by mx.google.com with ESMTPS id c26sm200616waa.50.2009.08.19.07.01.00 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 19 Aug 2009 07:01:01 -0700 (PDT) Date: Wed, 19 Aug 2009 08:00:40 -0600 From: Joshua Tolley To: Otniel Michael Cc: pgsql-sql@postgresql.org Subject: Re: Ask About SQL Message-ID: <20090819140040.GD31216@eddie> References: <1703cd180908190317p3b1c4140hc9cbe41331c0725e@mail.gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="7DO5AaGCk89r4vaK" Content-Disposition: inline In-Reply-To: <1703cd180908190317p3b1c4140hc9cbe41331c0725e@mail.gmail.com> User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/103 X-Sequence-Number: 33012 --7DO5AaGCk89r4vaK Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Aug 19, 2009 at 05:17:07PM +0700, Otniel Michael wrote: > Hi All. >=20 > Can help to create sql queries for this data : >=20 > tabel A > field1, field2, field3 > x1, y1, 5 > x1, y2, 1 > x2, y1, 2 > x2, y3, 4 > x1, y3, 4 >=20 > I want to get 2 record with the max value at field3 for each kombinati= on > of field1 : >=20 > tabel B > field1, field2, field3 > x1, y1, 5 > x1, y3, 4 > x2, y3, 4 > x2, y1, 2 >=20 > Anyone have an ideas? > Thanks anyway. >=20 > -- > ------------------------------------------------------------------- > "He who is quick to become angry will commit folly, and a crafty man is > hated" Your example doesn't match your description (the combination of x1 and y2 isn't listed). However, from your description it looks like what you want is DISTINCT ON http://www.postgresql.org/docs/8.4/interactive/sql-select.html#SQL-DISTINCT Something like this: SELECT DISTINCT ON (field1, field2) field1, field2, field3 FROM A ORDER BY field1, field2, field3; Here's an example. # select * from b order by f1, f2, f3; f1 | f2 | f3=20 ----+----+---- x1 | y1 | 5 x1 | y2 | 1 x1 | y2 | 3 x2 | y3 | 2 x2 | y3 | 4 (5 rows) # select distinct on (f1, f2) f1, f2, f3 from b order by 1, 2, 3 desc; f1 | f2 | f3=20 ----+----+---- x1 | y1 | 5 x1 | y2 | 3 x2 | y3 | 4 (3 rows) -- Joshua Tolley / eggyknap End Point Corporation http://www.endpoint.com --7DO5AaGCk89r4vaK Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkqMBYgACgkQRiRfCGf1UMMfgQCfeU3x4GVbPMK08aL37+n3pxl0 4+kAmwcLmJ/scoIykKuvusQVJEQHdImx =xFAu -----END PGP SIGNATURE----- --7DO5AaGCk89r4vaK--