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

sql的统计。我与不出来。帮忙看看。谢谢大家
TC_autoid     route_autoid               这第一个表   select   *   from   tr_consultative  
97                         15  
98                         15,16
99                         15,18
100                       16,17
101                       14,1
102                       1


route_autoid         route_name       第二个表select   *   from   tr_planRoute
1                                   上海
2                                   北京
3                                   正定
4                                   河北
5                                   河南
6                                   石家庄
7                                   福建    
...
15                                 大连
16                                 黑龙江
...
两个表。我想统计,他们的次数

------解决方案--------------------
select tmp.*,[count]=(select count(*) from tr_consultative where charindex( ', '+tmp.route_autoid+ ', ', ', '+route_autoid+ ', ')> 0)
from tr_planRoute as tmp
------解决方案--------------------
create table tr_consultative (TC_autoid int, route_autoid varchar(100))
insert tr_consultative select 97 , '15 '
union all select 98 , '15,16 '
union all select 99 , '15,18 '
union all select 100 , '16,17 '
union all select 101 , '14,1 '
union all select 102 , '1 '


create table tr_planRoute(route_autoid int, route_name varchar(10))-- 第二个表select * from tr_planRoute
insert tr_planRoute select 1 , '上海 '
union all select 2 , '北京 '
union all select 3 , '正定 '
union all select 4 , '河北 '
union all select 5 , '河南 '
union all select 6, '石家庄 '
union all select 7 , '福建 '
union all select 15 , '大连 '
union all select 16 , '黑龙江 '

select route_autoid,route_name,cou=(select count(1) from tr_consultative where
charindex( ', '+ltrim(a.route_autoid)+ ', ', ', '+route_autoid+ ', ')> 0) from tr_planroute a