Complex Update Queries with Fromlist

From: "Mark Dexter" <MDEXTER(at)dexterchaney(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Complex Update Queries with Fromlist
Date: 2004-10-14 23:55:17
Message-ID: 5E8F9F5B63726C48836757FE673B584E010EE031@dcimail.dexterchaney.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In Microsoft SQL Server, I can write an UPDATE query as follows:

update orders set RequiredDate =
(case when c.City IN ('Seattle','Portland') then o.OrderDate + 2 else
o.OrderDate + 1 end)
from orders o
join customers c on
o.Customerid = c.Customerid
where c.region in ('WA','OR')

This query finds 47 rows matching the WHERE clause and updates the
RequiredDate in the Orders table based on data in the orders table and
the customer table for these 47 rows.

It appears that I can do the same thing in Postgres with the following
syntax:

update orders set RequiredDate =
(case when c.city in ('Seattle','Portland') then date(o.OrderDate) + 1
else date(o.OrderDate) + 2 end)
from orders o
join customers c on
o.Customerid = c.Customerid
where c.region in ('WA','OR')
and orders.orderid = o.orderid

The only difference being that I need to add the join at the end to join
the orders table in the update statement with the "orders o" table in
the fromlist.

First, does this look correct? It appears to work the way I want.
Second, it would be really nice if there was better documentation of the
UPDATE statement in Postgres, including examples of this type.

Thanks.

Mark Dexter
Dexter + Chaney
9700 Lake City Way NE, Seattle, WA 98115-2347
Direct Phone: 206.777.6819 Fax: 206-367-9613
General Phone: 800-875-1400
Email: mdexter(at)dexterchaney(dot)com

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mike Mascari 2004-10-15 00:19:39 7.4 in-lining of SQL functions
Previous Message Scott Frankel 2004-10-14 23:16:06 attn.: psycopg users -- simple question