计算每人的时间

时间:2014-02-02 17:37:56

标签: sql ms-access-2010

我正在尝试为朋友建立一个查询,但我并不是100%确定我还需要做什么。我需要计算一组桌子上每人的总时间。

表格设置看起来有点像......

  

参与者(participant_id)

     

TableA(participant_id,time1,time2,time3)

     

TableB(participant_id,time1,time2,time3)

     

TableC(participant_id,time1,time2,time3)

参与者可能也可能不在每张桌子中。例如,参与者1可能在表A中有记录,但不是B或C.而time1,time2或time3可能为NULL。

通过这个给定的信息,我如何计算所有表格中每个人的总时间?

  

(tableA.time1 + tableA.time2 + tableA.time3)+(tableB.time1 +   tableB.time2 + tableB.time3)+(tableC.time1 + tableC.time2 +   tableC.time3)

我使用的TSQL比任何东西都多,但还有很多需要学习的地方。提前感谢任何能给我一些有用提示的人。

2 个答案:

答案 0 :(得分:2)

听起来你需要一个UNION查询来取消和合并表,然后聚合[participant_id],如下所示(在Access 2010中测试):

SELECT participant_id, SUM(time_value) AS total_time
FROM
    (
        SELECT participant_id, time1 AS time_value FROM TableA
        UNION ALL
        SELECT participant_id, time2 AS time_value FROM TableA
        UNION ALL
        SELECT participant_id, time3 AS time_value FROM TableA
        UNION ALL
        SELECT participant_id, time1 AS time_value FROM TableB
        UNION ALL
        SELECT participant_id, time2 AS time_value FROM TableB
        UNION ALL
        SELECT participant_id, time3 AS time_value FROM TableB
        UNION ALL
        SELECT participant_id, time1 AS time_value FROM TableC
        UNION ALL
        SELECT participant_id, time2 AS time_value FROM TableC
        UNION ALL
        SELECT participant_id, time3 AS time_value FROM TableC
    ) AS u
GROUP BY participant_id

编辑重新评论

如果您对子查询中允许的UNIONS数量“碰壁”并开始获得“查询过于复杂”错误,那么解决方法将是

  • 创建一个名为[TimeValues]的新空表,其列为[participant_id]和[time_value]

  • 使用类似

  • 的查询批量加载临时表
INSERT INTO TimeValues (participant_id, time_value)
SELECT participant_id, time_value FROM
    (
        SELECT participant_id, time1 AS time_value FROM TableA
        UNION ALL
        SELECT participant_id, time2 AS time_value FROM TableA
        UNION ALL
        SELECT participant_id, time3 AS time_value FROM TableA
        UNION ALL
        SELECT participant_id, time1 AS time_value FROM TableB
        UNION ALL
        SELECT participant_id, time2 AS time_value FROM TableB
        UNION ALL
        SELECT participant_id, time3 AS time_value FROM TableB
    ) AS u

INSERT INTO TimeValues (participant_id, time_value)
SELECT participant_id, time_value FROM
    (
        SELECT participant_id, time1 AS time_value FROM TableC
        UNION ALL
        SELECT participant_id, time2 AS time_value FROM TableC
        UNION ALL
        SELECT participant_id, time3 AS time_value FROM TableC
        UNION ALL
        SELECT participant_id, time1 AS time_value FROM TableD
        UNION ALL
        SELECT participant_id, time2 AS time_value FROM TableD
        UNION ALL
        SELECT participant_id, time3 AS time_value FROM TableD
    ) AS u
  • 然后在临时表上运行聚合查询。

答案 1 :(得分:0)

您可以使用left joinselect子句中的大量添加来执行此操作:

select p.particpant_id,
       (nz(a.time1) + nz(a.time2) + nz(a.time3) +
        nz(b.time1) + nz(b.time2) + nz(b.time3) +
         . . .
       ) /
       (iif(a.time1 is null, 0, 1) + iff(a.time2 is null, 0, 1) + iif(a.time3 is null, 0, 1) +
        iif(b.time1 is null, 0, 1) + iff(b.time2 is null, 0, 1) + iif(b.time3 is null, 0, 1) +
        . . .
       )
from (participant p left join
      tableA a
      on p.participant_id = a.participant_id
     ) left join
     tableB b
     on p.participant_id = b.participant_id
     . . . ;