jimdltn
Technical User
- Dec 10, 2004
- 7
How would I rearrange or rewrite this query to perform the AdminSurvey filter before it grabs the Max Date between dates? As written, it Selects the Max Dates in the date range and then filters out the AdminSurvey and I loose data. What I want is to find all records that are not AdminSurveys and then get the Max date between dates for each FacilityData.
SELECT CC_HazardLevel.Name, Main_FacilityType.Name, Main_Facility.Name, Main_Facility.Customer, Main_Facility.PrimaryAddress_Number, Main_Facility.PrimaryAddress_Street1, CC_Survey.FacilityData, CC_Survey.ID, CC_Survey.SurveyDate, CC_Survey.SurveyCompleted, CC_Survey.InCompliance, CC_Survey.PerformedBy, CC_Survey.EventId, CC_Survey.AdminSurvey, CC_Survey.childsub
FROM ((CC_FacilityData INNER JOIN CC_HazardLevel ON CC_FacilityData.CCHazardLevel = CC_HazardLevel.ID) INNER JOIN (Main_Facility INNER JOIN Main_FacilityType ON Main_Facility.Type = Main_FacilityType.ID) ON CC_FacilityData.Facility = Main_Facility.ID) INNER JOIN CC_Survey ON CC_FacilityData.ID = CC_Survey.FacilityData
WHERE (((Main_Facility.Customer)=1) AND ((CC_Survey.SurveyDate) In (SELECT Max(Tmp.SurveyDate) FROM CC_Survey As Tmp WHERE Tmp.FacilityData = CC_Survey.FacilityData AND Tmp.SurveyDate Between #2/15/2006# And #4/2/2006#)) AND ((CC_Survey.SurveyCompleted) Is Not Null) AND ((CC_Survey.AdminSurvey)=False Or (CC_Survey.AdminSurvey) Is Null))
ORDER BY Main_Facility.Name;
SELECT CC_HazardLevel.Name, Main_FacilityType.Name, Main_Facility.Name, Main_Facility.Customer, Main_Facility.PrimaryAddress_Number, Main_Facility.PrimaryAddress_Street1, CC_Survey.FacilityData, CC_Survey.ID, CC_Survey.SurveyDate, CC_Survey.SurveyCompleted, CC_Survey.InCompliance, CC_Survey.PerformedBy, CC_Survey.EventId, CC_Survey.AdminSurvey, CC_Survey.childsub
FROM ((CC_FacilityData INNER JOIN CC_HazardLevel ON CC_FacilityData.CCHazardLevel = CC_HazardLevel.ID) INNER JOIN (Main_Facility INNER JOIN Main_FacilityType ON Main_Facility.Type = Main_FacilityType.ID) ON CC_FacilityData.Facility = Main_Facility.ID) INNER JOIN CC_Survey ON CC_FacilityData.ID = CC_Survey.FacilityData
WHERE (((Main_Facility.Customer)=1) AND ((CC_Survey.SurveyDate) In (SELECT Max(Tmp.SurveyDate) FROM CC_Survey As Tmp WHERE Tmp.FacilityData = CC_Survey.FacilityData AND Tmp.SurveyDate Between #2/15/2006# And #4/2/2006#)) AND ((CC_Survey.SurveyCompleted) Is Not Null) AND ((CC_Survey.AdminSurvey)=False Or (CC_Survey.AdminSurvey) Is Null))
ORDER BY Main_Facility.Name;