【SQL】10g字符串处理函数regexp_substr的用法示例2

来源:互联网 发布:西安java开发工资 编辑:程序博客网 时间:2024/06/05 22:35

在网站访问日志表中,通常有一列表示客户端的ip地址(虽然ip地址通常不是真实用户地址)。

我们有时在做报表统计时,需要根据ip地址前段,统计出访问量,比如:

SQL> select * from v1;
IPSTR
-------------
10.105.239.8
10.105.239.9
10.105.239.10
10.105.3.105
10.105.3.106
10.105.3.107
192.168.1.100
192.168.10.50
192.168.10.51
10.1.11.10
10.1.12.20
10.1.12.21
12 rows selected


SQL> 

那么,我们需要得出如下的记录:

IPHEAD                                                                             CT
-------------------------------------------------------------------------------- ----------
10.105.3                                                                             3
192.168.1                                                                            1
192.168.10                                                                          2
10.1.12                                                                               2
10.105.239                                                                          3
10.1.11                                                                               1
6 rows selected


SQL> 

表示10.105.239这个ip段,有3次访问。那么sql应该怎么写?

结合上次讲到的regexp_substr函数可以很容易实现:

select ipstr1 || '.' || ipstr2 || '.' || ipstr3 as iphead, count(*) as ct
  from (select regexp_substr(ipstr, '[0-9]+', 1, 1) as ipstr1,
               regexp_substr(ipstr, '[0-9]+', 1, 2) as ipstr2,
               regexp_substr(ipstr, '[0-9]+', 1, 3) as ipstr3,
               regexp_substr(ipstr, '[0-9]+', 1, 4) as ipstr4
          from v1)
 group by ipstr1 || '.' || ipstr2 || '.' || ipstr3

0 0
原创粉丝点击