1

警告:这里是初学者 SQL!要温柔...

我有两个查询可以以合理的及时方式从相关表中独立地给我我想要的东西,但是当我尝试将两者组合成一个(丑陋的)联合时,事情很快就会落空,查询要么给我重复的记录,运行时间过长,或者根本拒绝运行,并引用我的各种语法错误。

注意:我必须创建一个“虚拟”表 (tblAllDates),其中包含一个包含 2008 年 1 月 1 日的日期的字段,因为我需要查询从每天返回一条记录,并且两个表中都有几天没有数据。这是我能想到的唯一方法,毫无疑问有更聪明的方法......

以下是查询:

选择 tblAllDates.date,SUM(tblvolumedata.STT)
FROM tblvolumedata RIGHT JOIN tblAllDates ON tblvolumedata.date=tblAllDates.date
按 tblAllDates.date 分组;

SELECT tblAllDates.date, SUM(NZ(tblTimesheetData.batching)+NZ(tblTimesheetData.categorisation)+NZ(tblTimesheetData.CDT)+NZ(tblTimesheetData.CSI)+NZ(tblTimesheetData.destruction)+NZ(tblTimesheetData.extraction)+NZ (tblTimesheetData.indexing)+NZ(tblTimesheetData.mail)+NZ(tblTimesheetData.newlodgement)+NZ(tblTimesheetData.recordedDeliveries)+NZ(tblTimesheetData.retrieval)+NZ(tblTimesheetData.scanning)) AS VA
FROM tblTimesheetData RIGHT JOIN tblAllDates ON tblTimesheetData.date=tblAllDates.date
按 tblAllDates.date 分组;

我管理的最佳结果如下:

SELECT tblAllDates.date, 0 AS STT, SUM(NZ(tblTimesheetData.batching)+NZ(tblTimesheetData.categorisation)+NZ(tblTimesheetData.CDT)+NZ(tblTimesheetData.CSI)+NZ(tblTimesheetData.destruction)+NZ(tblTimesheetData.提取)+NZ(tblTimesheetData.indexing)+NZ(tblTimesheetData.mail)+NZ(tblTimesheetData.newlodgement)+NZ(tblTimesheetData.recordedDeliveries)+NZ(tblTimesheetData.retrieval)+NZ(tblTimesheetData.scanning)) AS VA
FROM tblTimesheetData RIGHT JOIN tblAllDates ON tblTimesheetData.date=tblAllDates.date
按 tblAllDates.date 分组
UNION SELECT tblAllDates.date, SUM(tblvolumedata.STT) AS STT, 0 AS VA
FROM tblvolumedata RIGHT JOIN tblAllDates ON tblvolumedata.date=tblAllDates.date
按 tblAllDates.date 分组;

这为我提供了我想要的 VA 和 STT 数据,但在两条记录中,我在一天内同时拥有来自两者的数据,如下所示:

日期 STT VA
2008 年 7 月 28 日 0 54020
2008 年 7 月 28 日 33812 0
2008 年 7 月 29 日 0 53890
2008 年 7 月 29 日 33289 0
2008 年 7 月 30 日 0 51780
30/07/2008 30456 0
2008 年 7 月 31 日 0 52790
2008 年 7 月 31 日 31305 0

我追求的是每天单行的 STT 和 VA 数据。这是如何实现的,我离一个可以被认为是最优的查询还有多远?(别笑,我只求学!)

4

3 回答 3

5

您可以像这样将所有这些放入一个查询中

SELECT 
dates.date, 
SUM(volume.STT) AS STT,
SUM(NZ(timesheet.batching)+NZ(timesheet.categorisation)+NZ(timesheet.CDT)+NZ(timesheet.CSI)+NZ(timesheet.destruction)+NZ(timesheet.extraction)+NZ(timesheet.indexing)+NZ(timesheet.mail)+NZ(timesheet.newlodgement)+NZ(timesheet.recordedDeliveries)+NZ(timesheet.retrieval)+NZ(timesheet.scanning)) AS VA
FROM 
tblAllDates dates 
LEFT JOIN tblvolumedata volume
ON dates.date = volume.date
LEFT JOIN tblTimesheetData timesheet
ON 
dates.date timesheet.date
GROUP BY dates.date;

我将日期表放在FROM子句中,然后再LEFT JOIN编辑另外两个表。

jet 数据库在查询中有多个连接时可能会很有趣,因此您可能需要将其中一个连接括在括号中(我相信这被称为比尔的 SQL!) - 我建议LEFT JOIN在查询生成器中使用表然后查看 SQL 代码视图并对其进行修改以添加到SUMs、GROUP BY等中。

编辑:

确保每个表中的日期字段都已编入索引,因为您要在此字段上连接每个表。

编辑2:

这个怎么样 -

SELECT date, 
Sum(STT), 
Sum(VA)
FROM 
(SELECT dates.date, 0 AS STT, SUM(NZ(tblTimesheetData.batching)+NZ(tblTimesheetData.categorisation)+NZ(tblTimesheetData.CDT)+NZ(tblTimesheetData.CSI)+NZ(tblTimesheetData.destruction)+NZ(tblTimesheetData.extraction)+NZ(tblTimesheetData.indexing)+NZ(tblTimesheetData.mail)+NZ(tblTimesheetData.newlodgement)+NZ(tblTimesheetData.recordedDeliveries)+NZ(tblTimesheetData.retrieval)+NZ(tblTimesheetData.scanning)) AS VA
FROM tblTimesheetData RIGHT JOIN dates ON tblTimesheetData.date=dates.date
GROUP BY dates.date
UNION SELECT dates.date, SUM(tblvolumedata.STT) AS STT, 0  AS VA
FROM tblvolumedata RIGHT JOIN dates ON tblvolumedata.date=dates.date
GROUP BY dates.date
)
GROUP BY date;

有趣的是,当我针对一些测试数据运行第一个语句时,与第二个语句相比,STT 和 VA 的数字都乘以 4。非常奇怪的行为,当然不是我所期望的。

于 2009-02-24T13:21:59.783 回答
0

日期表是最好的方法。

在 FROM 子句中组合连接。像这样的东西......

SELECT d.date,
  a.value,
  b.value
FROM tableOfDates d
  RIGHT JOIN firstTable a
    ON d.date = a.date
  RIGHT JOIN secondTable b
    ON d.date = b.date
于 2009-02-24T13:17:08.470 回答
0

将 SQL 转换为视图并在日期上加入它们。

于 2009-02-24T13:17:23.447 回答