Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 6A6C163473E for ; Wed, 12 Aug 2009 16:01:12 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 20161-02 for ; Wed, 12 Aug 2009 19:00:58 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f220.google.com (mail-fx0-f220.google.com [209.85.220.220]) by mail.postgresql.org (Postfix) with ESMTP id 6D379634060 for ; Wed, 12 Aug 2009 16:01:01 -0300 (ADT) Received: by fxm20 with SMTP id 20so202206fxm.24 for ; Wed, 12 Aug 2009 12:00:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:from:date:x-google-sender-auth:message-id:subject:to:cc :content-type:content-transfer-encoding; bh=tawT3xaMVvizldsN9x9zsMnQFbSQf0qIH0Y9l2v4E1U=; b=PZcoQzLt4/yC5h+hzF2Zkcf4+1XuOZ4NvV9xg5BJTEajXVCY5OHgOXkeZ/hLgGp95d k0RvsjoSJfUwPJ+fEy7k2BcMOrF9Dy3k3Mb/JyXpiuGsCTADMeN4aPxsg4wmemQ24h3q zYcyxv5y4mr8DymoK0CwUayHOw9BoamA2ZON4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; b=TnsAv8X66ZNypzy3YSN7I3mtnP8IjYLXaGGY0agUS5ugmfrgPkWlWTr33hf57xV804 hUsZuz7mGC4PK+EneOzO2j6mSdEAiNYkVdnP//b0SQm13hCoDfWVG5PNeZ/B2HK7uXB6 PDR9qkV/45IQwETSi07hIjumtCH82/XCJ/fds= MIME-Version: 1.0 Received: by 10.102.218.6 with SMTP id q6mr118205mug.107.1250103658305; Wed, 12 Aug 2009 12:00:58 -0700 (PDT) In-Reply-To: References: <20090812181928.GF7527@cns.vt.edu> From: Greg Stark Date: Wed, 12 Aug 2009 20:00:38 +0100 X-Google-Sender-Auth: bac875dfe96f5ba3 Message-ID: <407d949e0908121200i5a732ec0u3e991c4f47393f82@mail.gmail.com> Subject: Re: mysql code questions To: Thomas Kellerer Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.547 tagged_above=-10 required=5 tests=AWL=1.052, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/60 X-Sequence-Number: 32969 On Wed, Aug 12, 2009 at 7:52 PM, Thomas Kellerer wrote: > Ray Stell wrote on 12.08.2009 20:19: > I would probably do it this way: > > SELECT tt. * > FROM testtable tt > WHERE create_date =3D (SELECT MAX(create_date) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 FROM testtable tt2 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 WHERE tt.id =3D tt2.id); The article is *very* unclear about the problem. You have to read the whole schema and their proposed solution to figure out what the problem they're trying to solve is. This query doesn't solve it. They want the newest record for *each* id. Ie, the equivalent of "select id,max(create_date) from tt group by id" except including the other attributes for the record with that date. There are a number of ways to solve it. Postgres supports all of the solutions in the comments including the one proposed in the post as well as the window-function based solution. It also supports an extension which could be even more efficient than all of them: select DISTINCT ON (id) from tt order by id,create_date desc If you have an index on (Or if you have an index on and use "order by id desc, create_date desc" in the query) then this could do a single index scan. --=20 greg http://mit.edu/~gsstark/resume.pdf