一个统计网站来源的SQL语句

gaofaq 2004-11-13 02:24:03
一个表三个字段 id shijian laiyuan
laiyuan字段是各式各样的url地址,格式都为“http://***.***.***/***.***”之类的
请问怎么统计出从各个地址来的数量呢
比如 从http://www.3721.com来的占多少个
因为从laiyuan事先不知道,所以不能like "http://www.3721.com"这样
谢谢了
...全文
145 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaofaq 2005-01-07
  • 打赏
  • 举报
回复
Thanks
ORARichard 2004-11-13
  • 打赏
  • 举报
回复
select laiyuan,count(1) from tb group by left(laiyuan,case charindex('/',laiyuan,8) when 0 then len(laiyuan) else charindex('/',laiyuan,8)-1 end)
zjcxc 元老 2004-11-13
  • 打赏
  • 举报
回复
--测试

--测试数据
create table tb(id int identity,shijian int,laiyuan varchar(100))
insert tb select 1,'http://www.3721.com'
union all select 2,'http://www.3721.com/index.htm'
union all select 3,'http://www.3721.com/index/aa.htm'
union all select 4,'http://www.21cn.com/aa/bb/123.asp'
union all select 5,'http://www.3721.com/index/cc/aa.htm'
union all select 6,'http://www.21cn.com'
union all select 7,'http://community.csdn.net/expert/forum.asp'
union all select 8,'http://community.csdn.net/Expert/topic/3549/3549840.xml?temp=.4138147'
go

--查询统计
select 网站=left(laiyuan,charindex('/',laiyuan+'/',8)-1),数量=count(*)
from tb
group by left(laiyuan,charindex('/',laiyuan+'/',8)-1)
go

--删除测试
drop table tb

/*--测试结果

网站 数量
------------------------------ ------
http://community.csdn.net 2
http://www.21cn.com 2
http://www.3721.com 4

(所影响的行数为 3 行)
--*/
zjcxc 元老 2004-11-13
  • 打赏
  • 举报
回复
--改一下,免得统计的时候受/的影响
select 网站=left(laiyuan,charindex('/',laiyuan+'/',8)-1),数量=count(*)
from tb
group by left(laiyuan,charindex('/',laiyuan+'/',8)-1)
zjcxc 元老 2004-11-13
  • 打赏
  • 举报
回复
select 网站=left(laiyuan,charindex('/',laiyuan+'/',8)),数量=count(*)
from 表
group by left(laiyuan,charindex('/',laiyuan+'/',8))
colinliu 2004-11-13
  • 打赏
  • 举报
回复
select laiyuan,num=count(*) from tb group by laiyuan
Andy__Huang 2004-11-13
  • 打赏
  • 举报
回复
select laiyuan,num=count(*) from tb group by laiyuan

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧