Click here to Skip to main content
15,923,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display data like this

user_id specialisation
---------- ----------------------------------------
10 Banking, marketing
20 Business law,civil,family


plz any one can help me how can i display different specilisation in a single row


Thanks in advance



Actually i want like this:

My first table like:

user_id specialisation

10 Banking

10 marketing

20 Business law

20 civil

20 family

but when i need to display i want like this


user_id specialisation
---------- ----------------------------------------
10 Banking, marketing
20 Business law,civil,family



i used this code:


SELECT distinct members.name, REPLACE(RTRIM((SELECT CAST([cat_name] AS VARCHAR(MAX)) + ' ' FROM category INNER JOIN specialisation ON category.cat_id=specialisation.cat_id WHERE (member_id = members.member_id) FOR XML PATH (''))),' ',', ') AS cat_name FROM specialisation INNER JOIN members ON specialisation.member_id = members.member_id INNER JOIN city_list ON members.city = city_list.city_id INNER JOIN category ON specialisation.cat_id = category.cat_id INNER JOIN state_list ON members.state = state_list.state_id where qualification_id=1


but i get the result like this:


name cat_name
---------- ----------------------------------------
10 Banking, marketing
20 Business, law,civil,law,family,law

every word is separated by comma but i need like Business law,civil law,family law

please any one can help me

Thanks
Posted
Updated 12-Oct-11 2:27am
v4
Comments
hitech_s 11-Oct-11 7:23am    
what are the tables you are using..and what is the data you are storing
without mentioning no one can say
and where you want to display the data?
rahul dev123 11-Oct-11 7:35am    
Actually i want like this:

My first table like:

user_id specialisation

10 Banking

10 marketing

20 Business law

20 civil

20 family

but when i need to display i want like this


user_id specialisation
---------- ----------------------------------------
10 Banking, marketing
20 Business law,civil,family

Try this.
SQL
SELECT   Col1, LEFT([Col2], LEN([Col2]) - 1) as [Col2]
            FROM  ( SELECT  DISTINCT Col1
                       FROM  tablename )  AS A

 OUTER APPLY ( SELECT  RTRIM(B.Col2)  + ', '
                 FROM  tablename B WHERE A.Col1 = B.Col1
                  FOR  XML PATH('') ) AS C ([Col2])


For more info see this[^].
 
Share this answer
 
You will have to use Pivot/UnPivot
 
Share this answer
 
v2
As many have said use the Pivot to put all Specialisations onto on row. To reverse this you need to use the unpivot. Below is the MSDN documentation on both

Pivot and Unpivot[^]
 
Share this answer
 
you can add (concate) several rows to one row with the + operator
for integers you have to convert to varchar convert(varchar,User_id)

SQL
select convert(varchar,User_id) + "," + business +"," + row3 +"," + row4


it's quiet simple
 
Share this answer
 
Comments
Simon_Whale 11-Oct-11 10:08am    
I would have a look up on Pivot and Unpivot if you have SQL Server 2005 or later

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