Correct update statement

Started by Khangelani Gamaalmost 12 years ago3 messagesgeneral
Jump to latest
#1Khangelani Gama
kgama@argility.com

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.

#2Sim Zacks
sim@compulab.co.il
In reply to: Khangelani Gama (#1)
Re: Correct update statement

<html style="direction: ltr;">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<style type="text/css">body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
</head>
<body style="direction: ltr;"
bidimailui-detected-decoding-type="UTF-8" bgcolor="#FFFFFF"
text="#000000">
<div class="moz-cite-prefix">update contacts set addr_id=b.addr_id <br>
from<br>
(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<br>
where contacts.con_id=b.con_id<br>
<br>
On 05/15/2014 09:19 AM, Khangelani Gama wrote:<br>
</div>
<blockquote
cite="mid:dd09ad0dac3046ab93e597688a2a31ff@mail.gmail.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="Generator" content="Microsoft Word 14 (filtered
medium)">
<style><!--
/* 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;}
--></style>
<div class="WordSection1">
<p class="MsoNormal">Hi</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">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 <b>addr_id </b>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</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Example:</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">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;<br>
 cus_acno | con_id | addr_id <br>
----------+--------+---------<br>
   243492 | 119360 |  187479<br>
   393701 | 119824 |  458532<br>
   388538 | 118413 |  453178<br>
<br>
</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"><span
style="font-size:10.0pt;font-family:&quot;Courier
New&quot;;color:blue" lang="EN-ZA"> </span><span
lang="EN-ZA"> </span></p>
<p class="MsoNormal">Thanks</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
</div>
<pre>
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.

</pre>
</blockquote>
<br>
</body>
</html>

#3Khangelani Gama
kgama@argility.com
In reply to: Sim Zacks (#2)
Re: Correct update statement

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 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.

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.