jueves, 25 de mayo de 2017

Postgres dblink - Consultando tablas de otra base de datos

With PostgreSQL 9.1 or later, installation of additional modules has been simplified. Registered extensions (including dblink) can be installed with CREATE EXTENSION:

CREATE EXTENSION dblink;

Run once per database. Or run it in the standard system database template1 to add it to every newly created DB automatically. Details in the manual.


Ejemplo:

select t.*
 from dblink('host=localhost dbname=bd user=postgres password=secret', 
                    'select id, name from table')
  as t(id int, name varchar)
 where id < 100
 order by id desc


Mas ejemplos:

http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html

martes, 23 de mayo de 2017

pgFormatter - Formateando query

http://sqlformat.darold.net/

This SQL formatter/beautifier supports keywords from SQL-92, SQL-99, SQL-2003, SQL-2008, SQL-2011 and PostgreSQL specifics keywords. May works with any other databases too.

Interesante... nice ;)

Links:

    Website: http://sqlformat.darold.net/
    Download: http://sourceforge.net/projects/pgformatter/
    Development: https://github.com/darold/pgFormatter
    Changelog: https://github.com/darold/pgFormatter/blob/master/ChangeLog

Emparejamiento difuso - Fuzzy Matching Algorithms To the Rescue

Levenshtein is a great algorithm to detect typos in a search query. It operates based on how distant one search term is from another term. Starting with the “source” word, it counts the number of operations (additions, subtractions, substitutions) it takes to arrive at the “destination” word. This make the Levenshtein algorithm particularly good at catching seach typos, and uncommon spellings.
(https://mrfrosti.com/2011/08/22/fuzzy-matching-in-postgresql-with-nicknames/)

Previamente habilitar la extensión:

CREATE EXTENSION fuzzystrmatch;

Ejemplo:

SELECT levenshtein('iden', 'idem');

Ejemplo: comparando el campo NOMBRE de dos tablas:

SELECT a.nombre1, b.nombre2, levenshtein(a.nombre1, b.nombre2) as leven
from tabla1 a, tabla2 b
where 
and levenshtein(a.nombre_com, b.nombre_c_1) >=  0
order by leven

links relacionados:

https://www.postgresql.org/docs/9.3/static/functions-matching.html
https://www.laurivan.com/fuzzy-string-matching-in-postgresql/
https://www.rdegges.com/2013/easy-fuzzy-text-searching-with-postgresql/