SQL 2008: Update multiple columns from select of same table

Experts,
need a quick hand with this.
My table has about 90 columns. On the occasional row about 80 of the columns are null.
I am looking to update the 80 columns using information selected from any other row of the same order number.
As the null columns are all header information, they are all the same for that order number.

Here is a real simplified example of the table


Table = ORDERS						
Order	TYPE	NAME	        ADDR1	                CITY	           QTY	ITEM
101	        R	        Jon Smith	123 My Street	Toronto	      10	ABC12
101	        R	        Jon Smith	123 My Street	Toronto	       7	ABC33
101	        O	        null	                  null	                 null	               5	ABC432

Open in new window


In this example I want to update NAME, ADDR1 and CITY
where the TYPE is O  from Any line (Max or Min?) where the TYPE is R.

Please advise
LVL 1
JDCamAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
The process is such:
Define the logic for determining if a row needs to be updated.  NULL values in what specific columns constitute needing an update?
Define the logic for determining which other row of the same OrderID will be used to fill those gaps?  Is there a date field that can be used to select the most recent entry?

After that is done the query isn't too difficult.

BTW, does the table have a unique identifier?
ste5anSenior DeveloperCommented:
What kind of table is it? Normal, report, dimension or fact?
JDCamAuthor Commented:
Here is where I am at the moment..... seems like it will work.  lots of typing however to get all the columns included.

Update Orders
Set
NAME = a.NAME,
ADDR1 = a.ADDR1,
CITY = a.CITY
FROM
(Select NAME, ADDR1, CITY from ORDERS
where TYPE = 'R' and Order = 101
Group by NAME, ADDR1, CITY) as a
where Type = 'O'
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Vikas GargAssociate Principal EngineerCommented:
Hello,

This the query which you can use for updating the record dynamically for any order rather then given hard-code value for order

update A
set 
A.NAME = B.NAME,
A.CITY = B.CITY,
A.ADDR1 = B.ADDR1
from ExcelTable A
inner join (SELECT DISTINCT [ORDER],NAME,CITY,ADDR1 FROM ExcelTable WHERE [TYPE] = 'R') B on 
A.[ORDER] = B.[ORDER]
WHERE A.TYPE = 'O'

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
There's no better way to do what you want. Your solution is good and just missed the INNER JOIN:
Update Orders Set 
 o.NAME = a.NAME,
 o.ADDR1 = a.ADDR1,
 o.CITY = a.CITY
FROM  ORDERS o
INNER JOIN (
    Select NAME, ADDR1, CITY 
    from ORDERS 
    where TYPE = 'R' and Order = 101
    Group by NAME, ADDR1, CITY) as a
ON o.Order = a.Order
where o.Type = 'O' 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
There is another way using merge -
merge into mytable a
using
(select distinct order, name, addr1, city
 from mytable
 where type = 'R') b
on (a.order = b.order)
when matched then
update set a.name = b.name, a.addr1 = b.addr1, a.city = b.city
where a.type = 'O';
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.