简体   繁体   English

将多个值汇总到一个表中

[英]Summing multiple values into one table

I have three tables in Microsoft Access: 我在Microsoft Access中有三个表:

  • Table1 includes the names of various companies and the corresponding unique ID of each (along with other unique information about the company); 表1包含各种公司的名称以及每个公司的相应唯一ID(以及有关该公司的其他唯一信息);
  • Table2 includes sales information for each company from 2013; 表2列出了2013年以来各公司的销售信息;
  • Table3 includes sales information for each from 2014. 表3列出了2014年的销售信息。

    In Tables 2 & 3, a single company is often listed more than once, corresponding to sales in different states. 在表2和表3中,单个公司经常被列出多次,分别对应于不同州的销售额。

In Tables 2 & 3, the company name is actually its corresponding ID number, and I'm using a lookup to display the name. 在表2和表3中,公司名称实际上是其对应的ID号,我正在使用查找来显示名称。 Thus, Table1 is the parent of Table2 and Table3. 因此,表1是表2和表3的父级。

I want to create a query that lists each company exactly once along with the sum of their sales from Tables 2 & 3. This works fine when I only try to use Table2 OR Table3, but when I incorporate both, the sales are inflated by a constant scalar (a company will show sales exactly 16X what they should be, for example). 我想创建一个查询,该查询只列出每家公司一次,以及它们来自表2和3的销售额之和。当我只尝试使用Table2或Table3时,这很好用,但是当我将两者合并时,销售额会因恒定的标量(例如,一家公司将其销售额精确地显示为应有的销售额的16倍)。 What's going on here? 这里发生了什么?

Here's what the SQL shows in Access: 这是SQL在Access中显示的内容:

SELECT Table2.CompanyName,
       Sum(Table2.TotalRevenue) AS [2013 Revenue],
       Sum(Table3.TotalRevenue) AS [2014 Revenue]
FROM (Table1 INNER JOIN Table2 ON Table1.Company_ID = Table2.CompanyName)
   INNER JOIN Table3 ON Table1.Company_ID = Table3.CompanyName
GROUP BY Table2.CompanyName;

Why is it pulling these Revenue values multiple times? 为什么要多次提取这些收入值? Thank you! 谢谢!

First you may consider merge TABLE2 and TABLE3 together and just add a YEAR field. 首先,您可以考虑将TABLE2TABLE3合并在一起,只添加一个YEAR字段。 If you keep growing one table for year will be messy in the future. 如果您每年继续增加一张桌子,将来会很混乱。

the problem is you are performing a cartesian product. 问题是您正在执行笛卡尔积。

[Every company] x [Sales2013] x [Sales2014]

You need 你需要

[Every company] x [Sales2013] 
UNION
[Every company] x [Sales2014] 

Not sure what is the proper syntaxis in Access but you need a calculate totals on the subquery. 不确定Access中正确的语法是什么,但是您需要在子查询上计算总计。

And you need LEFT JOIN in case the company didnt sell on that year. 而且,如果该公司在该年没有出售,您需要LEFT JOIN (Like a company join on 2014) and COALESCE to convert NULL to 0 . (就像2014年加入公司的公司)和COALESCENULL转换为0

SELECT Table1.*, 
       COALESCE(T2.Revenue2013, 0)  Revenue2013, 
       COALESCE(T3.Revenue2014, 0)  Revenue2014
FROM Table1
LEFT JOIN ( SELECT CompanyName, SUM(TotalRevenue) as Revenue2013
            FROM Table2
            GROUP BY CompanyName) as T2
       ON  Table1.Company_ID = T2.CompanyName
LEFT JOIN ( SELECT CompanyName, SUM(TotalRevenue) as Revenue2014
            FROM Table3
            GROUP BY CompanyName) as T3
       ON  Table1.Company_ID = T3.CompanyName

You are getting multiple values because your Table2 and Table3 are not at the company level. 您将获得多个值,因为Table2和Table3不在公司级别。 They are at the sales level (I assume). 它们处于销售级别(我认为)。 So each table has multiple records for each customer. 因此,每个表都有每个客户的多个记录。 Joining them on company_id will cause each record in Table2 to join to each record in Table3 for each company_id . company_id上将它们连接将导致Table2中的每个记录都与Table3中每个company_id每个记录company_id

If you want to understand this better, change your query to SELECT * FROM... with the same joins. 如果您想更好地理解这一点,请使用相同的联接将查询更改为SELECT * FROM... You'll see for every 2013 record, there are many 2014 records. 您会看到每2013条记录,都有许多2014年记录。 It won't make any sense, therefore your aggregation won't make much sense. 这没有任何意义,因此您的汇总没有任何意义。

Instead, consider summing them before joining like: 相反,考虑像加入之前对它们求和:

SELECT Table2.CompanyName,
       t2.TotalRevenue AS [2013 Revenue],
       t3.TotalRevenue AS [2014 Revenue]
FROM 
    Table1 
    INNER JOIN (SELECT Company_Id, CompanyName, sum(TotalRevenue) as TotalRevenue FROM Table2 GROUP BY COmpany_ID, CompanyName) as t2 ON Table1.Company_ID = T2.CompanyName)
    INNER JOIN (SELECT Company_Id, CompanyName, sum(TotalRevenue) as TotalRevenue FROM Table3 GROUP BY COmpany_ID, CompanyName) as t3 ON Table1.Company_ID = T3.CompanyName
GROUP BY T2.CompanyName;

This happens because you are performing the aggregation after joining your tables. 发生这种情况是因为您在加入表后正在执行聚合。 As an example 举个例子

Table 1
Company_Id, CompanyName
1           One
2           Two
3           Three

Table 2 (2013)
Company_name, State, Sales
1             CA     50
1             WA     70
2             AK     30

Table 3 (2014)
Company_name, State, Sales
1             CA     60
1             WA     90
2             AK     50

Based on your query, first the sum of table 2 is performed, obtaining on memory: 根据您的查询,首先执行表2的总和,并在内存中获取

Company_Id, Sales_2013
1           150
2           50

After the join with 2014 is performed, obtaining Still on memory: 在执行与2014的联接后,获取“ 仍在内存中”:

Company_Id, Sales_2013, State_2014, Sales_2014
1           150        CA           50
1           150        WA           90
2           50         AK           30

As you can see at this point you have duplicates. 如您现在所见,您有重复项。 Then, the second sum is performed, finally obtaining 然后,执行第二和,最终获得

Company_Id, Sales_2013, Sales_2014
1           300         140
2           50          30

For this to do what you expect you need to have a query that does: 为此,您需要执行以下查询:

SELECT TTable2.CompanyName,
   [2013 Revenue],
   [2014 Revenue]
FROM Table1 INNER JOIN (
    SELECT Table2.CompanyName,
        Sum(Table2.TotalRevenue) AS [2013 Revenue]
    FROM Table2
    GROUP BY Table2.CompanyName
)TTable2
ON Table1.Company_ID = TTable2.CompanyName
INNER JOIN (
    SELECT Table3.CompanyName,
        Sum(Table3.TotalRevenue) AS [2014 Revenue]
    FROM Table3
    GROUP BY Table3.CompanyName
)TTable3
ON Table1.Company_ID = TTable3.CompanyName;

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

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