在 SQL 语句中查找字符串中的字符串

发布于 2024-11-16 03:50:42 字数 1199 浏览 2 评论 0原文

我有一栏显示我们公司经历过的项目的标题,另一栏显示每个项目的工作时间。

项目标题包含关键字,关键字由格式“关键字:”定义,即“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2024-11-23 03:50:42

不是很漂亮,但您可以使用 MODEL 子句来进行分割(只是一种方式)。假设您有单独的 projectproject_hours 表,通过 ID 字段建立关系:

create table projects as (
    select 'Client: ETL: ASCX: update the import process' as project_title,
        1 as project_id from dual
    union all select 'Client: ETL: Bridge: something else', 2 from dual
    --union all select 'Interface: ETL:', 3 from dual
    --union all select 'ETL:', 4 from dual
)
/

create table project_hours as (
    select 1 as project_id, 20 as hours from dual
    union all select 2, 10 from dual
    --union all select 3, 10 from dual
    --union all select 4, 30 from dual
)
/

此:

with tmp_tab as (
    select project_id, trim(t) as keyword, i
        from projects
        model
        return updated rows
        partition by (project_id)
        dimension by (0 i)
        measures (project_title t)
        rules (t[for i from 1 to
                (length(regexp_replace(':' || t[0],'[^:]')) - 1) increment 1]
            = regexp_substr(t[0],'[^:]+',1,cv(i)))
    order by project_id, i
)
select tt.keyword,
    count(distinct tt.project_id) as total_projects,
    sum(h.hours) as total_hours
from tmp_tab tt
left join project_hours h on h.project_id = tt.project_id
group by tt.keyword
/

给出此:

KEYWORD                          TOTAL_PROJECTS     TOTAL_HOURS        
-------------------------------- ------------------ ------------------ 
Bridge                           1                  10                 
ETL                              2                  30                 
Client                           2                  30                 
ASCX                             1                  20                 

编辑 或者如果您的第二组示例是包含,给出:

KEYWORD                          TOTAL_PROJECTS     TOTAL_HOURS        
-------------------------------- ------------------ ------------------ 
ETL                              4                  70                 
Bridge                           1                  10                 
Interface                        1                  10                 
Client                           2                  30                 
ASCX                             1                  20                 

改编自答案 这里 - 所以任何功劳都应该归于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 separate project and project_hours tables with relationship through an ID field:

create table projects as (
    select 'Client: ETL: ASCX: update the import process' as project_title,
        1 as project_id from dual
    union all select 'Client: ETL: Bridge: something else', 2 from dual
    --union all select 'Interface: ETL:', 3 from dual
    --union all select 'ETL:', 4 from dual
)
/

create table project_hours as (
    select 1 as project_id, 20 as hours from dual
    union all select 2, 10 from dual
    --union all select 3, 10 from dual
    --union all select 4, 30 from dual
)
/

This:

with tmp_tab as (
    select project_id, trim(t) as keyword, i
        from projects
        model
        return updated rows
        partition by (project_id)
        dimension by (0 i)
        measures (project_title t)
        rules (t[for i from 1 to
                (length(regexp_replace(':' || t[0],'[^:]')) - 1) increment 1]
            = regexp_substr(t[0],'[^:]+',1,cv(i)))
    order by project_id, i
)
select tt.keyword,
    count(distinct tt.project_id) as total_projects,
    sum(h.hours) as total_hours
from tmp_tab tt
left join project_hours h on h.project_id = tt.project_id
group by tt.keyword
/

Gives this:

KEYWORD                          TOTAL_PROJECTS     TOTAL_HOURS        
-------------------------------- ------------------ ------------------ 
Bridge                           1                  10                 
ETL                              2                  30                 
Client                           2                  30                 
ASCX                             1                  20                 

Edit Or if your second set of examples is included, gives:

KEYWORD                          TOTAL_PROJECTS     TOTAL_HOURS        
-------------------------------- ------------------ ------------------ 
ETL                              4                  70                 
Bridge                           1                  10                 
Interface                        1                  10                 
Client                           2                  30                 
ASCX                             1                  20                 

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().

软的没边 2024-11-23 03:50:42
select count(*) as projectcount, sum(hours) as totalhours from projects WHERE title like '%mykeyword:%';
select count(*) as projectcount, sum(hours) as totalhours from projects WHERE title like '%mykeyword:%';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文