Short version: Tracking insurance policies. Running a query to determine whether what we have on file:
- Is current but doesn't meet requirements
- Null
- Is expired
I can do all three pretty easy, but the trouble comes when conditions one and three are met. We have received a new policy (that doesn't meet requirements) AND that particular company has a previous policy (expired) on file in the DB. How can I modify this Left Join to return only one record per company?
Code:
SELECT ActiveInsReqsSummary.BusinessLegalName, ActiveInsReqsSummary.CountOfContractCode, ActiveInsReqsSummary.AL, ActiveInsReqsSummary.ALPer, AL.ReqsMet, ActiveInsReqsSummary.ID, AL.PolicyEnd
FROM ActiveInsReqsSummary LEFT JOIN AL ON ActiveInsReqsSummary.ID = AL.Provider
WHERE (((ActiveInsReqsSummary.AL)>0) AND ((AL.ReqsMet)=False) AND ((AL.PolicyEnd)>Date())) OR (((ActiveInsReqsSummary.AL)>0) AND ((AL.Provider) Is Null)) OR (((ActiveInsReqsSummary.AL)>0) AND ((AL.PolicyEnd)<Date()));
ActiveInsReqsSummary is a query of all current contracts that summarizes their insurance requirements.
ActiveInsReqsSummary.AL is a count of how many contracts, per company, require a particular type of insurance.
AL is a table where I store a particular type of insurance information.
AL.ReqsMet is a simple True/False calculated field that says whether the requirements are met.
The trouble I have is when the first condition is met, indicating that an active policy doesn't meet requirements:
Code:
WHERE (((ActiveInsReqsSummary.AL)>0) AND ((AL.ReqsMet)=False) AND ((AL.PolicyEnd)>Date()))
AND the third condition is met, indicating a policy on file has expired:
Code:
OR (((ActiveInsReqsSummary.AL)>0) AND ((AL.PolicyEnd)<Date()))
This results in a company being listed in the query twice. In this case, I'd prefer to drop the second requirement, as the current policy is most important. Can I do this somehow with a MAX(PolicyEnd) that will still be unique per company?
And I also have to find a way to combine this idea with 6 other tables (7 total tables of various insurance types).
Any help is greatly appreciated. Please let me know if I need to clarify anything, or if another method would be a better approach.