Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE a (id int, fname text, lname text, email text, phone text);
- INSERT INTO a VALUES
- (1, 'john', 'doe', 'john@gmail.com', null),
- (2, 'peter', 'green', 'peter@gmail.com', null);
- CREATE TABLE b (id int, fname text, lname text, email text, phone text);
- INSERT INTO b VALUES
- (null, 'peter', 'glover', 'bob@gmail.com', '777'),
- (null, null, 'green', 'peter@gmail.com', '666');
- fname = 0.1
- lname = 0.3
- email = 0.5
- phone = 0.5
- (2, 'peter', 'green', 'peter@gmail.com', null) and
- (null, null, 'green', 'peter@gmail.com', '666') is 0.8 (lname + email)
- (2, 'peter', 'green', 'peter@gmail.com', null) and
- (null, 'peter', 'glover', 'bob@gmail.com', '777') is 0.1 (fname)
- id, fname, lname, email, phone, similarity
- 2, null,'green', 'peter@gmail.com', '666', 0.8
- create or replace function a_b_similarity(a, b)
- returns numeric language sql as $$
- select
- coalesce(($1.fname = $2.fname)::int, 0) * 0.1 +
- coalesce(($1.lname = $2.lname)::int, 0) * 0.3 +
- coalesce(($1.email = $2.email)::int, 0) * 0.5 +
- coalesce(($1.phone = $2.phone)::int, 0) * 0.5
- $$;
- select *, a_b_similarity(a, b)
- from b
- cross join a
- id | fname | lname | email | phone | id | fname | lname | email | phone | a_b_similarity
- ----+-------+--------+-----------------+-------+----+-------+-------+-----------------+-------+----------------
- | peter | glover | bob@gmail.com | 777 | 1 | john | doe | john@gmail.com | | 0.0
- | peter | glover | bob@gmail.com | 777 | 2 | peter | green | peter@gmail.com | | 0.1
- | | green | peter@gmail.com | 666 | 1 | john | doe | john@gmail.com | | 0.0
- | | green | peter@gmail.com | 666 | 2 | peter | green | peter@gmail.com | | 0.8
- (4 rows)
Add Comment
Please, Sign In to add comment