计算字符串 T-SQL 中单独出现的次数
我需要帮助计算在字符串中找到“模式”的次数。我正在使用一个工具将输入的模式提取到列中。我的日期集如下所示:
Id | MatchedPatterns |
---|---|
1 | “[模式一:SE09B00218],[模式二:0000000-09000,0974837-01]” |
2 | “[模式一:SE09B00218,DA02F11418],[模式二: 0000000-09,0974837-01]" |
3 | "[模式一:TR38G01678,DA02F11418,KJ73F4567]" |
4 | "[模式二: 0000009-00000007,1234567-02,0939485-0004,09836473-1]" |
您可以看到每个模式可以找到多次(最多 200 次),用逗号分隔,也可以为空。所识别的每个图案也可以具有不同的长度。
我希望输出如下所示:
Id | PatternOneCount | PatternTwoCount |
---|---|---|
1 | 1 | 2 |
2 | 2 | 2 |
3 | 3 | 0 |
4 | 0 | 4 |
这可能吗?
非常感谢
I need help counting how many times a 'pattern' is found within a string. I'm using a tool which extracts inputted patters into a column. My dateset looks like the following:
Id | MatchedPatterns |
---|---|
1 | "[Pattern One: SE09B00218], [Pattern Two: 0000000-09000,0974837-01]" |
2 | "[Pattern One: SE09B00218,DA02F11418], [Pattern Two: 0000000-09,0974837-01]" |
3 | "[Pattern One: TR38G01678,DA02F11418,KJ73F4567]" |
4 | "[Pattern Two: 0000009-00000007,1234567-02,0939485-0004,09836473-1]" |
You can see that each pattern can be found multiple times (up to 200), separated by a comma or it can be null. Each pattern identified can also be different lengths.
Here is what I want the output to look like:
Id | PatternOneCount | PatternTwoCount |
---|---|---|
1 | 1 | 2 |
2 | 2 | 2 |
3 | 3 | 0 |
4 | 0 | 4 |
Is this possible?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您使用的是(完全)受支持的 SQL Server 版本,您可以将数据转换为有效的 JSON,然后使用它。
因此,首先用双引号 (
"
) 替换方括号 ([]
),然后将冒号括起来 (:) 也用双引号括起来,然后将整个字符串括在大括号 (
{}
) 中,以获取 JSON 字符串,然后使用
OPENJSON
使用该 JSON,使用STRING_SPLIT
来分割分隔数据,然后最后使用条件聚合...恶心。Assuming you're on a (fully) supported version of SQL Server, you could transform your data into valid JSON, and then consume it.
So first you
REPLACE
the brackets ([]
) with double quotes ("
), and the wrap the colons (:
) with double quotes as well. Then you wrap the entire string in braces ({}
), to get your JSON string.Next you consume that JSON with
OPENJSON
, useSTRING_SPLIT
to split the delimited data, and then finally use conditional aggregation... yuck.