Solved

SQL simple update query

Posted on 2008-10-16
4
192 Views
Last Modified: 2012-05-05
I am trying update the following table:

id      SUB           pup          Atxt     btxt
----------------------------------------------
24    ENG           101          A          B
23     ENG          100          A         B
24    ENG           100          null       null
24    ENGLA       100          C          C


I want column Atxt and Btxt to be copied from records that have 'ENGLA' in column sub, TO records where column 'sub' has a value of ENG and where column 'pup' values match and only for records with 'id' value of 24

to end up with a table that looks like this:

id      SUB           pup          Atxt     btxt
----------------------------------------------
24    ENG           101          A          B
23     ENG          100          A         B
24    ENG           100          C         C
24    ENGLA       100          C          C

I was thinking of something like:

update table1

set atxt=btxt
where id = 24 and pup=pup  and sub =eng

but im lost with having 2 separate  where's in the query


Any suggestions?

0
Comment
Question by:nazateam
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22729466
What about this:
update t1
  set atxt= t2.atxt 
    , btxt = t2.btxt
from table1 t1
join table1 t2 
  on t2.id = t1.id
 and t2.pup = t1.pup
 and t2.atxt is null
where t1.id = 24
  and t1.atxt is not null

Open in new window

0
 

Author Comment

by:nazateam
ID: 22729536
Hmmm Perhaps, But where in there am i specifying the source of the update (records with 'ENGLA' under SUB and the destination (records with 'ENG' under SUB?


0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 22729621
yes, that's possible:
update t1

  set atxt= t2.atxt 

    , btxt = t2.btxt

from table1 t1

join table1 t2 

  on t2.id = t1.id

 and t2.pup = t1.pup

 and t2.atxt is not null

 and t2.sub = 'ENGA'

where t1.id = 24

  and t1.atxt is null

  and t1.sub = 'ENG'

Open in new window

0
 

Author Closing Comment

by:nazateam
ID: 31506662
Thanks for this, worked a treat!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now