Skip to content

Instantly share code, notes, and snippets.

@giacecco
Last active August 29, 2015 14:23
Show Gist options
  • Save giacecco/98d12e2aae7daad3d110 to your computer and use it in GitHub Desktop.
Save giacecco/98d12e2aae7daad3d110 to your computer and use it in GitHub Desktop.
psql:fix_pdc.sql:34: ERROR: function fix_pdc() does not exist
LINE 1: SELECT fix_pdc();
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
CREATE OR REPLACE FUNCTION fix_pdc () RETURNS text VOLATILE AS
$BODY$
DECLARE
no_of_records INTEGER;
no_of_iterations INTEGER;
BEGIN
EXECUTE 'SELECT COUNT(*) FROM ons_pd;' INTO no_of_records;
EXECUTE 'SELECT ' || no_of_records || ' / 10000 + 1;' INTO no_of_iterations;
FOR i IN 1..no_of_iterations LOOP
RAISE NOTICE 'Iteration no. % of %...', i, no_of_records;
INSERT INTO pcd_fix
SELECT old_pcd, new_pcd, distance_meters, distance_degrees
FROM
(SELECT pcd AS old_pcd, geom
FROM ons_pd
WHERE doterm IS NOT NULL
LIMIT 10000
OFFSET (i - 1) * 10000) AS a,
LATERAL (SELECT pcd AS new_pcd, ST_Distance_Sphere(a.geom, geom) AS distance_meters, ST_Distance(a.geom, geom) AS distance_degrees
FROM ons_pd
WHERE doterm IS NULL AND ST_DWithin(a.geom, geom, 0.2)
ORDER BY distance_meters ASC
LIMIT 1) AS b;
END LOOP;
RETURN 'Done.';
END
$BODY$
LANGUAGE plpgsql STABLE
COST 100;
DROP TABLE IF EXISTS pcd_fix;
CREATE TABLE pcd_fix (new_pcd CHAR(7), old_pcd CHAR(7), distance_meters REAL, distance_degrees REAL);
SELECT fix_pdc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment