Skip to content

Instantly share code, notes, and snippets.

@tomask-de
Last active April 11, 2018 13:54
Show Gist options
  • Select an option

  • Save tomask-de/6ba3c6def3e55d4bf7d8bad0e21bbdd0 to your computer and use it in GitHub Desktop.

Select an option

Save tomask-de/6ba3c6def3e55d4bf7d8bad0e21bbdd0 to your computer and use it in GitHub Desktop.
opengeodb search by zip code and radius incl. the federal state in result
SELECT
dest.zc_loc_id AS ID,
dest.zc_zip AS PLZ,
dest.zc_location_name AS ORT,
ACOS(
SIN(RADIANS(src.zc_lat)) * SIN(RADIANS(dest.zc_lat))
+ COS(RADIANS(src.zc_lat)) * COS(RADIANS(dest.zc_lat))
* COS(RADIANS(src.zc_lon) - RADIANS(dest.zc_lon))
) * 6380 AS Entfernung,
t5.text_val AS Bundesland
FROM zip_coordinates dest, geodb_textdata t1, geodb_textdata t2, geodb_textdata t3, geodb_textdata t4, geodb_textdata t5
CROSS JOIN zip_coordinates src
WHERE src.zc_id = (SELECT zc_id
FROM zip_coordinates
WHERE zc_zip = '08060' LIMIT 1)
AND t1.loc_id = dest.zc_loc_id
AND t2.text_val = dest.zc_zip
AND t1.loc_id = t2.loc_id
AND t2.text_type = '500300000' /* PLZ */
AND t3.loc_id = t1.loc_id
AND t3.text_type = '500600000' /* Amtlicher Gemeindeschlüsssel */
AND t4.text_type = '500600000'
AND t4.text_val = SUBSTR(t3.text_val,1,2)
AND t5.loc_id = t4.loc_id
AND t1.text_type = t5.text_type
AND t5.text_type = '500100000' /* Name */
HAVING Entfernung < 10
ORDER BY Entfernung
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment