在 SQL 语句中查找字符串中的字符串
我有一栏显示我们公司经历过的项目的标题,另一栏显示每个项目的工作时间。
项目标题包含关键字,关键字由格式“关键字:”定义,即“ETL:”
有些项目有多个关键字,即“客户:ETL:ASCX:”
因此,例如,项目标题可以是“客户:ETL:ASCX” :更新导入流程'
我提前不知道关键字。
我想要给定关键字的总小时数和项目数 因此,我们以以下两个项目标题为例:
- 客户:ETL:ASCX:已工作 20 小时 投入的工作。
- 客户:ETL: 桥梁:投入了10个小时的工作 它。
报告应该给出:
Keyword - Total Projects - Total Hours
Client: - 2 -30
ETL: - 2 - 30
ASCX: - 1 - 20
Bridge: - 1 - 10
获取关键字的第一个实例很容易 - 只需子字符串;但事实证明找到嵌套关键字很困难。
可以在 SQL 中进行嵌套搜索吗?
有接受者吗?
更新(最初作为“答案”发布):
进一步的示例:
假设我有两条具有以下项目标题的记录:
Record 1: Interface: ETL:
Record 2: ETL:
记录 1 有 10 小时,记录 2 有 30 小时。
现在,我的代码捕获第一个关键字实例,因此我现在的输出是 (关键字:小时)
ETL: 30
Interface: 10
但是,要求是显示 ETL 已分配 40 小时,因为两个项目将 ETL 作为关键字:
ETL: 40
Interface: 10
所以当然,我可以使用 LIKE 查找 ETL 或接口的所有实例,但我需要在选择每个关键字时进行细分。在上面的示例中,如果我使用类似“%ETL:%”的内容,我将获得两条记录,但我想查看所有关键字的所有时间,并按关键字细分。
也许更好的问题是:
如何获得如下所示的记录:
Interface: ETL:
进入如下所示的输出:
Interface:
ETL:
在 sql 中?
I have a column that shows the title of projects our company has gone through, another column has hours worked for each project.
The project titles contain keywords, keywords are defined by the format 'keyword:' i.e. 'ETL:'
Some projects have multiple keywords i.e. 'Client: ETL: ASCX: '
So for example, a project title could be 'Client: ETL: ASCX: update the import process'
I don't know the keywords ahead of time.
I want the total number of hours, and projects, for a given keyword
So let's use the following two project titles as an example:
- Client: ETL: ASCX: had 20 hours of
work put into it. - Client: ETL:
Bridge: had 10 hours of work put into
it.
The report should give:
Keyword - Total Projects - Total Hours
Client: - 2 -30
ETL: - 2 - 30
ASCX: - 1 - 20
Bridge: - 1 - 10
Getting the first instance of a keyword is easy - just substring; but finding the nested keyword is proving difficult.
Can nested searches be done within SQL?
any takers?
UPDATE (originally posted as an "answer"):
Further examples:
Let's say I have two records with the following project titles:
Record 1: Interface: ETL:
Record 2: ETL:
Record 1 has 10 hours and record 2 has 30 hours.
Right now, my code captures the first keyword instance, so my output right now is (keyword: hours)
ETL: 30
Interface: 10
However, the requirement is to show that ETL has 40 hours allocated, since two projects had ETL as a keyword:
ETL: 40
Interface: 10
So sure, I can use a LIKE to find all instances of ETL, or Interface, but I need to break down in the select each keyword. For in the above example, if I used a like '%ETL:%' I would get both records, but I want to see all hours for all keywords, broken down by keyword.
Maybe a better question would be:
How can I get a record that looks like this:
Interface: ETL:
Into an output that looks like this:
Interface:
ETL:
within sql?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是很漂亮,但您可以使用 MODEL 子句来进行分割(只是一种方式)。假设您有单独的
project
和project_hours
表,通过 ID 字段建立关系:此:
给出此:
编辑 或者如果您的第二组示例是包含,给出:
改编自答案 这里 - 所以任何功劳都应该归于Rob van Wijk。
我假设关键字后面总是跟着一个冒号,最后一个冒号之后的任何内容都不应该被视为关键字,它只需要在
length()
中添加 -1 即可。Not very pretty, but you can use the
MODEL
clause to do the split (just one way). Assuming you have separateproject
andproject_hours
tables with relationship through an ID field:This:
Gives this:
Edit Or if your second set of examples is included, gives:
Adapted from an answer here - so any credit should go to Rob van Wijk really.
I've assumed the keywords are always followed by a colon and anything after the last colon should not be treated as a keyword, which just needed the addition of a -1 to the
length()
.