求一SQL语句。三个表联合查询(刚才已结贴,要求有变化,再发50分)
A表:Ids取的B表中的id,以,隔开
selName Ids
a 001,002,003
B表
Id Naics
001 236115
002 421122
003 221100
C表
Naics Zip Locality
236115 9210 10
421122 2358 12
221100 9402 13
现在的参数是A表中的 "a "与C表中的9210,2358,9402如何把Locality的sum即10+12+13=35与selName两个字段搜索出来呢?
------解决方案--------------------select c.selName,sum(a.Locality)
from 表C a
left join 表B b on b.naics=a.naics
left join 表A c on ', ' + c.ids + ', ' like '%, ' + b.id + ',% '
group by c.selName
------解决方案----------------------试试
select _a.selName,sum(Locality)
from a _a
join b _b on _a.ids like '% '+ _b.id + '% '
join c _c on _c.Naics = _b.Naics
where _c.Zip in (9210,2358,9402)
group by _a.selName
------解决方案--------------------select A.selName,sum(Locality) from
A,B,C
WHERE A.Id LIKE '% '+B.Ids+ '% ' AND B.Naics=C.Naics
AND A.selName= 'a ' AND C.Zip IN ( '9210 ', '2358 ', '9402 ')
GROUP BY A.selName
反了~