Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Update multiple fields with inner select

Posted on 2013-01-20
9
280 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

840 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