UPDATE Query problem - Mailing list pgsql-sql

From Josh Berkus
Subject UPDATE Query problem
Date
Msg-id web-622592@davinci.ethosmedia.com
Whole thread Raw
Responses Re: UPDATE Query problem  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: UPDATE Query problem  (Stephan Szabo <sszabo@megazone23.bigpanda.com>)
List pgsql-sql
Folks,

I have a database that contains a chronological journal of activity.  Forvarious reasons, this journal contains both
completeand incomplete records,and while all records are timestamped, the primary key is not strictly orderedby
timestamp.

What I want to do is update each incomplete record with the contents of thelast previous complete record.  As a
simple-mindedtest case:
 

CREATE TABLE history AS (
history_id SERIAL PRIMARY KEY,
period_date TIMESTAMP,
fieldA VARCHAR(30),
fieldB INT4 );

CREATE VIEW complete_history_records AS
SELECT history.*
FROM history WHERE fieldA IS NOT NULL and fieldB IS NOT NULL

UPDATE history SET fieldA = chr.fieldA                  fieldB = chr.fieldB
FROM (SELECT complete_history_records.*     WHERE ??? ) chr
WHERE (history.fieldA IS NULL or          history.fieldB IS NULL);

The problem is that I cannot figure out a subselect that will allow me toselect the last complete history record prior
tothe one being updated.  Itseems like I need to reference a field in the main query in the subselect,which can't be
done.

To further hamper things, for portability reasons, I can use neither SELECTDISTINCT ON nor custom functions.  

I'm stumped.  Please offer suggestions!

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: User Permissions
Next
From: Tom Lane
Date:
Subject: Re: UPDATE Query problem