简体   繁体   中英

VBA SQL: update record based on the count of record

I have two diffrent tables and i want to update one of them base on several conditions. My tables are:

Table1

---------------------------
ID   |     N1   |    N2
---------------------------
1          22        12
1           5        0
1          87        12
2          67        0
2           6        0
2           3        0
2          60        12
3          55        0
3          64        12
4           8        0
4          75        12
4           4        0
5          58        12
5          69        12
5          36        12
5           3        0

Table2

--------------------------
ID   |     MX   |   RN
--------------------------
1          33        2
2          45        3
3          99        4
4          67        2
5          87        4

I want to calculate only those with one N2 = 0 in the table1 using the formula

N2= MX-N1-RN

So for example when ID=1 there is only one 0 so we will sum all the N1 that are not 0

N2=33-(87+22)-2 = -78

and the same in ID=3 and =5

N2=99-(64)-4 = 31

N2=87-(58+69+36)-4 = -80

Then N2 of that ID will be updated with the new record. ID=2 will be ignored because there are three records = 0 and so for ID=4 there are two records =0.

The updated table will be Table1

---------------------------
ID   |     N1  |     N2
---------------------------
1          22        12
1           5       -78
1          87        12
2          67         0
2           6         0
2           3         0
2          60        12
3          55        31
3          64        12
4           8         0
4          75        12
4           4         0
5          58        12
5          69        12
5          36        12
5           3       -81

So I wanted to do it using sql Query but I didn't know how to complete it correctly.

The code I've done is the following:

Sql Query:

UPDATE TABLE1 AS I INNER JOIN TABLE2 AS P ON I.ID = P.ID
SET I.N2 =P.MX- SUM(I.N2)- (P.RN)
WHERE (SELECT COUNT(S.ID) FROM TABLE1  AS S
WHERE S.ID = " & [S.ID] & "
AND N2 = 0) =1;

VBA code:

Private Sub GET_CAL()
    DoCmd.SetWarnings False
    DoCmd.OpenQuery "Query1"
    DoCmd.SetWarnings True
End Sub

Your calculation within your SQL statement seems a bit off. You want to get

N2 = Mx - Sum(N1=0) - RN

Your SQL statement will give you

N2 = Mx - Sum(N2) - RN

You need at least 2 nested queries to achieve your goal:

Query1 will count all zeros in Table1.N2 for each single ID.

Query2 will give you only those IDs where just a single corresponding N2 is zero.

Both will be nested inside the UPDATE query. You could then use DLookUp and DSum to get what you need:

UPDATE Table1 As t3
SET t3.N2 = DLookUp("MX","Table2","ID = " & t3.ID) -
DSum("N1","Table1","ID = " & t3.ID & " And N2 <>0") -
DLookUp("RN","Table2","ID = " & t3.ID)
WHERE t3.N2 = 0 And t3.[ID]
      In (SELECT t2.ID
          FROM (SELECT t1.ID, t1.N2
                FROM Table1 AS t1
                WHERE t1.N2 = 0)  AS t2
          GROUP BY t2.ID
          HAVING Count(t2.N2)=1)

DLookUp will get the corresponding MX and RN values. The DSum function will sum up all values for the corresponding ID where the N2 is not 0.

I could also think of another solution without DFunctions but it will involve more nested queries with calculated fields.

它应该看起来像这样

I have derived the query in pure SQL with help of sub queries. This query will work perfectly for you.

UPDATE Table_1 SET N2= t3.N2 FROM

(SELECT t1.ID,t2.SumOfN1,t1.MX,t1.RN (t1.MX-t2.SumOfN1-t1.RN)N2 FROM

(SELECT * FROM Table_2 WHERE ID in (SELECT ID FROM Table_1 WHERE N2=0 GROUP BY ID HAVING(COUNT(ID)<=1)))t1,
(SELECT ID,SUM(N1)SumOfN1 FROM Table_1 WHERE N2!=0 GROUP BY ID)t2

WHERE t1.id=t2.id )t3


WHERE Table_1.ID=t3.ID and Table_1.N2=0

The above query will do your need. If you want to understand how it derived, then run the subquery (except 1st and last line) alone in sql server query window and you can get the clear view. and you can run each sub query separately. still u cant understand then first learn about SubQuery in sql server and then walk through this query.

Note: The thing is you should understand, you can ask the question with clearly. No one will not do the code work for you/us. They will help us while we stuck with problem/error/issue/unknown things from what u tried but never help for whole functionality and also u don't expect it.

Hope it helps for you. (don't forget to mark as answer and vote)

Thanks, Kavin.S

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM