Solved

Update query

Posted on 2011-02-22
8
262 Views
Last Modified: 2012-05-11
I have the folllowing table named TechList
with the following sample data

TotLaborPartsAll    TotLaborPartsTA   TotLaborPartsXTR  PartsLabor
3324.73                   319.92                 1                              0  
400                          500                       2                             0



I am trying to write a query that will update partsLabor with the following

If TotLaborPartsALL > TotLaborPArtsTA +  TotLaborPartsXTR then totLaborPartsall - (totlaborPartsta - totlaborpartsxtr) else 0

Using the sample data above

 partsLabor in the first row should be set to 3003.81
(3324.73 - (319.92 + 1))

partsLabor in the second row should be set to 0 since TotLaborPartsALL is not greater than TotLaborPArtsTA +  TotLaborPartsXTR



0
Comment
Question by:johnnyg123
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 34954649
Update table TechList set partsLabor=
     CASE
          WHEN (TotLaborPartsALL > TotLaborPArtsTA +  TotLaborPartsXTR)
                    THEN totLaborPartsall - (totlaborPartsta - totlaborpartsxtr)
          ELSE 0
      END
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 34954670
test this

update TechList
set PartsLabor=iif([TotLaborPartsALL] > (nz([TotLaborPArtsTA] +  nz([TotLaborPartsXTR])), [TotLaborPartsALL]-(nz([TotLaborPArtsTA] - nz([TotLaborPartsXTR])),0)
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 34954671
sorry, the first line should be

Update TechList set partsLabor =
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34954704
Update table TechList
set
partsLabor=IIf(TotLaborPartsALL > TotLaborPArtsTA +  TotLaborPartsXTR, totLaborPartsall - (totlaborPartsta - totlaborpartsxtr),0)

assuming all have numeric values, if some may have null values, use NZ(col, 0) for all columns that may have null values like

Update table TechList
set
partsLabor=IIf(NZ(TotLaborPartsALL,0) > NZ(TotLaborPArtsTA,0) + NZ(TotLaborPartsXTR,0), NZ(totLaborPartsall,0) - (NZ(totlaborPartsta,0) - NZ(totlaborpartsxtr,0)),0)
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:johnnyg123
ID: 34954883
got the following to work


UPDATE TechList SET PartsLabor = IIf(nz(TotLaborPartsALL,0)>nz(TotLaborPArtsTA,0)+nz(TotLaborPartsXTR,0),nz(totLaborPartsall,0)-(nz(totlaborPartsta,0)+nz(totlaborpartsxtr,0)),0);
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34955086
the one worked is the one I posted but you did not give me any points :(
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34955151
HainKurt,
sorry but, the query you posted will give you an error.
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34957512
:) I forgot to remove keyword "table" from the query :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

19 Experts available now in Live!

Get 1:1 Help Now