Solved

Update multiple fields with inner select

Posted on 2013-01-20
9
255 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 142

Expert Comment

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

Author Comment

by:AndyC1000
Comment Utility
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
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.

 

Author Comment

by:AndyC1000
Comment Utility
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 142

Expert Comment

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

Author Comment

by:AndyC1000
Comment Utility
I replaced the semi-colons with commas no change.
0
 

Author Comment

by:AndyC1000
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

771 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

10 Experts available now in Live!

Get 1:1 Help Now