带有WHERE子句的MS Access仅影响其中一个LEFT JOINS

时间:2013-09-27 15:36:36

标签: sql ms-access where-clause outer-join having-clause

我有一个''MSAccess SQL查询,我想显示完整结果集。

在主表' JobsMain '中,结果是维护作业,或者不是。以下查询(MSAccess Relationships:http://m-ls.co.uk/ExtFiles/SQL-Relationship.png)从两侧提取 ALL 数据,如您所见:

SELECT   JobsMain.JobNo,
         JobsMain.JobName,
         JobsMain.IsMaintenance,
         GroupHoursEntry.TimeStamp,
         Groups.GroupName,
         GroupMonthlyRates.MonthlyRate,
         Count(MaintenanceSites.SiteID) AS CountOfSiteID,
         Sum(SiteRates.SiteMonthlySalesValue) AS SumOfSiteMonthlySalesValue,
         GroupHourlyRate.GroupHourlyRate,
         GroupHoursEntry.GroupMenEntry,
         GroupHoursEntry.GroupHoursEntry,
         UnitGang.UnitGangRef,
         JobDetails.NoOfMen,
         JobDetails.HrsOnSite,
         UnitGang_1.UnitGangRef
FROM     (((MaintenanceSites
            RIGHT OUTER JOIN
            (((UnitGang AS UnitGang_1
               RIGHT OUTER JOIN
               ((GroupHoursEntry
                 RIGHT OUTER JOIN
                 (JobsMain
                  LEFT OUTER JOIN
                  JobDetails
                  ON JobsMain.JobNo = JobDetails.JobNo)
                 ON GroupHoursEntry.JobNo = JobsMain.JobNo)
                LEFT OUTER JOIN
                UnitGang
                ON GroupHoursEntry.UnitGangID = UnitGang.UnitGangID)
               ON UnitGang_1.UnitGangID = JobDetails.UnitGangID)
              LEFT OUTER JOIN
              Groups
              ON GroupHoursEntry.GroupID = Groups.GroupID)
             LEFT OUTER JOIN
             SitesAndGroups
             ON Groups.GroupID = SitesAndGroups.GroupID)
            ON MaintenanceSites.SiteID = SitesAndGroups.SiteID)
           LEFT OUTER JOIN
           SiteRates
           ON MaintenanceSites.SiteID = SiteRates.SiteID)
          LEFT OUTER JOIN
          GroupHourlyRate
          ON Groups.GroupID = GroupHourlyRate.GroupID)
         LEFT OUTER JOIN
         GroupMonthlyRates
         ON Groups.GroupID = GroupMonthlyRates.GroupID
GROUP BY JobsMain.JobNo, JobsMain.JobName, JobsMain.IsMaintenance, GroupHoursEntry.TimeStamp, Groups.GroupName, GroupMonthlyRates.MonthlyRate, GroupHourlyRate.GroupHourlyRate, GroupHoursEntry.GroupMenEntry, GroupHoursEntry.GroupHoursEntry, UnitGang.UnitGangRef, JobDetails.NoOfMen, JobDetails.HrsOnSite, UnitGang_1.UnitGangRef
HAVING   (((JobsMain.JobNo) = 2156
           OR (JobsMain.JobNo) = 2157));

因此,如果其中一个 JobsMain 结果是维护作业,则会显示一部分数据,另一部分为空白。如果它不是维护工作,则反之亦然。这正是我想要的,因为我可以快速生成一组结果,然后根据这些结果进行一组计算。

最初我正在进行初始简单查询以查看它是否是维护作业,然后针对初始查询的每个结果运行两个单独的子查询之一。这需要很长时间才能产生(3-4分钟)。这个新查询需要几秒钟才能加载。

这个新的大型查询的问题是我需要在维护作业方面的一个JOINS中包含一个WHERE和HAVING子句。如果我在查询末尾包含一个WHERE子句并扩展HAVING查询以包含以下内容,则这适用于维护查询,但由于这些WHERE和HAVING查询阻止它们显示,因此未显示非维护结果。

SELECT   JobsMain.JobNo,
         JobsMain.JobName,
         JobsMain.IsMaintenance,
         GroupHoursEntry.TimeStamp,
         Groups.GroupName,
         GroupMonthlyRates.MonthlyRate,
         Count(MaintenanceSites.SiteID) AS CountOfSiteID,
         Sum(SiteRates.SiteMonthlySalesValue) AS SumOfSiteMonthlySalesValue,
         GroupHourlyRate.GroupHourlyRate,
         GroupHoursEntry.GroupMenEntry,
         GroupHoursEntry.GroupHoursEntry,
         (GroupHoursEntry.GroupMenEntry * GroupHoursEntry.GroupHoursEntry) AS SumOfMaintenanceHoursEntry,
         UnitGang.UnitGangRef,
         JobDetails.NoOfMen,
         JobDetails.HrsOnSite,
         (JobDetails.NoOfMen * JobDetails.HrsOnSite) AS SumOfJobHoursEntry,
         UnitGang_1.UnitGangRef
FROM     (((MaintenanceSites
            RIGHT OUTER JOIN
            (((UnitGang AS UnitGang_1
               RIGHT OUTER JOIN
               ((GroupHoursEntry
                 RIGHT OUTER JOIN
                 (JobsMain
                  LEFT OUTER JOIN
                  JobDetails
                  ON JobsMain.JobNo = JobDetails.JobNo)
                 ON GroupHoursEntry.JobNo = JobsMain.JobNo)
                LEFT OUTER JOIN
                UnitGang
                ON GroupHoursEntry.UnitGangID = UnitGang.UnitGangID)
               ON UnitGang_1.UnitGangID = JobDetails.UnitGangID)
              LEFT OUTER JOIN
              Groups
              ON GroupHoursEntry.GroupID = Groups.GroupID)
             LEFT OUTER JOIN
             SitesAndGroups
             ON Groups.GroupID = SitesAndGroups.GroupID)
            ON MaintenanceSites.SiteID = SitesAndGroups.SiteID)
           LEFT OUTER JOIN
           SiteRates
           ON MaintenanceSites.SiteID = SiteRates.SiteID)
          LEFT OUTER JOIN
          GroupHourlyRate
          ON Groups.GroupID = GroupHourlyRate.GroupID)
         LEFT OUTER JOIN
         GroupMonthlyRates
         ON Groups.GroupID = GroupMonthlyRates.GroupID
WHERE    (((GroupMonthlyRates.MonthNo) = (Month([JobsMain].[DateStarted]))))
GROUP BY JobsMain.JobNo, JobsMain.JobName, JobsMain.IsMaintenance, GroupHoursEntry.TimeStamp, Groups.GroupName, GroupMonthlyRates.MonthlyRate, GroupHourlyRate.GroupHourlyRate, GroupHoursEntry.GroupMenEntry, GroupHoursEntry.GroupHoursEntry, UnitGang.UnitGangRef, JobDetails.NoOfMen, JobDetails.HrsOnSite, UnitGang_1.UnitGangRef, MaintenanceSites.IsDormant
HAVING   (((JobsMain.JobNo) = 2157)
          AND ((MaintenanceSites.IsDormant) = False));

现在我可以对我的查询做些什么,以便我可以运行这个完整的查询,或者我应该继续运行子查询,还是有另一种方法来实现这个?

感谢您对此的支持,非常感谢。

最基本的问候

0 个答案:

没有答案