pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Josh Berkus <josh@agliodbs.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: UPDATE Query problem
Date: Thu, 17 Jan 2002 19:59:27 -0800
Message-ID: <web-622662@davinci.ethosmedia.com> (raw)
In-Reply-To: <1979.1011316800@sss.pgh.pa.us>

Tom,

> Sure you can.  You can't alias history in the UPDATE, but you can alias
> it in the subselect, so:
> 
> UPDATE history SET fieldA =
>   (SELECT fieldA FROM history older
>    WHERE older.key = history.key AND
>    older.fieldA IS NOT NULL AND older.fieldB IS NOT NULL AND
>    older.timestamp =
>      (SELECT max(timestamp) FROM history oldest
>       WHERE oldest.key = history.key AND
>       oldest.fieldA IS NOT NULL AND oldest.fieldB IS NOT NULL)),
> fieldB = (SELECT fieldB FROM ... repeat entire subselect above ...)
> WHERE (history.fieldA IS NULL or 
>        history.fieldB IS NULL);

Interesting.  however, it appears to give me the most recent record with
 non-NULL values.  What I want is the most recent record with non-NULL values
 *before* the record I'm trying to update.  In other words, if I have the
 following data:

history
id	timestamp	fieldA	fieldB
1341	6/30/00		KCKG	1
1345	7/31/00		KC	1
1402	8/31/00		NULL	NULL
2799	9/30/00		NULL	NULL
1581	10/31/00	KC	2
1673	11/30/00	KC	2

I want records 1402 and 2799 to be updated from record 1345, not from record
 1673.

> This will work and (AFAIK) is fully SQL-compliant, but it will be
> slower than the dickens because of all those subselects :-(.  Might
> be tolerable if the key field is near-unique and is indexed, but
> heaven help you if not.

The key field is unique.  And slow is OK ... the history-correction program
 runs overnight.  I just can't afford to take a procedural approach and correct
 one record at a time ... there are 200,000 records and growing at a rate of
 8,000 records per month.

> Also, just because PG can handle the above doesn't mean every RDBMS does
> (do I need to name names?).  What products do you really need it to
> be portable to?

Yeah, you guessed it ... MS SQL Server 7.  Which kills custom functions or
 custom aggregates, something that would make this whole process a lot easier.

Thanks for the help!

-Josh Berkus

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



view thread (7+ messages)  latest in thread

Message-ID: <web-622662@davinci.ethosmedia.com>
Permalink:  ../web-622662@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-622662@davinci.ethosmedia.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, tgl@sss.pgh.pa.us
  Subject: Re: UPDATE Query problem
  In-Reply-To: <web-622662@davinci.ethosmedia.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox