在编写SQL查询以查找包含3个具有相同值的连续字符的名字时,我需要帮助。有人可以帮忙吗?
谢谢
答案 0 :(得分:3)
另一种选择是使用临时提示表和简单的联接
示例
Declare @YourTable table (FirstName varchar(50))
Insert Into @YourTable values
('Johnnny')
,('Susan')
Select Distinct A.*
From @YourTable A
Join (Select Top 255 S=replicate(char(Row_Number() Over (Order By (Select NULL))),3)
From master..spt_values
) B
on charindex(S,FirstName)>0
返回
FirstName
Johnnny
答案 1 :(得分:1)
我建议写一个CTE来拆分每个单词的各个字符。 This question应该可以为您提供帮助。此CTE的结果应类似于以下内容(假设for /?
和John
的名字):
Ericcc
然后,您可以只使用连续的自联接(或+---+----+--------+
| i | ch | name |
+---+----+--------+
| 1 | E | Ericcc |
| 2 | r | Ericcc |
| 3 | i | Ericcc |
| 4 | c | Ericcc |
| 5 | c | Ericcc |
| 6 | c | Ericcc |
| 1 | J | John |
| 2 | o | John |
| 3 | h | John |
| 4 | n | John |
+---+----+--------+
/ LEAD
函数)来找出连续的字符。
由于这是出于学习目的,因此我不会给出答案。如果您想采用这种方法,并在尝试后遇到特定问题,则可以发布新问题。
答案 2 :(得分:0)
编辑,规格变得更加清晰。这是有史以来最丑的解决方案之一,性能会很差,但是:
SELECT *
FROM #T AS T
WHERE ColA LIKE '%AAA%' OR
ColA LIKE '%BBB%' OR
ColA LIKE '%CCC%' OR
ColA LIKE '%DDD%' OR
ColA LIKE '%EEE%' OR
ColA LIKE '%FFF%' OR
ColA LIKE '%GGG%' OR
ColA LIKE '%HHH%' OR
ColA LIKE '%III%' OR
ColA LIKE '%JJJ%' OR
ColA LIKE '%KKK%' OR
ColA LIKE '%LLL%' OR
ColA LIKE '%MMM%' OR
ColA LIKE '%NNN%' OR
ColA LIKE '%OOO%' OR
ColA LIKE '%PPP%' OR
ColA LIKE '%QQQ%' OR
ColA LIKE '%RRR%' OR
ColA LIKE '%SSS%' OR
ColA LIKE '%TTT%' OR
ColA LIKE '%UUU%' OR
ColA LIKE '%VVV%' OR
ColA LIKE '%WWW%' OR
ColA LIKE '%XXX%' OR
ColA LIKE '%YYY%' OR
ColA LIKE '%ZZZ%'
首次尝试: 您的问题有点含糊,但据我了解,您需要这样做:
USE TEMPDB
CREATE TABLE #T (ColA VARCHAR (10))
INSERT INTO #T VALUES ('AABB'), ('AAAA'), ('CFED'), ('CCCD')
SELECT *
FROM #T AS T
LEFT JOIN #T AS T2 ON T.ColA = T2.ColA
WHERE LEFT (T.ColA, 1) = SUBSTRING (T2.ColA, 2, 1)
AND LEFT (T.ColA, 1) = SUBSTRING (T2.ColA, 3, 1)
答案 3 :(得分:0)
您可以过滤长度> 2的记录 然后应用第二个过滤器以获取匹配3个重复字符的字符 像这样的东西: 从表中选择名字 哪里
len(名字)> 2和 substring(firstname,1,1)= substring(firstname,2,1)
和子字符串(firstname,2,1)= substring(firstname,3,1)
答案 4 :(得分:0)
试试看-首先,它会强制每行具有唯一的ID(您可能已经拥有了此ID) 然后我使用递归CTE将字符串分解为单个字母-然后查看是否可以将一个字母联接到上一个和下一个字符,因此连接的3行将始终是3个连续的字母 然后,我检查WHERE语句中的3个连续字母是否相等 匹配的行是使用GROUP BY
总结的CREATE TABLE #T (ColA VARCHAR (10))
INSERT INTO #T VALUES ('AABB'), ('AAAAXX'), ('CFKKKED'), ('CCDDD')
;WITH FORCE_ROW AS (SELECT *,row_number() OVER (ORDER BY ColA) RN FROM #T WHERE LEN(cola)>0),
LETTERS AS (
SELECT cola, RN, SUBSTRING(ColA,1, 1) X, 1 AS N FROM FORCE_ROW
UNION ALL
SELECT cola, RN, SUBSTRING(ColA,N + 1, 1), N + 1 FROM LETTERS WHERE N <= LEN(ColA)
)
select l1.cola from letters l1
JOIN letters l2 on L1.rn=l2.rn and l1.n + 1 = l2.n
Join letters l3 on L3.rn=l2.rn and l2.n + 1 = l3.n
where l1.x=l2.x and l2.x = l3.x
GROUP BY l1.RN,l1.cola
drop table #t;
-也供您尝试 -stoppress,现在不使用英文名称Keviiin
;WITH M AS (SELECT MAX(LEN(cola)) ML FROM #T),
NUMS AS (SELECT 1 N
UNION ALL
SELECT N + 1 FROM NUMS WHERE N < (SELECT ML FROM M))
SELECT distinct cola from Nums n1
cross join Nums n2
cross join Nums n3
CROSS APPLY
(SELECT ColA from #T WHERE n1.n +1 = n2.n and n2.n + 1 = n3.n and n3.n <= len(cola)) DQ
WHERE substring(cola,n1.n,1) = substring(cola,n2.n,1)
and
substring(cola,n2.n,1) = substring(cola,n3.n,1) and substring(cola,n3.n,1) != 'i';