从访问表中选择不同的总计

时间:2013-06-17 16:01:21

标签: ms-access

我有三张桌子:T_O,T_C和&每个T_D都有一个日期(run_date)和count列。 我需要选择数据以按日期显示每个计数的摘要。我无法获得嵌套的sql以使其看起来正确。

需要按运行日期分组。最终结果应如下所示:

Run_Date    Total Defects   Not Closed    Closed
05/29/13    178             100           78
06/04/13    204             103           101
06/11/13    234             114           120

1 个答案:

答案 0 :(得分:0)

我假设这三个表分别有两列:Run_DateTotal DefectsNot ClosedClosed。如果你有一个包含所有运行日期的表,那么这样的东西就可以了:

SELECT
  RunDates.Run_Date,
  T_D.[Total Defects],
  T_O.[Not Closed],
  T_C.[Closed]

FROM ((
  RunDates
  LEFT JOIN T_C ON RunDates.Run_Date = T_C.Run_Date)
  LEFT JOIN T_O ON RunDates.Run_Date = T_O.Run_Date)
  LEFT JOIN T_D ON RunDates.Run_Date = T_D.Run_Date

如果没有,您将需要使用UNION(而不是UNION ALL)构建一个:

SELECT
  RunDates.Run_Date,
  T_D.[Total Defects],
  T_O.[Not Closed],
  T_C.[Closed]

FROM ((
  (SELECT Run_Date FROM T_C
   UNION
   SELECT Run_Date FROM T_O
   UNION SELECT Run_Date FROM T_D) AS RunDates
   LEFT JOIN T_C ON RunDates.Run_Date = T_C.Run_Date)
   LEFT JOIN T_O ON RunDates.Run_Date = T_O.Run_Date)
   LEFT JOIN T_D ON RunDates.Run_Date = T_D.Run_Date