Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to All,

i have data in a table.
S.No  Company Location  
1        A       Chennai

2        A       Pune 

3        B       Delhi

4        A       Bangalore

5        B       Hyderabad

I need to display data below format.

Company    Location
A            Chennai,Pune,Banglore

B            Delhi,Hyderabad


How to display data as above format ?

Could any one know reply to this....


Regards
Nanda Kishore.CH
Posted
Updated 2-Sep-13 1:04am
v2

1 solution

Try this...
SQL
Create Table #TableName (Sno Int, Company Nvarchar(40), Location Nvarchar(100))

Insert into #TableName 
Select 1,'A','Chennai' Union All
Select 2,'A','Pune' Union All
Select 3,'B','Delhi' Union All
Select 4,'A','Bangalore' Union All
Select 5,'B','Hyderabad' 

Select Distinct t.Company,STUFF((Select Distinct','+ a.Location 
                                 From #TableName a 
                                 Where a.Company=t.Company 
                                 for Xml Path(''), TYPE
                                ).value('.','VARCHAR(Max)'),1, 1, '') 
                           as Location
From #TableName t

Drop Table #TableName 

Output:
SQL
Company   Location
-------  ------------
A	 Bangalore,Chennai,Pune
B	 Delhi,Hyderabad
 
Share this answer
 
v4
Comments
Raja Sekhar S 2-Sep-13 6:55am    
Did it Solve your Problem...?
RedDk 2-Sep-13 19:43pm    
ssmse says ... "poifect"!
Raja Sekhar S 3-Sep-13 0:52am    
Thank you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900