求助sql语句处理URL地址
例如表A:
id sourceUrl
---------------------------------
1 http://www.google.cn/search?client=aff
2 http://www.baidu.com/
3 http://txtx.cn/
4 http://www.baidu.com/s?wd=%CD%B6%D7%CA%CA%C0%BD%E7&cl=3
5
6 http://localhost:1053/Default.aspx
7 http://www.google.cn/search?client/tt.***
8 http://zs.shangdu.com/
要求是这样的:
sourceUrl count
--------------------
baidu.com 2023
375
google.cn 209
localhost 129
我想用查询语句取出所有域名并分组显示,还要统计每项总数。我到是想出一种办法
就是SQL太长了,我写如下:
select distinct replace(replace(left(substring(souceurl,charindex( '/ ',souceurl)+2,len(souceurl)-charindex( '/ ',souceurl)+2),PATINDEX( '%/% ',substring(souceurl,charindex( '// ',souceurl)+2,len(souceurl)-charindex( '/ ',souceurl)+2))), '/ ', ' '), 'www. ', ' ') as msource,count(*) as scount
from statistic
group by replace(replace(left(substring(souceurl,charindex( '/ ',souceurl)+2,len(souceurl)-charindex( '/ ',souceurl)+2),PATINDEX( '%/% ',substring(souceurl,charindex( '// ',souceurl)+2,len(souceurl)-charindex( '/ ',souceurl)+2))), '/ ', ' '), 'www. ', ' ')
order by scount desc
请问各位有什么好的方法处理吗?
------解决方案--------------------我也只想到这个方法,期待好点子
不过也可以试试,把几个域名定死,就统计这几个
------解决方案--------------------declare @a table(id int, sourceUrl varchar(1000))
insert @a select 1 , 'http://www.google.cn/search?client=aff '
union all select 2 , 'http://www.baidu.com/ '
union all select 3 , 'http://txtx.cn/ '
union all select 4, ' http://www.baidu.com/s?wd=%CD%B6%D7%CA%CA%C0%BD%E7&cl=3 '
union all select 5, ' '
union all select 6 , 'http://localhost:1053/Default.aspx '
union all select 7 , 'http://www.google.cn/search?client/tt.*** '
union all select 8 , 'http://zs.shangdu.com/ '
--select * from @a where sourceurl <> ' '
select
replace(replace(left(ltrim(sourceurl),charindex( '/ ',ltrim(sourceurl),8)-1), 'http:// ', ' '), 'www. ', ' ') mou,count(1) num
from @a where sourceurl <> ' '
group by replace(replace(left(ltrim(sourceurl),charindex( '/ ',ltrim(sourceurl),8)-1), 'http:// ', ' '), 'www. ', ' ')
order by num desc
------解决方案--------------------declare @a table(id int, sourceUrl varchar(1000))
insert @a select 1 , 'http://www.google.cn/search?client=aff '