简体   繁体   English

访问-是否基于唯一键汇总表值?

[英]Access — Summing table values based on unique keys?

I'm a beginner to Access. 我是Access的初学者。 I have a routing table that includes a list of cable trays, as well as the cables that run through those trays. 我有一个路由表,其中包含电缆桥架以及穿过这些电缆桥架的电缆的列表。 A sample of the data is as follows: 数据样本如下:

tray        Usable Area    cable                Total Cable Area (mm^2)
CM1001            25000    1-3/C-00AWG-Cu-TECK                  660.19
CM1001            25000     1-3/C-4AWG-Cu-TECK                  572.27
CM1001            25000    1-3/C-14AWG-Cu-TECK                  490.63
TC-100D.2         15600   1-3/C-750AWG-Al-TECK                1,017.36
TM-001.7B         72000   1-3/C-750AWG-Al-TECK                1,017.36
TM-001A.1         10000   1-3/C-750AWG-Al-TECK                1,017.36
TM-001A.2         90000     1-3/C-8AWG-Cu-TECK                  530.66
TM-001A.3         90000   1-3/C-750AWG-Al-TECK                1,017.36

The tray field contains both unique and duplicate values. 托盘字段包含唯一值和重复值。 The duplicate values are considered to be a single tray, just with multiple types of cables running through it (in the cable column). 重复的值被认为是单个托架,只是其中有多种类型的电缆穿过(在电缆列中)。 My goal is to sum up the Total Cable Area for a single tray, even if that tray is in fact a number of duplicate entries. 我的目标是总结单个托盘的总电缆面积,即使该托盘实际上是许多重复的条目。 For example, the Total Cable Area for tray CM1001 is 1723.06mm², TM-001A.1 is 1017.36mm², and TM-001A.2 is 530.66mm². 例如,托架CM1001的总电缆面积为1723.06mm²,TM-001A.1为1017.36mm²,TM-001A.2为530.66mm²。 I will then compare that Total Cable Area to the Usable Area of the tray, to see if the tray is full. 然后,我会将总电缆面积与托盘的可用面积进行比较,以查看托盘是否已满。

My question is: is there some kind of easy Access query I can run to sum up the values in the Total Cable Area column if there are duplicate 'tray' rows, but just display the value in Total Cable Area is the tray is a unique row? 我的问题是:如果存在重复的“托盘”行,是否可以通过某种简单的访问查询来汇总“总电缆面积”列中的值,但仅显示“总电缆面积”中的值是因为托盘是唯一的行? The Usable Area and cable fields are ultimately unimportant when creating this new query. 创建此新查询时,“可用面积”和“电缆”字段最终并不重要。 Ideally, my new query would look like: 理想情况下,我的新查询如下所示:

tray              Total Cable Area (mm^2)
CM1001                           1,723.06
TC-100D.2                        1,017.36
TM-001.7B                        1,017.36
TM-001A.1                        1,017.36
TM-001A.2                          530.66
TM-001A.3                        1,017.36

If there isn't some kind of general query type that I simply didn't know about, would you be willing to provide some starting points on how to accomplish this with VBA? 如果没有我根本不了解的某种通用查询类型,您是否愿意提供一些使用VBA完成此操作的起点? I'm familiar with VBA, just not with Access. 我熟悉VBA,但不熟悉Access。

Thank you so much! 非常感谢!

You need to select the tray column, the Sum of Total Cable Area (mm^2) , and group by tray . 您需要选择 tray列中, SumTotal Cable Area (mm^2)通过组 tray

SELECT [Total Cable Area (mm^2)], SUM([tray]) AS [tray]
FROM [MyTable]
GROUP BY [Total Cable Area (mm^2)]

I wrote a solution in VBA, but it writes the results to another table, which might not be proper form. 我用VBA编写了一个解决方案,但是将结果写到另一个表中,这可能不是正确的形式。 I've included it below if anyone in the future wants it, but Mat's solution is definitely better. 如果将来有人需要,我将其包括在下面,但是Mat的解决方案肯定更好。

Sub sum_duplicate_trays()

Dim rs_qry As DAO.Recordset
Dim rs_fill As DAO.Recordset
Set rs_qry = CurrentDb.OpenRecordset("qryCable_Area")
Set rs_fill = CurrentDb.OpenRecordset("tblTray_Fill")

'deletes contents on tblTray_Fill, since it will be replaced below.
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblTray_Fill"
DoCmd.SetWarnings True

If Not (rs_qry.EOF And rs_qry.BOF) Then
    rs_qry.MoveFirst
    Do Until rs_qry.EOF = True
        'sees if current tray already exists in Fill table (which contains only uniques)
         rs_fill.Index = "tray"
         rs_fill.Seek "=", rs_qry!tray
            If rs_fill.NoMatch Then
               rs_fill.AddNew
               rs_fill!tray = rs_qry!tray
               rs_fill!cable_area = rs_qry![Total Cable Area (mm^2)]
               rs_fill.Update
            Else 'already existing
               rs_fill.Edit
               rs_fill!cable_area = rs_fill!cable_area + rs_qry![Total Cable Area (mm^2)]
               rs_fill.Update
            End If
        rs_qry.MoveNext
    Loop
Else
    MsgBox "Error: the Cable Area query is empty."
End If
End Sub

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

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