MS ACCESS如何在2个表中平均3列

时间:2011-05-03 14:06:10

标签: sql ms-access

我有以下表格:

T1:
Repair_ID, Descreption,Cost,Level
(1,a,24,9)
(2,b,34,9)
(3,a,22,3)
(4,c,11,6)

T2:
Repair_ID, Start_Time, End_Time,Location_ID
(1,02:00:00,03:00:00,3)
(2,04:00:00,05:00:00,7)
(3,06:00:00,08:00:00,3)

我希望能够获得一个表,其中包含成本的平均值,平均值以及修复持续时间的平均值(通过从start_time减去end_Time)按Descreption和Location_ID进行分组 所以表格看起来像这样: Descreption,Location_ID,AvgCost,AvgLevel,Avg Duration (a,3,(24 + 22/2)= 24,(9 + 3/2 = 6),01:30:00 *)

  • 1小时维修_ID 1和2小时维修_ID 3 = 01:30:00平均因为这两次维修发生在同一地点并具有相同的净化 谢谢

1 个答案:

答案 0 :(得分:2)

这实际上很直接。加入三个表并进行您所描述的计算和分组

更新格式化日期差异的平均值有点棘手。在执行Aggregate之后,您需要执行格式化。最简单的方法是使用在线视图

SELECT 
     Descreption,
     Location_id,
     AvgLevel,
     AvgCost,
     Format(CDATE(AvgDuration),"hh:mm:ss") AvgDuration
FROM
(
    SELECT
        repair.Descreption,
        location.Location_id,
        AVG(repair.level) AvgLevel,
        AVG(repair.Cost) AvgCost,
        AVG(times.end_time - times.start_time)  AvgDuration 

    FROM 
        T3 location
        INNER JOIN t1 repair
        on location.repair_id = repair.repair_id
        INNER JOIN t2 times
        ON location.repair_id = times.repair_id
    GROUP BY
        repair.Descreption,
        location.Location_id) foo
ORDER BY
       AvgCost desc