 -- update primary district slugs - hid=9
 ----------------------------------------
 SELECT set_limit(0.525);
+-- trim names
+update tb_boundary set name=trim(name) where name like ' %' or name like '% ';
 
 CREATE MATERIALIZED VIEW mvw_dise_districts AS
 SELECT t1.district, t1.slug

UPDATE tb_boundary
SET dise_slug=t1.slug
FROM mvw_dise_districts t1
WHERE status=2 AND hid=9 AND lower(name)%lower(t1.district);

-- update boundaries that didnt match by name

UPDATE tb_boundary SET dise_slug='chikkamangalore' WHERE hid=9 AND name='chikmagalur';
UPDATE tb_boundary SET dise_slug='chamarajanagara' WHERE hid=9 AND name='chamrajnagar';
UPDATE tb_boundary SET dise_slug='uttara-kannada' WHERE hid=9 AND name='uttara kannada';

-- no slug should be set for bangalore, as there are 2 districts in DISE
-- for Bangalore. Bangalore U North and U South

SET dise_slug=t1.slug
from tb_boundary parent_bdry, mvw_dise_blocks t1, mvw_dise_districts t2
WHERE 
    tb_boundary.dise_slug is null AND
    tb_boundary.status=2 AND 
    tb_boundary.hid=10 AND 
    parent_bdry.id=tb_boundary.parent AND 
    t2.district=t1.district AND 
    t2.slug=parent_bdry.dise_slug AND 
    jarowinkler(tb_boundary.name, t1.block_name) = 1;
-- select bdry.name, parent_bdry.name, t1.block_name, t1.district, t1.slug
UPDATE tb_boundary
SET dise_slug=t1.slug
from tb_boundary parent_bdry, mvw_dise_blocks t1, mvw_dise_districts t2
WHERE 
    tb_boundary.dise_slug is null AND
    tb_boundary.status=2 AND 
    tb_boundary.hid=10 AND 
    parent_bdry.id=tb_boundary.parent AND 
    t2.district=t1.district AND 
    t2.slug=parent_bdry.dise_slug AND 
    jarowinkler(tb_boundary.name, t1.block_name) >= 0.95;
-- select bdry.name, parent_bdry.name, t1.block_name, t1.district, t1.slug
UPDATE tb_boundary
SET dise_slug=t1.slug
from tb_boundary parent_bdry, mvw_dise_blocks t1, mvw_dise_districts t2
WHERE 
    tb_boundary.dise_slug is null AND
    tb_boundary.status=2 AND 
    tb_boundary.hid=10 AND 
    parent_bdry.id=tb_boundary.parent AND 
    t2.district=t1.district AND 
    t2.slug=parent_bdry.dise_slug AND 
    jarowinkler(tb_boundary.name, t1.block_name) >= 0.9;

UPDATE tb_boundary SET dise_slug='uttara-kannada-sirsi-mundagod' WHERE hid=10 and name='mundagod';
UPDATE tb_boundary SET dise_slug='uttara-kannada-sirsi-joida' WHERE hid=10 and name='joida';
