Solved

Update multiple fields with inner select

Posted on 2013-01-20
9
289 Views
Last Modified: 2013-02-18
Hello,

I'm wondering if its possible to extend the following query to include multiple set statements based on different where clauses in one query?

Is there an easier way than repeating this query?

UPDATE ProfileData, SummaryTable SET ProfileData.CarValueO1 = [Total]*[CarValue]
WHERE (((SummaryTable.CarType)="Sedan") AND ((SummaryTable.NumberDrivers)="NumberDrivers1") AND ((SummaryTable.NumberClaims)="NumberClaims1"));

Open in new window


Thanks
0
Comment
Question by:AndyC1000
  • 5
  • 4
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38798306
I have described the options to do UPDATE WITH JOIN in this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html
the ms access syntax is at the end, in the additional comments, so yes, it's possible
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38798310
of course, you may need to combine that with IIF() expressions, but as your requirements is not 100% clear ...
0
 

Author Comment

by:AndyC1000
ID: 38800230
Thanks for your response.

Please provide syntax the to:
SET ProfileData.CarValueO2 = [Total]*[CarValue]
WHERE (((SummaryTable.CarType)="Sedan") And ((SummaryTable.NumberDrivers)="NumberDrivers2") And ((SummaryTable.NumberClaims)="NumberClaims2"));

within the update query below.

UPDATE ProfileData, SummaryTable SET ProfileData.CarValueO1 = [Total]*[CarValue]
WHERE (((SummaryTable.CarType)="Sedan") And ((SummaryTable.NumberDrivers)="NumberDrivers1") And ((SummaryTable.NumberClaims)="NumberClaims1"));

Open in new window

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800241
I would go with something along like this (not tested):
UPDATE ProfileData, SummaryTable 
 SET ProfileData.CarValueO1 = IIF( AND( SummaryTable.NumberDrivers = "NumberDrivers1" ; SummaryTable.NumberClaims ="NumberClaims1") ; [Total]*[CarValue] ; CarValueO1 )
  , ProfileData.CarValueO2 = IIF( AND( SummaryTable.NumberDrivers = "NumberDrivers2" ; SummaryTable.NumberClaims ="NumberClaims2") ; [Total]*[CarValue] ; CarValueO2 )
WHERE ((SummaryTable.CarType)="Sedan") ;

Open in new window

0
 

Author Comment

by:AndyC1000
ID: 38800293
I ran the query its says: syntax error missing operator in query expression 'IIF( AND ( SummaryTable.NumberDrivers = "NumberDrivers1"; SummaryTable.NumberClaims = "NumberClaims1"); [Total]*[CarValue];

I was unable to save the query because of the syntax error.  I've attached an example database.
profiledb.accdb
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800302
I used ";" you may need to use "," for the arguments...
0
 

Author Comment

by:AndyC1000
ID: 38800379
I replaced the semi-colons with commas no change.
0
 

Author Comment

by:AndyC1000
ID: 38803617
I made the following change to the query although now it says syntax error in UPDATE statement.

UPDATE ProfileData, SummaryTable 
SET ProfileData.CarValueO1 = IIF( SummaryTable.NumberDrivers = "NumberDrivers1" AND SummaryTable.NumberClaims ="NumberClaims1") , [Total]*[CarValue] , CarValueO1 ), 
ProfileData.CarValueO2 = IIF( SummaryTable.NumberDrivers = "NumberDrivers2" AND SummaryTable.NumberClaims ="NumberClaims2") , [Total]*[CarValue] , CarValueO2 )
WHERE ((SummaryTable.CarType)="Sedan");

Open in new window

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 38865015
this should work better:
UPDATE ProfileData p
INNER JOIN SummaryTable  s
SET p.CarValueO1 = IIF( s.NumberDrivers = "NumberDrivers1" AND s.NumberClaims ="NumberClaims1") , [Total]*[CarValue] , CarValueO1 ), 
p.CarValueO2 = IIF( s.NumberDrivers = "NumberDrivers2" AND s.NumberClaims ="NumberClaims2") , [Total]*[CarValue] , CarValueO2 )
WHERE ((s.CarType)="Sedan");

Open in new window

references:
http://www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html           
http://www.experts-exchange.com/Database/Miscellaneous/A_11135-Why-should-I-use-aliases-in-my-queries.html
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

685 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