473,773 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Complex Update Queries with Fromlist

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

update orders set RequiredDate =
(case when c.City IN ('Seattle','Por tland') 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','Por tland') then date(o.OrderDat e) + 1
else date(o.OrderDat e) + 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: md*****@dexterc haney.com

Nov 23 '05 #1
1 3272
Mark Dexter wrote:

update orders set RequiredDate =
(case when c.city in ('Seattle','Por tland') then date(o.OrderDat e) + 1
else date(o.OrderDat e) + 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.
That's because of the explicit join you're using. The "orders o" in the
FROM clause is different from the "orders" table in the UPDATE clause.

I'd probably use something like:

UPDATE orders
SET RequiredDate = ...
FROM
customers c
WHERE
orders.Customer id = c.Customerid
AND c.region in (...)

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.


Patches to the documentation are always gratefully received. The latest
version of the documentation is available on the main website (follow
the developers link). Contributions to the docs mailing-list in plain
text are generally fine.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

116
7553
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data and some who couldn't but that it wasn't important right now. And I said, 'sure, we can do that later'. So now I've developed an app without any thought to security and am trying to apply it afterwards. Doh!, doh! and triple doh!
1
3211
by: ravi | last post by:
I have created the following interest to calculate the interest for the following currency pairs. I have tried to combine them in macros using conditions but the next query that is run in the macro ends up deleting the previous interest value that has been generated by the query. For example if query 1 is run on the table with currency pair USD/CHF then the interest will be updated without any problem but if there is another entry in the...
1
3426
by: Aaron | last post by:
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; _____________________________________________________ SELECT "criteria"
8
2962
by: Steve Jorgensen | last post by:
Mailing List management is a good example of a case where my conundrum arises. Say there is a m-m relationship between parties and groups - anyone can be a member of any combintation of groups. Now, let's say the user wants to be able to send mailings to people who have various combinations of membership and non-membership in those groups. Here's a medium-complex example: (Knitting Group or Macrame Group) and Active Contact and Mailing...
3
11705
by: CSN | last post by:
I'm trying to do: update nodes n1 set n1.parent_id=(select n2.id from nodes n2 where n2.key=n1.parent_id); To set parent_id to the id of the parent (rather than the key). Would UPDATE FROM fromlist work? I couldn't find any examples of it's use. TIA,
3
2449
by: Slower Than You | last post by:
I am trying to write an SQL UPDATE statement for an MSAccess table and am having some problems getting my head around it. Can anyone help? TableName: CustTransactions TransactionKey AutoNumber (Primary Key) CustomerID Long Integer (Non-unique index) AmountSpent Double CustSelected Boolean What I would like to do is, for all of the records in descending order
1
1400
by: koehlerc14 | last post by:
So here is the deal, I am attempting to make what is the most complex form i have made yet. It really is not much, but as an amatuer it is a little overwhelming. Here's a few critical background point.s I have 2 Master Databses - I will be linking in only 1 table from each - tblMaster Each record contains demographic data and about 40 "variable" fields which consist of a name for each variable, and its data Variable Field 20-29 -...
0
2455
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following 1. The wine name, grape variety, year, winery, and region 2. The minimum cost of wine in the inventory 3. The number of bottles available at the minimum price 4. The total number of bottles available at any price 5. The total number of unique...
11
1963
by: sloney | last post by:
Hello All! I have an update query that is rather large and I keep getting a "Query is too complex" error. Does anyone know the limitations on update queries for MS Access 2000? I am running on Windows XP. The query has about 95 actions (updates) for approximately 35 tables. Thank you for the info! Sloney
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.