[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL UPDATE query based on calculations

Posted on 2011-09-13
11
Medium Priority
?
375 Views
Last Modified: 2012-05-12
Part of the criteria for this update query is a calculation.  Currently this figure 'liTotalCost' is made just once.  This number needs to be calculated for each record.  How do I integrate these calculations into the query?  I am working with VB6, Access 2003.  Thank you for your help.  
Dim strSQL5 As String
Dim liTotalCost As Long
Dim liBalMD As Long
Dim liEstMD As Long
Dim liActMD As Long

liBalMD = (CDbl([A]) + CDbl([B]) + CDbl([C]) 
liEstMD = (CDbl([X]) + CDbl([Y]) + CDbl([Z]) 

If liBalMD < 0 Then
    liActMD = (liEstMD + liBalMD)
Else
    liActMD = liEstMD
End If
liTotalCost = (CDbl([ActMDCost]) - liActMD)

strSQL5 = "Update DISTINCTROW [ProjOverview] o Left Join (SELECT ProjID, SUM(Amount) AS SumOfAmount From JONProjJoins GROUP BY ProjID) s ON s.ProjID = o.IDNmbr Set [Status] = 'Complete', [Complete] = Now , [TotalServicesAct] = [TotalServicesEst] Where (([Status] = 'Funded') AND (" & liTotalCost & " > Nz(s.SumOfAmount, 0))) OR (([Status] = 'Funded') AND (" & liTotalCost & ") > ([TotalEstimate]))"
DoCmd.RunSQL strSQL5

Open in new window

0
Comment
Question by:KimD2
[X]
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
  • 5
  • 5
11 Comments
 
LVL 4

Expert Comment

by:GeoffHarper
ID: 36531307
It looks OK to me, except for an extra paren.  It should be:
...Where (([Status] = 'Funded') AND (" & liTotalCost & " > Nz(s.SumOfAmount, 0)) OR (([Status] = 'Funded') AND (" & liTotalCost & ") > ([TotalEstimate]))"
DoCmd.RunSQL strSQL5

Open in new window

0
 

Author Comment

by:KimD2
ID: 36531380
"liTotalCost" needs to be re-calculated for each record.  Is it being recalculated when written like this?  I am new to this and not sure whay the query is not working.  Thank you for the paren note.  Unfortunately that was not it.  I do not receive an error - just the records that should be updated are not.    I appreciate your input.
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36531394
Put your Sum query in the Where clause

strSQL5 = "Update DISTINCTROW [ProjOverview] o " & _
             "Set [Status] = 'Complete', " & _
               "[Complete] = Now , " & _
       "[TotalServicesAct] = [TotalServicesEst] " & _
           "Where (([Status] = 'Funded') " & _
             "AND (" & liTotalCost & " > Nz(( SELECT SUM(Amount) AS SumOfAmount " & _
                     "From JONProjJoins s " & _
                    "where s.ProjID = o.IDNmbr " & _
                    "GROUP BY ProjID), 0))) OR (([Status] = 'Funded') AND (" & liTotalCost & ") > ([TotalEstimate]))"
DoCmd.RunSQL strSQL5
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:KimD2
ID: 36531562
Thank you but it is still not updating.  Do you have any other suggestions?  
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36531664
In what way is it not updating ?  Are you getting an error when you run the Update Query ?
0
 

Author Comment

by:KimD2
ID: 36531686
I do not get any errors.  It seems to run but the fields are not updated.  In the records where the criteria is met the fields (Status, Date.. ) remain unchanged.
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36531749
ok, check your where clause, step through the code and see if the where returns data.

Example:

Open a SQL editor:


 
Select * from [ProjOverview] o
Where ( ( [Status] = 'Funded') 
    AND ( 1000 > Nz(
                     ( SELECT SUM(Amount) AS SumOfAmount 
                        From JONProjJoins s 
                       where s.ProjID = o.IDNmbr 
                       GROUP BY s.ProjID), 0
                     )
                   )
             ) OR ( [Status] = 'Funded' AND 1000 > [TotalEstimate])

Open in new window

0
 
LVL 14

Accepted Solution

by:
Brook Braswell earned 2000 total points
ID: 36531890
The more I look at this the more I want to remove some of these parenthesis and help you clean it up  :)



Dim strSQL5 As String
Dim SumSql  As string
Dim liTotalCost As Long
Dim liBalMD As Long
Dim liEstMD As Long
Dim liActMD As Long

liBalMD = (CDbl([A]) + CDbl([B]) + CDbl([C]) 
liEstMD = (CDbl([X]) + CDbl([Y]) + CDbl([Z]) 

If liBalMD < 0 Then
    liActMD = (liEstMD + liBalMD)
Else
    liActMD = liEstMD
End If
liTotalCost = (CDbl([ActMDCost]) - liActMD)

SumSql = "SELECT SUM(Amount) AS SumOfAmount " & _
           "FROM JONProjJoins s " & _
          "WHERE s.ProjID = o.IDNmbr " & _
          "GROUP BY s.ProjID"

strSQL5 = "UPDATE [ProjOverview] o " & _
             "SET [Status] = 'Complete', " & _
                 "[Complete] = Now , " & _
                 "[TotalServicesAct] = [TotalServicesEst] " & _
           "WHERE [Status] = 'Funded' " & _
             "AND (" & liTotalCost & " > Nz(" & SumSql & ", 0 ) " & _
              " OR " & liTotalCost & " > [TotalEstimate] )"
DoCmd.RunSQL strSQL5

Open in new window

0
 

Author Comment

by:KimD2
ID: 36532485
Ok -- the where does return data.  
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36532552
Make sure to place parens around the SumSql
SumSql = "( SELECT SUM(Amount) AS SumOfAmount " & _
           "FROM JONProjJoins s " & _
          "WHERE s.ProjID = o.IDNmbr " & _
          "GROUP BY s.ProjID )"

Open in new window

0
 

Author Closing Comment

by:KimD2
ID: 36532851
Thank you!   I revisited the data --- good data combined with your code worked perfectly.   Thank you also for the clean formatting.  It is certainly much clearer and easier to read.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

650 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