在MS Access中,如何根据同一查询中的数据汇总数据?

时间:2016-07-12 00:09:12

标签: sql ms-access

我在MS Access中有一个类似于下表的表。我正在尝试创建一个查询,将“开销”汇总到“任务”中,使其看起来像下面的第二个表。

Quote       Task        Cost    Total USD
=========   ==========  ====    =========
Quote-001   Task001      1.5          1.5
Quote-001   Task002      1.4          1.4
Quote-001   Task003      1.3          1.3
Quote-001   Task004      1.2          1.2
Quote-001   Overhead-A    10           10
Quote-001   Overhead-B     5            5
Quote-001   Overhead-B     2            2
-----------------------------------------
                       Total         22.4

下表中的开销总和(10 + 5 + 2)= 17,分为四个任务(4.25添加到每个任务金额)。

Quote       Task        Cost    Total USD
=========   ==========  ====    =========
Quote-001   Task001     5.75         5.75
Quote-001   Task002     5.65         5.65
Quote-001   Task003     5.55         5.55
Quote-001   Task004     5.45         5.45
-----------------------------------------
                       Total         22.4

甚至可以使用Access查询吗?

提前感谢您的意见。

2 个答案:

答案 0 :(得分:1)

考虑聚合查询的派生表,它们总结开销成本并计算任务项目,每个项目由 Quote 加入。然后使用聚合调整费用总计字段。

SELECT t1.Quote, t1.Task,
       t1.Cost + (t2.OverheadCost/t3.TaskCount) As NewCost, 
       t1.[Total USD] + (t2.OverheadCost/t3.TaskCount) As NewTotal
FROM (Overheads As t1

INNER JOIN 
     (SELECT Overheads.Quote, Sum(Overheads.Cost) As OverheadCost
      FROM Overheads
      WHERE Overheads.Task ALIKE '%Overhead%'
      GROUP BY Overheads.Quote) As t2
ON t1.Quote = t2.Quote)

INNER JOIN
     (SELECT Overheads.Quote, Count(*) As TaskCount
      FROM Overheads
      WHERE Overheads.Task ALIKE '%Task%'
      GROUP BY Overheads.Quote) As t3
ON t1.Quote = t3.Quote

WHERE t1.Task ALIKE '%Task%';

-- Quote           Task  NewCost   NewTotal
-- Quote-001    Task001     5.75       5.75
-- Quote-001    Task002     5.65       5.65
-- Quote-001    Task003     5.55       5.55
-- Quote-001    Task004     5.45       5.45

或者,在MS Access中,您可以将聚合查询保存为存储的查询,并在最终查询中引用它们。

SELECT t1.Quote, t1.Task,
       t1.Cost + (t2.OverheadCost/t3.TaskCount) As NewCost, 
       t1.[Total USD] + (t2.OverheadCost/t3.TaskCount) As NewTotal
FROM (Overheads As t1    
INNER JOIN qrySumOverHeadCost As t2 ON t1.Quote = t2.Quote)    
INNER JOIN qryCountTaskItems As t3 ON t1.Quote = t3.Quote    
WHERE t1.Task ALIKE '%Task%';

答案 1 :(得分:0)

JET语法总是冒险。如果它是SQL Server,则可以将任务计数和开销总数存储在声明的变量中,但是在这里我们需要将它们存储在子查询中。另一种选择是在VBA模块中使用多个查询,这样可以更容易阅读,并且在大型集合中具有更好的性能。

SELECT a.Quote, 
  a.Task, 
  ROUND(a.Cost + OverTot.Cost / aCount.Count,2) AS [Count],
  ROUND(a.Total_USD + OverTot.Total_USD / aCount.Count,2) AS [Total_USD]
FROM
  (
    (
      SELECT t.Quote, 
        t.Task, 
        SUM(t.Cost) AS [Cost], 
        SUM(t.Total_USD) AS [Total_USD]
      FROM Table1 t
      WHERE LEFT(t.Task,8) <> "OverHead"
      GROUP BY t.Quote, 
        t.Task
    ) a
    INNER JOIN
    (
      SELECT  t.Quote, 
        SUM(t.Cost) AS [Cost], 
        SUM(t.Total_USD) AS [Total_USD]
      FROM Table1 t
      WHERE LEFT(t.Task,8) = "OverHead"
      GROUP BY t.Quote, 
        LEFT(t.Task,8)
    ) OverTot ON a.Quote = OverTot.Quote
  )
  INNER JOIN
  (
    SELECT t.Quote, 
      COUNT(t.Task) AS [Count]
    FROM Table1 t
    WHERE LEFT(t.Task,8) <> "OverHead"
    GROUP BY t.Quote
  ) aCount ON a.Quote = aCount.Quote