Re: highest match in group - Mailing list pgsql-general

From Arjen van der Meijden
Subject Re: highest match in group
Date
Msg-id 3FD74826.5000603@vulcanus.its.tudelft.nl
Whole thread Raw
In response to highest match in group  ("Dave [Hawk-Systems]" <dave@hawk-systems.com>)
Responses Update multiple columns with select statement?
List pgsql-general
Dave [Hawk-Systems] wrote:

> have a data table that records entries by date(unix timestamp) and customer
> number.  each custnum will have several entries showing a running ledger type
> snapshot.  we have the need to get the most recent entry from not one, but all
> unique customers, in the most cost effective manner.
>
> [snip]
>
> Currently we are running through all our customer numbers in one query, then
> for each customer number querying the summary table to get each customers
> latest entry (select order by date desc limit 1). Obviously this results in a
> large number of queries and is expensive.  Looking for a more concise, less
> expensive way.
>
> thanks
>
> Dave
What about something like:
SELECT so.* FROM summary so, (SELECT custnum, MAX(date) as date FROM
summary si GROUP BY custnum) as cd WHERE so.date = cd.date AND
so.custnum = cd.custnum

Best regards,

Arjen van der Meijden



pgsql-general by date:

Previous
From: "Dave [Hawk-Systems]"
Date:
Subject: highest match in group
Next
From: Christopher.Becker@apcc.com
Date:
Subject: "PostgreSQL Error Code(1)"