X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 389DD52853 for ; Wed, 1 Jun 2005 12:33:36 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 55683-03 for ; Wed, 1 Jun 2005 15:33:24 +0000 (GMT) Received: from svana.org (svana.org [203.20.62.76]) by svr1.postgresql.org (Postfix) with ESMTP id 8B7B552800 for ; Wed, 1 Jun 2005 12:33:23 -0300 (ADT) Received: from kleptog by svana.org with local (Exim 3.35 #1 (Debian)) id 1DdVDI-0001Jx-00; Thu, 02 Jun 2005 01:33:00 +1000 Date: Wed, 1 Jun 2005 17:32:59 +0200 From: Martijn van Oosterhout To: FC Cc: pgsql-general@postgresql.org Subject: Re: SQL question. Message-ID: <20050601153255.GB29582@svana.org> Reply-To: Martijn van Oosterhout Mail-Followup-To: FC , pgsql-general@postgresql.org References: <6EE9301F-4C50-4160-9DC0-667826764A74@myamail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="2oS5YaxWCcQjTEyO" Content-Disposition: inline In-Reply-To: <6EE9301F-4C50-4160-9DC0-667826764A74@myamail.com> User-Agent: Mutt/1.3.28i X-PGP-Key-ID: Length=1024; ID=0x0DC67BE6 X-PGP-Key-Fingerprint: 295F A899 A81A 156D B522 48A7 6394 F08A 0DC6 7BE6 X-PGP-Key-URL: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/22 X-Sequence-Number: 78730 --2oS5YaxWCcQjTEyO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 01, 2005 at 04:40:48PM +0200, FC wrote: >=20 > Hello SQL Aces ! >=20 > I want to do a select on a table distinct on linkid and sorted by =20 > date. I have try this How about a subquery?: SELECT * FROM ( SELECT DISTINCT ON (linkid) * FROM all_links WHERE uid=3D'2' AND DATE_TRUNC('day',read_date) =3D DATE_TRUNC('day', TIMESTAMP '2005-06-01') ORDER BY linkid, read_date) AS sub ORDER BY read_date DESC limit 100; Hope this helps, > My question is... How can I do the same thing in the more efficient =20 > way and without using a temporary table. Since I am using PHP and the =20 > table is not deleted at the end of the program because PHP keeps the =20 > connection to the database open. --=20 Martijn van Oosterhout http://svana.org/kleptog/ > Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a > tool for doing 5% of the work and then sitting around waiting for someone > else to do the other 95% so you can sue them. --2oS5YaxWCcQjTEyO Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQFCndUnY5Twig3Ge+YRAmMsAJ0cqkkhZ69vs56cHe575ivX84Ka+QCffxhD PmtBVPWjxg7BXhvooBJmFPI= =VEND -----END PGP SIGNATURE----- --2oS5YaxWCcQjTEyO--