I want to return whether or not we offer service in a particular location. The heirarchy is Zip Code > State > CountryWide
So if you enter a TX zip-code, it will first see if there’s a zip entry. If not, it will check for a TX entry. If still no hits, it will check countrywide policies.
To achieve this, I have a Calc field that indicates the level of specificty. Zip Codes are level 3, States = 2, CW =1. So a simple MAX will let me know what level of data I’m dealing with. An example of my query for this is:
SELECT Service_Availability.Service_Product, MAX(Service_Availability.Specificity) AS Spec
FROM Service_Availability
WHERE Service_Availability.State = "TX" OR Service_Availability.State = "CW"
GROUP BY Service_Product;
Now here is where my issue comes in.
I want to know if service is available at the highest level of specificity provided. However, I don’t have the correct approach…
SELECT Service_Availability.Service_Product, MAX(Service_Availability.Specificity) AS Spec
FROM Service_Availability
WHERE (Service_Availability.State = "TX" OR Service_Availability.State = "CW")
AND Service_Availability.Available = TRUE
GROUP BY Service_Product;
This returns the highest level of specificity where Service is available, not if the service is available at the highest level of specificity.
To say another way, suppose the zip is 68123 (Omaha, Nebraska).
My db shows that we do provide service in Nebraska, but for whatever reason, we specifically cannot provide service in Omaha (68123). I want the db to see I have zip level data, and use that to determine availability (FALSE).
I understand why my code is the wrong approach, but I’m not sure which direction to come at it from. Help is appreciated!
You’ll need to INNER JOIN your initial query (almost) as a subquery to get this to work: