Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 0B49E474E53 for ; Fri, 1 Nov 2002 17:06:12 -0500 (EST) Received: from utahisp.com (unknown [66.239.12.3]) by postgresql.org (Postfix) with ESMTP id E3965474E44 for ; Fri, 1 Nov 2002 17:06:10 -0500 (EST) Received: from chad [63.230.8.76] by utahisp.com (SMTPD32-5.05) id AA004CD600AA; Fri, 01 Nov 2002 15:02:40 -0700 Message-ID: <033301c281f2$aed1f7e0$32021aac@chad> From: "Chad Thompson" To: "Peter T. Brown" , References: <1036189492.2013.16.camel@localhost.localdomain> Subject: Re: making queries more effecient Date: Fri, 1 Nov 2002 15:04:42 -0700 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Declude-Sender: chad@weblinkservices.com [63.230.8.76] X-Note: This E-mail was scanned by Declude JunkMail (www.declude.com) for spam. X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/22 X-Sequence-Number: 10164 This should be all you need. insert into VisitorPointer839 ("VisitorID") select VisitorID from ProgramEvent Where ProgramID = 10 and Type = 0 group by VisitorID You dont need order by because its not important the order it goes in the database, just the order that it comes out. I have found that group by works faster than distinct in some cases. You may have to test it for your senario. Thanks Chad ----- Original Message ----- From: "Peter T. Brown" To: Sent: Friday, November 01, 2002 3:24 PM Subject: [SQL] making queries more effecient > Hi. I have this query that I have been trying to reduce to a single > statement, but haven't figured out how. Am I missing something? > > CREATE TEMP TABLE temp20561149207391 AS SELECT DISTINCT ON ("VisitorID") > "VisitorID","Type" FROM "ProgramEvent" WHERE "ProgramID" = 10 ORDER BY > "VisitorID","Created" DESC;INSERT INTO "VisitorPointer839" ("VisitorID") > SELECT temp20561149207391."VisitorID" FROM temp20561149207391 WHERE > temp20561149207391."Type" = 0 > > > Thanks > > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly >