2011-06-27 69 views
0

我有幾千個特許經營權,以及大約3或4個服務組。服務組在一個單獨的表格中,然後我有第三個表格將它們連接在一起。一個特許經營權可以連接到一個或多個服務組。我試圖達到的目標是列出特許經營權,列有「服務類別」欄。然後它將以逗號分隔列出他們提供的服務。所以,現在,這裏是我的輸出:將列中的值彙總/連接成單行

id Service Groups  name     email    address  City   State 
1 Cleaning Services Franchise of LocationX [email protected] 123 Fake st. Springfield TheOneTheSimpsonsLiveIn 
2 Disaster Services Franchise of LocationX [email protected] 123 Fake st. Springfield TheOneTheSimpsonsLiveIn 

我想使它成爲這個有它:

id Service Groups      name     email    address  City   State 
1 Cleaning Services, Disaster Services Franchise of LocationX [email protected] 123 Fake st. Springfield TheOneTheSimpsonsLiveIn 

我可以用它來做到這將是greately任何建議或方法/函數的引用讚賞。謝謝!

+0

你可以看看這個http://stackoverflow.com/questions/6344950/sql-query-to-get-aggregated-result-in-comma-seperators-along -with-group-by-column/6349132#6349132 – EricZ

+0

或者這一個:http://stackoverflow.com/questions/1874966/concatenate-row-values-t-sql –

+0

可能的重複[在MS SQL中模擬group_concat MySQL函數Server 2005?](http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-ms-sql-server-2005) –

回答

0

試試這個

Declare @TempServiceGroups varchar(max)='' 
Select COALESCE(@TempServiceGroups,'')+ServiceGroups+',' from MyTable 
Select @TempServiceGroups AS ServiceGroups , Name,Email,Address,City,State 
from MyTable 
+0

給它一個鏡頭。服務組列現在最後只有逗號。他們不結合:( – agmcleod

+0

檢查我的答案我編輯它 –