简体   繁体   English

Microsoft Access 2003查询 - 计算记录并对其求和

[英]Microsoft Access 2003 Query - Count records and sum it

I'm creating a query with Microsoft Access 2003 and had encounter an issue. 我正在使用Microsoft Access 2003创建查询并遇到问题。 I'm new! 我是新来的!

I've got 2 tables. 我有两张桌子。 First table, i have a list of records that include the name , property name and the country state . 第一个表,我有一个包含名称属性名称国家/地区状态的记录列表。 Second table, i have a list of property names , the number of units in the property and the property's country state . 第二个表,我有一个属性名称列表, 属性中 的单位数和属性的国家/地区状态

I will like to count the number of records in the first table by its state, meanwhile summing up the number of units the property has in the state. 我想计算第一个表中的状态记录数,同时总结该地产所在单位的数量。

What I encountered is, when I sum the number of units, the units repeats! 我遇到的是,当我总结单位数时,单位重复!

Taking for example; 举个例子;

Table1: 表格1:

Name | 名称| State | 国家| Property Name 物业名称

Mr 1 | 先生1 | State A | 州A | Building AAA 建立AAA

Mr 2 | 先生2 | State A | 州A | Building AAA 建立AAA

Mr 3 | 先生3 | State A | 州A | Building BBB 建设BBB

Mr 4 | 先生4 | State B | 州B | Building XXX 建设XXX

Mr 5 | 先生5 | State B | 州B | Building XXX 建设XXX

Table2: 表2:

Property Name | 物业名称| State | 国家| Number of Units 单位的数量

Building AAA | 建设AAA | State A | 州A | 100 100

Building BBB | 建设BBB | State A | 州A | 50 50

Building XXX | 建设XXX | State B | 州B | 20 20

My Result: 我的结果:

State | 国家| Number of Units | 单位数| No of Records 没有记录

State A | 州A | 250 | 250 | 3 3

State B | 州B | 40 | 40 | 2 2

The result i want: 我想要的结果:

State | 国家| Number of Units | 单位数| No of Records 没有记录

State A | 州A | 150 | 150 | 3 3

State B | 州B | 20 | 20 | 2 2

EXPANDED EXPANDED

Assuming you are using the Access query builder, you will need to construct three Select queries: 假设您使用的是Access查询构建器,则需要构造三个Select查询:

1) Table1 will be the source table for the first query. 1)Table1将是第一个查询的源表。 Use the State field twice in the query, first as a Group By field and second as a Count field. 在查询中使用State字段两次,首先作为Group By字段,第二个作为Count字段。 (Any of the fields could have been used for the count, since you are only interested in the number of records.) Save the query for use in the third query. (任何字段都可以用于计数,因为您只对记录数感兴趣。)保存查询以便在第三个查询中使用。

在此输入图像描述

2) Table2 will be the source table for the second query. 2)Table2将是第二个查询的源表。 Use the State field as a Group By field and the Units field as a Sum field. 使用State字段作为Group By字段,Units字段作为Sum字段。 Save this query, too. 也保存此查询。

在此输入图像描述

3) The third query will bring the information together. 3)第三个查询将把信息放在一起。 For the source, use the first and second queries, with a join between them on the State field. 对于源,使用第一个和第二个查询,在State字段上使用它们之间的连接。 Select the State field (from either query) as a Group By Field, the CountOfState field from the first query as a Sum field, and the SumofUnits field from the second query as a Sum field. 选择State字段(来自任一查询)作为Group By Field,将第一个查询中的CountOfState字段作为Sum字段,将第二个查询中的SumofUnits字段作为Sum字段。

在此输入图像描述

While the actual amount of work done by Access in producing the final result will not change, the three queries can be consolidated into a single query by editing the underlying SQL. 虽然Access在生成最终结果时所完成的实际工作量不会改变,但可以通过编辑基础SQL将这三个查询合并到一个查询中。

The new query was produced by inserting the Table1 and Table2 queries into the third, final result query, one on either side of the INNER JOIN statement. 通过将Table1和Table2查询插入到第三个最终结果查询中来生成新查询,该查询位于INNER JOIN语句的任一侧。 The T1 and T1 in the new query are aliases for the embedded queries that eliminate ambiguity in referencing the fields of those queries. 新查询中的T1和T1是嵌入式查询的别名,可消除引用这些查询字段时的歧义。

The new query cannot be created using the Query Builder (although the original three queries provide the raw material for it). 无法使用查询生成器创建新查询(尽管原始的三个查询为其提供了原始材料)。 Instead, the SQL must be written/pasted in/edited in the SQL View of the Query Builder. 相反,必须在查询生成器的SQL视图中编写/粘贴/编辑SQL。

    SELECT T1.State AS State,
           Sum(T1.CountOfState) AS Records, 
           Sum(T2.SumOfUnits) AS Units
    FROM 
          (SELECT Table1.State, 
                  Count(Table1.State) AS CountOfState
           FROM Table1
           GROUP BY Table1.State) T1
    INNER JOIN 
           (SELECT Table2.State, 
                   Sum(Table2.Units) AS SumOfUnits
            FROM Table2
            GROUP BY Table2.State) T2
    ON T1.State = T2.State
    GROUP BY T1.State;

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

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