2

我的表数据如下所示:

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

是否有可能得到渴望的结果,或者我的方式不对?谢谢。

4

3 回答 3

3

尝试这个

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
于 2012-10-10T09:02:05.513 回答
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:// 开头)

于 2012-10-10T08:42:53.873 回答
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
于 2012-10-10T09:15:09.533 回答