Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update through nested query 1

Status
Not open for further replies.

bryandj23

IS-IT--Management
Aug 18, 2003
105
US
Hi All. Attempting to make some changes to our database. I'm able to query the results that need to be changed, I just can't quite figure out how to update a field within the results.

Customer Table
==============
CustId - GUID identifier for customer.
SalesRep - Code for SalesRep at Customer Level.

Sales Table
===========
CustId - Relationship to customer.
SalesId - GUID identifier for sale.
SaleDate - Date of Sale
SalesRep - SalesRep for particular sale.

Query: Show sales where SalesRep <> !!7, but SalesRep at Customer Level is !!7.

Task: Change SalesRep at Sales level to !!7 where SalesRep isn't !!7 at the Sales Level, but is !!7 at Customer Level.

Successful query to display sales that need to be changed:
Code:
(select * from Sales where CustId in
(select CustId from Customer where SalesRep = '!!7')
and SalesRep <> '!!7'
and SaleDate <= '2008-09-25 00:00:00.000'
and Status = 'A')

This query gives me the records that need the "SalesRep" field to be changed. However, I'm lost at how to go about updating the Sales table from here.

I need:

Code:
Update Sales
Set SalesRep = '!!7'
Where [query results from previous query]

Would this be a place where an INNER JOIN comes into play? Any help would be sincerely appreciated.

Thanks in advance.

 
Code:
update Sales
set  Sales.SalesRep ='!!7'
from sales
inner join Customer 
on Customer.CustId  = sales.CustId 
and Customer.SalesRep ='!!7'
and sales.SalesRep <>'!!7'
 
pwise...

Thanks so very much!!! After messing with this for awhile, and not quite understanding "INNER JOIN"s, this makes it much much clearer!

Ran your code, and it processed the 133 rows that I needed. Thank you very much!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top