473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

highest match in group

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.

Table "summary"
Attribute | Type | Modifier
-----------+---------+----------
custnum | integer |
date | integer |
amount | float8 |
balance | float8 |

sample data;
custnum | date | amount | balance
----------+------------+---------+---------
12025702 | 1019151676 | 47.96 | 0
12045401 | 1019145600 | 17.12 | -17.12
12040601 | 1019229292 | 26.7 | 1.02
12045701 | 1019232000 | 16.59 | -16.59
12045702 | 1019232000 | 16.59 | -16.59
12045703 | 1019232000 | 9.87 | -9.87
12045704 | 1019232000 | 16.59 | -16.59
12045705 | 1019232000 | 16.59 | -16.59
12045704 | 1019408919 | 15.52 | -1.07
12045704 | 1019404800 | 15.52 | -16.59

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


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #1
4 1966
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

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2
"Dave [Hawk-Systems]" <da**@hawk-systems.com> writes:
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.


If you don't mind using a Postgres-only construct, SELECT DISTINCT ON
is made for this. See the "weather report" example in the SELECT
reference page.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #3
Is it possible to update multiple columns of a table using a select
statement to derive the values?

For example (trying to port from Oracle to Postgres)

-- update the image record for original size image
update WPImage set (WPImageStateID, Width, Height, ContentType,
ContentLength) = (
select 3, Width, Height, ContentType, ContentLength
from WPImageHeader
where WDResourceID = pResourceID
)
where WDResourceID = pResourceID and WPSizeTypeID = 0;

I have seen mention of a Postgres (specific) feature,

update ... set .... from {other_table} where {join_condition}
Is this the most appropriate way to do the above in postgres?

update WPImage
set WPImageStateID = 3,
Width = WPImageHeader.Width,
Height = WPImageHeader.Height,
ContentType = WPImageHeader.ContentType,
ContentLength = WPImageHeader.ContentLength
where WPImage.WDResourceID = WPImageHeader.WDResourceID
and WPImage.WDResourceID = pResourceID
and WPImage.WPSizeTypeID = 0;

{where pResourceID is a variable}

Thanks

John Sidney-Woollett

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #4
>"Dave [Hawk-Systems]" <da**@hawk-systems.com> writes:
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.


If you don't mind using a Postgres-only construct, SELECT DISTINCT ON
is made for this. See the "weather report" example in the SELECT
reference page.


thanks Tom, had a forest for the trees problem there and that put me on the
right track.

Dave

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: david.reitter | last post by:
Why does the following result in an IndexError? I try to match an optional group, and then access it via its group name. The group happens to not participate in the match, but is obviously defined...
4
by: Mark | last post by:
good spam subject ;). anyway, i'm alittle stumped. i'm in need of putting together a query that gets the next highest salary ( select max ( sal ) - 1?, from an emp_sal type table. another...
2
by: sukh | last post by:
From the table i want everything highlighted with a * I wanted an SQl expression to look at values in Column 1 (ID), look at the corresponding values in the second column (F1) and select the row...
5
by: andreas.muller | last post by:
Hello everyone, I'm trying to solve this problem but can't seem to figure out how to start. I would like to create a rating system where people can vote (1-5 stars) on randomly displayed items....
2
by: nick_faye | last post by:
hi guys, im still a newbie in ms-access and i would like to ask how can i get the highest and the lowest date in ms-access report? for example, i have these entries: 10/1/2003 10/5/2003...
2
by: Dirtyweeker | last post by:
Hi, I have a database which records fitness test results of pupils. There are the usual name fields and then a series of fields holding results, e.g. field BP1 and field BP2; each of these...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
5
by: owz | last post by:
Hi again had some more problems, all help welcome. Access 2000, SQL My problem is as stated in the title. I want 2 display the highest and lowest priced car sold for this month. This is what I...
1
by: Lance Hoffmeyer | last post by:
Hey all, I have a search: VAR = re.search("PROVEN.*?\n+\d(.*?)\n.*?" , pcpT9, re.S ).group(1) #.split() that does not match (sometimes it will and sometimes it will not match) Traceback...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.