如何求一个字符在字符串中连续出现的次数

How to find the number of continuous times a character appears in a string

我有一个字符串11111122111131111111

我想创建一个连续出现 1 的次数数组,即前 6 个字符是 1 -> 两个 2 -> 四个 1 -> 一个 3 -> 七个 1

所以我想要的输出是[6,4,7]

我知道如何查找一个字符在字符串中出现的次数,但如何查找它们在连续模式中出现的次数。

;WITH splitString(val) AS       
(
    -- convert the string to xml, seperating the elements by spaces
    SELECT    CAST('<r><i>' + REPLACE(@string,' ','</i><i>') + '</i></r>' AS XML)
)
SELECT  [Key],
        COUNT(*) [WordCount]
FROM    (   -- select all of the values from the xml created in the cte
            SELECT  p.value('.','varchar(100)') AS [Key]
            FROM    splitString
                    CROSS APPLY val.nodes('//i') t (p)) AS t
GROUP BY [Key]

以下示例适用于 BigQuery 标准 SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '11111122111131111111' line
)
SELECT line, ARRAY(SELECT LENGTH(e) FROM UNNEST(REGEXP_EXTRACT_ALL(line, r'1+')) e) result
FROM `project.dataset.table`   

结果

[
  {
    "line": "11111122111131111111",
    "result": [
      "6",
      "4",
      "7"
    ]
  }
]

对实际的 RDBMS 有点不清楚

这里我们使用临时计数 table(任何 table 的大小都可以)。然后我们应用标准的 Gaps-and-Islands。

例子

Declare @S varchar(500) = '11111122111131111111'
Declare @C varchar(10)  = '1'


Select Seq=Row_Number() over (Order by Seq)
      ,Cnt=count(*)
 From (
        Select N
              ,S = substring(@S,N,1)
              ,Seq = N - Row_Number() over (Order by N)
         From ( Select Top (len(@S)) 
                       N=Row_Number() Over (Order By (Select NULL)) 
                  From master..spt_values n1
              ) A
         Where substring(@S,N,1)=@C
      ) A
 Group By Seq
 Order By Seq

Returns

Seq Cnt
1   6
2   4
3   7