我正在寻找一种方法来获取所有记录,其中word
列完全由重复2次或更多次的单个字母组成
示例:在表格中我有这样的事情:
id word
1 house
2 dog
3 aaaa
4 eee
5 aaaaaa
6 car
7 aa
8 hook
9 arrow
10 oooooo
11 feel
12 tooth
我想要一种方法在该表上进行查询,只获得id:3,4,5,7,10
我想这可能是正则表达式
这是一种方式。
SELECT *
FROM YourTable
WHERE Word = REPLICATE(LEFT(Word,1),LEN(Word)) AND LEN(Word) >= 2
请用这个。基于集合的方法
;WITH SingleDigits(Number) AS
(
SELECT Number
FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8),
(9), (0)) AS X(Number)
)
,Series AS
(
SELECT (d1.Number+1) + (10*d2.Number) + (100*d3.Number) + (1000*d4.Number) Number
from
SingleDigits as d1,
SingleDigits as d2,
SingleDigits as d3,
SingleDigits as d4
)
SELECT Id,Word
FROM
(
SELECT Id,word,u,COUNT(*) OVER(PARTITION BY ID) u1
FROM
(
SELECT Id,word,Charx,DENSE_RANK() OVER(ORDER BY Id,Word,Charx) u
FROM Wrds N
CROSS APPLY
(
SELECT SUBSTRING(word,number,1) Charx , number
FROM
(
SELECT DISTINCT number FROM
Series WHERE number > 0 AND number <= DATALENGTH(N.word)
)v
)t
)z
GROUP BY Id,word,u
)x WHERE u1 = 1
OUTPUT
Id Word
----------- ----------
3 aaaa
4 eee
5 aaaaaa
7 aa
10 oooooo
(5 rows affected)
阅读更多 -