MS使用SUM访问重复值

时间:2016-09-12 21:42:47

标签: sql database ms-access

我在Microsoft Access 2016中编写查询时遇到问题,该查询将显示特定事件的费用总和,事件产生的符号总和,以及年份,事件描述和公司名称。

我认为我错过了一些简单的事情,一旦有人指出它,我会感到很荒谬。希望我设法很好地格式化我的问题,很容易发现!

以下是涉及的表格,以及我正在测试的虚拟数据。

All_Company           Company_Event             

------------------    ---------------------------
|  ID  | Company |    | ID |  EventDescription  |
|------|---------|    |----|--------------------|
|  1   | Crapple |    | 1  |      Concert       |
|  2   | Rito    |    | 2  |       Party        |
------------------    ---------------------------

Company_Target_Actual
----------------------------------------------------------------
|  All_CompanyID | Company_EventID   | Year | Quarter | Signed |
|----------------|-------------------|------|---------|--------|
|        1       |         2         | 2015 |    1    |    1   |
|        1       |         2         | 2015 |    2    |    0   |
|        1       |         2         | 2015 |    3    |    3   |
|        1       |         2         | 2015 |    4    |    1   |
----------------------------------------------------------------

Budget_Company_Expense
---------------------------------------------------------------------------------
| ID | All_CompanyID | Company_EventID | Year | Category | SubCategory| Expense |
---------------------------------------------------------------------------------
| 1  |       1       |        2        | 2015 |   ABCD   |    123     |   40    |
| 2  |       1       |        2        | 2015 |   ABCD   |    cat     |   113   |
| 3  |       1       |        2        | 2015 |   ABCD   |    dog     |   71    |
---------------------------------------------------------------------------------

这是我的查询代码,我从丑陋的Access长行代码中分解出来,以便于阅读。

SELECT DISTINCTROW All_Company.Company, Budget_Company_Expense.Year, 
Budget_Company_Expense.Company_EventID, Company_Event.EventDescription, 
Sum(Budget_Company_Expense.Expense) AS [Sum Of Expense USD],
Sum(Company_Target_Actual.Signed) AS [Sum Of Signed]
FROM Company_Event 
    INNER JOIN ((All_Company 
    INNER JOIN Company_Target_Actual 
    ON All_Company.[ID] = Company_Target_Actual.[All_CompanyID]) 
INNER JOIN Budget_Company_Expense 
    ON All_Company.[ID] = Budget_Company_Expense.[All_CompanyID]) 
    ON Company_Event.[ID] = Budget_Company_Expense.[Company_EventID]
GROUP BY All_Company.Company, Budget_Company_Expense.Year,
Budget_Company_Expense.Company_EventID, Company_Event.EventDescription;

这是运行查询的结果

Result
-------------------------------------------------------------------------------------------
| Company | Year | Company_EventID | EventDescription | Sum of Expense USD | Sum of Signed|
-------------------------------------------------------------------------------------------
| Crapple | 2015 |        2        |      Party       |       $896.00      |       15     |
-------------------------------------------------------------------------------------------

正如您所看到的,它总结为总符号(5)发生了3次(Company_Target_Actual表中的条目数),反之亦然。任何有关我的问题的帮助将不胜感激, 如果我忘记了任何可能有助于发现我的错误的信息,请告诉我我还能提供什么!

1 个答案:

答案 0 :(得分:0)

考虑将查询拆分为两个聚合,一个用于在Company_Target_Actual中求和Signed,另一个用于在Business_Company_Expense中求和Expense。然后,按公司,事件和年份加入两个查询,这两个查询是分组因子。

下面使用两个派生表(FROM/JOIN子句中的子查询)。但是,您可以将其中一个保存为单独的查询,然后将它们连接到最终查询中:

SELECT t1.Company, t1.Year, t1.Company_EventID, t1.EventDescription,
       t2.[Sum Of Expense USD], t1.[Sum of Signed]
FROM

   (SELECT ac.ID AS CompanyID, ac.Company, ca.Year, ca.Company_EventID, ev.EventDescription, 
           SUM(ca.Signed) AS [Sum Of Signed]
    FROM (Company_Target_Actual ca
    INNER JOIN Company_Event ev 
          ON ca.Company_EventID = ev.ID)
    INNER JOIN All_Company ac
          ON ca.All_CompanyID = ac.ID
    GROUP BY ac.ID, ac.Company, ca.Year, ca.Company_EventID, ev.EventDescription) AS t1

INNER JOIN

   (SELECT ac.ID AS CompanyID, ac.Company, be.Year, be.Company_EventID, ev.EventDescription, 
           SUM(be.Expense) AS [Sum Of Expense USD]
    FROM (Budget_Company_Expense be
    INNER JOIN Company_Event ev 
          ON be.Company_EventID = ev.ID)
    INNER JOIN All_Company ac
          ON be.All_CompanyID = ac.ID
    GROUP BY ac.ID, ac.Company, be.Year, be.Company_EventID, ev.EventDescription) AS t2

ON t1.CompanyID = t2.CompanyID 
AND t1.Company_EventID = t2.Company_EventID
AND t1.Year = t2.Year