I have a table with words from a text (the table is called token), each word is a row in the table. I want to retrieve adjacent words in the result.
Example: My name is Renato must return:
My | name
name | is
is | Renato
The following query works, but is slow. The textblockid determines the text that the word belongs, the sentence is the sentence count in the textblock (but at the moment the value is 1 for all) and the position attribute determines the order of the words.
select w1.text,w2.text
from token as w1,
(select textblockid,sentence,position,text from token
order by textblockid,sentence,position) as w2
where w1.textblockid = w2.textblockid
and w1.sentence = w2.sentence
and w1.position = w2.position - 1
Is there a better/faster way to do this?
I don’t know postgresql in detail but for sure query could be simpler in sql server:
(I think it is better to use simplest query and leave the rest for optimizer, which may be misleaded by your from subquery).
However if you have index on (textblockid, sentence, position) you really can’t get anything more with sql.