0

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
Community
  • 1
  • 1
B.Germ
  • 107
  • 1
  • 12
  • Your calculation is confusing, For ID=1, you are adding N1 values of N2 != 0. But For ID=3, you are get N1 value of N2=0. Why ? here also you should take the N1 value of N2 !=0 right ? i.e. N2=33-(64)-2 = -33. – Kavin Jul 26 '17 at 08:09
  • Sorry that was my mistake. I corrected it. – B.Germ Jul 26 '17 at 08:20
  • again mistake in you have mentioned calculation for ID=2,3. You have mentioned wrong MX and RN value for ID=2 & 3. it should be like following For ID=2, N2=99-(64)-4 = 31, For ID=3, N2=87-(58+69+36)-4 = -80 – Kavin Jul 26 '17 at 10:47
  • OMG I didn't notice those either >.< Thank u for the correction :) – B.Germ Jul 26 '17 at 10:55

2 Answers2

1

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.

It should look like this

Thuro G
  • 167
  • 12
  • I don't want to sum where N2=0 if there are more than on 0 for each ID. – B.Germ Jul 26 '17 at 10:13
  • And I tried running ur query it assigned Null values to all. – B.Germ Jul 26 '17 at 10:35
  • My mistake regarding the N2=0. No idea why you get Null values though. It was working perfectly fine for me, though updating doing more as intended. Please check the updated answer, it should solve your problem. – Thuro G Jul 26 '17 at 11:57
1

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

Kavin
  • 303
  • 2
  • 14
  • Thank you for your answer but there is one thing I don't understand, in the first line we can use `FROM` with our update? because I never done it ? `UPDATE Table_1 SET N2= t3.N2 FROM`.. it give me syntax error in it. – B.Germ Jul 26 '17 at 11:47
  • Apparently, it should be possible to use FROM in an UPDATE query. Check https://stackoverflow.com/questions/13881649/update-statement-using-nested-query for instance. However, it is not working on my verison of Access as I get an syntax error. Maybe it was added/removed at some point? – Thuro G Jul 26 '17 at 11:52
  • We can use FROM with update in Sql server but I don't have experience in Access. So i don't know why its not working. You can search with that error in google and find and use the instead of that. – Kavin Jul 26 '17 at 11:56
  • tell me, FROM with UPDATE is only causing the syntax error? or wat ? – Kavin Jul 26 '17 at 12:00
  • For me it does, yes. – Thuro G Jul 26 '17 at 12:03
  • OK @ThuroG.. Your above updated answer is based on my query ? – Kavin Jul 26 '17 at 12:09
  • No its not. I initially answered 2 hours ago, though missed one part of the issue. I adjusted my answer and saw your's afterwards. They are not too alike except from using common abbreviations (t1, t2 etc.), though - if that is what your aiming at. – Thuro G Jul 26 '17 at 12:16
  • Ohh! No. Its my misunderstanding bcoz of text lines above your query (tat time i didnt see your query). ok got it. – Kavin Jul 26 '17 at 12:25