日期:2014-05-18  浏览次数:20533 次

求一条高效的SQL语句
有一个log表:
id userid url flow
1 100221 http://www.baidu.com/xxx/4xxx.html 120
2 100222 http://www.baidu.com/12xxx.html 180
3 100223 http://www.baidu.com/xxx/js.js 30
4 100221 http://www.goole.com/ppp/x32x.html 120
5 100221 http://www.goole.com/kkk/x3212.html 320
6 100226 http://www.163.com/xxoox/xxx.html 160

想要的结果:

id userid url flow
 1 100221 www.baidu.com 120
 2 100221 www.goole.com 440 (注:120+320 同一个userid 且同一个站点流量相加)
 3 100222 www.baidu.com 180
 4 100223 www.baidu.com 30
 5 100226 www.163.com 160

--实际的log表有很多数据,且url不全是以http://www这样记录的,但大部分都是。


------解决方案--------------------
SQL code


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 行受影响)

------解决方案--------------------
SQL code
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/