Skip to content

Ajuda query

Peter edited this page Aug 27, 2021 · 6 revisions
CREATE VIEW grade_all_ids_sample AS
SELECT * FROM grade_id04 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id13 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id14 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id15 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id23 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id24 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id25 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id26 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id27 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id33 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id34 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id35 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id36 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id37 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id39 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id42 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id43 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id44 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id45 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id46 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id47 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id50 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id51 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id52 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id53 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id54 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id55 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id56 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id57 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id58 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id60 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id61 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id62 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id63 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id64 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id65 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id66 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id67 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id68 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id69 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id70 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id71 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id72 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id73 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id74 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id75 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id76 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id77 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id80 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id81 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id82 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id83 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id84 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id85 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id92 TABLESAMPLE BERNOULLI(0.01)
UNION ALL SELECT * FROM  grade_id93 TABLESAMPLE BERNOULLI(0.01)
;
CREATE VIEW grade_all_ids AS
SELECT * FROM grade_id04
UNION ALL SELECT * FROM  grade_id13
UNION ALL SELECT * FROM  grade_id14
UNION ALL SELECT * FROM  grade_id15
UNION ALL SELECT * FROM  grade_id23
UNION ALL SELECT * FROM  grade_id24
UNION ALL SELECT * FROM  grade_id25
UNION ALL SELECT * FROM  grade_id26
UNION ALL SELECT * FROM  grade_id27
UNION ALL SELECT * FROM  grade_id33
UNION ALL SELECT * FROM  grade_id34
UNION ALL SELECT * FROM  grade_id35
UNION ALL SELECT * FROM  grade_id36
UNION ALL SELECT * FROM  grade_id37
UNION ALL SELECT * FROM  grade_id39
UNION ALL SELECT * FROM  grade_id42
UNION ALL SELECT * FROM  grade_id43
UNION ALL SELECT * FROM  grade_id44
UNION ALL SELECT * FROM  grade_id45
UNION ALL SELECT * FROM  grade_id46
UNION ALL SELECT * FROM  grade_id47
UNION ALL SELECT * FROM  grade_id50
UNION ALL SELECT * FROM  grade_id51
UNION ALL SELECT * FROM  grade_id52
UNION ALL SELECT * FROM  grade_id53
UNION ALL SELECT * FROM  grade_id54
UNION ALL SELECT * FROM  grade_id55
UNION ALL SELECT * FROM  grade_id56
UNION ALL SELECT * FROM  grade_id57
UNION ALL SELECT * FROM  grade_id58
UNION ALL SELECT * FROM  grade_id60
UNION ALL SELECT * FROM  grade_id61
UNION ALL SELECT * FROM  grade_id62
UNION ALL SELECT * FROM  grade_id63
UNION ALL SELECT * FROM  grade_id64
UNION ALL SELECT * FROM  grade_id65
UNION ALL SELECT * FROM  grade_id66
UNION ALL SELECT * FROM  grade_id67
UNION ALL SELECT * FROM  grade_id68
UNION ALL SELECT * FROM  grade_id69
UNION ALL SELECT * FROM  grade_id70
UNION ALL SELECT * FROM  grade_id71
UNION ALL SELECT * FROM  grade_id72
UNION ALL SELECT * FROM  grade_id73
UNION ALL SELECT * FROM  grade_id74
UNION ALL SELECT * FROM  grade_id75
UNION ALL SELECT * FROM  grade_id76
UNION ALL SELECT * FROM  grade_id77
UNION ALL SELECT * FROM  grade_id80
UNION ALL SELECT * FROM  grade_id81
UNION ALL SELECT * FROM  grade_id82
UNION ALL SELECT * FROM  grade_id83
UNION ALL SELECT * FROM  grade_id84
UNION ALL SELECT * FROM  grade_id85
UNION ALL SELECT * FROM  grade_id92
UNION ALL SELECT * FROM  grade_id93
;

-------
CREATE FUNCTION grade_all_ids_search_latlon(
  lat real, lon real
) RETURNS TABLE (LIKE grade_id04) AS $f$
  SELECT g.*
  FROM  grade_all_ids g,
        ( SELECT ST_SetSRID( ST_MakePoint(lon,lat),4326) ) t(pt_geom)
  WHERE g.geom && t.pt_geom
$f$ LANGUAGE SQL IMMUTABLE;

----
SELECT  * FROM grade_all_ids_search_latlon(-23.550385,-46.633956); 

SELECT quadrante, id_unico, nome_10km, nome_50km
FROM  grade_all_ids_sample LIMIT 2;

SELECT COUNT(*) FROM grade_all_ids_sample; -- ~1200

SELECT COUNT(*) FROM grade_all_ids_sample t, LATERAL ST_DumpPoints(t.geom) g; -- ~6400

---
SELECT COUNT(*) tot,
       COUNT(*) FILTER (WHERE NOT(is_equal) ) AS tot_false,
       array_agg(item) FILTER (WHERE NOT(is_equal) ) AS quads_false
FROM (
 SELECT quadrante,
        grid_ibge.xy_to_quadrante_text(x,y) = quadrante AS is_equal,
        quadrante||'-'||x::text||'_'||y::text as item
 FROM (
   SELECT t1.quadrante, round(st_x(g.geom))::int x, round(st_y(g.geom))::int y
   FROM (
     SELECT quadrante, ST_Transform( st_simplify(geom,0.00001), 952019) as geom
     FROM grade_all_ids_sample
   ) t1, LATERAL ST_DumpPoints(t1.geom) g
 ) t2  order by 1,2,3
) t3;
Clone this wiki locally