简体   繁体   English

如何仅选择至少具有共同字符的项目

[英]How to select only items that have at least so many characters in common

I am trying to figure out how to pull comparative data. 我试图弄清楚如何提取比较数据。

For simplicity sake some fake data is below. 为简单起见,下面提供了一些虚假数据。

12red34
56red78
90blue23
45blue67
89yellow10

What I want is to pull only items with 3 sequential non defined items. 我想要的是仅提取具有3个连续未定义项目的项目。

In this case, it would pull all of the items except 89yellow10 as no other item on the list contains 3 characters in a row that match it. 在这种情况下,它将拉除89yellow10以外的所有项目,因为列表中没有其他项目连续包含与其匹配的3个字符。 90blue23 and 45blue67 both contain "blue (or some combination of those letters)" and the same with the "red" items. 90blue23和45blue67都包含“蓝色(或这些字母的某种组合)”,并且与“红色”项相同。

This is on a very large table and manually searching using a contain clause doesn't seem feasible. 这是在非常大的表上,使用contain子句手动搜索似乎不可行。

Thank you. 谢谢。

This will perform with the sample data, however , Laughing Vergil has completely valid points. 这将与示例数据一起执行, 但是 ,Laughing Vergil具有完全有效的观点。

Declare @YourTable Table (ID int,SomeString varchar(50))
Insert Into @YourTable values
(1,'12red34'),
(2,'56red78'),
(3,'90blue23'),
(4,'45blue67'),
(5,'89yellow10')


Declare @MatchLen int = 3
;with cte as (
      Select ID,B.*
      From  @YourTable A
      Cross Apply (
                    Select N,S=substring(A.SomeString,N,@MatchLen)
                    From (Select Top (Len(A.SomeString)) N=Row_Number() Over (Order By Number) From master..spt_values) N
                   ) B 
      Where substring(A.SomeString,N,3) like Replicate('[A-Z]',@MatchLen)
)
Select A.*
      ,MinS = min(S)
      ,MaxS = max(S)
 From  @YourTable A
 Join ( 
        Select  S
         From   cte A
         Group  By S
         Having Count(*)>=2
      ) B 
 On  CharIndex(S,SomeString)>0  
 Group By ID,SomeString
 Order By ID

Returns 退货

ID  SomeString  MinS    MaxS
1   12red34     red     red
2   56red78     red     red
3   90blue23    blu     lue
4   45blue67    blu     lue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何只选择每天至少有一条记录的ID? - How to select only ID that have at least one record every day? 试图选择最小公数值 - Trying to select the least common value 计算多少行具有至少一个特定值 - Count how many rows have at least one certain value 如何在 python sqlalchemy 中为 select() 设置通用模块? - How to have common module for select() in python sqlalchemy? 如何设计数据库约束,以便两个实体中的两个字段值匹配时只能具有多对多关系? - How to I design a database constraint so two entities can only have a many to many relationship if two field values within them match? 如何找到最常见和最不常见的数字 - How to find the most common and least common numbers 如何查找子表中哪些行至少有一个非空字段(一对多),哪些行只有 null 字段? - How to find which rows have at least one non-null field in a subtable (one-to-many), and which rows have only null fields? 如何 select 在给定列中至少具有两个特定实例的 ID - How to select IDs that have at least two specific instaces in a given column 如何在sql中选择具有至少3个已审核产品ID且产品得分至少为4/5的用户ID对 - How to select pair of userID that have at least 3 reviewed productID with a product score of at least 4/5 in sql 仅在列存在时选择至少 - Select least only if column exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM