简体   繁体   English

VBA SQL:根据记录数更新记录

[英]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 表格1

---------------------------
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 表2

--------------------------
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 我只想使用公式计算table1中N2 = 0的那些

N2= MX-N1-RN 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 因此,例如,当ID = 1时,只有一个0,所以我们将所有非0的N1相加

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

and the same in ID=3 and =5 并且在ID = 3和= 5中相同

N2=99-(64)-4 = 31 N2 = 99-(64)-4 = 31

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

Then N2 of that ID will be updated with the new record. 然后,将使用新记录更新该ID的N2。 ID=2 will be ignored because there are three records = 0 and so for ID=4 there are two records =0. ID = 2将被忽略,因为有3条记录= 0,因此对于ID = 4有2条记录= 0。

The updated table will be Table1 更新后的表将是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. 所以我想用sql Query来做,但是我不知道如何正确地完成它。

The code I've done is the following: 我完成的代码如下:

Sql Query: SQL查询:

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: VBA代码:

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. 您在SQL语句中的计算似乎有些偏离。 You want to get 你想得到

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

Your SQL statement will give you 您的SQL语句会给您

N2 = Mx - Sum(N2) - RN N2 = Mx-总和(N2)-RN

You need at least 2 nested queries to achieve your goal: 您至少需要2个嵌套查询才能实现目标:

Query1 will count all zeros in Table1.N2 for each single ID. Query1将为每个单个ID计算Table1.N2中的全零。

Query2 will give you only those IDs where just a single corresponding N2 is zero. Query2将仅为您提供仅一个对应的N2为零的ID。

Both will be nested inside the UPDATE query. 两者都将嵌套在UPDATE查询中。 You could then use DLookUp and DSum to get what you need: 然后,您可以使用DLookUp和DSum获得所需的内容:

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. DLookUp将获得相应的MX和RN值。 The DSum function will sum up all values for the corresponding ID where the N2 is not 0. DSum函数将汇总N2不为0的相应ID的所有值。

I could also think of another solution without DFunctions but it will involve more nested queries with calculated fields. 我也可以想到另一种没有DFunctions的解决方案,但是它将涉及更多带有计算字段的嵌套查询。

它应该看起来像这样

I have derived the query in pure SQL with help of sub queries. 我已经在子查询的帮助下以纯SQL派生了查询。 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. 如果您想了解它的派生方式,则在sql server查询窗口中单独运行子查询(第一行和最后一行除外),即可获得清晰的视图。 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. 您仍然无法理解,然后首先了解sql server中的SubQuery ,然后逐步完成此查询。

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.S

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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