Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 09822475BF9 for ; Thu, 21 Nov 2002 14:06:10 -0500 (EST) Received: from seapdc01.PNTS (ip63.c216.blk1.bel.nwlink.com [209.20.216.63]) by postgresql.org (Postfix) with ESMTP id 6F06C475B33 for ; Thu, 21 Nov 2002 14:06:07 -0500 (EST) Received: from busbydev (BUSBY-DEV [10.0.0.64]) by seapdc01.PNTS with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id VLKPFZCW; Thu, 21 Nov 2002 11:11:42 -0800 Message-ID: <006901c29190$7beea230$4000000a@busbydev> From: "David Busby" To: "Dave" , References: Subject: Re: Selecting distinct records Date: Thu, 21 Nov 2002 11:02:05 -0800 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.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/84 X-Sequence-Number: 1666 Dave, Try: select distinct on (sessionid) sum(sessiontime) from logs where name='joeblowuser' and datetime > 1036040400 group by sessionid. sum() is an aggerate (sp?) function, that means it munges a field (sessiontime) from multiple records into one field in one record. Since you are also selecting sessionid (from mulitple records) you need to munge it some how, that munge is accomplished via 'group by'. From your previous e-mail it seems that (IMHO) the real problem is that duplicates are getting inserted via external hardware interaction, this select might be a bandage on a wound whose true size isn't known... PostgreSQL docs: Distinct: http://www.postgresql.org/idocs/index.php?queries-select-lists.html Group By: http://www.postgresql.org/idocs/index.php?sql-select.html /B ----- Original Message ----- From: "Dave" To: Sent: Thursday, November 21, 2002 10:43 Subject: Re: [PHP] Selecting distinct records > >What I am looking to do is > >- grab every record for $user > >- remove any records that have identical ipaddress+sessionid+refid > >- then sort the results by date_time or something else > > this last requirement is where the problem is... > > is you do a sum() or order by in the select statement you get and error, for > example; > > select distinct on (sessionid) sum(sessiontime) from logs where > name='joeblowuser' and datetime > 1036040400; > > ERROR: Attribute logs.sessionid must be GROUPed or used in an aggregate > function > > Same if you have to order by datetime or something... > > Dave > > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org