Solved

Update query

Posted on 2011-02-22
8
285 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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
 

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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

830 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