[英]PostgreSQL multiple pg_trgm similarity score sub-query
I'm fairly new to SQL and I'm struggling with a sub query. 我对SQL还是很陌生,并且正努力处理子查询。 I've got a table that looks like this:
我有一张桌子,看起来像这样:
sss | mm | sid
------------------+----+-----
IBM LTD | | 003
I.B.M. | | 003
A.BM LTD | | 004
IMB LTD | | 004
IMB UK | | 005
IBMUK LTD | | 006
IBMUKLTD | | 007
IBM LIMITED | | 008
IBM U.K. | | 008
IBM U.K. LIMITED | | 009
I.B.M UK LTD | 1 | 001
IBM | 1 | 001
IBM UK | 1 | 001
IBM UK LTD | 1 | 001
The rows with a 1
in the mm
column are rows that have been manually matched and the ID is correct. mm
列中带有1
的行是手动匹配的行,并且ID是正确的。 I want to use the similarity
function to try and find close matches to the records that have already been matched manually. 我想使用
similarity
函数尝试查找与已手动匹配的记录的紧密匹配。
I know that normally you put a sub-query in the from statement but I can't do that with the similarity
function, but I'm not sure how to do it, this is what I'd like to do: 我知道通常您会在from语句中放置一个子查询,但是我无法使用
similarity
函数来执行此操作,但是我不确定该怎么做,这就是我想做的事情:
SELECT
sss,
similarity(sss, select(sss from tusm where mm = 1) as match_score)
from
tusm
where
mm is null and
where match_score >= 0.5
;
You have a few problems here. 您在这里遇到一些问题。 You can't use column aliases in the where clause, you are trying to assign a column alias on the wrong side of the parenthesis, you can't feed a set to the second argument of
similarity
, and you've just generally mangled the syntax in several places. 您不能在where子句中使用列别名,而是试图在括号的错误一侧分配列别名,不能将集合提供给
similarity
的第二个参数,并且通常只对语法在几个地方。
To overcome the inability of similarity
to deal with sets, do a self join: 要克服
similarity
无法处理集合的问题,请执行自我连接:
select a.sss as a_sss, b.sss as b_sss, similarity(a.sss, b.sss) as score from
tusm a, tusm b
where a.mm is null and b.mm = 1 and
similarity(a.sss, b.sss) > 0.5;
giving 给
a_sss | b_sss | score
-----------+------------+----------
IBM LTD | IBM UK LTD | 0.727273
IBMUK LTD | IBM UK LTD | 0.615385
If the table gets large, it will be hard to make this perform well. 如果表变大,将很难使其性能良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.