selfServe:...:completed
就我个人而言,我会使用一个字符串拆分函数来解决这个问题,该函数在一个逗号分隔的字符串中寻找合适的。如果您使用的是相当现代的 SQL Server 版本,则可以使用内置string_split
函数执行此操作:
string_split
declare @t table(id int,t varchar(1000));
insert into @t values
(1,'OpenQ,Payment,Payment:selfServe:Payment-Cancel_Scheduled:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,HUP')
,(2,'OpenQ2,Payment,Payment:selfServe:Payment:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,Payment:selfServe:Payment:completed::,HUP');
select t.id
,s.value
,t.t
from @t as t
cross apply string_split(t.t,',') as s
where case when patindex('%:selfServe:%',s.value) > 0
and patindex('%:completed:%',s.value) > 0
then 1
else 0
end = 1;
输出
+----+---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | value | t |
+----+---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 2 | Payment:selfServe:Payment:completed:: | OpenQ2,Payment,Payment:selfServe:Payment:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,Payment:selfServe:Payment:completed::,HUP |
+----+---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
如果没有,您将需要滚动自己的字符串拆分器。我将假设您在这里可能有一些相当长的字符串(超过 4000 个字符),因此我使用了一个XML
适用于max
数据类型的基于拆分器。
当您在我假设不允许您在数据库中创建新的表值函数的 BI 工具中执行此操作时,您将需要一个相当复杂的语句来处理您的数据,该语句包含字符串拆分器内联:
自己滚
declare @t table(id int,t varchar(1000));
insert into @t values
(1,'OpenQ,Payment,Payment:selfServe:Payment-Cancel_Scheduled:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,HUP')
,(2,'OpenQ2,Payment,Payment:selfServe:Payment:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,Payment:selfServe:Payment:completed::,HUP');
with s as
( -- Convert the string to an XML value, replacing the delimiter with XML tags
select id
,t
,convert(xml,'<x>' + replace((select '' + t for xml path('')),',','</x><x>') + '</x>').query('.') as s
from @t
)
select id
,item
,t -- Select the values from the generated XML value by CROSS APPLYing to the XML nodes
from(select id
,t
,n.x.value('.','nvarchar(max)') as item
from s
cross apply s.nodes('x') as n(x)
) a
where case when patindex('%:selfServe:%',a.item) > 0
and patindex('%:completed:%',a.item) > 0
then 1
else 0
end = 1;
输出
+----+-----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | item | t |
+----+-----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 2 | Payment:selfServe:Payment:completed:: | OpenQ2,Payment,Payment:selfServe:Payment:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,Payment:selfServe:Payment:completed::,HUP |
+----+-----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+