如何在更新语句中正确使用 Avg 函数?

发布于 2024-12-03 00:52:18 字数 970 浏览 1 评论 0原文

我试图获取插入表中的每个 MIUID 的平均值。相关的表和列信息如下:

Table1.[MIU ID]、Table1.[Avg RSSI]

Table2.MIUID、Table2.MeanNum

如果我只是使用 select 语句,我会执行以下操作:

Select DISTINCT Table1.[MIU ID], Avg(Table1.[Avg RSSI]) as MeanNum
From Table1
GROUP BY Table1.[MIU ID]  

但是我需要此信息插入到表 2 的列中。我已经尝试了以下内容和以下内容的变体,我得到的错误是它不允许我使用 Group By,另一个错误表明 MeanNum 不是聚合函数的一部分。

UPDATE Table2  
INNER JOIN Table1
ON Table2.MIUID = Table1.[MIU ID]  
SET Table2.MeanNum = Avg([Table1].[Avg RSSI]);

我尝试过的另一个查询是:

UPDATE Table2  
SET Table2.MeanNum = Avg([Table1].[Avg RSSI]) 
WHERE Table2.MIUID = Table1.[MIU ID]
Group By [Table1].[Avg RSSI]

Summary
重申一下,我要做的就是获取表 1 中每个不同 MIU ID 的平均 RSSI 列的平均值,并将每个值插入到表 2 中的相应行中。

注意
Table2 中有一个名为 AvgNum 的列,如果使用它会更容易的话,可以对它进行平均以获得需要进入 MeanNum 列的相同数字。

我知道如何分两步完成我想要做的事情,但是我更希望能够在一个 sql 语句中完成它。

I am trying to get the average for each MIUID inserted into a table. The relevant tables and column information are as follows:

Table1.[MIU ID], Table1.[Avg RSSI]

and

Table2.MIUID, Table2.MeanNum

If I were simply using a select statement I would do the following:

Select DISTINCT Table1.[MIU ID], Avg(Table1.[Avg RSSI]) as MeanNum
From Table1
GROUP BY Table1.[MIU ID]  

However I need this information to be inserted into a column in Table2. I have tried the following and variations of the following and the errors I'm getting are that it won't let me use Group By, and another error saying that MeanNum is not part of the aggregate function.

UPDATE Table2  
INNER JOIN Table1
ON Table2.MIUID = Table1.[MIU ID]  
SET Table2.MeanNum = Avg([Table1].[Avg RSSI]);

And the other query I've tried is:

UPDATE Table2  
SET Table2.MeanNum = Avg([Table1].[Avg RSSI]) 
WHERE Table2.MIUID = Table1.[MIU ID]
Group By [Table1].[Avg RSSI]

Summary
To reiterate all I'm trying to do is get the average of the Avg RSSI column in Table1 for each distinct MIU ID and insert each value into the appropriate row in Table2.

Note
There is a column in Table2 called AvgNum that could be average to get the same number that needs to go into the MeanNum column if using that would be easier.

I know how to do what I'm trying to do in two steps I would prefer to be able to do it in one sql statement however.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2024-12-10 00:52:18

编辑:下面的代码在 MS-Access/Jet 中不起作用。请参阅此链接:

操作必须使用可更新查询。 (错误 3073)Microsoft Access

原始答案:

您可以在子查询中使用原始 SELECT 查询并加入它。没有语法检查,我对 T-SQL 比 MS-Access 更熟悉,但类似:

UPDATE 
    t2
SET 
    t2.MeanNum = sub.MeanNum
From 
    Table2 t2
    INNER JOIN 
        (
        Select DISTINCT 
            Table1.[MIU ID], 
            Avg(Table1.[Avg RSSI] as MeanNum
        From 
            Table1  
        GROUP BY 
            Table1.[MIU ID] 
        ) sub
        ON sub.[MIU ID] = t2.MIUID 

Edit: the code below will not work in MS-Access/Jet. See this link:

Operation must use an updatable query. (Error 3073) Microsoft Access

Original answer:

You could use the original SELECT query in a subquery and join to it. Not syntax checked and I am more familiar with T-SQL than MS-Access, but something like:

UPDATE 
    t2
SET 
    t2.MeanNum = sub.MeanNum
From 
    Table2 t2
    INNER JOIN 
        (
        Select DISTINCT 
            Table1.[MIU ID], 
            Avg(Table1.[Avg RSSI] as MeanNum
        From 
            Table1  
        GROUP BY 
            Table1.[MIU ID] 
        ) sub
        ON sub.[MIU ID] = t2.MIUID 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文