Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id E20899F97D2 for ; Wed, 15 Aug 2007 18:00:11 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 36579-06 for ; Wed, 15 Aug 2007 18:00:04 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from web31807.mail.mud.yahoo.com (web31807.mail.mud.yahoo.com [68.142.207.70]) by postgresql.org (Postfix) with SMTP id 8A8A39F982C for ; Wed, 15 Aug 2007 18:00:07 -0300 (ADT) Received: (qmail 98162 invoked by uid 60001); 15 Aug 2007 21:00:05 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=tzLMI74MaJcRj/sjHRLbeW+kAdTEHwQfSZgB2g/EUpxFd2x8lUI38xz+HpP1mdQa3Q4Xi2slR5pk6F0y1FO6KxLThAd6YkJSdLxbEB4MnFvRsy7G8TBbqsYdZw9eBqHv+kgmISkTY9wMBJGpZPy9pT0Kx18WRC0E/5QB1OVHYII=; X-YMail-OSG: xzZyOEUVM1mrlz1Yg4KFSn4FuTwkuzLWti5iW2z1GAtAfF6xqsm1sL.VgT2ZFRuGGg-- Received: from [12.180.237.138] by web31807.mail.mud.yahoo.com via HTTP; Wed, 15 Aug 2007 14:00:05 PDT Date: Wed, 15 Aug 2007 14:00:05 -0700 (PDT) From: Richard Broersma Jr Subject: Re: Problem with phone list. To: Mike Diehl , SQL Postgresql List In-Reply-To: <200708151428.17740.jdiehl@sandia.gov> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <846214.97432.qm@web31807.mail.mud.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/135 X-Sequence-Number: 29073 --- Mike Diehl wrote: > I've qot a problem I need to solve. I'm sure it's pretty simple; I just can't > seem to get it, so here goes... > > I've got a table, actually a view that joins 3 tables, that contains a phone > number, a unique id, and a call duration. > > The phone number has duplicates in it but the unique id is unique. > > I need to get a list of distinct phone numbers and the coorisponding largest > call duration. > > I've got the idea that this should be a self-join on phone number where > a.id<>b.id, but I just can't seem to get the max duration. SELECT phone_number, max( duration ) as max_duration FROM your_view GROUP BY phone_number; if you need the unique Id also, SELECT DISTINCT ON ( phone_number ) id, phone_number, duration FROM your_view ORDER BY duration desc; or SELECT V1.id, V1.phone_number, V1.duration FROM your_view AS V1 INNER JOIN ( SELECT phone_number, max( duration ) FROM your_view GROUP BY phone_number ) AS V2( phone_number, duration ) ON (V1.phone_number, V1.duration) = (V2.phone_number, V2.duration); Regards, Richard Broersma Jr.