Обсуждение: Correct update statement
Hi
Please help, we are using postgreSQL 9.2.4. I need to update over 9000 rows. See the query below: A table called contact has got addr_id field as null which is incorrect. So now I need to update contact table for each account (cus_acno is in cus table) where contact_addr_id is null. For example using the first of the results below: I need take set addr_id (in contact table) to 187479 where cus_acno = 243492 and con_id = 119360
Example:
select distinct(cus_acno), contact.con_id, address.addr_id from address join person using (addr_id) join cus using (per_id) join link_contact using (cus_acno) join contact using (con_id) where contact.addr_id is null;
cus_acno | con_id | addr_id
----------+--------+---------
243492 | 119360 | 187479
393701 | 119824 | 458532
388538 | 118413 | 453178
Thanks
CONFIDENTIALITY NOTICE The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.
body p { margin-bottom: 0cm; margin-top: 0pt; } <body style="direction: ltr;" bidimailui-detected-decoding-type="UTF-8" bgcolor="#FFFFFF" text="#000000"> update contacts set addr_id=b.addr_id from (select distinct(cus_acno), contact.con_id, address.addr_id from address join person using (addr_id) join cus using (per_id) join link_contact using (cus_acno) join contact using (con_id) where contact.addr_id is null) b where contacts.con_id=b.con_id On 05/15/2014 09:19 AM, Khangelani Gama wrote: <blockquote cite="mid:dd09ad0dac3046ab93e597688a2a31ff@mail.gmail.com" type="cite"> <meta name="Generator" content="Microsoft Word 14 (filtered medium)"> <!-- /* Font Definitions */ @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;} @font-face {font-family:Tahoma; panose-1:2 11 6 4 3 5 4 4 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:11.0pt; font-family:"Calibri","sans-serif";} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;} p.MsoAcetate, li.MsoAcetate, div.MsoAcetate {mso-style-priority:99; mso-style-link:"Balloon Text Char"; margin:0in; margin-bottom:.0001pt; font-size:8.0pt; font-family:"Tahoma","sans-serif";} span.EmailStyle17 {mso-style-type:personal-compose; font-family:"Calibri","sans-serif"; color:windowtext;} span.BalloonTextChar {mso-style-name:"Balloon Text Char"; mso-style-priority:99; mso-style-link:"Balloon Text"; font-family:"Tahoma","sans-serif";} .MsoChpDefault {mso-style-type:export-only; font-family:"Calibri","sans-serif";} @page WordSection1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in;} div.WordSection1 {page:WordSection1;} --> Hi  Please help, we are using postgreSQL 9.2.4. I need to  update over 9000 rows. See the query below: A table called contact has got addr_id field as null which is incorrect. So now I need to update contact table for each account (cus_acno is in cus table) where contact_addr_id is null. For example using the first of the results below: I need take set addr_id (in contact table)  to 187479 where cus_acno = 243492 and con_id = 119360   Example:  select distinct(cus_acno), contact.con_id, address.addr_id from address join person using (addr_id) join cus using (per_id) join link_contact using (cus_acno) join contact using (con_id) where contact.addr_id is null;  cus_acno | con_id | addr_id ----------+--------+---------   243492 | 119360 | 187479   393701 | 119824 | 458532   388538 | 118413 | 453178  <span style="font-size:10.0pt;font-family:"Courier New";color:blue" lang="EN-ZA"> <span lang="EN-ZA"> Thanks    CONFIDENTIALITY NOTICE The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.
Thank very much
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Sim Zacks
Sent: Thursday, May 15, 2014 10:42 AM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Correct update statement
update contacts set addr_id=b.addr_id
from
(select distinct(cus_acno), contact.con_id, address.addr_id from address join person using (addr_id) join cus using (per_id) join link_contact using (cus_acno) join contact using (con_id) where contact.addr_id is null) b
where contacts.con_id=b.con_id
On 05/15/2014 09:19 AM, Khangelani Gama wrote:
Hi
Please help, we are using postgreSQL 9.2.4. I need to update over 9000 rows. See the query below: A table called contact has got addr_id field as null which is incorrect. So now I need to update contact table for each account (cus_acno is in cus table) where contact_addr_id is null. For example using the first of the results below: I need take set addr_id (in contact table) to 187479 where cus_acno = 243492 and con_id = 119360
Example:
select distinct(cus_acno), contact.con_id, address.addr_id from address join person using (addr_id) join cus using (per_id) join link_contact using (cus_acno) join contact using (con_id) where contact.addr_id is null;
cus_acno | con_id | addr_id
----------+--------+---------
243492 | 119360 | 187479
393701 | 119824 | 458532
388538 | 118413 | 453178
Thanks
CONFIDENTIALITY NOTICEThe contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidentialinformation of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyoneother than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediatelyand destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liabilityfor unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.
CONFIDENTIALITY NOTICE The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.