简体   繁体   English

按 oracle sql 中的部分字符串对数据进行分组(Oracle9i Enterprise Edition Release 9.2.0.4.0)

[英]Group data by part of string in oracle sql ( Oracle9i Enterprise Edition Release 9.2.0.4.0)

I have the following query:我有以下查询:

select referrer, count(distinct ad_id) as Adverts,
       sum(case f when 'Y' then hits else 0 end) as clicks,
       sum(case f when 'N' then hits else 0 end) as views
from advert_view_hits
where ad_id in ({$id_strings})
group by referrer"

This will return data like the following:这将返回如下数据:

 Referrer  Adverts  Clicks  Views
 Caterer     3       124     74
 Indeed      5       234     136

Which is fine but some cases the referrer has been stored in the db like this:这很好,但在某些情况下,引用者已存储在数据库中,如下所示:

user1@jwrecruitment.co.uk_200890,
user2@jwrecruitment.co.uk_200890

user1@gatewayjobs.co.uk_200890, 
user3@towngate-personnel.co.uk_2

How would I go about grouping the data based on just the company of the user emails that have been used as the referrer.我将如何 go 关于仅根据已用作推荐人的用户电子邮件的公司对数据进行分组。

so the data would look like this:所以数据看起来像这样:

Referrer             Adverts  Clicks  Views
 Caterer               3       124     74
 Indeed                5       234     136
 jwrecruitment.co.uk   8       456     782 
 gatewayjobs.co.uk     9       897     959

So that all data for an emails like jwrecruitment.co.uk would be grouped together and displayed.这样像 jwrecruitment.co.uk 这样的电子邮件的所有数据都会被组合在一起并显示。

If I followed you correctly, you could use regexp_replace() :如果我正确地跟着你,你可以使用regexp_replace()

select 
    regexp_replace(referrer, '^.*@([^_]+).*$', '\1') referrer, 
    count(distinct ad_id) as Adverts,
    sum(case f when 'Y' then hits else 0 end) as clicks,
    sum(case f when 'N' then hits else 0 end) as views
from advert_view_hits
where ad_id in ({$id_strings})
group by regexp_replace(referrer, '^.*@([^_]+).*$', '\1')

The regexp matches on referrer that contain a arobas, and captures the part after the arobas and before the next underscore.正则表达式匹配包含 arobas 的referrer,并捕获 arobas 之后和下一个下划线之前的部分。 If the referrer does not match the regex, it is left untouched.如果引用者与正则表达式不匹配,则保持不变。

If I understand correctly, you can take everything after the @ -- and you can do that using regexp_substr() :如果我理解正确,您可以在@ -- 之后获取所有内容,您可以使用regexp_substr()来做到这一点:

select regexp_substr(referrer, '[^@]+$') as referrer, count(distinct ad_id) as Adverts,
       sum(case f when 'Y' then hits else 0 end) as clicks,
       sum(case f when 'N' then hits else 0 end) as views
from advert_view_hits
where ad_id in ({$id_strings})
group by regexp_substr(referrer, '[^@]+$') ;

You can replace the regexp_substr() logic with:您可以将regexp_substr()逻辑替换为:

select substr(referrer, instr(referrer, '@') + 1) as referrer, count(distinct ad_id) as Adverts,
       sum(case f when 'Y' then hits else 0 end) as clicks,
       sum(case f when 'N' then hits else 0 end) as views
from advert_view_hits
where ad_id in ({$id_strings})
group by  substr(referrer, instr(referrer, '@') + 1) ;

You can also use the regexp_substr to find the string between @ and _ as follows:您还可以使用regexp_substr来查找@_之间的字符串,如下所示:

select REGEXP_SUBSTR(referrer,'@([^_]+)',1,1,NULL,1) referrer, 
       count(distinct ad_id) as Adverts,
       sum(case f when 'Y' then hits else 0 end) as clicks,
       sum(case f when 'N' then hits else 0 end) as views
  from advert_view_hits
where ad_id in ({$id_strings})
group by REGEXP_SUBSTR(referrer,'@([^_]+)',1,1,NULL,1)

If you are on older version then instead of regexp_substr use the combination of the SUBSTR and INSTR as follows:如果您使用的是旧版本,那么请使用 SUBSTR 和 INSTR 的组合而不是 regexp_substr,如下所示:

SUBSTR(referrer, 
       INSTR(referrer, '@') + 1, 
       DECODE(INSTR(referrer, '_', - 1), 
              0, 
              LENGTH(referrer) - INSTR(referrer, '@'), 
              INSTR(referrer, '_', - 1) - INSTR(referrer, '@') - 1)
      )

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM