--例子: declare @t table(aa varchar(100)) insert into @t select 'http://post.baidu.com/f?ct=335544320' union all select 'http://post.baidu.com/f?ct=335544320' union all select 'http://post.baidu.com/f?ct=335544320&tn=baiduPostBrowser' union all select 'http://www2.dcg.cn/bbs/viewthread.php?tid=66433' union all select 'http://seek.3721.com/left.htm?fw=cm2&name=%D3%FB%CD%FB' union all select 'http://post.baidu.com/f?ct=335544320&tn=' union all select 'http://www.yourhue.com/'--分组统计 select 域名=left(aa,charindex('/',aa,8)),数量=count(*) from @t group by left(aa,charindex('/',aa,8))/*--测试结果 域名 数量 ---------------------------- --- http://post.baidu.com/ 4 http://seek.3721.com/ 1 http://www.yourhue.com/ 1 http://www2.dcg.cn/ 1(所影响的行数为 4 行)--*/
--加排序 select 域名=lft(aa,charindex('/',aa,8)),数量=count(*) from 表 group by lft(aa,charindex('/',aa,8)) order by 域名
楼上有笔误: select left(aa,charindex('/',aa,8)),count(*) from 表 group by left(aa,charindex('/',aa,8))
declare @t table(aa varchar(100))
insert into @t
select 'http://post.baidu.com/f?ct=335544320'
union all select 'http://post.baidu.com/f?ct=335544320'
union all select 'http://post.baidu.com/f?ct=335544320&tn=baiduPostBrowser'
union all select 'http://www2.dcg.cn/bbs/viewthread.php?tid=66433'
union all select 'http://seek.3721.com/left.htm?fw=cm2&name=%D3%FB%CD%FB'
union all select 'http://post.baidu.com/f?ct=335544320&tn='
union all select 'http://www.yourhue.com/'--分组统计
select 域名=left(aa,charindex('/',aa,8)),数量=count(*)
from @t group by left(aa,charindex('/',aa,8))/*--测试结果
域名 数量
---------------------------- ---
http://post.baidu.com/ 4
http://seek.3721.com/ 1
http://www.yourhue.com/ 1
http://www2.dcg.cn/ 1(所影响的行数为 4 行)--*/
select 域名=lft(aa,charindex('/',aa,8)),数量=count(*)
from 表 group by lft(aa,charindex('/',aa,8))
order by 域名
select left(aa,charindex('/',aa,8)),count(*)
from 表 group by left(aa,charindex('/',aa,8))
返回字符串中指定表达式的起始位置。 语法
CHARINDEX ( expression1 , expression2 [ , start_location ] ) 参数
expression1一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。expression2一个表达式,通常是一个用于搜索指定序列的列。expression2 属于字符串数据类型分类。start_location在 expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2 的起始位置开始搜索。返回类型
int只有这样帮助了
注释
如果 expression1 或 expression2 之一属于 Unicode 数据类型(nvarchar 或 nchar)而另一个不属于,则将另一个转换为 Unicode 数据类型。如果 expression1 或 expression2 之一为 NULL 值,则当数据库兼容级别为 70 或更大时,CHARINDEX 返回 NULL 值。当数据库兼容级别为 65 或更小时,CHARINDEX 仅在 expression1 和 expression2 都为 NULL 时返回 NULL 值。 如果在 expression2 内没有找到 expression1,则 CHARINDEX 返回 0。
insert @web values('http://post.baidu.com/f?ct=335544320')
insert @web values('http://post.baidu.com/f?ct=335544320')
insert @web values('http://post.baidu.com/f?ct=335544320&tn=baiduPostBrowser')
insert @web values('http://www2.dcg.cn/bbs/viewthread.php?tid=66433')
insert @web values('http://seek.3721.com/left.htm?fw=cm2&name=%D3%FB%CD%FB')
insert @web values('http://post.baidu.com/f?ct=335544320&tn=')
insert @web values('http://www.yourhue.com/')
select 'http://' + left(substring(url,8,1000),CHARINDEX('/',substring(url,8,1000))) url
from @web
group by 'http://' + left(substring(url,8,1000),CHARINDEX('/',substring(url,8,1000)))
/*
url
--------------------------
http://post.baidu.com/
http://seek.3721.com/
http://www.yourhue.com/
http://www2.dcg.cn/(所影响的行数为 4 行)
*/