0

我有一个“”的 MSAccess SQL 查询,我想显示一整套结果。

在主表“ JobsMain ”中,结果要么是维护作业,要么不是。如您所见,以下查询(MSAccess 关系: http: //m-ls.co.uk/ExtFiles/SQL-Relationship.png )从双方提取所有数据:

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));

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

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

最亲切的问候

保罗

4

0 回答 0