Re: Problem with phone list.

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: Mike Diehl <jdiehl(at)sandia(dot)gov>, SQL Postgresql List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Problem with phone list.
Date: 2007-08-15 21:00:05
Message-ID: 846214.97432.qm@web31807.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


--- Mike Diehl <jdiehl(at)sandia(dot)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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Glaesemann 2007-08-15 21:04:37 Re: Problem with phone list.
Previous Message Fernando Hevia 2007-08-15 20:58:22 Re: Problem with phone list.