从字符串中每次出现第 n 次后获取文本并对其编号

Posted

技术标签:

【中文标题】从字符串中每次出现第 n 次后获取文本并对其编号【英文标题】:Get text after each nth occurrence from string and number them 【发布时间】:2019-08-01 10:22:13 【问题描述】:

通过 Aginity 查询 Redshift

我有一个只有 1 个字段的小表,我想在每次出现 XX 值后获取值,直到下一个空格,还有一列对每次出现进行编号。

MYTABLE:

MYFIELD
The quick XX brown fox XX jumps over the XX lazy dog
Get text XX after each XX nth XX occurrence XX from string

所需的输出:

MYFIELD                                                     OCC   FIELDOUTPUT
The quick XX brown fox XX jumps over the XX lazy dog        1     brown  
The quick XX brown fox XX jumps over the XX lazy dog        2     jumps
The quick XX brown fox XX jumps over the XX lazy dog        3     lazy
Get text XX after each XX nth XX occurrence XX from string  1     after
Get text XX after each XX nth XX occurrence XX from string  2     nth
Get text XX after each XX nth XX occurrence XX from string  3     occurrence 
Get text XX after each XX nth XX occurrence XX from string  4    from

SQL 小提琴:http://sqlfiddle.com/#!15/991c8d

【问题讨论】:

【参考方案1】:

你可以用ORDINALITY分割字符串:

WITH cte AS (
  SELECT *
  FROM MyTABLE, regexp_split_to_table(MYFIELD, E'\\s+') WITH ORDINALITY s(c,rn)
), cte2 AS (
  SELECT myfield, c, LEAD(c) OVER(PARTITION BY MYFIELD ORDER BY rn) AS FieldOutput, rn 
  FROM cte
)
SELECT MYFIELD, Fieldoutput,
       ROW_NUMBER() OVER(PARTITION BY MYFIELD ORDER BY rn) AS occ
FROM cte2
WHERE c = 'XX'
ORDER BY MYFIELD,rn;

db<>fiddle demo

【讨论】:

遇到错误:ERROR: 42601: syntax error at or near "WITH" @Matt 请尝试使用内联视图的版本:dbfiddle.uk/…【参考方案2】:
WITH dummy_values AS (
  SELECT 1 UNION ALL 
  SELECT 1 UNION ALL
  SELECT 1
)
, seq AS (
  SELECT (ROW_NUMBER() OVER ())::INT occ 
  FROM dummy_values d1, dummy_values d2, dummy_values d3
)
SELECT 
  "MYFIELD"
, occ
, REGEXP_REPLACE(REGEXP_SUBSTR("MYFIELD", 'XX \\S+', 1, occ), 'XX ', '') fieldoutput
FROM mytable
JOIN seq ON occ <= REGEXP_COUNT("MYFIELD", 'XX ')

【讨论】:

@Matt,此答案对 postgresql 有效,我看到问题已被编辑,现在指的是 redshift,请参阅更新后的答案

以上是关于从字符串中每次出现第 n 次后获取文本并对其编号的主要内容,如果未能解决你的问题,请参考以下文章

C#怎么把list中某个元素出现n次后的内容存入另一个list

9.28 二叉树计数

矩阵取数游戏

洛谷 1005 矩阵取数游戏

sql 从连接的查询中获取所有列,搜索列名并对其进行排序

P1005矩阵取数游戏