PostgreSQL
 SQL >> Database >  >> RDS >> PostgreSQL

Confrontare righe corrente, precedente e successiva in PostgreSQL

Le window function PostgreSQL come lag() e lead() espongono valori di righe vicine.

SELECT text
FROM (
  SELECT text, category,
         lag(category) OVER w AS previous_cat,
         lead(category) OVER w AS next_cat
  FROM token t
  JOIN textblockhastoken tb ON tb.tokenid = t.id
  WINDOW w AS (PARTITION BY textblockid, sentence ORDER BY position)
) tokcat
WHERE category <> 'NAME'
  AND previous_cat = 'NAME'
  AND next_cat = 'NAME';