pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Broersma Jr <rabroersma@yahoo.com>
To: Mike Diehl <jdiehl@sandia.gov>
To: SQL Postgresql List <pgsql-sql@postgresql.org>
Subject: Re: Problem with phone list.
Date: Wed, 15 Aug 2007 14:00:05 -0700 (PDT)
Message-ID: <846214.97432.qm@web31807.mail.mud.yahoo.com> (raw)
In-Reply-To: <200708151428.17740.jdiehl@sandia.gov>


--- Mike Diehl <jdiehl@sandia.gov> 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.



view thread (7+ messages)  latest in thread

Message-ID: <846214.97432.qm@web31807.mail.mud.yahoo.com>
Permalink:  ../846214.97432.qm@web31807.mail.mud.yahoo.com/
Also on:    postgresql.org/message-id/846214.97432.qm@web31807.mail.mud.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: rabroersma@yahoo.com, jdiehl@sandia.gov
  Subject: Re: Problem with phone list.
  In-Reply-To: <846214.97432.qm@web31807.mail.mud.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox