日期:2014-05-18 浏览次数:20685 次
declare @t table (
id int,
userid  int,
url varchar(128),
flow int
)
insert into @t
select 1,100221,'http://www.baidu.com/xxx/4xxx.html',120 union all
select 2,100222,'http://www.baidu.com/12xxx.html',180 union all
select 3,100223,'http://www.baidu.com/xxx/js.js',30 union all
select 4,100221,'http://www.goole.com/ppp/x32x.html',120 union all
select 5,100221,'http://www.goole.com/kkk/x3212.html',320 union all
select 6,100226,'http://www.163.com/xxoox/xxx.html',160 
;with t as 
(
    select userid,left( replace(url,'http://',''),charindex('/',replace(url,'http://',''))-1) as url,flow from @t
)
select userid,url,sum(flow)as flow from t
group by userid,url
--------------------------
(6 行受影响)
userid      url                                                                                                                                                                                                                                                              flow
----------- ---------------------------------------------------------------------------------------------------------------- -----------
100226      www.163.com                                                                                                                                                                                                                                                      160
100221      www.baidu.com                                                                                                                                                                                                                                                    120
100222      www.baidu.com                                                                                                                                                                                                                                                    180
100223      www.baidu.com                                                                                                                                                                                                                                                    30
100221      www.goole.com                                                                                                                                                                                                                                                    440
(5 行受影响)
------解决方案--------------------
create table tb ( id int identity(1,1),userid bigint,url varchar(128),flow int)
insert tb 
select  100221,'http://www.baidu.com/xxx/4xxx.html',120 union all
select  100222 ,'http://www.baidu.com/12xxx.html',180 union all
select  100223 ,'http://www.baidu.com/xxx/js.js',30 union all
select  100221 ,'http://www.goole.com/ppp/x32x.html',120 union all
select  100221 ,'http://www.goole.com/kkk/