2013-08-06 75 views
-2

使用組我有這樣的一個表:Sql Server中的String列,CONCAT和條款

Name   Address        Matches 
Tim Jones  1 London Road, London, W10EU  Peter Jones 
Tim Jones  1 London Road, London, W10EU  Smith Jones 
Tim Jones  1 London Road, London, W10EU  Tim Jones 
Tim Jones  1 London Road, London, W10EU  S Singh 
Jack Sons  10 West Street, London, W900U  John Graham 
Jack Sons  10 West Street, London, W900U  Jack Sons 

我想要的結果是這樣的:

Name   Address       Matches 
Tim Jones  1 London Road, London, W10EU  Peter Jones,Smith johns,Tim Johns, S Singh 
Jack Sons  10 West Street, London, W900U John Graham, Jack Sons 

應該串連所有「匹配」列並按名稱,地址分組。 SQLServer08

回答

0
Select distinct b.name, b.address, 
       substring((Select ','+a.matches AS [text()] 
       From test15 a 
       Where a.name=b.name and a.Address=b.Address 
       ORDER BY a.name, a.address 
       For XML PATH ('')),2, 1000) [Match] 
     From dbo.test15 b