Update with ORDER BY and LIMIT

From: Paul M Foster <paulf(at)quillandmouse(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Update with ORDER BY and LIMIT
Date: 2011-08-08 20:24:15
Message-ID: 20110808202415.GE21240@quillandmouse.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Two tables:

1) cust (one record each customer)
contains:
a) lpmtdt (date = last payment date)
b) lpmtamt (numeric = last payment amount)
c) custno (varchar(6) = customer string)
2) cashh (one record each income/cash transaction)
contains
a) custno (varchar(6) = customer string)
b) rcptamt (numeric = amount of receipt)
c) rcptdt (date = date of receipt)

For whatever reason, the cust.lpmtamt and cust.lpmtdt are sometimes
lacking values and shouldn't be. I want to update the customer table to
update these values from the cashh table. I don't want to use an
internal function. The PG version is 8.X.

I can get the proper updating record with:

SELECT rcptamt, rcptdt FROM cashh WHERE custno = 'COL1' ORDER BY rcptdt
DESC LIMIT 1;

(This gives me the latest cash receipt for this customer.)
But I can't seem to merge this with an "UPDATE cust ..." query so the
update happens in one step.

Any help?

Paul

--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2011-08-08 21:34:14 Re: Update with ORDER BY and LIMIT
Previous Message pasman pasmański 2011-08-08 20:01:58 Re: Problem with planner