难以组合JET SQL查询

Lun*_*tik 1 sql ms-access jet

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

我有两个查询,以合理及时的方式独立地从相关表中给我我想要的东西,但是当我尝试将两者结合在一个(非常简单的)联合中时,事情很快就会变成一些,并且查询会给我重复的记录,需要花费非常长的时间来运行,或者拒绝运行所有引用各种语法错误.

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

以下是查询:

SELECT tblAllDates.date, SUM(tblvolumedata.STT)
FROM tblvolumedata RIGHT JOIN tblAllDates ON tblvolumedata.date=tblAllDates.date
GROUP BY 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
GROUP BY 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.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
GROUP BY tblAllDates.date
UNION SELECT tblAllDates.date, SUM(tblvolumedata.STT) AS STT, 0  AS VA
FROM tblvolumedata RIGHT JOIN tblAllDates ON tblvolumedata.date=tblAllDates.date
GROUP BY tblAllDates.date;

这给了我想要的VA和STT数据,但在两个记录中,我在一天内有两个数据,如下所示:

date         STT     VA
28/07/2008  0       54020
28/07/2008  33812   0
29/07/2008  0       53890
29/07/2008  33289   0
30/07/2008  0       51780
30/07/2008  30456   0
31/07/2008  0       52790
31/07/2008  31305   0

我所追求的是每天单行的STT和VA数据.如何实现这一目标,以及我可以在多大程度上远离可以被认为是最优的查询?(不要笑,我只是想学习!)

Rus*_*Cam 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;
Run Code Online (Sandbox Code Playgroud)

我把日期表放在第一个FROM条款中,然后LEFT JOIN编辑其他两个表.

jet数据库在查询中有多个连接可能很有趣,所以你可能需要在括号中包含一个连接(我相信这被称为Bill的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;
Run Code Online (Sandbox Code Playgroud)

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