Re: UPDATE Query problem
От | Josh Berkus |
---|---|
Тема | Re: UPDATE Query problem |
Дата | |
Msg-id | web-622662@davinci.ethosmedia.com обсуждение исходный текст |
Ответ на | Re: UPDATE Query problem (Tom Lane <tgl@sss.pgh.pa.us>) |
Список | pgsql-sql |
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 withnon-NULL values. What I want is the most recentrecord with non-NULL values*before* the record I'm trying to update. In other words, if I have thefollowing 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 record1673. > 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 programruns overnight. I just can't afford to take aprocedural approach and correctone record at a time ... there are 200,000 records and growing at a rate of8,000 recordsper 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 orcustom aggregates, something that would make thiswhole process a lot easier. Thanks for the help! -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 по дате отправления: