仅在SQL Server中获取URL部分

时间:2012-10-10 08:37:51

标签: sql-server sql-server-2008

我的表格数据如下:

SessionRefere  

http://www.google.com/url?sa=t&rct=j&q=aaa bbb&source=web&cd=1&cad=rja&sqi=2&ved=0CB4QFjAA&url=http://www.abc.com/&ei=QFR0UM-JKIrQrQfsuoG4CQ&usg=AFQjCNExYcKkcvobBbktLGNksptf1giQRw
-------------------------------------------------
http://www.google.com/reader/view/?hl=es&tab=Xq&at=RTknd_lBUUnvNqan2641EA
----------------------------------------------------
http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&cad=rja&ved=0CEgQFjAE&url=http://www.abc.com/ppp.aspx&ei=dmd0UOO9AYnY4QS1sYGwBw&usg=AFQjCNGIFcJUUSVpl_ZiZoSWDP2LkIagtw

这只是样本数据。有很多这样的案例。所以在计算之前我想要原始的网址部分。

我使用此查询来获取数据。

DECLARE @temp table(RefPage nvarchar(200))
insert into @temp
SELECT
   CASE CHARINDEX( '?', SessionReferer)
       WHEN 0 THEN SessionReferer
       ELSE LEFT(SessionReferer, CHARINDEX( '?', SessionReferer) - 1) END AS RefPage 
FROM Tracker WHERE [start] between '1-1-2010' and '1-1-2015' and  SessionReferer<> ''

 select distinct RefPage, count(*) as [VisitTime] from @temp
  group by RefPage
order by [VisitTime] desc

我的结果是:

SessionRefere                           VisitTime
http://www.google.com/url                 2
http://www.google.com/reader/view/        1 

但我想要一个结果:

SessionRefere                           VisitTime
http://www.google.com                      3

是否有可能获得欲望结果或我的方式错误?感谢。

3 个答案:

答案 0 :(得分:2)

尝试在CHARINDEX('/',SessionReferrer,9)

分割
 select 
   case when CHARINDEX('/',SessionReferrer,9) = 0 
     then SessionReferrer
     else left(SessionReferrer, CHARINDEX('/',SessionReferrer,9)-1)
   end,
   count(*)
 from Tracker      
 group by
   case when CHARINDEX('/',SessionReferrer,9) = 0 
     then SessionReferrer
     else left(SessionReferrer, CHARINDEX('/',SessionReferrer,9)-1)
   end

(我懒得假设所有推荐人都以http://或https://开头)

答案 1 :(得分:2)

试试这个

Declare @T Table(URL Varchar(MAX))
Insert Into @T Values('http://www.google.com/url?sa=t&rct=j&q=aaa bbb&source=web&cd=1&cad=rja&sqi=2&ved=0CB4QFjAA&url=http://www.abc.com/&ei=QFR0UM-JKIrQrQfsuoG4CQ&usg=AFQjCNExYcKkcvobBbktLGNksptf1giQRw')
Insert Into @T Values('http://www.google.com/reader/view/?hl=es&tab=Xq&at=RTknd_lBUUnvNqan2641EA')
Insert Into @T Values('http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&cad=rja&ved=0CEgQFjAE&url=http://www.abc.com/ppp.aspx&ei=dmd0UOO9AYnY4QS1sYGwBw&usg=AFQjCNGIFcJUUSVpl_ZiZoSWDP2LkIagtw')
Insert Into @T Values('http://www.yahoo.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&cad=rja&ved=0CEgQFjAE&url=http://www.abc.com/ppp.aspx&ei=dmd0UOO9AYnY4QS1sYGwBw&usg=AFQjCNGIFcJUUSVpl_ZiZoSWDP2LkIagtw')

;WITH CTE AS
(
    SELECT 
        URL
        ,SessionRefere = SUBSTRING(URL,0,CHARINDEX('://', URL)+3) + SUBSTRING(URL,CHARINDEX('://', URL)+3,CHARINDEX('/', SUBSTRING(URL,CHARINDEX('://', URL)+3,LEN(URL)))-1)
    FROM @T
)

SELECT SessionRefere,[VisitTime] = COUNT(SessionRefere)
FROM CTE
GROUP BY SessionRefere

<强>输出

SessionRefere   VisitTime
http://www.google.com   3
http://www.yahoo.com    1

答案 2 :(得分:1)

我在这里:

表示字符串@test:
我在URL中分隔'//'和'/'之间的字符串 根据给定的URL

整合http:或https
Declare  @test nvarchar(100)
Declare @http nvarchar(8)
set @test = 'http://www.google.com/url?sa=t&rct=j&q=aaa bbb&source=web&cd=1&cad=rja&sqi=2&ved=0CB4QFjAA&url=http://www.abc.com/&ei=QFR0UM-JKIrQrQfsuoG4CQ&usg=AFQjCNExYcKkcvobBbktLGNksptf1giQRw'
declare @length int
declare @start int
declare @subString nvarchar(100)
set @start =( select CharIndex('//', @test))
set @http = SUBSTRING(@test, 0 , @start)
set @length = Len(@test)
set @subString = SUBSTRING ( @test ,@start+2 , @length )
set @start =( select CharIndex('/', @subString))
set @subString = SUBSTRING ( @subString ,0 , @start )
set @subString = @http+'//'+ @subString