[英]Nested select in postgres
我有一个bit(10)字符串,我想执行AND操作,然后得到数字1,然后将其除以位字符串的长度(在这种情况下为10)。 这是查询:
select a.doc
, a.sentenceid
, b.doc
, b.sentenceid
, LENGTH( REPLACE( CAST( select a.tokenizedsentence & b.tokenizedsentence AS TEXT ), '0', '')) / LENGTH(a.tokenizedsentence)
from
nlpdata a, nlpdata b
where
a.sentenceid < b.sentenceid;
单独使用时,两个查询都有效,但是如何组合它们呢?
select a.sentenceid
, b.sentenceid
, a.tokenizedsentence & b.tokenizedsentence
from nlpdata a, nlpdata b;
和
select length( replace( cast( tokenizedsentence AS TEXT ), '0', ''))
from nlpdata;
您无需在CAST
Select
。 删除Select
以修复错误
SELECT a.doc,
a.sentenceid,
b.doc,
b.sentenceid,
Length(Replace(Cast(a.tokenizedsentence & b.tokenizedsentence AS TEXT), '0', '')) / Length(a.tokenizedsentence)
FROM nlpdata a
INNER JOIN nlpdata b
ON a.sentenceid < b.sentenceid;
附带说明一下,请始终使用INNER JOIN
来联接两个表,而不要使用旧式逗号分隔的联接。
在性能方面没有任何区别。 INNER JOIN
是更具有可读性然后comma separated join
其中两个过滤器和连接条件将出现在Where
子句。 在Inner Join
您可以保持Inner Join
条件为ON
子句,并将过滤器移至Where
子句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.