SELECT *
FROM(SELECT geography_type
,geography_element2_code state_code
,geography_element3_code county_code
,geography_element4_code city_code
FROM hz_geographies geography
WHERE geography.geography_type='STATE'
AND SYSDATE BETWEEN geography.start_date AND geography.end_date
AND geography_element1_code='US'
AND NOT EXISTS (SELECT 1
FROM zx_jurisdictions_b tax_juridiction
WHERE tax_juridiction.zone_geography_id=geography.geography_id
AND tax_juridiction.tax_regime_code = '<<TAX_REGIME_CODE>>'
AND SYSDATE BETWEEN tax_juridiction.effective_from AND NVL(tax_juridiction.effective_to,'31-DEC-4999')
AND tax_juridiction.tax=geography.geography_type
)
UNION
SELECT geography_type
,geography_element2_code state_code
,geography_element3_code county_code
,geography_element4_code city_code
FROM hz_geographies geography
WHERE geography.geography_type='COUNTY'
AND SYSDATE BETWEEN geography.start_date AND geography.end_date
AND geography_element1_code='US'
AND NOT EXISTS (SELECT 1
FROM zx_jurisdictions_b tax_juridiction
WHERE tax_juridiction.zone_geography_id=geography.geography_id
AND tax_juridiction.tax_regime_code='<<TAX_REGIME_CODE>>'
AND SYSDATE BETWEEN tax_juridiction.effective_from AND NVL(tax_juridiction.effective_to,'31-DEC-4999')
AND tax_juridiction.tax=geography.geography_type
)
UNION
SELECT geography_type
,geography_element2_code state_code
,geography_element3_code county_code
,geography_element4_code city_code
FROM hz_geographies geography
WHERE geography.geography_type='CITY'
AND SYSDATE BETWEEN geography.start_date AND geography.end_date
AND geography_element1_code='US'
AND NOT EXISTS (SELECT 1
FROM zx_jurisdictions_b tax_juridiction
WHERE tax_juridiction.zone_geography_id = geography.geography_id
AND tax_juridiction.tax_regime_code='_<<TAX_REGIME_CODE>>'
AND SYSDATE BETWEEN tax_juridiction.effective_from AND NVL(tax_juridiction.effective_to,'31-DEC-4999')
AND tax_juridiction.tax=geography.geography_type
)
)
ORDER BY geography_type
,state_code
,county_code
,city_code