Re: max question

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: "A(dot) R(dot) Van Hook" <hook(at)lake-lotawana(dot)mo(dot)us>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: max question
Date: 2005-04-13 13:20:19
Message-ID: 20050413132019.GB4594@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, Apr 12, 2005 at 12:24:21 -0500,
"A. R. Van Hook" <hook(at)lake-lotawana(dot)mo(dot)us> wrote:
> I have the following in a table:
> oid | citkey | contby | contdate | abcontinue | ccdate
> ---------+------------+----------+------------+------------+------------
> 5774835 | 04-0594703 | | | |
> 5775325 | 04-0594703 | Attorney | 04/06/2005 | 6 | 03/07/2005
> 5776060 | 04-0594703 | Attorney | 05/04/2005 | 6 | 04/05/2005
> 5776067 | 04-0594703 | Attorney | 05/04/2005 | 6 | 04/05/2005
>
> I am trying to pull rows that have the max. contdate. Why does the
> following give more than 2 rows?
> ql "select oid,* from ccontinue where citkey ='04-0594703' group by
> oid,citkey,contby,contdate,abcontinue,ccdate having max(contdate) =
> contdate"

If you group by oid you are going to get a separate group for each row.
You haven't told us what you actually expect for output, so I don't
have specific advise for a query. However, you might find the Postgres
extension DISTINCT ON useful, depending on exactly what you want.

> oid | citkey | contby | contdate | abcontinue | ccdate
> ---------+------------+----------+------------+------------+------------
> 5776067 | 04-0594703 | Attorney | 05/04/2005 | 6 | 04/05/2005
> 5775325 | 04-0594703 | Attorney | 04/06/2005 | 6 | 03/07/2005
> 5776060 | 04-0594703 | Attorney | 05/04/2005 | 6 | 04/05/2005
> (3 rows)
>
> thanks
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Kai Hessing 2005-04-13 13:44:25 can a function return a virtual table?
Previous Message Bruno Wolff III 2005-04-13 13:14:10 Re: operating "inet" type