我需要将用户输入与字符串列匹配。两者都包含空格分隔的单词。
订购标准是:
所以给出以下样本数据
"one sample"
"un moment"
"no match"
"sample uno"
"uno sample"
"sample oun"
和示例输入"sa un foo"
,输出应为:
"sample uno"
- 2个前缀匹配"uno sample"
- 相同但单词的顺序不同"sample oun"
- 1个前缀匹配+ 2个匹配的词"un moment", "one sample"
- 1个前缀匹配问题是:我可以在单个SQL查询(postgresql
)中实现这个吗?
我对SQL很缺乏经验,所以我很感激任何帮助。谢谢!
我添加了一个简单的SQL FIDDLE
答案 0 :(得分:1)
space
space
如果一个输入字显示为前缀而第二个字显示为包含相同字,则我认为可能需要特殊注意
数据:FORMAT
输入:FO AT
WITH data as (
SELECT
row_number() OVER (ORDER BY field) AS id,
field
FROM
dtable
),
data_split as (
SELECT
id,
unnest(string_to_array(field, ' ')) AS elem
FROM data
),
input_split as (
SELECT
unnest(string_to_array(field, ' ')) AS elem
FROM input
),
match as (
SELECT *, strpos(d.elem, i.elem) as match_pos
FROM input_split i, data_split d
)
select
match.id,
data.field,
SUM( CASE WHEN match_pos = 1 THEN 1 ELSE 0 END ) prefix,
SUM( CASE WHEN match_pos > 1 THEN 1 ELSE 0 END ) contain
from
match inner join
data on match.id = data.id
group by match.id, data.field
order by 3 desc, 4 desc